@xlt-token/nestjs 1.1.0 → 1.2.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/index.cjs +3 -3
- package/dist/index.mjs +3 -3
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -2
package/dist/index.cjs
CHANGED
|
@@ -210,15 +210,15 @@ let RedisStore = class RedisStore {
|
|
|
210
210
|
}
|
|
211
211
|
async keys(pattern) {
|
|
212
212
|
const result = [];
|
|
213
|
-
let cursor = 0;
|
|
213
|
+
let cursor = "0";
|
|
214
214
|
do {
|
|
215
215
|
const reply = await this.redisClient.scan(cursor, {
|
|
216
216
|
MATCH: pattern,
|
|
217
217
|
COUNT: 100
|
|
218
218
|
});
|
|
219
|
-
cursor = reply.cursor;
|
|
219
|
+
cursor = String(reply.cursor);
|
|
220
220
|
result.push(...reply.keys);
|
|
221
|
-
} while (cursor !== 0);
|
|
221
|
+
} while (cursor !== "0");
|
|
222
222
|
return result;
|
|
223
223
|
}
|
|
224
224
|
};
|
package/dist/index.mjs
CHANGED
|
@@ -209,15 +209,15 @@ let RedisStore = class RedisStore {
|
|
|
209
209
|
}
|
|
210
210
|
async keys(pattern) {
|
|
211
211
|
const result = [];
|
|
212
|
-
let cursor = 0;
|
|
212
|
+
let cursor = "0";
|
|
213
213
|
do {
|
|
214
214
|
const reply = await this.redisClient.scan(cursor, {
|
|
215
215
|
MATCH: pattern,
|
|
216
216
|
COUNT: 100
|
|
217
217
|
});
|
|
218
|
-
cursor = reply.cursor;
|
|
218
|
+
cursor = String(reply.cursor);
|
|
219
219
|
result.push(...reply.keys);
|
|
220
|
-
} while (cursor !== 0);
|
|
220
|
+
} while (cursor !== "0");
|
|
221
221
|
return result;
|
|
222
222
|
}
|
|
223
223
|
};
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":["StpLogic","XLT_TOKEN_CONFIG","XLT_TOKEN_STORE","XLT_TOKEN_STRATEGY","XLT_TOKEN_HOOKS","StpPermLogic","XLT_STP_INTERFACE","MemoryStore","UuidStrategy","XLT_TOKEN_CONFIG","XltMode","XltMode","NotLoginType","createExpressContext","CoreNotLoginException","CoreNotPermissionException","CoreNotRoleException","CoreNotSafeException","XLT_TOKEN_CONFIG","CoreNotLoginException","XLT_TOKEN_CONFIG"],"sources":["../src/xlt-token.module.ts","../src/store/redis-store.ts","../src/token/jwt-strategy.ts","../src/decorators/xlt-check-login.decorator.ts","../src/decorators/xlt-ignore.decorator.ts","../src/decorators/login-id.decorator.ts","../src/decorators/token-value.decorator.ts","../src/decorators/xlt-check-permission.decorator.ts","../src/decorators/xlt-check-role.decorator.ts","../src/decorators/xlt-check-safe.decorator.ts","../src/exceptions/not-login.exception.ts","../src/exceptions/not-permission.exception.ts","../src/exceptions/not-role.exception.ts","../src/exceptions/not-safe.exception.ts","../src/http/nest-bridge.ts","../src/guards/xlt-token.guard.ts","../src/guards/xlt-abstract-login.guard.ts"],"sourcesContent":["import type { ModuleMetadata, Provider } from '@nestjs/common';\nimport { Module } from '@nestjs/common';\nimport type { StpInterface, TokenStrategy, XltHooks, XltTokenConfig, XltTokenConfigInput, XltTokenStore, } from '@xlt-token/core';\nimport {\n DEFAULT_XLT_TOKEN_CONFIG,\n MemoryStore,\n setStpLogic,\n setStpPermLogic,\n StpLogic,\n StpPermLogic,\n UuidStrategy,\n XLT_STP_INTERFACE,\n XLT_TOKEN_CONFIG,\n XLT_TOKEN_HOOKS,\n XLT_TOKEN_STORE,\n XLT_TOKEN_STRATEGY,\n normalizeXltTokenConfig\n} from '@xlt-token/core';\n\nexport interface XltTokenModuleOptions {\n config?: Partial<XltTokenConfigInput>;\n store?: { useClass: new (...args: any[]) => XltTokenStore } | { useValue: XltTokenStore };\n strategy?: { useClass: new (...args: any[]) => TokenStrategy };\n isGlobal?: boolean;\n providers?: Provider[];\n stpInterface?: new (...args: any[]) => StpInterface;\n hooks?: XltHooks;\n}\n\nexport interface XltTokenModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> {\n useFactory: (...args: any[]) => Promise<XltTokenModuleOptions> | XltTokenModuleOptions;\n inject?: any[];\n store?: { useClass: new (...args: any[]) => XltTokenStore } | { useValue: XltTokenStore };\n strategy?: { useClass: new (...args: any[]) => TokenStrategy };\n isGlobal?: boolean;\n providers?: Provider[];\n stpInterface?: new (...args: any[]) => StpInterface;\n hooks?: XltHooks;\n}\n\n@Module({})\nexport class XltTokenModule {\n private static readonly stpLogicProvider: Provider = {\n provide: StpLogic,\n useFactory: (\n config: XltTokenConfig,\n store: XltTokenStore,\n strategy: TokenStrategy,\n hooks: XltHooks,\n ) => new StpLogic(config, store, strategy, hooks),\n inject: [ XLT_TOKEN_CONFIG, XLT_TOKEN_STORE, XLT_TOKEN_STRATEGY, XLT_TOKEN_HOOKS ],\n };\n private static readonly stpPermLogicProvider: Provider = {\n provide: StpPermLogic,\n useFactory: (\n stpInterface: StpInterface,\n store: XltTokenStore,\n config: XltTokenConfig,\n ) => new StpPermLogic(stpInterface, store, config),\n inject: [ XLT_STP_INTERFACE, XLT_TOKEN_STORE, XLT_TOKEN_CONFIG ],\n };\n private static readonly initProvider: Provider = {\n provide: 'XLT_TOKEN_INIT',\n useFactory: (stpLogic: StpLogic, stpPermLogic: StpPermLogic) => {\n setStpLogic(stpLogic);\n setStpPermLogic(stpPermLogic);\n return true;\n },\n inject: [ StpLogic, StpPermLogic ],\n };\n private static readonly moduleExports = [ XLT_TOKEN_CONFIG, XLT_TOKEN_STORE, XLT_TOKEN_STRATEGY, StpLogic, StpPermLogic ];\n\n static forRoot(options: XltTokenModuleOptions = {}) {\n const {config: userConfig, store, strategy, isGlobal = false, providers = [], stpInterface} = options;\n\n return {\n module: XltTokenModule,\n providers: [\n {provide: XLT_TOKEN_CONFIG, useValue: normalizeXltTokenConfig(userConfig)},\n XltTokenModule.createStoreProvider(store),\n XltTokenModule.createStrategyProvider(strategy),\n XltTokenModule.createStpInterfaceProvider(stpInterface),\n XltTokenModule.createHooksProvider(options.hooks),\n XltTokenModule.stpLogicProvider,\n XltTokenModule.stpPermLogicProvider,\n XltTokenModule.initProvider,\n ...providers,\n ],\n exports: XltTokenModule.moduleExports,\n global: isGlobal,\n };\n }\n\n static forRootAsync(options: XltTokenModuleAsyncOptions) {\n const {\n useFactory,\n inject = [],\n imports = [],\n store,\n strategy,\n isGlobal = false,\n providers = [],\n stpInterface\n } = options;\n\n return {\n module: XltTokenModule,\n imports,\n providers: [\n {\n provide: XLT_TOKEN_CONFIG,\n useFactory: async (...args: any[]) => {\n const {config = {}} = await useFactory(...args);\n return normalizeXltTokenConfig(config)\n },\n inject,\n },\n XltTokenModule.createStoreProvider(store),\n XltTokenModule.createStrategyProvider(strategy),\n XltTokenModule.createStpInterfaceProvider(stpInterface),\n XltTokenModule.createHooksProvider(options.hooks),\n XltTokenModule.stpLogicProvider,\n XltTokenModule.stpPermLogicProvider,\n XltTokenModule.initProvider,\n ...providers,\n ],\n exports: XltTokenModule.moduleExports,\n global: isGlobal,\n };\n }\n\n private static createStoreProvider(\n store?: XltTokenModuleOptions['store'],\n ): Provider {\n if ( !store ) return {provide: XLT_TOKEN_STORE, useClass: MemoryStore};\n return 'useClass' in store\n ? {provide: XLT_TOKEN_STORE, useClass: store.useClass}\n : {provide: XLT_TOKEN_STORE, useValue: store.useValue};\n }\n\n private static createStrategyProvider(\n strategy?: XltTokenModuleOptions['strategy'],\n ): Provider {\n return strategy?.useClass\n ? {provide: XLT_TOKEN_STRATEGY, useClass: strategy.useClass}\n : {provide: XLT_TOKEN_STRATEGY, useClass: UuidStrategy};\n }\n\n private static createStpInterfaceProvider(\n stpInterface?: new (...args: any[]) => StpInterface,\n ): Provider {\n if ( stpInterface ) return {provide: XLT_STP_INTERFACE, useClass: stpInterface};\n return {\n provide: XLT_STP_INTERFACE,\n useValue: {\n getPermissionList: () => {\n throw new Error('StpInterface not registered: getPermissionList');\n },\n getRoleList: () => {\n throw new Error('StpInterface not registered: getRoleList');\n },\n },\n };\n }\n\n private static createHooksProvider(\n hooks?: XltTokenModuleOptions['hooks'],\n ): Provider {\n return {provide: XLT_TOKEN_HOOKS, useValue: hooks ?? {}};\n }\n}\n","import type { XltTokenStore } from '@xlt-token/core';\nimport { Inject, Injectable } from '@nestjs/common';\n\n\nexport const XLT_REDIS_CLIENT = 'XLT_REDIS_CLIENT';\n\n\n@Injectable()\nexport class RedisStore implements XltTokenStore {\n\n constructor(\n @Inject(XLT_REDIS_CLIENT)\n private readonly redisClient: any,\n ) {\n\n }\n\n\n async get(key:string):Promise<string | null> {\n return this.redisClient.get(key);\n }\n\n\n async set(key:string, value:string, timeoutSec:number):Promise<void> {\n if (timeoutSec === -1){\n await this.redisClient.set(key, value);\n }else {\n await this.redisClient.set(key, value, {EX: timeoutSec});\n }\n }\n\n\n async delete(key:string):Promise<void> {\n await this.redisClient.del(key);\n }\n\n\n async update(key:string, value:string):Promise<void> {\n const result = await this.redisClient.set(key, value,{XX:true,KEEPTTL:true})\n if (result ===null){\n throw new Error(`Key not found: ${key}`);\n }\n }\n\n\n async has(key:string):Promise<boolean> {\n const result = await this.redisClient.exists(key);\n return result === 1;\n }\n\n\n async updateTimeout(key:string, timeoutSec:number):Promise<void> {\n const exists = await this.redisClient.exists(key);\n\n if (!exists) {\n throw new Error(`Key not found: ${key}`);\n }\n\n if (timeoutSec === -1) {\n await this.redisClient.persist(key);\n }else {\n await this.redisClient.expire(key, timeoutSec);\n }\n\n }\n\n async getTimeout(key:string):Promise<number> {\n const result = await this.redisClient.ttl(key);\n // Redis TTL 返回值约定:\n // -2 = key 不存在\n // -1 = key 存在但无过期时间(永久)\n // >0 = 剩余秒数\n // 恰好与 XltTokenStore 接口约定一致\n return result;\n }\n\n async keys(pattern: string): Promise<string[]> {\n const result: string[] = [];\n let cursor = 0;\n do {\n const reply = await this.redisClient.scan(cursor, { MATCH: pattern, COUNT: 100 });\n cursor = reply.cursor;\n result.push(...reply.keys);\n } while (cursor !== 0);\n return result;\n }\n}\n","import { Inject, Injectable } from \"@nestjs/common\";\nimport { randomUUID } from 'node:crypto';\nimport { createRequire } from 'node:module';\nimport { XLT_TOKEN_CONFIG } from '@xlt-token/core';\nimport type { DurationInput, TokenStrategy, XltTokenConfig } from '@xlt-token/core';\n\nconst require = createRequire(import.meta.url);\n\nlet jsonwebtoken: typeof import('jsonwebtoken') | undefined;\n\nexport type XltJwtPayload = Record<string, any> & { sub: string; jti: string };\n\nfunction getJsonwebtoken(): typeof import('jsonwebtoken') {\n try {\n jsonwebtoken ??= require('jsonwebtoken') as typeof import('jsonwebtoken');\n return jsonwebtoken;\n } catch (error) {\n const err = error as NodeJS.ErrnoException;\n if (err.code === 'MODULE_NOT_FOUND') {\n throw new Error(\n 'JwtStrategy requires the optional peer dependency \"jsonwebtoken\". '\n + 'Install it in your application with \"pnpm add jsonwebtoken\".',\n );\n }\n throw error;\n }\n}\n\n@Injectable()\nexport class JwtStrategy implements TokenStrategy<XltJwtPayload> {\n constructor(\n @Inject(XLT_TOKEN_CONFIG) private readonly config: XltTokenConfig\n ) { }\n\n private ensureJwtConfig(config?: XltTokenConfig): NonNullable<XltTokenConfig['jwt']> {\n const jwt = (config ?? this.config).jwt;\n if (!jwt || !jwt.secret) {\n throw new Error(\n 'JwtStrategy requires jwt config with a secret. '\n + 'Provide { jwt: { secret: \"your-secret\" } } in the module config.',\n );\n }\n return jwt;\n }\n\n createToken(loginId: string, config: XltTokenConfig, options?: { timeout?: DurationInput }): string {\n const { sign } = getJsonwebtoken();\n const jwt = this.ensureJwtConfig(config);\n const jti = randomUUID();\n\n const resolvedTimeout = options?.timeout ?? config.timeout;\n const hasExpiry = typeof resolvedTimeout === 'number' ? resolvedTimeout > 0 : true;\n\n return sign({\n sub: loginId, jti\n }, jwt.secret, {\n algorithm: jwt.algorithm ?? 'HS256',\n ...(jwt.issuer && { issuer: jwt.issuer }),\n ...(jwt.audience && { audience: jwt.audience }),\n ...(hasExpiry && { expiresIn: resolvedTimeout }),\n })\n }\n\n generateToken(payload: any): string {\n const { sign } = getJsonwebtoken();\n const jwt = this.ensureJwtConfig();\n return sign(payload, jwt.secret);\n }\n\n verifyToken(token: string): XltJwtPayload {\n const { verify } = getJsonwebtoken();\n const jwt = this.ensureJwtConfig();\n return verify(token, jwt.secret) as XltJwtPayload;\n }\n\n}\n","// 登录校验装饰器\n\nimport { SetMetadata } from '@nestjs/common';\nimport { XLT_CHECK_LOGIN_KEY } from '@xlt-token/core';\n\n\n/**\n * 登录校验装饰器\n * @constructor\n */\nexport const XltCheckLogin = () => SetMetadata(XLT_CHECK_LOGIN_KEY, true);\n","// 忽略校验装饰器\n\nimport { SetMetadata } from '@nestjs/common';\nimport { XLT_IGNORE_KEY } from '@xlt-token/core';\n\nexport const XltIgnore = () => SetMetadata(XLT_IGNORE_KEY, true);\n","// 注入当前用户 ID\n\nimport { createParamDecorator, ExecutionContext } from '@nestjs/common';\n\n/**\n * 注入当前用户 ID\n * @constructor\n */\nexport const LoginId = createParamDecorator(\n (data: unknown, ctx: ExecutionContext) => {\n const request = ctx.switchToHttp().getRequest();\n return request.stpLoginId;\n },\n);\n","// 注入当前 Token\n\nimport { createParamDecorator, ExecutionContext } from '@nestjs/common';\n\n\n/**\n * 注入当前 Token\n * @constructor\n */\nexport const TokenValue = createParamDecorator((data: any, ctx: ExecutionContext) => {\n const request = ctx.switchToHttp().getRequest();\n return request.stpToken;\n});\n","import { XLT_PERMISSION_KEY, XltMode } from '@xlt-token/core';\nimport { SetMetadata } from '@nestjs/common';\n\n\n/**\n * 权限检查装饰器\n * @param {string | string[]} permissions 权限列表\n * @param {Object} [options] 模式选项\n * @param {XltMode} [options.mode] 模式选项\n * @constructor\n */\nexport const XltCheckPermission = (permissions: string | string[], options?: { mode: XltMode; }) => {\n const perms = Array.isArray(permissions) ? permissions : [permissions];\n const mode = options?.mode ?? XltMode.AND;\n return SetMetadata(XLT_PERMISSION_KEY, { permissions: perms, mode });\n};\n","import { XLT_ROLE_KEY, XltMode } from '@xlt-token/core';\nimport { SetMetadata } from '@nestjs/common';\n\n\n/**\n * 角色检查装饰器\n * @param {string | string[]} roles 角色列表\n * @param {Object} [options] 模式选项\n * @param {XltMode} [options.mode] 模式选项\n * @constructor\n */\nexport const XltCheckRole = (roles: string | string[], options?: { mode: XltMode; }) => {\n const _roles = Array.isArray(roles) ? roles : [roles];\n const mode = options?.mode ?? XltMode.AND;\n return SetMetadata(XLT_ROLE_KEY, { roles: _roles, mode });\n\n};\n","import { SetMetadata } from \"@nestjs/common\"\n\nexport const XLT_CHECK_SAFE_KEY = 'XLT_CHECK_SAFE';\n\nexport const XltCheckSafe = (business: string) => {\n return SetMetadata(XLT_CHECK_SAFE_KEY, business);\n}\n","import { UnauthorizedException } from '@nestjs/common';\nimport { NotLoginType } from '@xlt-token/core';\n\nexport class NotLoginException extends UnauthorizedException {\n public readonly type: NotLoginType;\n public readonly token: string | undefined;\n\n constructor(type: NotLoginType, token?: string) {\n super({\n statusCode: 401,\n type,\n message: NotLoginException.describeType(type),\n });\n this.type = type;\n this.token = token;\n }\n\n private static describeType(type: NotLoginType): string {\n const map: Record<NotLoginType, string> = {\n [NotLoginType.NOT_TOKEN]: '未提供 Token',\n [NotLoginType.INVALID_TOKEN]: 'Token 无效',\n [NotLoginType.TOKEN_TIMEOUT]: 'Token 已过期',\n [NotLoginType.TOKEN_FREEZE]: 'Token 已被冻结',\n [NotLoginType.BE_REPLACED]: '已被顶下线',\n [NotLoginType.KICK_OUT]: '已被踢下线',\n };\n return map[type] ?? '未登录';\n }\n}\n\n","import { ForbiddenException } from '@nestjs/common';\nimport { XltMode } from '@xlt-token/core';\n\nexport class NotPermissionException extends ForbiddenException {\n public readonly permission: string | string[];\n public readonly mode: XltMode;\n\n constructor(permission: string | string[], mode: XltMode) {\n super({\n statusCode: 403,\n type: 'NOT_PERMISSION',\n message: `缺少权限: ${Array.isArray(permission) ? permission.join(', ') : permission}`,\n });\n this.permission = permission;\n this.mode = mode;\n }\n}\n","import { ForbiddenException } from '@nestjs/common';\nimport { XltMode } from '@xlt-token/core';\n\nexport class NotRoleException extends ForbiddenException {\n public readonly role: string | string[];\n public readonly mode: XltMode;\n\n constructor(role: string | string[], mode: XltMode) {\n super({\n statusCode: 403,\n type: 'NOT_ROLE',\n message: `缺少角色: ${Array.isArray(role) ? role.join(', ') : role}`,\n });\n this.role = role;\n this.mode = mode;\n }\n}\n","import { ForbiddenException } from \"@nestjs/common\";\n\nexport class NotSafeException extends ForbiddenException {\n readonly business: string;\n\n\n constructor(business: string) {\n super({\n statusCode: 403,\n type: 'NOT_SAFE',\n message: `二级认证未开启:${business}`,\n });\n this.business = business;\n }\n\n}\n","import {\n createExpressContext,\n type CookieOptions,\n type ExpressLikeResponse,\n NotLoginException as CoreNotLoginException,\n NotPermissionException as CoreNotPermissionException,\n NotRoleException as CoreNotRoleException,\n NotSafeException as CoreNotSafeException,\n} from '@xlt-token/core';\nimport { NotLoginException } from '../exceptions/not-login.exception.js';\nimport { NotPermissionException } from '../exceptions/not-permission.exception.js';\nimport { NotRoleException } from '../exceptions/not-role.exception.js';\nimport { NotSafeException } from '../exceptions/not-safe.exception.js';\n\n/**\n * Fastify reply 的写回 API 与 Express response 不同:\n * - 写 header:Express 用 `res.setHeader(n, v)`,Fastify 用 `reply.header(n, v)`\n * - 写 cookie:Express 用 `res.cookie(n, v, o)`,Fastify 用 `reply.setCookie(n, v, o)`\n *\n * 这里把任意一种 response 归一化成 core 期望的 {@link ExpressLikeResponse} 形态,\n * 让核心层无需感知底层 HTTP 平台。读取侧(headers/cookies/query)两个平台形态一致,\n * 直接复用 core 的 createExpressContext。\n */\nfunction normalizeResponse(res: any): ExpressLikeResponse {\n return {\n setHeader(name: string, value: string): void {\n if (typeof res?.setHeader === 'function') {\n // Express response / 原生 Node 响应\n res.setHeader(name, value);\n } else if (typeof res?.header === 'function') {\n // Fastify reply\n res.header(name, value);\n } else {\n throw new Error(\n 'xlt-token: 当前 response 不支持写入 header(既无 setHeader 也无 header 方法)',\n );\n }\n },\n cookie(name: string, value: string, options?: CookieOptions): void {\n if (typeof res?.cookie === 'function') {\n // Express response(或已注册 @fastify/cookie 暴露的 cookie 别名)\n res.cookie(name, value, options);\n } else if (typeof res?.setCookie === 'function') {\n // Fastify reply + @fastify/cookie 插件\n res.setCookie(name, value, options);\n } else {\n throw new Error(\n 'xlt-token: 当前 response 不支持写入 cookie。'\n + '若使用 Fastify,请先注册 @fastify/cookie 插件。',\n );\n }\n },\n };\n}\n\nexport function createNestHttpContext(req: any, res: any) {\n return createExpressContext(req, normalizeResponse(res));\n}\n\nexport function rethrowCoreAuthException(error: unknown): never {\n if (error instanceof CoreNotLoginException) {\n throw new NotLoginException(error.type, error.token);\n }\n if (error instanceof CoreNotPermissionException) {\n throw new NotPermissionException(error.permission, error.mode);\n }\n if (error instanceof CoreNotRoleException) {\n throw new NotRoleException(error.role, error.mode);\n }\n if (error instanceof CoreNotSafeException) {\n throw new NotSafeException(error.business);\n }\n throw error;\n}\n","// 全局守卫\n\nimport { CanActivate, ExecutionContext, Inject, Injectable, Optional } from '@nestjs/common';\nimport { Reflector } from '@nestjs/core';\nimport {\n type AuthResult,\n StpLogic,\n StpPermLogic,\n XLT_CHECK_LOGIN_KEY,\n XLT_IGNORE_KEY,\n XLT_PERMISSION_KEY,\n XLT_ROLE_KEY,\n XLT_TOKEN_CONFIG,\n type XltTokenConfig,\n} from '@xlt-token/core';\nimport { XLT_CHECK_SAFE_KEY } from '../decorators/xlt-check-safe.decorator.js';\nimport { createNestHttpContext, rethrowCoreAuthException } from '../http/nest-bridge.js';\n\n@Injectable()\nexport class XltTokenGuard implements CanActivate {\n constructor(\n private readonly reflector: Reflector,\n @Inject(XLT_TOKEN_CONFIG) private readonly config: XltTokenConfig,\n private readonly stpLogic: StpLogic,\n @Optional() private readonly stpPermLogic?: StpPermLogic,\n ) {\n }\n\n async canActivate(context: ExecutionContext): Promise<boolean> {\n if (!this.requiresLogin(context)) return true;\n\n const request = context.switchToHttp().getRequest();\n const response = context.switchToHttp().getResponse();\n\n let result: AuthResult;\n try {\n result = await this.stpLogic.checkLogin(createNestHttpContext(request, response));\n } catch (error) {\n rethrowCoreAuthException(error);\n }\n\n const business = this.getBusiness(context);\n request.stpLoginId = result.loginId;\n request.stpToken = result.token;\n\n try {\n if (this.stpPermLogic) {\n const handler = context.getHandler();\n const cls = context.getClass();\n\n const permMeta = this.reflector.getAllAndOverride(XLT_PERMISSION_KEY, [handler, cls]);\n if (permMeta) {\n await this.stpPermLogic.checkPermission(result.loginId!, permMeta.permissions, permMeta.mode);\n }\n\n const roleMeta = this.reflector.getAllAndOverride(XLT_ROLE_KEY, [handler, cls]);\n if (roleMeta) {\n await this.stpPermLogic.checkRole(result.loginId!, roleMeta.roles, roleMeta.mode);\n }\n }\n\n if (business) {\n await this.stpLogic.checkSafe(result.token!, business);\n }\n } catch (error) {\n rethrowCoreAuthException(error);\n }\n\n return true;\n }\n\n private requiresLogin(context: ExecutionContext): boolean {\n const isIgnored = this.reflector.getAllAndOverride<boolean>(XLT_IGNORE_KEY, [\n context.getHandler(),\n context.getClass(),\n ]);\n\n if (this.config.defaultCheck) {\n return !isIgnored;\n }\n\n const shouldCheck = this.reflector.getAllAndOverride<boolean>(XLT_CHECK_LOGIN_KEY, [\n context.getHandler(),\n context.getClass(),\n ]);\n return shouldCheck ?? false;\n }\n\n private getBusiness(context: ExecutionContext): string {\n return this.reflector.getAllAndOverride<string>(XLT_CHECK_SAFE_KEY, [\n context.getHandler(),\n context.getClass(),\n ]);\n }\n}\n","import { CanActivate, ExecutionContext, Inject, Injectable } from '@nestjs/common';\nimport { Reflector } from '@nestjs/core';\nimport {\n NotLoginException as CoreNotLoginException,\n NotLoginType,\n StpLogic,\n XLT_CHECK_LOGIN_KEY,\n XLT_IGNORE_KEY,\n XLT_TOKEN_CONFIG,\n type XltTokenConfig,\n} from '@xlt-token/core';\nimport { NotLoginException } from '../exceptions/not-login.exception.js';\nimport { createNestHttpContext } from '../http/nest-bridge.js';\n\n@Injectable()\nexport abstract class XltAbstractLoginGuard implements CanActivate {\n protected constructor(\n protected readonly reflector: Reflector,\n @Inject(XLT_TOKEN_CONFIG) protected readonly config: XltTokenConfig,\n protected readonly stpLogic: StpLogic,\n ) {}\n\n async canActivate(ctx: ExecutionContext): Promise<boolean> {\n if (!this.requiresLogin(ctx)) return true;\n\n const request = ctx.switchToHttp().getRequest();\n const response = ctx.switchToHttp().getResponse();\n\n let result: { ok: boolean; loginId?: string; token?: string; reason?: NotLoginType };\n try {\n result = await this.stpLogic.checkLogin(createNestHttpContext(request, response));\n } catch (err) {\n if (err instanceof CoreNotLoginException) {\n await this.onAuthFail?.({ ok: false, reason: err.type, token: err.token }, request);\n throw new NotLoginException(err.type, err.token);\n }\n throw err;\n }\n\n request.stpLoginId = result.loginId;\n request.stpToken = result.token;\n await this.onAuthSuccess?.(result, request);\n return true;\n }\n\n protected requiresLogin(ctx: ExecutionContext): boolean {\n const isIgnored = this.reflector.getAllAndOverride<boolean>(XLT_IGNORE_KEY, [ctx.getHandler(), ctx.getClass()]);\n\n if (this.config.defaultCheck) return !isIgnored;\n\n return this.reflector.getAllAndOverride<boolean>(XLT_CHECK_LOGIN_KEY, [ctx.getHandler(), ctx.getClass()]) ?? false;\n }\n\n protected onAuthSuccess?(\n result: {\n ok: boolean;\n loginId?: string | undefined;\n token?: string | undefined;\n reason?: NotLoginType | undefined;\n },\n request: any,\n ): void | Promise<void>;\n\n protected onAuthFail?(\n result: {\n ok: boolean;\n loginId?: string | undefined;\n token?: string | undefined;\n reason?: NotLoginType | undefined;\n },\n request: any,\n ): void | Promise<void>;\n\n protected onPermissionDenied?(\n result: {\n ok: boolean;\n loginId?: string | undefined;\n token?: string | undefined;\n reason?: NotLoginType | undefined;\n },\n request: any,\n ): void | Promise<void>;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAyCO,2BAAM,eAAe;;;;;0BAC6B;GACjD,SAASA;GACT,aACI,QACA,OACA,UACA,UACC,IAAIA,WAAS,QAA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AAO,QAAQ,IAAI;KAAM,OAAO,IAAI;KAAO,EAAE,QAAQ;AACnF,UAAM,IAAI,kBAAkB,IAAI,MAAM,IAAI,MAAM;;AAElD,SAAM;;AAGR,UAAQ,aAAa,OAAO;AAC5B,UAAQ,WAAW,OAAO;AAC1B,QAAM,KAAK,gBAAgB,QAAQ,QAAQ;AAC3C,SAAO;;CAGT,AAAU,cAAc,KAAgC;EACtD,MAAM,YAAY,KAAK,UAAU,kBAA2B,gBAAgB,CAAC,IAAI,YAAY,EAAE,IAAI,UAAU,CAAC,CAAC;AAE/G,MAAI,KAAK,OAAO,aAAc,QAAO,CAAC;AAEtC,SAAO,KAAK,UAAU,kBAA2B,qBAAqB,CAAC,IAAI,YAAY,EAAE,IAAI,UAAU,CAAC,CAAC,IAAI;;;;CApChH,YAAY;oBAIR,OAAOC,mBAAiB"}
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["StpLogic","XLT_TOKEN_CONFIG","XLT_TOKEN_STORE","XLT_TOKEN_STRATEGY","XLT_TOKEN_HOOKS","StpPermLogic","XLT_STP_INTERFACE","MemoryStore","UuidStrategy","XLT_TOKEN_CONFIG","XltMode","XltMode","NotLoginType","createExpressContext","CoreNotLoginException","CoreNotPermissionException","CoreNotRoleException","CoreNotSafeException","XLT_TOKEN_CONFIG","CoreNotLoginException","XLT_TOKEN_CONFIG"],"sources":["../src/xlt-token.module.ts","../src/store/redis-store.ts","../src/token/jwt-strategy.ts","../src/decorators/xlt-check-login.decorator.ts","../src/decorators/xlt-ignore.decorator.ts","../src/decorators/login-id.decorator.ts","../src/decorators/token-value.decorator.ts","../src/decorators/xlt-check-permission.decorator.ts","../src/decorators/xlt-check-role.decorator.ts","../src/decorators/xlt-check-safe.decorator.ts","../src/exceptions/not-login.exception.ts","../src/exceptions/not-permission.exception.ts","../src/exceptions/not-role.exception.ts","../src/exceptions/not-safe.exception.ts","../src/http/nest-bridge.ts","../src/guards/xlt-token.guard.ts","../src/guards/xlt-abstract-login.guard.ts"],"sourcesContent":["import type { ModuleMetadata, Provider } from '@nestjs/common';\nimport { Module } from '@nestjs/common';\nimport type { StpInterface, TokenStrategy, XltHooks, XltTokenConfig, XltTokenConfigInput, XltTokenStore, } from '@xlt-token/core';\nimport {\n DEFAULT_XLT_TOKEN_CONFIG,\n MemoryStore,\n setStpLogic,\n setStpPermLogic,\n StpLogic,\n StpPermLogic,\n UuidStrategy,\n XLT_STP_INTERFACE,\n XLT_TOKEN_CONFIG,\n XLT_TOKEN_HOOKS,\n XLT_TOKEN_STORE,\n XLT_TOKEN_STRATEGY,\n normalizeXltTokenConfig\n} from '@xlt-token/core';\n\nexport interface XltTokenModuleOptions {\n config?: Partial<XltTokenConfigInput>;\n store?: { useClass: new (...args: any[]) => XltTokenStore } | { useValue: XltTokenStore };\n strategy?: { useClass: new (...args: any[]) => TokenStrategy };\n isGlobal?: boolean;\n providers?: Provider[];\n stpInterface?: new (...args: any[]) => StpInterface;\n hooks?: XltHooks;\n}\n\nexport interface XltTokenModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> {\n useFactory: (...args: any[]) => Promise<XltTokenModuleOptions> | XltTokenModuleOptions;\n inject?: any[];\n store?: { useClass: new (...args: any[]) => XltTokenStore } | { useValue: XltTokenStore };\n strategy?: { useClass: new (...args: any[]) => TokenStrategy };\n isGlobal?: boolean;\n providers?: Provider[];\n stpInterface?: new (...args: any[]) => StpInterface;\n hooks?: XltHooks;\n}\n\n@Module({})\nexport class XltTokenModule {\n private static readonly stpLogicProvider: Provider = {\n provide: StpLogic,\n useFactory: (\n config: XltTokenConfig,\n store: XltTokenStore,\n strategy: TokenStrategy,\n hooks: XltHooks,\n ) => new StpLogic(config, store, strategy, hooks),\n inject: [ XLT_TOKEN_CONFIG, XLT_TOKEN_STORE, XLT_TOKEN_STRATEGY, XLT_TOKEN_HOOKS ],\n };\n private static readonly stpPermLogicProvider: Provider = {\n provide: StpPermLogic,\n useFactory: (\n stpInterface: StpInterface,\n store: XltTokenStore,\n config: XltTokenConfig,\n ) => new StpPermLogic(stpInterface, store, config),\n inject: [ XLT_STP_INTERFACE, XLT_TOKEN_STORE, XLT_TOKEN_CONFIG ],\n };\n private static readonly initProvider: Provider = {\n provide: 'XLT_TOKEN_INIT',\n useFactory: (stpLogic: StpLogic, stpPermLogic: StpPermLogic) => {\n setStpLogic(stpLogic);\n setStpPermLogic(stpPermLogic);\n return true;\n },\n inject: [ StpLogic, StpPermLogic ],\n };\n private static readonly moduleExports = [ XLT_TOKEN_CONFIG, XLT_TOKEN_STORE, XLT_TOKEN_STRATEGY, StpLogic, StpPermLogic ];\n\n static forRoot(options: XltTokenModuleOptions = {}) {\n const {config: userConfig, store, strategy, isGlobal = false, providers = [], stpInterface} = options;\n\n return {\n module: XltTokenModule,\n providers: [\n {provide: XLT_TOKEN_CONFIG, useValue: normalizeXltTokenConfig(userConfig)},\n XltTokenModule.createStoreProvider(store),\n XltTokenModule.createStrategyProvider(strategy),\n XltTokenModule.createStpInterfaceProvider(stpInterface),\n XltTokenModule.createHooksProvider(options.hooks),\n XltTokenModule.stpLogicProvider,\n XltTokenModule.stpPermLogicProvider,\n XltTokenModule.initProvider,\n ...providers,\n ],\n exports: XltTokenModule.moduleExports,\n global: isGlobal,\n };\n }\n\n static forRootAsync(options: XltTokenModuleAsyncOptions) {\n const {\n useFactory,\n inject = [],\n imports = [],\n store,\n strategy,\n isGlobal = false,\n providers = [],\n stpInterface\n } = options;\n\n return {\n module: XltTokenModule,\n imports,\n providers: [\n {\n provide: XLT_TOKEN_CONFIG,\n useFactory: async (...args: any[]) => {\n const {config = {}} = await useFactory(...args);\n return normalizeXltTokenConfig(config)\n },\n inject,\n },\n XltTokenModule.createStoreProvider(store),\n XltTokenModule.createStrategyProvider(strategy),\n XltTokenModule.createStpInterfaceProvider(stpInterface),\n XltTokenModule.createHooksProvider(options.hooks),\n XltTokenModule.stpLogicProvider,\n XltTokenModule.stpPermLogicProvider,\n XltTokenModule.initProvider,\n ...providers,\n ],\n exports: XltTokenModule.moduleExports,\n global: isGlobal,\n };\n }\n\n private static createStoreProvider(\n store?: XltTokenModuleOptions['store'],\n ): Provider {\n if ( !store ) return {provide: XLT_TOKEN_STORE, useClass: MemoryStore};\n return 'useClass' in store\n ? {provide: XLT_TOKEN_STORE, useClass: store.useClass}\n : {provide: XLT_TOKEN_STORE, useValue: store.useValue};\n }\n\n private static createStrategyProvider(\n strategy?: XltTokenModuleOptions['strategy'],\n ): Provider {\n return strategy?.useClass\n ? {provide: XLT_TOKEN_STRATEGY, useClass: strategy.useClass}\n : {provide: XLT_TOKEN_STRATEGY, useClass: UuidStrategy};\n }\n\n private static createStpInterfaceProvider(\n stpInterface?: new (...args: any[]) => StpInterface,\n ): Provider {\n if ( stpInterface ) return {provide: XLT_STP_INTERFACE, useClass: stpInterface};\n return {\n provide: XLT_STP_INTERFACE,\n useValue: {\n getPermissionList: () => {\n throw new Error('StpInterface not registered: getPermissionList');\n },\n getRoleList: () => {\n throw new Error('StpInterface not registered: getRoleList');\n },\n },\n };\n }\n\n private static createHooksProvider(\n hooks?: XltTokenModuleOptions['hooks'],\n ): Provider {\n return {provide: XLT_TOKEN_HOOKS, useValue: hooks ?? {}};\n }\n}\n","import type { XltTokenStore } from '@xlt-token/core';\nimport { Inject, Injectable } from '@nestjs/common';\n\n\nexport const XLT_REDIS_CLIENT = 'XLT_REDIS_CLIENT';\n\n\n@Injectable()\nexport class RedisStore implements XltTokenStore {\n\n constructor(\n @Inject(XLT_REDIS_CLIENT)\n private readonly redisClient: any,\n ) {\n\n }\n\n\n async get(key:string):Promise<string | null> {\n return this.redisClient.get(key);\n }\n\n\n async set(key:string, value:string, timeoutSec:number):Promise<void> {\n if (timeoutSec === -1){\n await this.redisClient.set(key, value);\n }else {\n await this.redisClient.set(key, value, {EX: timeoutSec});\n }\n }\n\n\n async delete(key:string):Promise<void> {\n await this.redisClient.del(key);\n }\n\n\n async update(key:string, value:string):Promise<void> {\n const result = await this.redisClient.set(key, value,{XX:true,KEEPTTL:true})\n if (result ===null){\n throw new Error(`Key not found: ${key}`);\n }\n }\n\n\n async has(key:string):Promise<boolean> {\n const result = await this.redisClient.exists(key);\n return result === 1;\n }\n\n\n async updateTimeout(key:string, timeoutSec:number):Promise<void> {\n const exists = await this.redisClient.exists(key);\n\n if (!exists) {\n throw new Error(`Key not found: ${key}`);\n }\n\n if (timeoutSec === -1) {\n await this.redisClient.persist(key);\n }else {\n await this.redisClient.expire(key, timeoutSec);\n }\n\n }\n\n async getTimeout(key:string):Promise<number> {\n const result = await this.redisClient.ttl(key);\n // Redis TTL 返回值约定:\n // -2 = key 不存在\n // -1 = key 存在但无过期时间(永久)\n // >0 = 剩余秒数\n // 恰好与 XltTokenStore 接口约定一致\n return result;\n }\n\n async keys(pattern: string): Promise<string[]> {\n const result: string[] = [];\n let cursor = \"0\";\n do {\n const reply = await this.redisClient.scan(cursor, { MATCH: pattern, COUNT: 100 });\n cursor = String(reply.cursor);\n result.push(...reply.keys);\n } while (cursor !== \"0\");\n return result;\n }\n}\n","import { Inject, Injectable } from \"@nestjs/common\";\nimport { randomUUID } from 'node:crypto';\nimport { createRequire } from 'node:module';\nimport { XLT_TOKEN_CONFIG } from '@xlt-token/core';\nimport type { DurationInput, TokenStrategy, XltTokenConfig } from '@xlt-token/core';\n\nconst require = createRequire(import.meta.url);\n\nlet jsonwebtoken: typeof import('jsonwebtoken') | undefined;\n\nexport type XltJwtPayload = Record<string, any> & { sub: string; jti: string };\n\nfunction getJsonwebtoken(): typeof import('jsonwebtoken') {\n try {\n jsonwebtoken ??= require('jsonwebtoken') as typeof import('jsonwebtoken');\n return jsonwebtoken;\n } catch (error) {\n const err = error as NodeJS.ErrnoException;\n if (err.code === 'MODULE_NOT_FOUND') {\n throw new Error(\n 'JwtStrategy requires the optional peer dependency \"jsonwebtoken\". '\n + 'Install it in your application with \"pnpm add jsonwebtoken\".',\n );\n }\n throw error;\n }\n}\n\n@Injectable()\nexport class JwtStrategy implements TokenStrategy<XltJwtPayload> {\n constructor(\n @Inject(XLT_TOKEN_CONFIG) private readonly config: XltTokenConfig\n ) { }\n\n private ensureJwtConfig(config?: XltTokenConfig): NonNullable<XltTokenConfig['jwt']> {\n const jwt = (config ?? this.config).jwt;\n if (!jwt || !jwt.secret) {\n throw new Error(\n 'JwtStrategy requires jwt config with a secret. '\n + 'Provide { jwt: { secret: \"your-secret\" } } in the module config.',\n );\n }\n return jwt;\n }\n\n createToken(loginId: string, config: XltTokenConfig, options?: { timeout?: DurationInput }): string {\n const { sign } = getJsonwebtoken();\n const jwt = this.ensureJwtConfig(config);\n const jti = randomUUID();\n\n const resolvedTimeout = options?.timeout ?? config.timeout;\n const hasExpiry = typeof resolvedTimeout === 'number' ? resolvedTimeout > 0 : true;\n\n return sign({\n sub: loginId, jti\n }, jwt.secret, {\n algorithm: jwt.algorithm ?? 'HS256',\n ...(jwt.issuer && { issuer: jwt.issuer }),\n ...(jwt.audience && { audience: jwt.audience }),\n ...(hasExpiry && { expiresIn: resolvedTimeout }),\n })\n }\n\n generateToken(payload: any): string {\n const { sign } = getJsonwebtoken();\n const jwt = this.ensureJwtConfig();\n return sign(payload, jwt.secret);\n }\n\n verifyToken(token: string): XltJwtPayload {\n const { verify } = getJsonwebtoken();\n const jwt = this.ensureJwtConfig();\n return verify(token, jwt.secret) as XltJwtPayload;\n }\n\n}\n","// 登录校验装饰器\n\nimport { SetMetadata } from '@nestjs/common';\nimport { XLT_CHECK_LOGIN_KEY } from '@xlt-token/core';\n\n\n/**\n * 登录校验装饰器\n * @constructor\n */\nexport const XltCheckLogin = () => SetMetadata(XLT_CHECK_LOGIN_KEY, true);\n","// 忽略校验装饰器\n\nimport { SetMetadata } from '@nestjs/common';\nimport { XLT_IGNORE_KEY } from '@xlt-token/core';\n\nexport const XltIgnore = () => SetMetadata(XLT_IGNORE_KEY, true);\n","// 注入当前用户 ID\n\nimport { createParamDecorator, ExecutionContext } from '@nestjs/common';\n\n/**\n * 注入当前用户 ID\n * @constructor\n */\nexport const LoginId = createParamDecorator(\n (data: unknown, ctx: ExecutionContext) => {\n const request = ctx.switchToHttp().getRequest();\n return request.stpLoginId;\n },\n);\n","// 注入当前 Token\n\nimport { createParamDecorator, ExecutionContext } from '@nestjs/common';\n\n\n/**\n * 注入当前 Token\n * @constructor\n */\nexport const TokenValue = createParamDecorator((data: any, ctx: ExecutionContext) => {\n const request = ctx.switchToHttp().getRequest();\n return request.stpToken;\n});\n","import { XLT_PERMISSION_KEY, XltMode } from '@xlt-token/core';\nimport { SetMetadata } from '@nestjs/common';\n\n\n/**\n * 权限检查装饰器\n * @param {string | string[]} permissions 权限列表\n * @param {Object} [options] 模式选项\n * @param {XltMode} [options.mode] 模式选项\n * @constructor\n */\nexport const XltCheckPermission = (permissions: string | string[], options?: { mode: XltMode; }) => {\n const perms = Array.isArray(permissions) ? permissions : [permissions];\n const mode = options?.mode ?? XltMode.AND;\n return SetMetadata(XLT_PERMISSION_KEY, { permissions: perms, mode });\n};\n","import { XLT_ROLE_KEY, XltMode } from '@xlt-token/core';\nimport { SetMetadata } from '@nestjs/common';\n\n\n/**\n * 角色检查装饰器\n * @param {string | string[]} roles 角色列表\n * @param {Object} [options] 模式选项\n * @param {XltMode} [options.mode] 模式选项\n * @constructor\n */\nexport const XltCheckRole = (roles: string | string[], options?: { mode: XltMode; }) => {\n const _roles = Array.isArray(roles) ? roles : [roles];\n const mode = options?.mode ?? XltMode.AND;\n return SetMetadata(XLT_ROLE_KEY, { roles: _roles, mode });\n\n};\n","import { SetMetadata } from \"@nestjs/common\"\n\nexport const XLT_CHECK_SAFE_KEY = 'XLT_CHECK_SAFE';\n\nexport const XltCheckSafe = (business: string) => {\n return SetMetadata(XLT_CHECK_SAFE_KEY, business);\n}\n","import { UnauthorizedException } from '@nestjs/common';\nimport { NotLoginType } from '@xlt-token/core';\n\nexport class NotLoginException extends UnauthorizedException {\n public readonly type: NotLoginType;\n public readonly token: string | undefined;\n\n constructor(type: NotLoginType, token?: string) {\n super({\n statusCode: 401,\n type,\n message: NotLoginException.describeType(type),\n });\n this.type = type;\n this.token = token;\n }\n\n private static describeType(type: NotLoginType): string {\n const map: Record<NotLoginType, string> = {\n [NotLoginType.NOT_TOKEN]: '未提供 Token',\n [NotLoginType.INVALID_TOKEN]: 'Token 无效',\n [NotLoginType.TOKEN_TIMEOUT]: 'Token 已过期',\n [NotLoginType.TOKEN_FREEZE]: 'Token 已被冻结',\n [NotLoginType.BE_REPLACED]: '已被顶下线',\n [NotLoginType.KICK_OUT]: '已被踢下线',\n };\n return map[type] ?? '未登录';\n }\n}\n\n","import { ForbiddenException } from '@nestjs/common';\nimport { XltMode } from '@xlt-token/core';\n\nexport class NotPermissionException extends ForbiddenException {\n public readonly permission: string | string[];\n public readonly mode: XltMode;\n\n constructor(permission: string | string[], mode: XltMode) {\n super({\n statusCode: 403,\n type: 'NOT_PERMISSION',\n message: `缺少权限: ${Array.isArray(permission) ? permission.join(', ') : permission}`,\n });\n this.permission = permission;\n this.mode = mode;\n }\n}\n","import { ForbiddenException } from '@nestjs/common';\nimport { XltMode } from '@xlt-token/core';\n\nexport class NotRoleException extends ForbiddenException {\n public readonly role: string | string[];\n public readonly mode: XltMode;\n\n constructor(role: string | string[], mode: XltMode) {\n super({\n statusCode: 403,\n type: 'NOT_ROLE',\n message: `缺少角色: ${Array.isArray(role) ? role.join(', ') : role}`,\n });\n this.role = role;\n this.mode = mode;\n }\n}\n","import { ForbiddenException } from \"@nestjs/common\";\n\nexport class NotSafeException extends ForbiddenException {\n readonly business: string;\n\n\n constructor(business: string) {\n super({\n statusCode: 403,\n type: 'NOT_SAFE',\n message: `二级认证未开启:${business}`,\n });\n this.business = business;\n }\n\n}\n","import {\n createExpressContext,\n type CookieOptions,\n type ExpressLikeResponse,\n NotLoginException as CoreNotLoginException,\n NotPermissionException as CoreNotPermissionException,\n NotRoleException as CoreNotRoleException,\n NotSafeException as CoreNotSafeException,\n} from '@xlt-token/core';\nimport { NotLoginException } from '../exceptions/not-login.exception.js';\nimport { NotPermissionException } from '../exceptions/not-permission.exception.js';\nimport { NotRoleException } from '../exceptions/not-role.exception.js';\nimport { NotSafeException } from '../exceptions/not-safe.exception.js';\n\n/**\n * Fastify reply 的写回 API 与 Express response 不同:\n * - 写 header:Express 用 `res.setHeader(n, v)`,Fastify 用 `reply.header(n, v)`\n * - 写 cookie:Express 用 `res.cookie(n, v, o)`,Fastify 用 `reply.setCookie(n, v, o)`\n *\n * 这里把任意一种 response 归一化成 core 期望的 {@link ExpressLikeResponse} 形态,\n * 让核心层无需感知底层 HTTP 平台。读取侧(headers/cookies/query)两个平台形态一致,\n * 直接复用 core 的 createExpressContext。\n */\nfunction normalizeResponse(res: any): ExpressLikeResponse {\n return {\n setHeader(name: string, value: string): void {\n if (typeof res?.setHeader === 'function') {\n // Express response / 原生 Node 响应\n res.setHeader(name, value);\n } else if (typeof res?.header === 'function') {\n // Fastify reply\n res.header(name, value);\n } else {\n throw new Error(\n 'xlt-token: 当前 response 不支持写入 header(既无 setHeader 也无 header 方法)',\n );\n }\n },\n cookie(name: string, value: string, options?: CookieOptions): void {\n if (typeof res?.cookie === 'function') {\n // Express response(或已注册 @fastify/cookie 暴露的 cookie 别名)\n res.cookie(name, value, options);\n } else if (typeof res?.setCookie === 'function') {\n // Fastify reply + @fastify/cookie 插件\n res.setCookie(name, value, options);\n } else {\n throw new Error(\n 'xlt-token: 当前 response 不支持写入 cookie。'\n + '若使用 Fastify,请先注册 @fastify/cookie 插件。',\n );\n }\n },\n };\n}\n\nexport function createNestHttpContext(req: any, res: any) {\n return createExpressContext(req, normalizeResponse(res));\n}\n\nexport function rethrowCoreAuthException(error: unknown): never {\n if (error instanceof CoreNotLoginException) {\n throw new NotLoginException(error.type, error.token);\n }\n if (error instanceof CoreNotPermissionException) {\n throw new NotPermissionException(error.permission, error.mode);\n }\n if (error instanceof CoreNotRoleException) {\n throw new NotRoleException(error.role, error.mode);\n }\n if (error instanceof CoreNotSafeException) {\n throw new NotSafeException(error.business);\n }\n throw error;\n}\n","// 全局守卫\n\nimport { CanActivate, ExecutionContext, Inject, Injectable, Optional } from '@nestjs/common';\nimport { Reflector } from '@nestjs/core';\nimport {\n type AuthResult,\n StpLogic,\n StpPermLogic,\n XLT_CHECK_LOGIN_KEY,\n XLT_IGNORE_KEY,\n XLT_PERMISSION_KEY,\n XLT_ROLE_KEY,\n XLT_TOKEN_CONFIG,\n type XltTokenConfig,\n} from '@xlt-token/core';\nimport { XLT_CHECK_SAFE_KEY } from '../decorators/xlt-check-safe.decorator.js';\nimport { createNestHttpContext, rethrowCoreAuthException } from '../http/nest-bridge.js';\n\n@Injectable()\nexport class XltTokenGuard implements CanActivate {\n constructor(\n private readonly reflector: Reflector,\n @Inject(XLT_TOKEN_CONFIG) private readonly config: XltTokenConfig,\n private readonly stpLogic: StpLogic,\n @Optional() private readonly stpPermLogic?: StpPermLogic,\n ) {\n }\n\n async canActivate(context: ExecutionContext): Promise<boolean> {\n if (!this.requiresLogin(context)) return true;\n\n const request = context.switchToHttp().getRequest();\n const response = context.switchToHttp().getResponse();\n\n let result: AuthResult;\n try {\n result = await this.stpLogic.checkLogin(createNestHttpContext(request, response));\n } catch (error) {\n rethrowCoreAuthException(error);\n }\n\n const business = this.getBusiness(context);\n request.stpLoginId = result.loginId;\n request.stpToken = result.token;\n\n try {\n if (this.stpPermLogic) {\n const handler = context.getHandler();\n const cls = context.getClass();\n\n const permMeta = this.reflector.getAllAndOverride(XLT_PERMISSION_KEY, [handler, cls]);\n if (permMeta) {\n await this.stpPermLogic.checkPermission(result.loginId!, permMeta.permissions, permMeta.mode);\n }\n\n const roleMeta = this.reflector.getAllAndOverride(XLT_ROLE_KEY, [handler, cls]);\n if (roleMeta) {\n await this.stpPermLogic.checkRole(result.loginId!, roleMeta.roles, roleMeta.mode);\n }\n }\n\n if (business) {\n await this.stpLogic.checkSafe(result.token!, business);\n }\n } catch (error) {\n rethrowCoreAuthException(error);\n }\n\n return true;\n }\n\n private requiresLogin(context: ExecutionContext): boolean {\n const isIgnored = this.reflector.getAllAndOverride<boolean>(XLT_IGNORE_KEY, [\n context.getHandler(),\n context.getClass(),\n ]);\n\n if (this.config.defaultCheck) {\n return !isIgnored;\n }\n\n const shouldCheck = this.reflector.getAllAndOverride<boolean>(XLT_CHECK_LOGIN_KEY, [\n context.getHandler(),\n context.getClass(),\n ]);\n return shouldCheck ?? false;\n }\n\n private getBusiness(context: ExecutionContext): string {\n return this.reflector.getAllAndOverride<string>(XLT_CHECK_SAFE_KEY, [\n context.getHandler(),\n context.getClass(),\n ]);\n }\n}\n","import { CanActivate, ExecutionContext, Inject, Injectable } from '@nestjs/common';\nimport { Reflector } from '@nestjs/core';\nimport {\n NotLoginException as CoreNotLoginException,\n NotLoginType,\n StpLogic,\n XLT_CHECK_LOGIN_KEY,\n XLT_IGNORE_KEY,\n XLT_TOKEN_CONFIG,\n type XltTokenConfig,\n} from '@xlt-token/core';\nimport { NotLoginException } from '../exceptions/not-login.exception.js';\nimport { createNestHttpContext } from '../http/nest-bridge.js';\n\n@Injectable()\nexport abstract class XltAbstractLoginGuard implements CanActivate {\n protected constructor(\n protected readonly reflector: Reflector,\n @Inject(XLT_TOKEN_CONFIG) protected readonly config: XltTokenConfig,\n protected readonly stpLogic: StpLogic,\n ) {}\n\n async canActivate(ctx: ExecutionContext): Promise<boolean> {\n if (!this.requiresLogin(ctx)) return true;\n\n const request = ctx.switchToHttp().getRequest();\n const response = ctx.switchToHttp().getResponse();\n\n let result: { ok: boolean; loginId?: string; token?: string; reason?: NotLoginType };\n try {\n result = await this.stpLogic.checkLogin(createNestHttpContext(request, response));\n } catch (err) {\n if (err instanceof CoreNotLoginException) {\n await this.onAuthFail?.({ ok: false, reason: err.type, token: err.token }, request);\n throw new NotLoginException(err.type, err.token);\n }\n throw err;\n }\n\n request.stpLoginId = result.loginId;\n request.stpToken = result.token;\n await this.onAuthSuccess?.(result, request);\n return true;\n }\n\n protected requiresLogin(ctx: ExecutionContext): boolean {\n const isIgnored = this.reflector.getAllAndOverride<boolean>(XLT_IGNORE_KEY, [ctx.getHandler(), ctx.getClass()]);\n\n if (this.config.defaultCheck) return !isIgnored;\n\n return this.reflector.getAllAndOverride<boolean>(XLT_CHECK_LOGIN_KEY, [ctx.getHandler(), ctx.getClass()]) ?? false;\n }\n\n protected onAuthSuccess?(\n result: {\n ok: boolean;\n loginId?: string | undefined;\n token?: string | undefined;\n reason?: NotLoginType | undefined;\n },\n request: any,\n ): void | Promise<void>;\n\n protected onAuthFail?(\n result: {\n ok: boolean;\n loginId?: string | undefined;\n token?: string | undefined;\n reason?: NotLoginType | undefined;\n },\n request: any,\n ): void | Promise<void>;\n\n protected onPermissionDenied?(\n result: {\n ok: boolean;\n loginId?: string | undefined;\n token?: string | undefined;\n reason?: NotLoginType | undefined;\n },\n request: any,\n ): void | Promise<void>;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAyCO,2BAAM,eAAe;;;;;0BAC6B;GACjD,SAASA;GACT,aACI,QACA,OACA,UACA,UACC,IAAIA,WAAS,QAAQ,OAAO,UAAU,MAAM;GACjD,QAAQ;IAAEC;IAAkBC;IAAiBC;IAAoBC;IAAiB;GACrF;;;8BACwD;GACrD,SAASC;GACT,aACI,cACA,OACA,WACC,IAAIA,eAAa,cAAc,OAAO,OAAO;GAClD,QAAQ;IAAEC;IAAmBJ;IAAiBD;IAAkB;GACnE;;;sBACgD;GAC7C,SAAS;GACT,aAAa,UAAoB,iBAA+B;AAC5D,kBAAY,SAAS;AACrB,sBAAgB,aAAa;AAC7B,WAAO;;GAEX,QAAQ,CAAED,YAAUK,eAAc;GACrC;;;uBACuC;GAAEJ;GAAkBC;GAAiBC;GAAoBH;GAAUK;GAAc;;CAEzH,OAAO,QAAQ,UAAiC,EAAE,EAAE;EAChD,MAAM,EAAC,QAAQ,YAAY,OAAO,UAAU,WAAW,OAAO,YAAY,EAAE,EAAE,iBAAgB;AAE9F,SAAO;GACH;GACA,WAAW;IACP;KAAC,SAASJ;KAAkB,UAAU,wBAAwB,WAAW;KAAC;oBAC3D,o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token/nestjs",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "NestJS integration for xlt-token",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"nestjs",
|
|
@@ -49,6 +49,8 @@
|
|
|
49
49
|
"peerDependencies": {
|
|
50
50
|
"@nestjs/common": "^10.0.0 || ^11.0.0 || ^12.0.0",
|
|
51
51
|
"@nestjs/core": "^10.0.0 || ^11.0.0 || ^12.0.0",
|
|
52
|
+
"jsonwebtoken": "^9.0.0",
|
|
53
|
+
"redis": "^4.0.0 || ^5.0.0",
|
|
52
54
|
"reflect-metadata": "^0.1.13 || ^0.2.0",
|
|
53
55
|
"rxjs": "^7.0.0"
|
|
54
56
|
},
|
|
@@ -61,7 +63,7 @@
|
|
|
61
63
|
}
|
|
62
64
|
},
|
|
63
65
|
"dependencies": {
|
|
64
|
-
"@xlt-token/core": "^1.
|
|
66
|
+
"@xlt-token/core": "^1.2.0",
|
|
65
67
|
"uuid": "^10.0.0"
|
|
66
68
|
},
|
|
67
69
|
"devDependencies": {
|