@xlt-token/nestjs 1.0.0-rc.3
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 +29 -0
- package/dist/index.cjs +687 -0
- package/dist/index.d.cts +233 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +233 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +525 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +90 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,687 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
let _nestjs_common = require("@nestjs/common");
|
|
3
|
+
let _xlt_token_core = require("@xlt-token/core");
|
|
4
|
+
let jsonwebtoken = require("jsonwebtoken");
|
|
5
|
+
let _nestjs_core = require("@nestjs/core");
|
|
6
|
+
|
|
7
|
+
//#region \0@oxc-project+runtime@0.112.0/helpers/decorate.js
|
|
8
|
+
function __decorate(decorators, target, key, desc) {
|
|
9
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
10
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
11
|
+
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;
|
|
12
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
//#endregion
|
|
16
|
+
//#region src/xlt-token.module.ts
|
|
17
|
+
var _XltTokenModule;
|
|
18
|
+
let XltTokenModule = class XltTokenModule {
|
|
19
|
+
static {
|
|
20
|
+
_XltTokenModule = this;
|
|
21
|
+
}
|
|
22
|
+
static createStoreProvider(store) {
|
|
23
|
+
if (!store) return {
|
|
24
|
+
provide: _xlt_token_core.XLT_TOKEN_STORE,
|
|
25
|
+
useClass: _xlt_token_core.MemoryStore
|
|
26
|
+
};
|
|
27
|
+
return "useClass" in store ? {
|
|
28
|
+
provide: _xlt_token_core.XLT_TOKEN_STORE,
|
|
29
|
+
useClass: store.useClass
|
|
30
|
+
} : {
|
|
31
|
+
provide: _xlt_token_core.XLT_TOKEN_STORE,
|
|
32
|
+
useValue: store.useValue
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
static createStrategyProvider(strategy) {
|
|
36
|
+
return strategy?.useClass ? {
|
|
37
|
+
provide: _xlt_token_core.XLT_TOKEN_STRATEGY,
|
|
38
|
+
useClass: strategy.useClass
|
|
39
|
+
} : {
|
|
40
|
+
provide: _xlt_token_core.XLT_TOKEN_STRATEGY,
|
|
41
|
+
useClass: _xlt_token_core.UuidStrategy
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
static createStpInterfaceProvider(stpInterface) {
|
|
45
|
+
if (stpInterface) return {
|
|
46
|
+
provide: _xlt_token_core.XLT_STP_INTERFACE,
|
|
47
|
+
useClass: stpInterface
|
|
48
|
+
};
|
|
49
|
+
return {
|
|
50
|
+
provide: _xlt_token_core.XLT_STP_INTERFACE,
|
|
51
|
+
useValue: {
|
|
52
|
+
getPermissionList: () => {
|
|
53
|
+
throw new Error("StpInterface not registered: getPermissionList");
|
|
54
|
+
},
|
|
55
|
+
getRoleList: () => {
|
|
56
|
+
throw new Error("StpInterface not registered: getRoleList");
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
static createHooksProvider(hooks) {
|
|
62
|
+
return {
|
|
63
|
+
provide: _xlt_token_core.XLT_TOKEN_HOOKS,
|
|
64
|
+
useValue: hooks ?? {}
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
static {
|
|
68
|
+
this.stpLogicProvider = {
|
|
69
|
+
provide: _xlt_token_core.StpLogic,
|
|
70
|
+
useFactory: (config, store, strategy, hooks) => new _xlt_token_core.StpLogic(config, store, strategy, hooks),
|
|
71
|
+
inject: [
|
|
72
|
+
_xlt_token_core.XLT_TOKEN_CONFIG,
|
|
73
|
+
_xlt_token_core.XLT_TOKEN_STORE,
|
|
74
|
+
_xlt_token_core.XLT_TOKEN_STRATEGY,
|
|
75
|
+
_xlt_token_core.XLT_TOKEN_HOOKS
|
|
76
|
+
]
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
static {
|
|
80
|
+
this.stpPermLogicProvider = {
|
|
81
|
+
provide: _xlt_token_core.StpPermLogic,
|
|
82
|
+
useFactory: (stpInterface, store, config) => new _xlt_token_core.StpPermLogic(stpInterface, store, config),
|
|
83
|
+
inject: [
|
|
84
|
+
_xlt_token_core.XLT_STP_INTERFACE,
|
|
85
|
+
_xlt_token_core.XLT_TOKEN_STORE,
|
|
86
|
+
_xlt_token_core.XLT_TOKEN_CONFIG
|
|
87
|
+
]
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
static {
|
|
91
|
+
this.initProvider = {
|
|
92
|
+
provide: "XLT_TOKEN_INIT",
|
|
93
|
+
useFactory: (stpLogic, stpPermLogic) => {
|
|
94
|
+
(0, _xlt_token_core.setStpLogic)(stpLogic);
|
|
95
|
+
(0, _xlt_token_core.setStpPermLogic)(stpPermLogic);
|
|
96
|
+
return true;
|
|
97
|
+
},
|
|
98
|
+
inject: [_xlt_token_core.StpLogic, _xlt_token_core.StpPermLogic]
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
static {
|
|
102
|
+
this.moduleExports = [
|
|
103
|
+
_xlt_token_core.XLT_TOKEN_CONFIG,
|
|
104
|
+
_xlt_token_core.XLT_TOKEN_STORE,
|
|
105
|
+
_xlt_token_core.XLT_TOKEN_STRATEGY,
|
|
106
|
+
_xlt_token_core.StpLogic,
|
|
107
|
+
_xlt_token_core.StpPermLogic
|
|
108
|
+
];
|
|
109
|
+
}
|
|
110
|
+
static forRoot(options = {}) {
|
|
111
|
+
const { config: userConfig, store, strategy, isGlobal = false, providers = [], stpInterface } = options;
|
|
112
|
+
return {
|
|
113
|
+
module: _XltTokenModule,
|
|
114
|
+
providers: [
|
|
115
|
+
{
|
|
116
|
+
provide: _xlt_token_core.XLT_TOKEN_CONFIG,
|
|
117
|
+
useValue: {
|
|
118
|
+
..._xlt_token_core.DEFAULT_XLT_TOKEN_CONFIG,
|
|
119
|
+
...userConfig
|
|
120
|
+
}
|
|
121
|
+
},
|
|
122
|
+
_XltTokenModule.createStoreProvider(store),
|
|
123
|
+
_XltTokenModule.createStrategyProvider(strategy),
|
|
124
|
+
_XltTokenModule.createStpInterfaceProvider(stpInterface),
|
|
125
|
+
_XltTokenModule.createHooksProvider(options.hooks),
|
|
126
|
+
_XltTokenModule.stpLogicProvider,
|
|
127
|
+
_XltTokenModule.stpPermLogicProvider,
|
|
128
|
+
_XltTokenModule.initProvider,
|
|
129
|
+
...providers
|
|
130
|
+
],
|
|
131
|
+
exports: _XltTokenModule.moduleExports,
|
|
132
|
+
global: isGlobal
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
static forRootAsync(options) {
|
|
136
|
+
const { useFactory, inject = [], imports = [], store, strategy, isGlobal = false, providers = [], stpInterface } = options;
|
|
137
|
+
return {
|
|
138
|
+
module: _XltTokenModule,
|
|
139
|
+
imports,
|
|
140
|
+
providers: [
|
|
141
|
+
{
|
|
142
|
+
provide: _xlt_token_core.XLT_TOKEN_CONFIG,
|
|
143
|
+
useFactory: async (...args) => {
|
|
144
|
+
const { config = {} } = await useFactory(...args);
|
|
145
|
+
return {
|
|
146
|
+
..._xlt_token_core.DEFAULT_XLT_TOKEN_CONFIG,
|
|
147
|
+
...config
|
|
148
|
+
};
|
|
149
|
+
},
|
|
150
|
+
inject
|
|
151
|
+
},
|
|
152
|
+
_XltTokenModule.createStoreProvider(store),
|
|
153
|
+
_XltTokenModule.createStrategyProvider(strategy),
|
|
154
|
+
_XltTokenModule.createStpInterfaceProvider(stpInterface),
|
|
155
|
+
_XltTokenModule.createHooksProvider(options.hooks),
|
|
156
|
+
_XltTokenModule.stpLogicProvider,
|
|
157
|
+
_XltTokenModule.stpPermLogicProvider,
|
|
158
|
+
_XltTokenModule.initProvider,
|
|
159
|
+
...providers
|
|
160
|
+
],
|
|
161
|
+
exports: _XltTokenModule.moduleExports,
|
|
162
|
+
global: isGlobal
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
XltTokenModule = _XltTokenModule = __decorate([(0, _nestjs_common.Module)({})], XltTokenModule);
|
|
167
|
+
|
|
168
|
+
//#endregion
|
|
169
|
+
//#region \0@oxc-project+runtime@0.112.0/helpers/decorateMetadata.js
|
|
170
|
+
function __decorateMetadata(k, v) {
|
|
171
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
//#endregion
|
|
175
|
+
//#region \0@oxc-project+runtime@0.112.0/helpers/decorateParam.js
|
|
176
|
+
function __decorateParam(paramIndex, decorator) {
|
|
177
|
+
return function(target, key) {
|
|
178
|
+
decorator(target, key, paramIndex);
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
//#endregion
|
|
183
|
+
//#region src/store/redis-store.ts
|
|
184
|
+
const XLT_REDIS_CLIENT = "XLT_REDIS_CLIENT";
|
|
185
|
+
let RedisStore = class RedisStore {
|
|
186
|
+
constructor(redisClient) {
|
|
187
|
+
this.redisClient = redisClient;
|
|
188
|
+
}
|
|
189
|
+
async get(key) {
|
|
190
|
+
return this.redisClient.get(key);
|
|
191
|
+
}
|
|
192
|
+
async set(key, value, timeoutSec) {
|
|
193
|
+
if (timeoutSec === -1) await this.redisClient.set(key, value);
|
|
194
|
+
else await this.redisClient.set(key, value, { EX: timeoutSec });
|
|
195
|
+
}
|
|
196
|
+
async delete(key) {
|
|
197
|
+
await this.redisClient.del(key);
|
|
198
|
+
}
|
|
199
|
+
async update(key, value) {
|
|
200
|
+
if (await this.redisClient.set(key, value, {
|
|
201
|
+
XX: true,
|
|
202
|
+
KEEPTTL: true
|
|
203
|
+
}) === null) throw new Error(`Key not found: ${key}`);
|
|
204
|
+
}
|
|
205
|
+
async has(key) {
|
|
206
|
+
return await this.redisClient.exists(key) === 1;
|
|
207
|
+
}
|
|
208
|
+
async updateTimeout(key, timeoutSec) {
|
|
209
|
+
if (!await this.redisClient.exists(key)) throw new Error(`Key not found: ${key}`);
|
|
210
|
+
if (timeoutSec === -1) await this.redisClient.persist(key);
|
|
211
|
+
else await this.redisClient.expire(key, timeoutSec);
|
|
212
|
+
}
|
|
213
|
+
async getTimeout(key) {
|
|
214
|
+
return await this.redisClient.ttl(key);
|
|
215
|
+
}
|
|
216
|
+
async keys(pattern) {
|
|
217
|
+
const result = [];
|
|
218
|
+
let cursor = 0;
|
|
219
|
+
do {
|
|
220
|
+
const reply = await this.redisClient.scan(cursor, {
|
|
221
|
+
MATCH: pattern,
|
|
222
|
+
COUNT: 100
|
|
223
|
+
});
|
|
224
|
+
cursor = reply.cursor;
|
|
225
|
+
result.push(...reply.keys);
|
|
226
|
+
} while (cursor !== 0);
|
|
227
|
+
return result;
|
|
228
|
+
}
|
|
229
|
+
};
|
|
230
|
+
RedisStore = __decorate([
|
|
231
|
+
(0, _nestjs_common.Injectable)(),
|
|
232
|
+
__decorateParam(0, (0, _nestjs_common.Inject)(XLT_REDIS_CLIENT)),
|
|
233
|
+
__decorateMetadata("design:paramtypes", [Object])
|
|
234
|
+
], RedisStore);
|
|
235
|
+
|
|
236
|
+
//#endregion
|
|
237
|
+
//#region src/token/jwt-strategy.ts
|
|
238
|
+
var _ref$2;
|
|
239
|
+
let JwtStrategy = class JwtStrategy {
|
|
240
|
+
constructor(config) {
|
|
241
|
+
this.config = config;
|
|
242
|
+
}
|
|
243
|
+
createToken(loginId, config) {
|
|
244
|
+
const jwt = config.jwt;
|
|
245
|
+
return (0, jsonwebtoken.sign)({
|
|
246
|
+
sub: loginId,
|
|
247
|
+
jti: crypto.randomUUID()
|
|
248
|
+
}, jwt.secret, {
|
|
249
|
+
algorithm: jwt.algorithm ?? "HS256",
|
|
250
|
+
...jwt.issuer && { issuer: jwt.issuer },
|
|
251
|
+
...jwt.audience && { audience: jwt.audience },
|
|
252
|
+
...config.timeout > 0 && { expiresIn: config.timeout }
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
generateToken(payload) {
|
|
256
|
+
return (0, jsonwebtoken.sign)(payload, this.config.jwt.secret);
|
|
257
|
+
}
|
|
258
|
+
verifyToken(token) {
|
|
259
|
+
return (0, jsonwebtoken.verify)(token, this.config.jwt.secret);
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
JwtStrategy = __decorate([
|
|
263
|
+
(0, _nestjs_common.Injectable)(),
|
|
264
|
+
__decorateParam(0, (0, _nestjs_common.Inject)(_xlt_token_core.XLT_TOKEN_CONFIG)),
|
|
265
|
+
__decorateMetadata("design:paramtypes", [typeof (_ref$2 = typeof _xlt_token_core.XltTokenConfig !== "undefined" && _xlt_token_core.XltTokenConfig) === "function" ? _ref$2 : Object])
|
|
266
|
+
], JwtStrategy);
|
|
267
|
+
|
|
268
|
+
//#endregion
|
|
269
|
+
//#region src/decorators/xlt-check-login.decorator.ts
|
|
270
|
+
/**
|
|
271
|
+
* 登录校验装饰器
|
|
272
|
+
* @constructor
|
|
273
|
+
*/
|
|
274
|
+
const XltCheckLogin = () => (0, _nestjs_common.SetMetadata)(_xlt_token_core.XLT_CHECK_LOGIN_KEY, true);
|
|
275
|
+
|
|
276
|
+
//#endregion
|
|
277
|
+
//#region src/decorators/xlt-ignore.decorator.ts
|
|
278
|
+
const XltIgnore = () => (0, _nestjs_common.SetMetadata)(_xlt_token_core.XLT_IGNORE_KEY, true);
|
|
279
|
+
|
|
280
|
+
//#endregion
|
|
281
|
+
//#region src/decorators/login-id.decorator.ts
|
|
282
|
+
/**
|
|
283
|
+
* 注入当前用户 ID
|
|
284
|
+
* @constructor
|
|
285
|
+
*/
|
|
286
|
+
const LoginId = (0, _nestjs_common.createParamDecorator)((data, ctx) => {
|
|
287
|
+
return ctx.switchToHttp().getRequest().stpLoginId;
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
//#endregion
|
|
291
|
+
//#region src/decorators/token-value.decorator.ts
|
|
292
|
+
/**
|
|
293
|
+
* 注入当前 Token
|
|
294
|
+
* @constructor
|
|
295
|
+
*/
|
|
296
|
+
const TokenValue = (0, _nestjs_common.createParamDecorator)((data, ctx) => {
|
|
297
|
+
return ctx.switchToHttp().getRequest().stpToken;
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
//#endregion
|
|
301
|
+
//#region src/decorators/xlt-check-permission.decorator.ts
|
|
302
|
+
/**
|
|
303
|
+
* 权限检查装饰器
|
|
304
|
+
* @param {string | string[]} permissions 权限列表
|
|
305
|
+
* @param {Object} [options] 模式选项
|
|
306
|
+
* @param {XltMode} [options.mode] 模式选项
|
|
307
|
+
* @constructor
|
|
308
|
+
*/
|
|
309
|
+
const XltCheckPermission = (permissions, options) => {
|
|
310
|
+
return (0, _nestjs_common.SetMetadata)(_xlt_token_core.XLT_PERMISSION_KEY, {
|
|
311
|
+
permissions: Array.isArray(permissions) ? permissions : [permissions],
|
|
312
|
+
mode: options?.mode ?? _xlt_token_core.XltMode.AND
|
|
313
|
+
});
|
|
314
|
+
};
|
|
315
|
+
|
|
316
|
+
//#endregion
|
|
317
|
+
//#region src/decorators/xlt-check-role.decorator.ts
|
|
318
|
+
/**
|
|
319
|
+
* 角色检查装饰器
|
|
320
|
+
* @param {string | string[]} roles 角色列表
|
|
321
|
+
* @param {Object} [options] 模式选项
|
|
322
|
+
* @param {XltMode} [options.mode] 模式选项
|
|
323
|
+
* @constructor
|
|
324
|
+
*/
|
|
325
|
+
const XltCheckRole = (roles, options) => {
|
|
326
|
+
return (0, _nestjs_common.SetMetadata)(_xlt_token_core.XLT_ROLE_KEY, {
|
|
327
|
+
roles: Array.isArray(roles) ? roles : [roles],
|
|
328
|
+
mode: options?.mode ?? _xlt_token_core.XltMode.AND
|
|
329
|
+
});
|
|
330
|
+
};
|
|
331
|
+
|
|
332
|
+
//#endregion
|
|
333
|
+
//#region src/decorators/xlt-check-safe.decorator.ts
|
|
334
|
+
const XLT_CHECK_SAFE_KEY = "XLT_CHECK_SAFE";
|
|
335
|
+
const XltCheckSafe = (business) => {
|
|
336
|
+
return (0, _nestjs_common.SetMetadata)(XLT_CHECK_SAFE_KEY, business);
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
//#endregion
|
|
340
|
+
//#region src/exceptions/not-login.exception.ts
|
|
341
|
+
var NotLoginException = class NotLoginException extends _nestjs_common.UnauthorizedException {
|
|
342
|
+
constructor(type, token) {
|
|
343
|
+
super({
|
|
344
|
+
statusCode: 401,
|
|
345
|
+
type,
|
|
346
|
+
message: NotLoginException.describeType(type)
|
|
347
|
+
});
|
|
348
|
+
this.type = type;
|
|
349
|
+
this.token = token;
|
|
350
|
+
}
|
|
351
|
+
static describeType(type) {
|
|
352
|
+
return {
|
|
353
|
+
[_xlt_token_core.NotLoginType.NOT_TOKEN]: "未提供 Token",
|
|
354
|
+
[_xlt_token_core.NotLoginType.INVALID_TOKEN]: "Token 无效",
|
|
355
|
+
[_xlt_token_core.NotLoginType.TOKEN_TIMEOUT]: "Token 已过期",
|
|
356
|
+
[_xlt_token_core.NotLoginType.TOKEN_FREEZE]: "Token 已被冻结",
|
|
357
|
+
[_xlt_token_core.NotLoginType.BE_REPLACED]: "已被顶下线",
|
|
358
|
+
[_xlt_token_core.NotLoginType.KICK_OUT]: "已被踢下线"
|
|
359
|
+
}[type] ?? "未登录";
|
|
360
|
+
}
|
|
361
|
+
};
|
|
362
|
+
|
|
363
|
+
//#endregion
|
|
364
|
+
//#region src/exceptions/not-permission.exception.ts
|
|
365
|
+
var NotPermissionException = class extends _nestjs_common.ForbiddenException {
|
|
366
|
+
constructor(permission, mode) {
|
|
367
|
+
super({
|
|
368
|
+
statusCode: 403,
|
|
369
|
+
type: "NOT_PERMISSION",
|
|
370
|
+
message: `缺少权限: ${Array.isArray(permission) ? permission.join(", ") : permission}`
|
|
371
|
+
});
|
|
372
|
+
this.permission = permission;
|
|
373
|
+
this.mode = mode;
|
|
374
|
+
}
|
|
375
|
+
};
|
|
376
|
+
|
|
377
|
+
//#endregion
|
|
378
|
+
//#region src/exceptions/not-role.exception.ts
|
|
379
|
+
var NotRoleException = class extends _nestjs_common.ForbiddenException {
|
|
380
|
+
constructor(role, mode) {
|
|
381
|
+
super({
|
|
382
|
+
statusCode: 403,
|
|
383
|
+
type: "NOT_ROLE",
|
|
384
|
+
message: `缺少角色: ${Array.isArray(role) ? role.join(", ") : role}`
|
|
385
|
+
});
|
|
386
|
+
this.role = role;
|
|
387
|
+
this.mode = mode;
|
|
388
|
+
}
|
|
389
|
+
};
|
|
390
|
+
|
|
391
|
+
//#endregion
|
|
392
|
+
//#region src/exceptions/not-safe.exception.ts
|
|
393
|
+
var NotSafeException = class extends _nestjs_common.ForbiddenException {
|
|
394
|
+
constructor(business) {
|
|
395
|
+
super({
|
|
396
|
+
statusCode: 403,
|
|
397
|
+
type: "NOT_SAFE",
|
|
398
|
+
message: `二级认证未开启:${business}`
|
|
399
|
+
});
|
|
400
|
+
this.business = business;
|
|
401
|
+
}
|
|
402
|
+
};
|
|
403
|
+
|
|
404
|
+
//#endregion
|
|
405
|
+
//#region src/http/nest-bridge.ts
|
|
406
|
+
function createNestHttpContext(req, res) {
|
|
407
|
+
return (0, _xlt_token_core.createExpressContext)(req, res);
|
|
408
|
+
}
|
|
409
|
+
function rethrowCoreAuthException(error) {
|
|
410
|
+
if (error instanceof _xlt_token_core.NotLoginException) throw new NotLoginException(error.type, error.token);
|
|
411
|
+
if (error instanceof _xlt_token_core.NotPermissionException) throw new NotPermissionException(error.permission, error.mode);
|
|
412
|
+
if (error instanceof _xlt_token_core.NotRoleException) throw new NotRoleException(error.role, error.mode);
|
|
413
|
+
if (error instanceof _xlt_token_core.NotSafeException) throw new NotSafeException(error.business);
|
|
414
|
+
throw error;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
//#endregion
|
|
418
|
+
//#region src/guards/xlt-token.guard.ts
|
|
419
|
+
var _ref$1, _ref2$1, _ref3;
|
|
420
|
+
let XltTokenGuard = class XltTokenGuard {
|
|
421
|
+
constructor(reflector, config, stpLogic, stpPermLogic) {
|
|
422
|
+
this.reflector = reflector;
|
|
423
|
+
this.config = config;
|
|
424
|
+
this.stpLogic = stpLogic;
|
|
425
|
+
this.stpPermLogic = stpPermLogic;
|
|
426
|
+
}
|
|
427
|
+
async canActivate(context) {
|
|
428
|
+
if (!this.requiresLogin(context)) return true;
|
|
429
|
+
const request = context.switchToHttp().getRequest();
|
|
430
|
+
const response = context.switchToHttp().getResponse();
|
|
431
|
+
let result;
|
|
432
|
+
try {
|
|
433
|
+
result = await this.stpLogic.checkLogin(createNestHttpContext(request, response));
|
|
434
|
+
} catch (error) {
|
|
435
|
+
rethrowCoreAuthException(error);
|
|
436
|
+
}
|
|
437
|
+
const business = this.getBusiness(context);
|
|
438
|
+
request.stpLoginId = result.loginId;
|
|
439
|
+
request.stpToken = result.token;
|
|
440
|
+
try {
|
|
441
|
+
if (this.stpPermLogic) {
|
|
442
|
+
const handler = context.getHandler();
|
|
443
|
+
const cls = context.getClass();
|
|
444
|
+
const permMeta = this.reflector.getAllAndOverride(_xlt_token_core.XLT_PERMISSION_KEY, [handler, cls]);
|
|
445
|
+
if (permMeta) await this.stpPermLogic.checkPermission(result.loginId, permMeta.permissions, permMeta.mode);
|
|
446
|
+
const roleMeta = this.reflector.getAllAndOverride(_xlt_token_core.XLT_ROLE_KEY, [handler, cls]);
|
|
447
|
+
if (roleMeta) await this.stpPermLogic.checkRole(result.loginId, roleMeta.roles, roleMeta.mode);
|
|
448
|
+
}
|
|
449
|
+
if (business) await this.stpLogic.checkSafe(result.token, business);
|
|
450
|
+
} catch (error) {
|
|
451
|
+
rethrowCoreAuthException(error);
|
|
452
|
+
}
|
|
453
|
+
return true;
|
|
454
|
+
}
|
|
455
|
+
requiresLogin(context) {
|
|
456
|
+
const isIgnored = this.reflector.getAllAndOverride(_xlt_token_core.XLT_IGNORE_KEY, [context.getHandler(), context.getClass()]);
|
|
457
|
+
if (this.config.defaultCheck) return !isIgnored;
|
|
458
|
+
return this.reflector.getAllAndOverride(_xlt_token_core.XLT_CHECK_LOGIN_KEY, [context.getHandler(), context.getClass()]) ?? false;
|
|
459
|
+
}
|
|
460
|
+
getBusiness(context) {
|
|
461
|
+
return this.reflector.getAllAndOverride(XLT_CHECK_SAFE_KEY, [context.getHandler(), context.getClass()]);
|
|
462
|
+
}
|
|
463
|
+
};
|
|
464
|
+
XltTokenGuard = __decorate([
|
|
465
|
+
(0, _nestjs_common.Injectable)(),
|
|
466
|
+
__decorateParam(1, (0, _nestjs_common.Inject)(_xlt_token_core.XLT_TOKEN_CONFIG)),
|
|
467
|
+
__decorateParam(3, (0, _nestjs_common.Optional)()),
|
|
468
|
+
__decorateMetadata("design:paramtypes", [
|
|
469
|
+
typeof (_ref$1 = typeof _nestjs_core.Reflector !== "undefined" && _nestjs_core.Reflector) === "function" ? _ref$1 : Object,
|
|
470
|
+
Object,
|
|
471
|
+
typeof (_ref2$1 = typeof _xlt_token_core.StpLogic !== "undefined" && _xlt_token_core.StpLogic) === "function" ? _ref2$1 : Object,
|
|
472
|
+
typeof (_ref3 = typeof _xlt_token_core.StpPermLogic !== "undefined" && _xlt_token_core.StpPermLogic) === "function" ? _ref3 : Object
|
|
473
|
+
])
|
|
474
|
+
], XltTokenGuard);
|
|
475
|
+
|
|
476
|
+
//#endregion
|
|
477
|
+
//#region src/guards/xlt-abstract-login.guard.ts
|
|
478
|
+
var _ref, _ref2;
|
|
479
|
+
let XltAbstractLoginGuard = class XltAbstractLoginGuard {
|
|
480
|
+
constructor(reflector, config, stpLogic) {
|
|
481
|
+
this.reflector = reflector;
|
|
482
|
+
this.config = config;
|
|
483
|
+
this.stpLogic = stpLogic;
|
|
484
|
+
}
|
|
485
|
+
async canActivate(ctx) {
|
|
486
|
+
if (!this.requiresLogin(ctx)) return true;
|
|
487
|
+
const request = ctx.switchToHttp().getRequest();
|
|
488
|
+
const response = ctx.switchToHttp().getResponse();
|
|
489
|
+
let result;
|
|
490
|
+
try {
|
|
491
|
+
result = await this.stpLogic.checkLogin(createNestHttpContext(request, response));
|
|
492
|
+
} catch (err) {
|
|
493
|
+
if (err instanceof _xlt_token_core.NotLoginException) {
|
|
494
|
+
await this.onAuthFail?.({
|
|
495
|
+
ok: false,
|
|
496
|
+
reason: err.type,
|
|
497
|
+
token: err.token
|
|
498
|
+
}, request);
|
|
499
|
+
throw new NotLoginException(err.type, err.token);
|
|
500
|
+
}
|
|
501
|
+
throw err;
|
|
502
|
+
}
|
|
503
|
+
request.stpLoginId = result.loginId;
|
|
504
|
+
request.stpToken = result.token;
|
|
505
|
+
await this.onAuthSuccess?.(result, request);
|
|
506
|
+
return true;
|
|
507
|
+
}
|
|
508
|
+
requiresLogin(ctx) {
|
|
509
|
+
const isIgnored = this.reflector.getAllAndOverride(_xlt_token_core.XLT_IGNORE_KEY, [ctx.getHandler(), ctx.getClass()]);
|
|
510
|
+
if (this.config.defaultCheck) return !isIgnored;
|
|
511
|
+
return this.reflector.getAllAndOverride(_xlt_token_core.XLT_CHECK_LOGIN_KEY, [ctx.getHandler(), ctx.getClass()]) ?? false;
|
|
512
|
+
}
|
|
513
|
+
};
|
|
514
|
+
XltAbstractLoginGuard = __decorate([
|
|
515
|
+
(0, _nestjs_common.Injectable)(),
|
|
516
|
+
__decorateParam(1, (0, _nestjs_common.Inject)(_xlt_token_core.XLT_TOKEN_CONFIG)),
|
|
517
|
+
__decorateMetadata("design:paramtypes", [
|
|
518
|
+
typeof (_ref = typeof _nestjs_core.Reflector !== "undefined" && _nestjs_core.Reflector) === "function" ? _ref : Object,
|
|
519
|
+
Object,
|
|
520
|
+
typeof (_ref2 = typeof _xlt_token_core.StpLogic !== "undefined" && _xlt_token_core.StpLogic) === "function" ? _ref2 : Object
|
|
521
|
+
])
|
|
522
|
+
], XltAbstractLoginGuard);
|
|
523
|
+
|
|
524
|
+
//#endregion
|
|
525
|
+
Object.defineProperty(exports, 'DEFAULT_XLT_TOKEN_CONFIG', {
|
|
526
|
+
enumerable: true,
|
|
527
|
+
get: function () {
|
|
528
|
+
return _xlt_token_core.DEFAULT_XLT_TOKEN_CONFIG;
|
|
529
|
+
}
|
|
530
|
+
});
|
|
531
|
+
Object.defineProperty(exports, 'JwtStrategy', {
|
|
532
|
+
enumerable: true,
|
|
533
|
+
get: function () {
|
|
534
|
+
return JwtStrategy;
|
|
535
|
+
}
|
|
536
|
+
});
|
|
537
|
+
exports.LoginId = LoginId;
|
|
538
|
+
Object.defineProperty(exports, 'MemoryStore', {
|
|
539
|
+
enumerable: true,
|
|
540
|
+
get: function () {
|
|
541
|
+
return _xlt_token_core.MemoryStore;
|
|
542
|
+
}
|
|
543
|
+
});
|
|
544
|
+
exports.NotLoginException = NotLoginException;
|
|
545
|
+
Object.defineProperty(exports, 'NotLoginType', {
|
|
546
|
+
enumerable: true,
|
|
547
|
+
get: function () {
|
|
548
|
+
return _xlt_token_core.NotLoginType;
|
|
549
|
+
}
|
|
550
|
+
});
|
|
551
|
+
exports.NotPermissionException = NotPermissionException;
|
|
552
|
+
exports.NotRoleException = NotRoleException;
|
|
553
|
+
exports.NotSafeException = NotSafeException;
|
|
554
|
+
Object.defineProperty(exports, 'RedisStore', {
|
|
555
|
+
enumerable: true,
|
|
556
|
+
get: function () {
|
|
557
|
+
return RedisStore;
|
|
558
|
+
}
|
|
559
|
+
});
|
|
560
|
+
Object.defineProperty(exports, 'StpLogic', {
|
|
561
|
+
enumerable: true,
|
|
562
|
+
get: function () {
|
|
563
|
+
return _xlt_token_core.StpLogic;
|
|
564
|
+
}
|
|
565
|
+
});
|
|
566
|
+
Object.defineProperty(exports, 'StpPermLogic', {
|
|
567
|
+
enumerable: true,
|
|
568
|
+
get: function () {
|
|
569
|
+
return _xlt_token_core.StpPermLogic;
|
|
570
|
+
}
|
|
571
|
+
});
|
|
572
|
+
Object.defineProperty(exports, 'StpUtil', {
|
|
573
|
+
enumerable: true,
|
|
574
|
+
get: function () {
|
|
575
|
+
return _xlt_token_core.StpUtil;
|
|
576
|
+
}
|
|
577
|
+
});
|
|
578
|
+
exports.TokenValue = TokenValue;
|
|
579
|
+
Object.defineProperty(exports, 'UuidStrategy', {
|
|
580
|
+
enumerable: true,
|
|
581
|
+
get: function () {
|
|
582
|
+
return _xlt_token_core.UuidStrategy;
|
|
583
|
+
}
|
|
584
|
+
});
|
|
585
|
+
exports.XLT_CHECK_SAFE_KEY = XLT_CHECK_SAFE_KEY;
|
|
586
|
+
exports.XLT_REDIS_CLIENT = XLT_REDIS_CLIENT;
|
|
587
|
+
Object.defineProperty(exports, 'XLT_STP_INTERFACE', {
|
|
588
|
+
enumerable: true,
|
|
589
|
+
get: function () {
|
|
590
|
+
return _xlt_token_core.XLT_STP_INTERFACE;
|
|
591
|
+
}
|
|
592
|
+
});
|
|
593
|
+
Object.defineProperty(exports, 'XLT_TOKEN_CONFIG', {
|
|
594
|
+
enumerable: true,
|
|
595
|
+
get: function () {
|
|
596
|
+
return _xlt_token_core.XLT_TOKEN_CONFIG;
|
|
597
|
+
}
|
|
598
|
+
});
|
|
599
|
+
Object.defineProperty(exports, 'XLT_TOKEN_HOOKS', {
|
|
600
|
+
enumerable: true,
|
|
601
|
+
get: function () {
|
|
602
|
+
return _xlt_token_core.XLT_TOKEN_HOOKS;
|
|
603
|
+
}
|
|
604
|
+
});
|
|
605
|
+
Object.defineProperty(exports, 'XLT_TOKEN_STORE', {
|
|
606
|
+
enumerable: true,
|
|
607
|
+
get: function () {
|
|
608
|
+
return _xlt_token_core.XLT_TOKEN_STORE;
|
|
609
|
+
}
|
|
610
|
+
});
|
|
611
|
+
Object.defineProperty(exports, 'XLT_TOKEN_STRATEGY', {
|
|
612
|
+
enumerable: true,
|
|
613
|
+
get: function () {
|
|
614
|
+
return _xlt_token_core.XLT_TOKEN_STRATEGY;
|
|
615
|
+
}
|
|
616
|
+
});
|
|
617
|
+
Object.defineProperty(exports, 'XltAbstractLoginGuard', {
|
|
618
|
+
enumerable: true,
|
|
619
|
+
get: function () {
|
|
620
|
+
return XltAbstractLoginGuard;
|
|
621
|
+
}
|
|
622
|
+
});
|
|
623
|
+
exports.XltCheckLogin = XltCheckLogin;
|
|
624
|
+
exports.XltCheckPermission = XltCheckPermission;
|
|
625
|
+
exports.XltCheckRole = XltCheckRole;
|
|
626
|
+
exports.XltCheckSafe = XltCheckSafe;
|
|
627
|
+
exports.XltIgnore = XltIgnore;
|
|
628
|
+
Object.defineProperty(exports, 'XltMode', {
|
|
629
|
+
enumerable: true,
|
|
630
|
+
get: function () {
|
|
631
|
+
return _xlt_token_core.XltMode;
|
|
632
|
+
}
|
|
633
|
+
});
|
|
634
|
+
Object.defineProperty(exports, 'XltSession', {
|
|
635
|
+
enumerable: true,
|
|
636
|
+
get: function () {
|
|
637
|
+
return _xlt_token_core.XltSession;
|
|
638
|
+
}
|
|
639
|
+
});
|
|
640
|
+
Object.defineProperty(exports, 'XltTokenGuard', {
|
|
641
|
+
enumerable: true,
|
|
642
|
+
get: function () {
|
|
643
|
+
return XltTokenGuard;
|
|
644
|
+
}
|
|
645
|
+
});
|
|
646
|
+
Object.defineProperty(exports, 'XltTokenModule', {
|
|
647
|
+
enumerable: true,
|
|
648
|
+
get: function () {
|
|
649
|
+
return XltTokenModule;
|
|
650
|
+
}
|
|
651
|
+
});
|
|
652
|
+
Object.defineProperty(exports, 'createExpressContext', {
|
|
653
|
+
enumerable: true,
|
|
654
|
+
get: function () {
|
|
655
|
+
return _xlt_token_core.createExpressContext;
|
|
656
|
+
}
|
|
657
|
+
});
|
|
658
|
+
Object.defineProperty(exports, 'createMockHttpContext', {
|
|
659
|
+
enumerable: true,
|
|
660
|
+
get: function () {
|
|
661
|
+
return _xlt_token_core.createMockHttpContext;
|
|
662
|
+
}
|
|
663
|
+
});
|
|
664
|
+
Object.defineProperty(exports, 'createXltToken', {
|
|
665
|
+
enumerable: true,
|
|
666
|
+
get: function () {
|
|
667
|
+
return _xlt_token_core.createXltToken;
|
|
668
|
+
}
|
|
669
|
+
});
|
|
670
|
+
Object.defineProperty(exports, 'matchPermission', {
|
|
671
|
+
enumerable: true,
|
|
672
|
+
get: function () {
|
|
673
|
+
return _xlt_token_core.matchPermission;
|
|
674
|
+
}
|
|
675
|
+
});
|
|
676
|
+
Object.defineProperty(exports, 'setStpLogic', {
|
|
677
|
+
enumerable: true,
|
|
678
|
+
get: function () {
|
|
679
|
+
return _xlt_token_core.setStpLogic;
|
|
680
|
+
}
|
|
681
|
+
});
|
|
682
|
+
Object.defineProperty(exports, 'setStpPermLogic', {
|
|
683
|
+
enumerable: true,
|
|
684
|
+
get: function () {
|
|
685
|
+
return _xlt_token_core.setStpPermLogic;
|
|
686
|
+
}
|
|
687
|
+
});
|