@eternalvisionshining/passport 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/constants/index.d.ts +1 -0
- package/dist/constants/index.js +17 -0
- package/dist/constants/passport.constant.d.ts +1 -0
- package/dist/constants/passport.constant.js +4 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +19 -0
- package/dist/interfaces/async-module-options.interface.d.ts +6 -0
- package/dist/interfaces/async-module-options.interface.js +2 -0
- package/dist/interfaces/index.d.ts +3 -0
- package/dist/interfaces/index.js +19 -0
- package/dist/interfaces/module-options.interface.d.ts +3 -0
- package/dist/interfaces/module-options.interface.js +2 -0
- package/dist/interfaces/token.interface.d.ts +8 -0
- package/dist/interfaces/token.interface.js +2 -0
- package/dist/passport.module.d.ts +6 -0
- package/dist/passport.module.js +38 -0
- package/dist/passport.provider.d.ts +4 -0
- package/dist/passport.provider.js +23 -0
- package/dist/passport.service.d.ts +23 -0
- package/dist/passport.service.js +71 -0
- package/dist/utils/base64.d.ts +2 -0
- package/dist/utils/base64.js +22 -0
- package/dist/utils/crypto.d.ts +1 -0
- package/dist/utils/crypto.js +11 -0
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/index.js +18 -0
- package/package.json +31 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './passport.constant';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./passport.constant"), exports);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const PASSPORT_OPTIONS: unique symbol;
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./interfaces"), exports);
|
|
18
|
+
__exportStar(require("./passport.module"), exports);
|
|
19
|
+
__exportStar(require("./passport.service"), exports);
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { FactoryProvider, ModuleMetadata } from '@nestjs/common';
|
|
2
|
+
import { ModuleOptions } from './module-options.interface';
|
|
3
|
+
export interface AsyncModuleOptions extends Pick<ModuleMetadata, 'imports'> {
|
|
4
|
+
useFactory: (...args: any[]) => Promise<ModuleOptions> | ModuleOptions;
|
|
5
|
+
inject: FactoryProvider['inject'];
|
|
6
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./async-module-options.interface"), exports);
|
|
18
|
+
__exportStar(require("./module-options.interface"), exports);
|
|
19
|
+
__exportStar(require("./token.interface"), exports);
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { DynamicModule } from '@nestjs/common';
|
|
2
|
+
import { AsyncModuleOptions, ModuleOptions } from './interfaces';
|
|
3
|
+
export declare class PassportModule {
|
|
4
|
+
static register(options: ModuleOptions): DynamicModule;
|
|
5
|
+
static registerAsync(options: AsyncModuleOptions): DynamicModule;
|
|
6
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var PassportModule_1;
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.PassportModule = void 0;
|
|
11
|
+
const common_1 = require("@nestjs/common");
|
|
12
|
+
const passport_provider_1 = require("./passport.provider");
|
|
13
|
+
const passport_service_1 = require("./passport.service");
|
|
14
|
+
const constants_1 = require("./constants");
|
|
15
|
+
let PassportModule = PassportModule_1 = class PassportModule {
|
|
16
|
+
static register(options) {
|
|
17
|
+
const provider = (0, passport_provider_1.createPassportProvider)(options);
|
|
18
|
+
return {
|
|
19
|
+
module: PassportModule_1,
|
|
20
|
+
providers: [provider, passport_service_1.PassportService],
|
|
21
|
+
exports: [passport_service_1.PassportService, constants_1.PASSPORT_OPTIONS]
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
static registerAsync(options) {
|
|
25
|
+
const provider = (0, passport_provider_1.createAsyncPassportPorvider)(options);
|
|
26
|
+
return {
|
|
27
|
+
module: PassportModule_1,
|
|
28
|
+
imports: options.imports ?? [],
|
|
29
|
+
providers: [provider, passport_service_1.PassportService],
|
|
30
|
+
exports: [passport_service_1.PassportService, constants_1.PASSPORT_OPTIONS]
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
exports.PassportModule = PassportModule;
|
|
35
|
+
exports.PassportModule = PassportModule = PassportModule_1 = __decorate([
|
|
36
|
+
(0, common_1.Global)(),
|
|
37
|
+
(0, common_1.Module)({})
|
|
38
|
+
], PassportModule);
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { Provider } from '@nestjs/common';
|
|
2
|
+
import { AsyncModuleOptions, ModuleOptions } from './interfaces';
|
|
3
|
+
export declare function createPassportProvider(options: ModuleOptions): Provider;
|
|
4
|
+
export declare function createAsyncPassportPorvider(options: AsyncModuleOptions): Provider;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createPassportProvider = createPassportProvider;
|
|
4
|
+
exports.createAsyncPassportPorvider = createAsyncPassportPorvider;
|
|
5
|
+
const constants_1 = require("./constants");
|
|
6
|
+
function createPassportProvider(options) {
|
|
7
|
+
return {
|
|
8
|
+
provide: constants_1.PASSPORT_OPTIONS,
|
|
9
|
+
useValue: Object.freeze({ ...options })
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
function createAsyncPassportPorvider(options) {
|
|
13
|
+
return {
|
|
14
|
+
provide: constants_1.PASSPORT_OPTIONS,
|
|
15
|
+
useFactory: async (...args) => {
|
|
16
|
+
const resolved = await options.useFactory(...args);
|
|
17
|
+
if (!resolved || typeof resolved.secretKey !== 'string')
|
|
18
|
+
throw new Error('[PassportModule] secretKey is required and must be a string');
|
|
19
|
+
return Object.freeze({ ...resolved });
|
|
20
|
+
},
|
|
21
|
+
inject: options.inject ?? []
|
|
22
|
+
};
|
|
23
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { ModuleOptions } from './interfaces';
|
|
2
|
+
export declare class PassportService {
|
|
3
|
+
private readonly options;
|
|
4
|
+
private static readonly HMAC_DOMAIN;
|
|
5
|
+
private static readonly INTERNAL_SEPARATOR;
|
|
6
|
+
private readonly secret;
|
|
7
|
+
constructor(options: ModuleOptions);
|
|
8
|
+
private now;
|
|
9
|
+
private serialize;
|
|
10
|
+
private computeHmac;
|
|
11
|
+
generate(userId: string, ttl: number): string;
|
|
12
|
+
verify(token: string): {
|
|
13
|
+
error: boolean;
|
|
14
|
+
reason: string;
|
|
15
|
+
process?: undefined;
|
|
16
|
+
userId?: undefined;
|
|
17
|
+
} | {
|
|
18
|
+
process: string;
|
|
19
|
+
userId: string;
|
|
20
|
+
error?: undefined;
|
|
21
|
+
reason?: undefined;
|
|
22
|
+
};
|
|
23
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
+
};
|
|
14
|
+
var PassportService_1;
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.PassportService = void 0;
|
|
17
|
+
const common_1 = require("@nestjs/common");
|
|
18
|
+
const crypto_1 = require("crypto");
|
|
19
|
+
const utils_1 = require("./utils");
|
|
20
|
+
let PassportService = class PassportService {
|
|
21
|
+
static { PassportService_1 = this; }
|
|
22
|
+
options;
|
|
23
|
+
static HMAC_DOMAIN = 'PassportTokeAuth/v1';
|
|
24
|
+
static INTERNAL_SEPARATOR = '|';
|
|
25
|
+
secret;
|
|
26
|
+
constructor(options) {
|
|
27
|
+
this.options = options;
|
|
28
|
+
this.secret = options.secretKey;
|
|
29
|
+
}
|
|
30
|
+
now() {
|
|
31
|
+
return Math.floor(Date.now() / 1000);
|
|
32
|
+
}
|
|
33
|
+
serialize(user, iat, exp) {
|
|
34
|
+
return [PassportService_1.HMAC_DOMAIN, user, iat, exp].join(PassportService_1.INTERNAL_SEPARATOR);
|
|
35
|
+
}
|
|
36
|
+
computeHmac(secret, data) {
|
|
37
|
+
return (0, crypto_1.createHmac)('sha256', secret).update(data).digest('hex');
|
|
38
|
+
}
|
|
39
|
+
generate(userId, ttl) {
|
|
40
|
+
const issuedAt = Date.now();
|
|
41
|
+
const expiresIn = issuedAt + ttl;
|
|
42
|
+
const userPart = (0, utils_1.base64UrlEncode)(userId);
|
|
43
|
+
const iatPart = (0, utils_1.base64UrlEncode)(String(issuedAt));
|
|
44
|
+
const expPart = (0, utils_1.base64UrlEncode)(String(expiresIn));
|
|
45
|
+
const serialized = this.serialize(userPart, iatPart, expPart);
|
|
46
|
+
const mac = this.computeHmac(this.secret, serialized);
|
|
47
|
+
return `${userPart}.${iatPart}.${expPart}.${mac}`;
|
|
48
|
+
}
|
|
49
|
+
verify(token) {
|
|
50
|
+
const parts = token.split('.');
|
|
51
|
+
if (parts.length !== 4)
|
|
52
|
+
return { error: true, reason: 'Invalid token format' };
|
|
53
|
+
const [userPart, iatPart, expPart, mac] = parts;
|
|
54
|
+
const serialized = this.serialize(userPart, iatPart, expPart);
|
|
55
|
+
const expectedMac = this.computeHmac(this.secret, serialized);
|
|
56
|
+
if (!(0, utils_1.constantTimeEqual)(expectedMac, mac))
|
|
57
|
+
return { error: true, reason: 'Invalid token signature' };
|
|
58
|
+
const expiresIn = +(0, utils_1.base64UrlDecode)(expPart);
|
|
59
|
+
if (!Number.isFinite(expiresIn))
|
|
60
|
+
return { error: true, reason: 'Invalid expires token time' };
|
|
61
|
+
if (this.now() > expiresIn)
|
|
62
|
+
return { error: true, reason: 'Token has expired' };
|
|
63
|
+
return { process: 'success', userId: (0, utils_1.base64UrlDecode)(userPart) };
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
exports.PassportService = PassportService;
|
|
67
|
+
exports.PassportService = PassportService = PassportService_1 = __decorate([
|
|
68
|
+
(0, common_1.Injectable)(),
|
|
69
|
+
__param(0, (0, common_1.Inject)('PASSPORT_OPTIONS')),
|
|
70
|
+
__metadata("design:paramtypes", [Object])
|
|
71
|
+
], PassportService);
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.base64UrlEncode = base64UrlEncode;
|
|
4
|
+
exports.base64UrlDecode = base64UrlDecode;
|
|
5
|
+
function base64UrlEncode(buf) {
|
|
6
|
+
const s = typeof buf === 'string' ? Buffer.from(buf) : buf;
|
|
7
|
+
return s
|
|
8
|
+
.toString('base64')
|
|
9
|
+
.replace(/\+/g, '-')
|
|
10
|
+
.replace(/\//g, '_')
|
|
11
|
+
.replace(/=+$/g, '');
|
|
12
|
+
}
|
|
13
|
+
function base64UrlDecode(str) {
|
|
14
|
+
str = str.replace(/-/g, '+').replace(/_/g, '/');
|
|
15
|
+
while (str.length % 4)
|
|
16
|
+
str += '=';
|
|
17
|
+
return Buffer.from(str, 'base64').toString();
|
|
18
|
+
}
|
|
19
|
+
module.exports = {
|
|
20
|
+
base64UrlDecode,
|
|
21
|
+
base64UrlEncode
|
|
22
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function constantTimeEqual(a: string, b: string): boolean;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.constantTimeEqual = constantTimeEqual;
|
|
4
|
+
const crypto_1 = require("crypto");
|
|
5
|
+
function constantTimeEqual(a, b) {
|
|
6
|
+
const buffA = Buffer.from(a);
|
|
7
|
+
const buffB = Buffer.from(b);
|
|
8
|
+
if (buffA.length !== buffB.length)
|
|
9
|
+
return false;
|
|
10
|
+
return (0, crypto_1.timingSafeEqual)(buffA, buffB);
|
|
11
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./base64"), exports);
|
|
18
|
+
__exportStar(require("./crypto"), exports);
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@eternalvisionshining/passport",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"main": "./dist/index.js",
|
|
5
|
+
"types": "./dist/index.d.ts",
|
|
6
|
+
"files": [
|
|
7
|
+
"dist"
|
|
8
|
+
],
|
|
9
|
+
"publishConfig": {
|
|
10
|
+
"access": "public"
|
|
11
|
+
},
|
|
12
|
+
"scripts": {
|
|
13
|
+
"format": "prettier --write lib/**/*.ts",
|
|
14
|
+
"c": "tsc ./lib/index.ts ",
|
|
15
|
+
"build": "tsc -p tsconfig.build.json"
|
|
16
|
+
},
|
|
17
|
+
"license": "ISC",
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@nestjs/common": "^11.1.17",
|
|
20
|
+
"@nestjs/core": "^11.1.17",
|
|
21
|
+
"prettier": "^3.8.1",
|
|
22
|
+
"reflect-metadata": "^0.2.2",
|
|
23
|
+
"rxjs": "^7.8.2"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@eternalvisionshining/core": "^1.0.2",
|
|
27
|
+
"@types/node": "^25.5.0",
|
|
28
|
+
"prettier": "^3.8.1",
|
|
29
|
+
"typescript": "^5.9.3"
|
|
30
|
+
}
|
|
31
|
+
}
|