@my-nestjs-kit/auth 0.0.0-stage → 0.1.0-dev.20261129010337
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/LICENSE +21 -0
- package/README.md +27 -2
- package/dist/access-tokens/access-token.d.ts +13 -0
- package/dist/access-tokens/access-token.js +10 -0
- package/dist/access-tokens/access-tokens.module.d.ts +2 -0
- package/dist/access-tokens/access-tokens.module.js +22 -0
- package/dist/access-tokens/access-tokens.service.d.ts +16 -0
- package/dist/access-tokens/access-tokens.service.js +72 -0
- package/dist/access-tokens/access-tokens.types.d.ts +5 -0
- package/dist/access-tokens/access-tokens.types.js +9 -0
- package/dist/access-tokens/dto/access-token.dto.d.ts +13 -0
- package/dist/access-tokens/dto/access-token.dto.js +33 -0
- package/dist/auth-sessions/devices/dto/create-device.dto.d.ts +6 -0
- package/dist/auth-sessions/devices/dto/create-device.dto.js +33 -0
- package/dist/auth-sessions/dto/create-auth-session.dto.d.ts +8 -0
- package/dist/auth-sessions/dto/create-auth-session.dto.js +36 -0
- package/dist/auth.constants.d.ts +30 -0
- package/dist/auth.constants.js +36 -0
- package/dist/decorators/GetAuthParams.decorator.d.ts +34 -0
- package/dist/decorators/GetAuthParams.decorator.js +64 -0
- package/dist/decorators/GetSession.decorator.d.ts +2 -0
- package/dist/decorators/GetSession.decorator.js +13 -0
- package/dist/decorators/GetUser.decorator.d.ts +2 -0
- package/dist/decorators/GetUser.decorator.js +13 -0
- package/dist/decorators/Public.decorator.d.ts +3 -0
- package/dist/decorators/Public.decorator.js +18 -0
- package/dist/decorators/SetAuthConfig.decorator.d.ts +3 -0
- package/dist/decorators/SetAuthConfig.decorator.js +13 -0
- package/dist/dto/confirm-secure-action.dto.d.ts +8 -0
- package/dist/dto/confirm-secure-action.dto.js +46 -0
- package/dist/dto/log-in-by-email.dto.d.ts +16 -0
- package/dist/dto/log-in-by-email.dto.js +15 -0
- package/dist/dto/login-user-auth.dto.d.ts +4 -0
- package/dist/dto/login-user-auth.dto.js +29 -0
- package/dist/dto/verify-user-registration.dto.d.ts +4 -0
- package/dist/dto/verify-user-registration.dto.js +25 -0
- package/dist/entities/abstract-device-base.entity.d.ts +10 -0
- package/dist/entities/abstract-device-base.entity.js +77 -0
- package/dist/entities/abstract-o-auth-connection.entity.d.ts +28 -0
- package/dist/entities/abstract-o-auth-connection.entity.js +100 -0
- package/dist/entities/abstract-o-auth-profile.entity.d.ts +10 -0
- package/dist/entities/abstract-o-auth-profile.entity.js +38 -0
- package/dist/entities/auth-session-base.entity.d.ts +18 -0
- package/dist/entities/auth-session-base.entity.js +73 -0
- package/dist/guards/AbstractRefreshJwtToken.guard.d.ts +9 -0
- package/dist/guards/AbstractRefreshJwtToken.guard.js +45 -0
- package/dist/guards/abstract/AppAuthGuard-abstract.d.ts +219 -0
- package/dist/guards/abstract/AppAuthGuard-abstract.js +187 -0
- package/dist/guards/index.d.ts +0 -0
- package/dist/guards/index.js +0 -0
- package/dist/helper/base-auth-service.helper.d.ts +60 -0
- package/dist/helper/base-auth-service.helper.js +184 -0
- package/dist/helper/jsw-auth-controller-abstract.d.ts +33 -0
- package/dist/helper/jsw-auth-controller-abstract.js +89 -0
- package/dist/helper/jwt-auth-service-abstract.d.ts +40 -0
- package/dist/helper/jwt-auth-service-abstract.js +181 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.js +55 -0
- package/dist/jwt/index.d.ts +5 -0
- package/dist/jwt/index.js +21 -0
- package/dist/jwt/jwt-auth.module.d.ts +6 -0
- package/dist/jwt/jwt-auth.module.js +45 -0
- package/dist/jwt/jwt-auth.service.d.ts +33 -0
- package/dist/jwt/jwt-auth.service.js +170 -0
- package/dist/jwt/jwt-keys.helper.d.ts +12 -0
- package/dist/jwt/jwt-keys.helper.js +32 -0
- package/dist/jwt/types/enums.d.ts +19 -0
- package/dist/jwt/types/enums.js +26 -0
- package/dist/jwt/types/index.d.ts +2 -0
- package/dist/jwt/types/index.js +18 -0
- package/dist/jwt/types/jwt-auth.types.d.ts +44 -0
- package/dist/jwt/types/jwt-auth.types.js +2 -0
- package/dist/o-auth/abstract-o-auth.service.d.ts +30 -0
- package/dist/o-auth/abstract-o-auth.service.js +64 -0
- package/dist/o-auth/o-auth.namespace.d.ts +131 -0
- package/dist/o-auth/o-auth.namespace.js +256 -0
- package/dist/o-auth/sdk/base-ext-o-auth-sdk.d.ts +104 -0
- package/dist/o-auth/sdk/base-ext-o-auth-sdk.js +56 -0
- package/dist/route-auth-configs.helper.d.ts +29 -0
- package/dist/route-auth-configs.helper.js +36 -0
- package/dist/services/abstrac-auth-sessions.service.d.ts +28 -0
- package/dist/services/abstrac-auth-sessions.service.js +41 -0
- package/dist/services/abstract-sessions-repo.service.d.ts +28 -0
- package/dist/services/abstract-sessions-repo.service.js +28 -0
- package/dist/types/app-request.inteface.d.ts +14 -0
- package/dist/types/app-request.inteface.js +2 -0
- package/dist/types/auth.types.d.ts +43 -0
- package/dist/types/auth.types.js +12 -0
- package/dist/types/ws-client.types.d.ts +14 -0
- package/dist/types/ws-client.types.js +2 -0
- package/package.json +95 -3
|
@@ -0,0 +1,170 @@
|
|
|
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
19
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
20
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
21
|
+
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;
|
|
22
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
23
|
+
};
|
|
24
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
25
|
+
if (mod && mod.__esModule) return mod;
|
|
26
|
+
var result = {};
|
|
27
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
28
|
+
__setModuleDefault(result, mod);
|
|
29
|
+
return result;
|
|
30
|
+
};
|
|
31
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
32
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
33
|
+
};
|
|
34
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
35
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
36
|
+
};
|
|
37
|
+
var JwtAuthService_1;
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.JwtAuthService = exports.JWT_AUTH_MODULE_OPTIONS = void 0;
|
|
40
|
+
const common_1 = require("@nestjs/common");
|
|
41
|
+
const jwt = __importStar(require("jsonwebtoken"));
|
|
42
|
+
const core_1 = require("@my-nestjs-kit/core");
|
|
43
|
+
const enums_1 = require("./types/enums");
|
|
44
|
+
exports.JWT_AUTH_MODULE_OPTIONS = Symbol('JWT_AUTH_MODULE_OPTIONS');
|
|
45
|
+
const ALLOWED_ALGORITHMS = Object.values(enums_1.JwtAlgorithmEnum);
|
|
46
|
+
/**
|
|
47
|
+
* Typed JWT sign/verify per token type (ACCESS / REFRESH / VERIFICATION / ...).
|
|
48
|
+
* - key rotation: tokens carry `kid`; verification picks the key by `kid` (verify-only keys stay valid)
|
|
49
|
+
* - every token carries a `typ` claim, checked on verify (an access token is never accepted as refresh)
|
|
50
|
+
* - `aud` / `iss` are enforced when configured
|
|
51
|
+
* - only HMAC algorithms; any verify failure is a 401 AppHttpException
|
|
52
|
+
*/
|
|
53
|
+
let JwtAuthService = JwtAuthService_1 = class JwtAuthService {
|
|
54
|
+
constructor(options) {
|
|
55
|
+
this.options = options;
|
|
56
|
+
this._logger = new common_1.Logger(JwtAuthService_1.name);
|
|
57
|
+
this._types = new Map();
|
|
58
|
+
for (const [type, typeOptions] of Object.entries(options.tokens ?? {})) {
|
|
59
|
+
if (!typeOptions)
|
|
60
|
+
continue;
|
|
61
|
+
this._types.set(type, this.resolveType(type, typeOptions));
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
resolveType(type, typeOptions) {
|
|
65
|
+
const keys = (typeOptions.keys ?? []).filter((k) => k?.kid && k?.secret);
|
|
66
|
+
const signKey = keys.find((k) => k.canSign !== false);
|
|
67
|
+
if (!signKey) {
|
|
68
|
+
throw new Error(`[JwtAuth] ${type}: no signing key configured`);
|
|
69
|
+
}
|
|
70
|
+
const algorithm = typeOptions.algorithm ?? enums_1.JwtAlgorithmEnum.HS256;
|
|
71
|
+
if (!ALLOWED_ALGORITHMS.includes(algorithm)) {
|
|
72
|
+
throw new Error(`[JwtAuth] ${type}: algorithm ${algorithm} is not allowed`);
|
|
73
|
+
}
|
|
74
|
+
return {
|
|
75
|
+
...typeOptions,
|
|
76
|
+
keys,
|
|
77
|
+
signKey,
|
|
78
|
+
algorithm,
|
|
79
|
+
byKid: new Map(keys.map((k) => [k.kid, k])),
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
getType(type) {
|
|
83
|
+
const resolved = this._types.get(type);
|
|
84
|
+
if (!resolved) {
|
|
85
|
+
throw new core_1.AppHttpException({
|
|
86
|
+
statusCode: common_1.HttpStatus.INTERNAL_SERVER_ERROR,
|
|
87
|
+
message: `JWT token type "${type}" is not configured`,
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
return resolved;
|
|
91
|
+
}
|
|
92
|
+
hasType(type) {
|
|
93
|
+
return this._types.has(type);
|
|
94
|
+
}
|
|
95
|
+
signAsync(type, payload, params = {}) {
|
|
96
|
+
const conf = this.getType(type);
|
|
97
|
+
// a caller-provided `exp` (seconds from now) wins over the configured lifetime — ERP AppJwtService compat
|
|
98
|
+
const { exp, ...rest } = payload;
|
|
99
|
+
const expiresIn = params.expiresIn ??
|
|
100
|
+
(typeof exp === 'number' && exp > 0 ? exp : conf.expiresIn);
|
|
101
|
+
const audience = params.audience ?? this.options.defaultAudience;
|
|
102
|
+
return new Promise((resolve, reject) => jwt.sign({ ...rest, typ: type }, conf.signKey.secret, {
|
|
103
|
+
algorithm: conf.algorithm,
|
|
104
|
+
keyid: conf.signKey.kid,
|
|
105
|
+
...(expiresIn !== undefined ? { expiresIn: expiresIn } : {}),
|
|
106
|
+
...(audience ? { audience } : {}),
|
|
107
|
+
...(this.options.issuer ? { issuer: this.options.issuer } : {}),
|
|
108
|
+
}, (err, token) => (err ? reject(err) : resolve(token))));
|
|
109
|
+
}
|
|
110
|
+
verify(type, token, params = {}) {
|
|
111
|
+
const conf = this.getType(type);
|
|
112
|
+
try {
|
|
113
|
+
const key = this.pickKey(conf, token);
|
|
114
|
+
const payload = jwt.verify(token, key.secret, this.verifyOptions(conf, params));
|
|
115
|
+
return this.checkType(type, payload);
|
|
116
|
+
}
|
|
117
|
+
catch (e) {
|
|
118
|
+
throw this.unauthorized(type, e);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
async verifyAsync(type, token, params = {}) {
|
|
122
|
+
return this.verify(type, token, params);
|
|
123
|
+
}
|
|
124
|
+
/** decodes without verifying — never trust the result for auth decisions */
|
|
125
|
+
decode(token) {
|
|
126
|
+
return jwt.decode(token, { complete: true });
|
|
127
|
+
}
|
|
128
|
+
verifyOptions(conf, params) {
|
|
129
|
+
const audience = params.audience ?? this.options.defaultAudience;
|
|
130
|
+
return {
|
|
131
|
+
algorithms: [conf.algorithm],
|
|
132
|
+
...(audience ? { audience: audience } : {}),
|
|
133
|
+
...(this.options.issuer ? { issuer: this.options.issuer } : {}),
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
pickKey(conf, token) {
|
|
137
|
+
const decoded = jwt.decode(token, { complete: true });
|
|
138
|
+
if (!decoded)
|
|
139
|
+
throw new Error('Malformed token');
|
|
140
|
+
const kid = decoded.header?.kid ?? decoded.payload?.version; // `version` = ERP app-jwt tokens
|
|
141
|
+
const key = kid ? conf.byKid.get(kid) : conf.signKey;
|
|
142
|
+
if (!key)
|
|
143
|
+
throw new Error(`Unknown key id "${kid}"`);
|
|
144
|
+
return key;
|
|
145
|
+
}
|
|
146
|
+
checkType(type, payload) {
|
|
147
|
+
if (payload.typ !== type) {
|
|
148
|
+
throw new Error(`Token type mismatch: expected ${type}, got ${payload.typ ?? 'none'}`);
|
|
149
|
+
}
|
|
150
|
+
return payload;
|
|
151
|
+
}
|
|
152
|
+
unauthorized(type, e) {
|
|
153
|
+
if (e instanceof core_1.AppHttpException)
|
|
154
|
+
return e;
|
|
155
|
+
const reason = e instanceof Error ? e.message : String(e);
|
|
156
|
+
this._logger.debug(`${type} token rejected: ${reason}`);
|
|
157
|
+
return new core_1.AppHttpException({
|
|
158
|
+
statusCode: common_1.HttpStatus.UNAUTHORIZED,
|
|
159
|
+
message: 'Invalid or expired token',
|
|
160
|
+
reason,
|
|
161
|
+
meta: { tokenType: type },
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
exports.JwtAuthService = JwtAuthService;
|
|
166
|
+
exports.JwtAuthService = JwtAuthService = JwtAuthService_1 = __decorate([
|
|
167
|
+
(0, common_1.Injectable)(),
|
|
168
|
+
__param(0, (0, common_1.Inject)(exports.JWT_AUTH_MODULE_OPTIONS)),
|
|
169
|
+
__metadata("design:paramtypes", [Object])
|
|
170
|
+
], JwtAuthService);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { JwtKey } from './types/jwt-auth.types';
|
|
2
|
+
export interface ParseJwtKeysContext {
|
|
3
|
+
/** keys tagged with another `stage=` / `host=` are skipped */
|
|
4
|
+
stage?: string;
|
|
5
|
+
host?: string;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Reads JWT keys from an env value. Supported formats:
|
|
9
|
+
* - ERP query-string list: `?id=id1&secret=s1&stage=dev?id=id2&secret=s2&sign=false`
|
|
10
|
+
* - a plain secret: `s1` (single key, kid `def`)
|
|
11
|
+
*/
|
|
12
|
+
export declare function parseJwtKeys(value: string | undefined, ctx?: ParseJwtKeysContext): JwtKey[];
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseJwtKeys = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Reads JWT keys from an env value. Supported formats:
|
|
6
|
+
* - ERP query-string list: `?id=id1&secret=s1&stage=dev?id=id2&secret=s2&sign=false`
|
|
7
|
+
* - a plain secret: `s1` (single key, kid `def`)
|
|
8
|
+
*/
|
|
9
|
+
function parseJwtKeys(value, ctx = {}) {
|
|
10
|
+
if (!value?.trim())
|
|
11
|
+
return [];
|
|
12
|
+
const raw = value.trim();
|
|
13
|
+
if (!raw.includes('='))
|
|
14
|
+
return [{ kid: 'def', secret: raw, canSign: true }];
|
|
15
|
+
return raw
|
|
16
|
+
.replace(/^\?/, '')
|
|
17
|
+
.split('?')
|
|
18
|
+
.map((chunk) => new URLSearchParams(chunk.trim()))
|
|
19
|
+
.filter((q) => {
|
|
20
|
+
const stage = q.get('stage');
|
|
21
|
+
const host = q.get('host') ?? q.get('provider');
|
|
22
|
+
return ((!stage || !ctx.stage || stage === ctx.stage) &&
|
|
23
|
+
(!host || !ctx.host || host === ctx.host));
|
|
24
|
+
})
|
|
25
|
+
.filter((q) => q.get('id') && q.get('secret'))
|
|
26
|
+
.map((q) => ({
|
|
27
|
+
kid: q.get('id'),
|
|
28
|
+
secret: q.get('secret'),
|
|
29
|
+
canSign: q.get('sign') !== 'false',
|
|
30
|
+
}));
|
|
31
|
+
}
|
|
32
|
+
exports.parseJwtKeys = parseJwtKeys;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export declare enum JwtTypeEnum {
|
|
2
|
+
ACCESS = "ACCESS",
|
|
3
|
+
REFRESH = "REFRESH",
|
|
4
|
+
VERIFICATION = "VERIFICATION",
|
|
5
|
+
CREDENTIALS_CONFIRM = "CREDENTIALS_CONFIRM"
|
|
6
|
+
}
|
|
7
|
+
export type JwtType = keyof typeof JwtTypeEnum;
|
|
8
|
+
export declare enum JwtAudienceEnum {
|
|
9
|
+
USER = "USER",
|
|
10
|
+
CUSTOMER = "CUSTOMER",
|
|
11
|
+
PERMISSION = "PERMISSION",
|
|
12
|
+
INTEGRATION = "INTEGRATION",
|
|
13
|
+
ADMIN = "ADMIN"
|
|
14
|
+
}
|
|
15
|
+
export declare enum JwtAlgorithmEnum {
|
|
16
|
+
HS256 = "HS256",
|
|
17
|
+
HS384 = "HS384",
|
|
18
|
+
HS512 = "HS512"
|
|
19
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.JwtAlgorithmEnum = exports.JwtAudienceEnum = exports.JwtTypeEnum = void 0;
|
|
4
|
+
var JwtTypeEnum;
|
|
5
|
+
(function (JwtTypeEnum) {
|
|
6
|
+
JwtTypeEnum["ACCESS"] = "ACCESS";
|
|
7
|
+
JwtTypeEnum["REFRESH"] = "REFRESH";
|
|
8
|
+
JwtTypeEnum["VERIFICATION"] = "VERIFICATION";
|
|
9
|
+
JwtTypeEnum["CREDENTIALS_CONFIRM"] = "CREDENTIALS_CONFIRM";
|
|
10
|
+
})(JwtTypeEnum || (exports.JwtTypeEnum = JwtTypeEnum = {}));
|
|
11
|
+
// Who the token is for (`aud` claim). Services may use their own string audiences as well.
|
|
12
|
+
var JwtAudienceEnum;
|
|
13
|
+
(function (JwtAudienceEnum) {
|
|
14
|
+
JwtAudienceEnum["USER"] = "USER";
|
|
15
|
+
JwtAudienceEnum["CUSTOMER"] = "CUSTOMER";
|
|
16
|
+
JwtAudienceEnum["PERMISSION"] = "PERMISSION";
|
|
17
|
+
JwtAudienceEnum["INTEGRATION"] = "INTEGRATION";
|
|
18
|
+
JwtAudienceEnum["ADMIN"] = "ADMIN";
|
|
19
|
+
})(JwtAudienceEnum || (exports.JwtAudienceEnum = JwtAudienceEnum = {}));
|
|
20
|
+
// Shared-secret algorithms only: keys are HMAC secrets from env. `none` is deliberately absent.
|
|
21
|
+
var JwtAlgorithmEnum;
|
|
22
|
+
(function (JwtAlgorithmEnum) {
|
|
23
|
+
JwtAlgorithmEnum["HS256"] = "HS256";
|
|
24
|
+
JwtAlgorithmEnum["HS384"] = "HS384";
|
|
25
|
+
JwtAlgorithmEnum["HS512"] = "HS512";
|
|
26
|
+
})(JwtAlgorithmEnum || (exports.JwtAlgorithmEnum = JwtAlgorithmEnum = {}));
|
|
@@ -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("./enums"), exports);
|
|
18
|
+
__exportStar(require("./jwt-auth.types"), exports);
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { ModuleMetadata } from '@nestjs/common/interfaces';
|
|
2
|
+
import { JwtAlgorithmEnum, JwtType } from './enums';
|
|
3
|
+
export interface JwtKey {
|
|
4
|
+
/** key id, written to the token header (`kid`) and used to pick the verify key */
|
|
5
|
+
kid: string;
|
|
6
|
+
secret: string;
|
|
7
|
+
/** false -> verify-only (a rotated-out key that still validates old tokens) */
|
|
8
|
+
canSign?: boolean;
|
|
9
|
+
}
|
|
10
|
+
export interface JwtTokenTypeOptions {
|
|
11
|
+
/** first signing-capable key signs; all keys verify */
|
|
12
|
+
keys: JwtKey[];
|
|
13
|
+
/** seconds or a vercel/ms string ('15m', '7d') */
|
|
14
|
+
expiresIn?: string | number;
|
|
15
|
+
algorithm?: JwtAlgorithmEnum;
|
|
16
|
+
}
|
|
17
|
+
export interface JwtAuthModuleOptions {
|
|
18
|
+
issuer?: string;
|
|
19
|
+
/** audience used when a call does not pass one */
|
|
20
|
+
defaultAudience?: string;
|
|
21
|
+
tokens: Partial<Record<JwtType, JwtTokenTypeOptions>>;
|
|
22
|
+
}
|
|
23
|
+
export interface JwtAuthModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
|
|
24
|
+
useFactory: (...args: any[]) => Promise<JwtAuthModuleOptions> | JwtAuthModuleOptions;
|
|
25
|
+
inject?: any[];
|
|
26
|
+
isGlobal?: boolean;
|
|
27
|
+
}
|
|
28
|
+
export interface JwtSignParams {
|
|
29
|
+
audience?: string;
|
|
30
|
+
/** overrides the token type `expiresIn` */
|
|
31
|
+
expiresIn?: string | number;
|
|
32
|
+
}
|
|
33
|
+
export interface JwtVerifyParams {
|
|
34
|
+
/** accepted audience(s); defaults to the module `defaultAudience` (if set) */
|
|
35
|
+
audience?: string | string[];
|
|
36
|
+
}
|
|
37
|
+
/** claims every kit token carries in addition to the caller payload */
|
|
38
|
+
export interface JwtStandardClaims {
|
|
39
|
+
typ: JwtType;
|
|
40
|
+
aud?: string | string[];
|
|
41
|
+
iss?: string;
|
|
42
|
+
iat?: number;
|
|
43
|
+
exp?: number;
|
|
44
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { AuthSessionBaseEntity } from '../entities/auth-session-base.entity';
|
|
2
|
+
import { AbstractAuthSessionsAuthService } from '../services/abstrac-auth-sessions.service';
|
|
3
|
+
import { AbstractAuthSessionRepoService } from '../services/abstract-sessions-repo.service';
|
|
4
|
+
import { DataSource } from 'typeorm';
|
|
5
|
+
import { AppHttpException, ExceptionResponseArgs } from '@my-nestjs-kit/core';
|
|
6
|
+
import { OAuth } from './o-auth.namespace';
|
|
7
|
+
import { AccessTokensService } from '../access-tokens/access-tokens.service';
|
|
8
|
+
export declare abstract class AbstractOAuthService<Target extends AuthSessionBaseEntity = AuthSessionBaseEntity, SessionsAuthService extends AbstractAuthSessionsAuthService<Target> = AbstractAuthSessionsAuthService<Target>, SessionsRepoService extends AbstractAuthSessionRepoService<Target> = AbstractAuthSessionRepoService<Target>> {
|
|
9
|
+
protected readonly meta: any;
|
|
10
|
+
protected readonly dSource: DataSource;
|
|
11
|
+
protected readonly _sessionsRepoSrv: SessionsRepoService;
|
|
12
|
+
protected readonly _sessionsAuthSrv: SessionsAuthService;
|
|
13
|
+
protected readonly accessTokensSrv: AccessTokensService;
|
|
14
|
+
static ACCESS_TOKENS_NAMESPACE: string;
|
|
15
|
+
protected constructor(meta: any, dSource: DataSource, _sessionsRepoSrv: SessionsRepoService, _sessionsAuthSrv: SessionsAuthService, accessTokensSrv: AccessTokensService);
|
|
16
|
+
findAccessToken(connectionId: string, code: string): Promise<import("..").AccessToken>;
|
|
17
|
+
getAccessToken(connectionId: string, code: string): Promise<import("..").AccessToken>;
|
|
18
|
+
deleteAccessToken(connectionId: string, code: string): Promise<{
|
|
19
|
+
result: boolean;
|
|
20
|
+
}>;
|
|
21
|
+
setAccessToken(connectionId: string, data: {
|
|
22
|
+
holderId: string;
|
|
23
|
+
trackId?: string;
|
|
24
|
+
}): Promise<import("..").AccessToken>;
|
|
25
|
+
fail(exc: ExceptionResponseArgs | AppHttpException): void;
|
|
26
|
+
abstract saveTokens<T>(input: {
|
|
27
|
+
dto: OAuth.Server.SaveExtTokensDto;
|
|
28
|
+
connection: OAuth.Connection.Type;
|
|
29
|
+
}): Promise<Target>;
|
|
30
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AbstractOAuthService = void 0;
|
|
4
|
+
const core_1 = require("@my-nestjs-kit/core");
|
|
5
|
+
const access_tokens_types_1 = require("../access-tokens/access-tokens.types");
|
|
6
|
+
class AbstractOAuthService {
|
|
7
|
+
constructor(meta, dSource, _sessionsRepoSrv, _sessionsAuthSrv, accessTokensSrv) {
|
|
8
|
+
this.meta = meta;
|
|
9
|
+
this.dSource = dSource;
|
|
10
|
+
this._sessionsRepoSrv = _sessionsRepoSrv;
|
|
11
|
+
this._sessionsAuthSrv = _sessionsAuthSrv;
|
|
12
|
+
this.accessTokensSrv = accessTokensSrv;
|
|
13
|
+
}
|
|
14
|
+
async findAccessToken(connectionId, code) {
|
|
15
|
+
const token = await this.accessTokensSrv.get({
|
|
16
|
+
namespace: AbstractOAuthService.ACCESS_TOKENS_NAMESPACE,
|
|
17
|
+
queries: {
|
|
18
|
+
connectionId,
|
|
19
|
+
code,
|
|
20
|
+
},
|
|
21
|
+
});
|
|
22
|
+
return token;
|
|
23
|
+
}
|
|
24
|
+
async getAccessToken(connectionId, code) {
|
|
25
|
+
const token = await this.accessTokensSrv.get({
|
|
26
|
+
namespace: AbstractOAuthService.ACCESS_TOKENS_NAMESPACE,
|
|
27
|
+
queries: {
|
|
28
|
+
connectionId,
|
|
29
|
+
code,
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
return token;
|
|
33
|
+
}
|
|
34
|
+
async deleteAccessToken(connectionId, code) {
|
|
35
|
+
return await this.accessTokensSrv.delete({
|
|
36
|
+
namespace: AbstractOAuthService.ACCESS_TOKENS_NAMESPACE,
|
|
37
|
+
queries: {
|
|
38
|
+
connectionId,
|
|
39
|
+
code,
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
async setAccessToken(connectionId, data) {
|
|
44
|
+
const code = this.accessTokensSrv.generateCode();
|
|
45
|
+
return await this.accessTokensSrv.set({
|
|
46
|
+
namespace: AbstractOAuthService.ACCESS_TOKENS_NAMESPACE,
|
|
47
|
+
queries: {
|
|
48
|
+
connectionId,
|
|
49
|
+
code,
|
|
50
|
+
},
|
|
51
|
+
data: {
|
|
52
|
+
type: access_tokens_types_1.AccessTokenTypeEnum.code,
|
|
53
|
+
value: code,
|
|
54
|
+
...data,
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
// TODO need to be extended from base service
|
|
59
|
+
fail(exc) {
|
|
60
|
+
throw exc instanceof core_1.AppHttpException ? exc : new core_1.AppHttpException(exc);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
exports.AbstractOAuthService = AbstractOAuthService;
|
|
64
|
+
AbstractOAuthService.ACCESS_TOKENS_NAMESPACE = 'o_auth_code';
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { HttpApi } from '@my-nestjs-kit/core';
|
|
3
|
+
import { AuthConfigsMetaStorage } from '../route-auth-configs.helper';
|
|
4
|
+
import { CreateAuthSessionDto } from '../auth-sessions/dto/create-auth-session.dto';
|
|
5
|
+
import { ProviderNameEnum } from '@my-nestjs-kit/core';
|
|
6
|
+
import { UUID } from 'node:crypto';
|
|
7
|
+
export declare namespace OAuth {
|
|
8
|
+
namespace Provider {
|
|
9
|
+
export const configsMap: {
|
|
10
|
+
facebook: {
|
|
11
|
+
scopes: readonly ["email", "openID", "profile"];
|
|
12
|
+
idKey: string;
|
|
13
|
+
};
|
|
14
|
+
google: {
|
|
15
|
+
scopes: readonly ["email", "openID", "profile"];
|
|
16
|
+
idKey: string;
|
|
17
|
+
};
|
|
18
|
+
refme: {
|
|
19
|
+
scopes: readonly ["email", "openID", "reference", "profile", "wallets"];
|
|
20
|
+
idKey: string;
|
|
21
|
+
};
|
|
22
|
+
mia: {
|
|
23
|
+
scopes: readonly ["email", "openID", "reference", "profile"];
|
|
24
|
+
idKey: string;
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
class ExtraDataJson {
|
|
28
|
+
[key: string]: any;
|
|
29
|
+
}
|
|
30
|
+
export namespace ExtraDataJsonByType {
|
|
31
|
+
class Google {
|
|
32
|
+
engagementSource?: string;
|
|
33
|
+
}
|
|
34
|
+
class Refme {
|
|
35
|
+
reference?: string;
|
|
36
|
+
engagementSource?: string;
|
|
37
|
+
}
|
|
38
|
+
class Mia {
|
|
39
|
+
reference?: string;
|
|
40
|
+
engagementSource?: string;
|
|
41
|
+
}
|
|
42
|
+
class MappedData extends ExtraDataJson {
|
|
43
|
+
[ProviderNameEnum.google]: Google;
|
|
44
|
+
[ProviderNameEnum.facebook]: ExtraDataJson;
|
|
45
|
+
[ProviderNameEnum.meta]: ExtraDataJson;
|
|
46
|
+
[ProviderNameEnum.refme]: Refme;
|
|
47
|
+
[ProviderNameEnum.mia]: Mia;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
export type Type = keyof typeof ProviderNameEnum | ProviderNameEnum | string;
|
|
51
|
+
export {};
|
|
52
|
+
}
|
|
53
|
+
namespace Connection {
|
|
54
|
+
export interface Entity<Provider extends string = ProviderNameEnum> {
|
|
55
|
+
_id?: string;
|
|
56
|
+
provider?: Provider;
|
|
57
|
+
label?: string;
|
|
58
|
+
publicKey?: string;
|
|
59
|
+
privateKey?: string;
|
|
60
|
+
scopes?: string[];
|
|
61
|
+
[key: string]: any;
|
|
62
|
+
}
|
|
63
|
+
export type Type<Provider extends ProviderNameEnum = ProviderNameEnum> = Entity<Provider>;
|
|
64
|
+
interface BaseEntity {
|
|
65
|
+
supportInfo?: {
|
|
66
|
+
email?: string;
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
export type Dto = BaseEntity;
|
|
70
|
+
export {};
|
|
71
|
+
}
|
|
72
|
+
enum ActionType {
|
|
73
|
+
callback = "callback",
|
|
74
|
+
auth_url = "auth-url",
|
|
75
|
+
save_tokens = "save-tokens",
|
|
76
|
+
logIn = "logIn"
|
|
77
|
+
}
|
|
78
|
+
type ProviderType = Provider.Type;
|
|
79
|
+
enum TokenType {
|
|
80
|
+
Bearer = "Bearer"
|
|
81
|
+
}
|
|
82
|
+
class MetaStorage extends AuthConfigsMetaStorage {
|
|
83
|
+
static KEY: string;
|
|
84
|
+
isSecure?: boolean;
|
|
85
|
+
isPublic?: boolean;
|
|
86
|
+
isRefresh?: boolean;
|
|
87
|
+
isVerifyCode?: string;
|
|
88
|
+
constructor(payload: MetaStorage);
|
|
89
|
+
}
|
|
90
|
+
function RouteMeta(meta: MetaStorage): <TFunction extends Function, Y>(target: object | TFunction, propertyKey?: string | symbol, descriptor?: TypedPropertyDescriptor<Y>) => void;
|
|
91
|
+
namespace Server {
|
|
92
|
+
export type Request<Query = OAuth.Server.BaseIncomingQuery> = HttpApi.Request<any, Query>;
|
|
93
|
+
export class BaseIncomingQuery<Provider = ProviderNameEnum> {
|
|
94
|
+
publicKey: string;
|
|
95
|
+
privateKey?: string;
|
|
96
|
+
code?: string;
|
|
97
|
+
redirectUri?: string;
|
|
98
|
+
scopes?: string[];
|
|
99
|
+
provider?: Provider;
|
|
100
|
+
constructor(payload?: Partial<BaseIncomingQuery<Provider>>);
|
|
101
|
+
}
|
|
102
|
+
export class GetAuthUrlQuery extends BaseIncomingQuery {
|
|
103
|
+
}
|
|
104
|
+
const SaveExtTokensDto_base: import("@nestjs/common").Type<CreateAuthSessionDto & BaseIncomingQuery<unknown>>;
|
|
105
|
+
export class SaveExtTokensDto<Provider extends OAuth.ProviderType = ProviderNameEnum> extends SaveExtTokensDto_base {
|
|
106
|
+
access_token: string;
|
|
107
|
+
refresh_token: string;
|
|
108
|
+
email: string;
|
|
109
|
+
userId: UUID;
|
|
110
|
+
extId: string;
|
|
111
|
+
reference?: string;
|
|
112
|
+
tokenType?: TokenType | string;
|
|
113
|
+
provider?: Provider;
|
|
114
|
+
}
|
|
115
|
+
export {};
|
|
116
|
+
}
|
|
117
|
+
namespace Client {
|
|
118
|
+
class GetAuthUrlQuery extends Server.BaseIncomingQuery {
|
|
119
|
+
provider: ProviderNameEnum;
|
|
120
|
+
}
|
|
121
|
+
class CallbackQuery extends Server.BaseIncomingQuery {
|
|
122
|
+
code: string;
|
|
123
|
+
state?: string;
|
|
124
|
+
scope?: string;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
function getConfigsByProvider(provider: OAuth.ProviderType): {
|
|
128
|
+
getId: (entry: any) => string;
|
|
129
|
+
};
|
|
130
|
+
namespace Consumer { }
|
|
131
|
+
}
|