@nest-boot/auth 5.0.0-alpha.1 → 5.0.0-alpha.2

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.
Files changed (56) hide show
  1. package/dist/auth.constants.d.ts +2 -0
  2. package/dist/auth.constants.js +5 -19
  3. package/dist/auth.constants.js.map +1 -0
  4. package/dist/auth.guard.d.ts +13 -0
  5. package/dist/auth.guard.js +51 -89
  6. package/dist/auth.guard.js.map +1 -0
  7. package/dist/auth.middleware.d.ts +13 -0
  8. package/dist/auth.middleware.js +35 -64
  9. package/dist/auth.middleware.js.map +1 -0
  10. package/dist/auth.module-definition.d.ts +4 -0
  11. package/dist/auth.module-definition.js +9 -28
  12. package/dist/auth.module-definition.js.map +1 -0
  13. package/dist/auth.module.d.ts +8 -0
  14. package/dist/auth.module.js +35 -62
  15. package/dist/auth.module.js.map +1 -0
  16. package/dist/auth.service.d.ts +12 -0
  17. package/dist/auth.service.js +38 -82
  18. package/dist/auth.service.js.map +1 -0
  19. package/dist/decorators/can.decorator.d.ts +1 -0
  20. package/dist/decorators/can.decorator.js +7 -12
  21. package/dist/decorators/can.decorator.js.map +1 -0
  22. package/dist/decorators/current-entity.decorator.d.ts +2 -0
  23. package/dist/decorators/current-entity.decorator.js +9 -23
  24. package/dist/decorators/current-entity.decorator.js.map +1 -0
  25. package/dist/decorators/current-personal-access-token.decorator.d.ts +1 -0
  26. package/dist/decorators/current-personal-access-token.decorator.js +8 -14
  27. package/dist/decorators/current-personal-access-token.decorator.js.map +1 -0
  28. package/dist/decorators/index.d.ts +4 -0
  29. package/dist/decorators/index.js +20 -20
  30. package/dist/decorators/index.js.map +1 -0
  31. package/dist/decorators/require-auth.decorator.d.ts +1 -0
  32. package/dist/decorators/require-auth.decorator.js +7 -12
  33. package/dist/decorators/require-auth.decorator.js.map +1 -0
  34. package/dist/index.d.ts +8 -0
  35. package/dist/index.js +24 -24
  36. package/dist/index.js.map +1 -0
  37. package/dist/interfaces/access-token.interface.d.ts +9 -0
  38. package/dist/interfaces/access-token.interface.js +2 -3
  39. package/dist/interfaces/access-token.interface.js.map +1 -0
  40. package/dist/interfaces/auth-module-options.interface.d.ts +10 -0
  41. package/dist/interfaces/auth-module-options.interface.js +2 -3
  42. package/dist/interfaces/auth-module-options.interface.js.map +1 -0
  43. package/dist/interfaces/has-permission.interface.d.ts +3 -0
  44. package/dist/interfaces/has-permission.interface.js +2 -3
  45. package/dist/interfaces/has-permission.interface.js.map +1 -0
  46. package/dist/interfaces/index.d.ts +3 -0
  47. package/dist/interfaces/index.js +19 -19
  48. package/dist/interfaces/index.js.map +1 -0
  49. package/dist/tsconfig.tsbuildinfo +1 -0
  50. package/dist/utils/index.d.ts +1 -0
  51. package/dist/utils/index.js +17 -17
  52. package/dist/utils/index.js.map +1 -0
  53. package/dist/utils/mixin-permissions.utils.d.ts +3 -0
  54. package/dist/utils/mixin-permissions.utils.js +9 -30
  55. package/dist/utils/mixin-permissions.utils.js.map +1 -0
  56. package/package.json +8 -10
@@ -0,0 +1,2 @@
1
+ export declare const AUTH_ENTITY = "AUTH_ENTITY";
2
+ export declare const AUTH_ACCESS_TOKEN = "AUTH_ACCESS_TOKEN";
@@ -1,20 +1,6 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- function _export(target, all) {
6
- for(var name in all)Object.defineProperty(target, name, {
7
- enumerable: true,
8
- get: all[name]
9
- });
10
- }
11
- _export(exports, {
12
- AUTH_ENTITY: function() {
13
- return AUTH_ENTITY;
14
- },
15
- AUTH_ACCESS_TOKEN: function() {
16
- return AUTH_ACCESS_TOKEN;
17
- }
18
- });
19
- const AUTH_ENTITY = "AUTH_ENTITY";
20
- const AUTH_ACCESS_TOKEN = "AUTH_ACCESS_TOKEN";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AUTH_ACCESS_TOKEN = exports.AUTH_ENTITY = void 0;
4
+ exports.AUTH_ENTITY = "AUTH_ENTITY";
5
+ exports.AUTH_ACCESS_TOKEN = "AUTH_ACCESS_TOKEN";
6
+ //# sourceMappingURL=auth.constants.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth.constants.js","sourceRoot":"","sources":["../src/auth.constants.ts"],"names":[],"mappings":";;;AAAa,QAAA,WAAW,GAAG,aAAa,CAAC;AAC5B,QAAA,iBAAiB,GAAG,mBAAmB,CAAC"}
@@ -0,0 +1,13 @@
1
+ import { EntityManager } from "@mikro-orm/core";
2
+ import { type CanActivate, type ExecutionContext } from "@nestjs/common";
3
+ import { Reflector } from "@nestjs/core";
4
+ import { AuthModuleOptions } from "./interfaces";
5
+ export declare class AuthGuard implements CanActivate {
6
+ private readonly options;
7
+ private readonly reflector;
8
+ private readonly entityManager;
9
+ constructor(options: AuthModuleOptions, reflector: Reflector, entityManager: EntityManager);
10
+ private t;
11
+ canActivate(executionContext: ExecutionContext): Promise<boolean>;
12
+ getPermissions(): Promise<string[]>;
13
+ }
@@ -1,100 +1,74 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- Object.defineProperty(exports, "AuthGuard", {
6
- enumerable: true,
7
- get: function() {
8
- return AuthGuard;
9
- }
10
- });
11
- const _core = require("@mikro-orm/core");
12
- const _i18n = require("@nest-boot/i18n");
13
- const _requestcontext = require("@nest-boot/request-context");
14
- const _common = require("@nestjs/common");
15
- const _core1 = require("@nestjs/core");
16
- const _lodash = /*#__PURE__*/ _interop_require_default(require("lodash"));
17
- const _authconstants = require("./auth.constants");
18
- const _authmoduledefinition = require("./auth.module-definition");
19
- const _interfaces = require("./interfaces/index");
20
- function _define_property(obj, key, value) {
21
- if (key in obj) {
22
- Object.defineProperty(obj, key, {
23
- value: value,
24
- enumerable: true,
25
- configurable: true,
26
- writable: true
27
- });
28
- } else {
29
- obj[key] = value;
30
- }
31
- return obj;
32
- }
33
- function _interop_require_default(obj) {
34
- return obj && obj.__esModule ? obj : {
35
- default: obj
36
- };
37
- }
38
- function _ts_decorate(decorators, target, key, desc) {
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
39
3
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
40
4
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
41
- 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;
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
42
6
  return c > 3 && r && Object.defineProperty(target, key, r), r;
43
- }
44
- function _ts_metadata(k, v) {
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
45
9
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
46
- }
47
- function _ts_param(paramIndex, decorator) {
48
- return function(target, key) {
49
- decorator(target, key, paramIndex);
50
- };
51
- }
52
- let AuthGuard = class AuthGuard {
10
+ };
11
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
12
+ return function (target, key) { decorator(target, key, paramIndex); }
13
+ };
14
+ var __importDefault = (this && this.__importDefault) || function (mod) {
15
+ return (mod && mod.__esModule) ? mod : { "default": mod };
16
+ };
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.AuthGuard = void 0;
19
+ const core_1 = require("@mikro-orm/core");
20
+ const i18n_1 = require("@nest-boot/i18n");
21
+ const request_context_1 = require("@nest-boot/request-context");
22
+ const common_1 = require("@nestjs/common");
23
+ const core_2 = require("@nestjs/core");
24
+ const lodash_1 = __importDefault(require("lodash"));
25
+ const auth_constants_1 = require("./auth.constants");
26
+ const auth_module_definition_1 = require("./auth.module-definition");
27
+ let AuthGuard = exports.AuthGuard = class AuthGuard {
28
+ constructor(options, reflector, entityManager) {
29
+ this.options = options;
30
+ this.reflector = reflector;
31
+ this.entityManager = entityManager;
32
+ this.reflector = reflector;
33
+ }
53
34
  t(key) {
54
- return _requestcontext.RequestContext.get(_i18n.I18N)?.t(key, {
55
- ns: "auth"
56
- }) ?? key;
35
+ var _a, _b;
36
+ return (_b = (_a = request_context_1.RequestContext.get(i18n_1.I18N)) === null || _a === void 0 ? void 0 : _a.t(key, { ns: "auth" })) !== null && _b !== void 0 ? _b : key;
57
37
  }
58
38
  async canActivate(executionContext) {
59
- if (![
60
- "http",
61
- "graphql"
62
- ].includes(executionContext.getType())) {
39
+ var _a, _b, _c;
40
+ if (!["http", "graphql"].includes(executionContext.getType())) {
63
41
  return true;
64
42
  }
65
- if (typeof _requestcontext.RequestContext.get(_requestcontext.REQUEST)?.authMiddlewareUsed === "undefined") {
43
+ if (typeof ((_a = request_context_1.RequestContext.get(request_context_1.REQUEST)) === null || _a === void 0 ? void 0 : _a.authMiddlewareUsed) === "undefined") {
66
44
  return true;
67
45
  }
68
- // 获取方法是否需要认证
69
- const requireAuth = this.reflector.get(_authmoduledefinition.REQUIRE_AUTH_METADATA_KEY, executionContext.getHandler()) ?? this.reflector.get(_authmoduledefinition.REQUIRE_AUTH_METADATA_KEY, executionContext.getClass());
70
- // 如果默认公开或有公共访问权限直接放行
71
- if (!(requireAuth ?? this.options.defaultRequireAuth ?? false)) {
46
+ const requireAuth = (_b = this.reflector.get(auth_module_definition_1.REQUIRE_AUTH_METADATA_KEY, executionContext.getHandler())) !== null && _b !== void 0 ? _b : this.reflector.get(auth_module_definition_1.REQUIRE_AUTH_METADATA_KEY, executionContext.getClass());
47
+ if (!((_c = requireAuth !== null && requireAuth !== void 0 ? requireAuth : this.options.defaultRequireAuth) !== null && _c !== void 0 ? _c : false)) {
72
48
  return true;
73
49
  }
74
- const accessToken = _requestcontext.RequestContext.get(_authconstants.AUTH_ACCESS_TOKEN);
75
- // 如果上下文中没有认证信息拒绝访问
50
+ const accessToken = request_context_1.RequestContext.get(auth_constants_1.AUTH_ACCESS_TOKEN);
76
51
  if (typeof accessToken === "undefined") {
77
- throw new _common.UnauthorizedException(this.t("The access token is invalid."));
52
+ throw new common_1.UnauthorizedException(this.t("The access token is invalid."));
78
53
  }
79
- // 获取方法权限
80
- const permissions = this.reflector.get(_authmoduledefinition.PERMISSIONS_METADATA_KEY, executionContext.getHandler());
81
- // 如果没有权限要求直接放行
82
- // 判断用户权限和配置权限是否有交集,如果有放行
83
- if (typeof permissions === "undefined" || _lodash.default.intersection(await this.getPermissions(), permissions).length > 0) {
54
+ const permissions = this.reflector.get(auth_module_definition_1.PERMISSIONS_METADATA_KEY, executionContext.getHandler());
55
+ if (typeof permissions === "undefined" ||
56
+ lodash_1.default.intersection(await this.getPermissions(), permissions).length > 0) {
84
57
  accessToken.lastUsedAt = new Date();
85
58
  await this.entityManager.flush();
86
59
  return true;
87
60
  }
88
- throw new _common.ForbiddenException(this.t("Permission denied."));
61
+ throw new common_1.ForbiddenException(this.t("Permission denied."));
89
62
  }
90
63
  async getPermissions() {
91
64
  let permissions = [];
92
- const entity = _requestcontext.RequestContext.get(_authconstants.AUTH_ENTITY);
65
+ const entity = request_context_1.RequestContext.get(auth_constants_1.AUTH_ENTITY);
93
66
  if (typeof entity !== "undefined") {
94
67
  if (typeof entity.permissions !== "undefined") {
95
68
  permissions = permissions.concat(entity.permissions);
96
- } else {
97
- const newPermissions = await _core.Reference.create(entity).load("permissions");
69
+ }
70
+ else {
71
+ const newPermissions = await core_1.Reference.create(entity).load("permissions");
98
72
  if (typeof newPermissions !== "undefined") {
99
73
  permissions = permissions.concat(newPermissions);
100
74
  }
@@ -102,23 +76,11 @@ let AuthGuard = class AuthGuard {
102
76
  }
103
77
  return permissions;
104
78
  }
105
- constructor(options, reflector, entityManager){
106
- _define_property(this, "options", void 0);
107
- _define_property(this, "reflector", void 0);
108
- _define_property(this, "entityManager", void 0);
109
- this.options = options;
110
- this.reflector = reflector;
111
- this.entityManager = entityManager;
112
- this.reflector = reflector;
113
- }
114
79
  };
115
- AuthGuard = _ts_decorate([
116
- (0, _common.Injectable)(),
117
- _ts_param(0, (0, _common.Inject)(_authmoduledefinition.MODULE_OPTIONS_TOKEN)),
118
- _ts_metadata("design:type", Function),
119
- _ts_metadata("design:paramtypes", [
120
- typeof _interfaces.AuthModuleOptions === "undefined" ? Object : _interfaces.AuthModuleOptions,
121
- typeof _core1.Reflector === "undefined" ? Object : _core1.Reflector,
122
- typeof _core.EntityManager === "undefined" ? Object : _core.EntityManager
123
- ])
80
+ exports.AuthGuard = AuthGuard = __decorate([
81
+ (0, common_1.Injectable)(),
82
+ __param(0, (0, common_1.Inject)(auth_module_definition_1.MODULE_OPTIONS_TOKEN)),
83
+ __metadata("design:paramtypes", [Object, core_2.Reflector,
84
+ core_1.EntityManager])
124
85
  ], AuthGuard);
86
+ //# sourceMappingURL=auth.guard.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth.guard.js","sourceRoot":"","sources":["../src/auth.guard.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,0CAA2D;AAC3D,0CAAkD;AAClD,gEAAqE;AACrE,2CAOwB;AACxB,uCAAyC;AACzC,oDAAuB;AAEvB,qDAAkE;AAClE,qEAIkC;AAQ3B,IAAM,SAAS,uBAAf,MAAM,SAAS;IACpB,YAEmB,OAA0B,EAC1B,SAAoB,EACpB,aAA4B;QAF5B,YAAO,GAAP,OAAO,CAAmB;QAC1B,cAAS,GAAT,SAAS,CAAW;QACpB,kBAAa,GAAb,aAAa,CAAe;QAE7C,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;IAC7B,CAAC;IAEO,CAAC,CAAC,GAAW;;QACnB,OAAO,MAAA,MAAA,gCAAc,CAAC,GAAG,CAAO,WAAI,CAAC,0CAAE,CAAC,CAAC,GAAG,EAAE,EAAE,EAAE,EAAE,MAAM,EAAE,CAAC,mCAAI,GAAG,CAAC;IACvE,CAAC;IAEM,KAAK,CAAC,WAAW,CACtB,gBAAkC;;QAElC,IAAI,CAAC,CAAC,MAAM,EAAE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AI,CAAC,CAAC,CAAC,oBAAoB,CAAC,CAAC,CAAC;IAC7D,CAAC;IAED,KAAK,CAAC,cAAc;QAClB,IAAI,WAAW,GAAa,EAAE,CAAC;QAE/B,MAAM,MAAM,GAAG,gCAAc,CAAC,GAAG,CAA0B,4BAAW,CAAC,CAAC;QAExE,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE;YACjC,IAAI,OAAO,MAAM,CAAC,WAAW,KAAK,WAAW,EAAE;gBAC7C,WAAW,GAAG,WAAW,CAAC,MAAM,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;aACtD;iBAAM;gBACL,MAAM,cAAc,GAAG,MAAM,gBAAS,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,CACxD,aAAa,CACd,CAAC;gBAEF,IAAI,OAAO,cAAc,KAAK,WAAW,EAAE;oBACzC,WAAW,GAAG,WAAW,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC;iBAClD;aACF;SACF;QAED,OAAO,WAAW,CAAC;IACrB,CAAC;CACF,CAAA;oBA9FY,SAAS;IADrB,IAAA,mBAAU,GAAE;IAGR,WAAA,IAAA,eAAM,EAAC,6CAAoB,CAAC,CAAA;6CAED,gBAAS;QACL,oBAAa;GALpC,SAAS,CA8FrB"}
@@ -0,0 +1,13 @@
1
+ import { EntityManager } from "@mikro-orm/core";
2
+ import { type NestMiddleware } from "@nestjs/common";
3
+ import { type Request, type Response } from "express";
4
+ import { AuthService } from "./auth.service";
5
+ import { AuthModuleOptions } from "./interfaces";
6
+ export declare class AuthMiddleware implements NestMiddleware {
7
+ private readonly options;
8
+ private readonly entityManager;
9
+ private readonly authService;
10
+ constructor(options: AuthModuleOptions, entityManager: EntityManager, authService: AuthService);
11
+ use(req: Request, res: Response, next: () => void): Promise<void>;
12
+ private extractAccessToken;
13
+ }
@@ -1,65 +1,49 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- Object.defineProperty(exports, "AuthMiddleware", {
6
- enumerable: true,
7
- get: function() {
8
- return AuthMiddleware;
9
- }
10
- });
11
- const _core = require("@mikro-orm/core");
12
- const _requestcontext = require("@nest-boot/request-context");
13
- const _common = require("@nestjs/common");
14
- const _authconstants = require("./auth.constants");
15
- const _authmoduledefinition = require("./auth.module-definition");
16
- const _authservice = require("./auth.service");
17
- const _interfaces = require("./interfaces/index");
18
- function _define_property(obj, key, value) {
19
- if (key in obj) {
20
- Object.defineProperty(obj, key, {
21
- value: value,
22
- enumerable: true,
23
- configurable: true,
24
- writable: true
25
- });
26
- } else {
27
- obj[key] = value;
28
- }
29
- return obj;
30
- }
31
- function _ts_decorate(decorators, target, key, desc) {
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
32
3
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
33
4
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
34
- 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;
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
35
6
  return c > 3 && r && Object.defineProperty(target, key, r), r;
36
- }
37
- function _ts_metadata(k, v) {
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
38
9
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
39
- }
40
- function _ts_param(paramIndex, decorator) {
41
- return function(target, key) {
42
- decorator(target, key, paramIndex);
43
- };
44
- }
45
- let AuthMiddleware = class AuthMiddleware {
10
+ };
11
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
12
+ return function (target, key) { decorator(target, key, paramIndex); }
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.AuthMiddleware = void 0;
16
+ const core_1 = require("@mikro-orm/core");
17
+ const request_context_1 = require("@nest-boot/request-context");
18
+ const common_1 = require("@nestjs/common");
19
+ const auth_constants_1 = require("./auth.constants");
20
+ const auth_module_definition_1 = require("./auth.module-definition");
21
+ const auth_service_1 = require("./auth.service");
22
+ let AuthMiddleware = exports.AuthMiddleware = class AuthMiddleware {
23
+ constructor(options, entityManager, authService) {
24
+ this.options = options;
25
+ this.entityManager = entityManager;
26
+ this.authService = authService;
27
+ }
46
28
  async use(req, res, next) {
47
29
  req.authMiddlewareUsed = true;
48
30
  const accessTokenString = this.extractAccessToken(req);
49
31
  if (accessTokenString !== null) {
50
32
  const accessToken = await this.authService.getToken(accessTokenString);
51
33
  if (accessToken !== null) {
52
- _requestcontext.RequestContext.set(_authconstants.AUTH_ACCESS_TOKEN, accessToken);
53
- const entity = await this.entityManager.getRepository(accessToken.entityName).findOne(accessToken.entityId);
54
- // 设置访问令牌和用户到运行上下文
34
+ request_context_1.RequestContext.set(auth_constants_1.AUTH_ACCESS_TOKEN, accessToken);
35
+ const entity = await this.entityManager
36
+ .getRepository(accessToken.entityName)
37
+ .findOne(accessToken.entityId);
55
38
  if (entity !== null) {
56
- _requestcontext.RequestContext.set(_authconstants.AUTH_ENTITY, entity);
39
+ request_context_1.RequestContext.set(auth_constants_1.AUTH_ENTITY, entity);
57
40
  }
58
41
  }
59
42
  }
60
43
  next();
61
44
  }
62
45
  extractAccessToken(req) {
46
+ var _a;
63
47
  const authorizationHeader = req.get("authorization");
64
48
  if (typeof authorizationHeader === "string") {
65
49
  const matched = authorizationHeader.match(/(\S+)\s+(\S+)/);
@@ -67,29 +51,16 @@ let AuthMiddleware = class AuthMiddleware {
67
51
  return matched[2];
68
52
  }
69
53
  }
70
- if (typeof req.cookies?.token === "string") {
54
+ if (typeof ((_a = req.cookies) === null || _a === void 0 ? void 0 : _a.token) === "string") {
71
55
  return req.cookies.token;
72
56
  }
73
57
  return null;
74
58
  }
75
- constructor(options, entityManager, authService){
76
- _define_property(this, "options", void 0);
77
- _define_property(this, "entityManager", void 0);
78
- _define_property(this, "authService", void 0);
79
- this.options = options;
80
- this.entityManager = entityManager;
81
- this.authService = authService;
82
- }
83
59
  };
84
- AuthMiddleware = _ts_decorate([
85
- (0, _common.Injectable)({
86
- scope: _common.Scope.REQUEST
87
- }),
88
- _ts_param(0, (0, _common.Inject)(_authmoduledefinition.MODULE_OPTIONS_TOKEN)),
89
- _ts_metadata("design:type", Function),
90
- _ts_metadata("design:paramtypes", [
91
- typeof _interfaces.AuthModuleOptions === "undefined" ? Object : _interfaces.AuthModuleOptions,
92
- typeof _core.EntityManager === "undefined" ? Object : _core.EntityManager,
93
- typeof _authservice.AuthService === "undefined" ? Object : _authservice.AuthService
94
- ])
60
+ exports.AuthMiddleware = AuthMiddleware = __decorate([
61
+ (0, common_1.Injectable)({ scope: common_1.Scope.REQUEST }),
62
+ __param(0, (0, common_1.Inject)(auth_module_definition_1.MODULE_OPTIONS_TOKEN)),
63
+ __metadata("design:paramtypes", [Object, core_1.EntityManager,
64
+ auth_service_1.AuthService])
95
65
  ], AuthMiddleware);
66
+ //# sourceMappingURL=auth.middleware.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth.middleware.js","sourceRoot":"","sources":["../src/auth.middleware.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,0CAAgD;AAChD,gEAA4D;AAC5D,2CAAgF;AAGhF,qDAAkE;AAClE,qEAAgE;AAChE,iDAA6C;AAItC,IAAM,cAAc,4BAApB,MAAM,cAAc;IACzB,YAEmB,OAA0B,EAC1B,aAA4B,EAC5B,WAAwB;QAFxB,YAAO,GAAP,OAAO,CAAmB;QAC1B,kBAAa,GAAb,aAAa,CAAe;QAC5B,gBAAW,GAAX,WAAW,CAAa;IACxC,CAAC;IAEJ,KAAK,CAAC,GAAG,CAAC,GAAY,EAAE,GAAa,EAAE,IAAgB;QACpD,GAAW,CAAC,kBAAkB,GAAG,IAAI,CAAC;QAEvC,MAAM,iBAAiB,GAAG,IAA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,EAAC,6CAAoB,CAAC,CAAA;6CAEG,oBAAa;QACf,0BAAW;GALhC,cAAc,CAoD1B"}
@@ -0,0 +1,4 @@
1
+ import { type AuthModuleOptions } from "./interfaces";
2
+ export declare const PERMISSIONS_METADATA_KEY: `${string}-${string}-${string}-${string}-${string}`;
3
+ export declare const REQUIRE_AUTH_METADATA_KEY: `${string}-${string}-${string}-${string}-${string}`;
4
+ export declare const ConfigurableModuleClass: import("@nestjs/common").ConfigurableModuleCls<AuthModuleOptions, "register", "create", {}>, MODULE_OPTIONS_TOKEN: string | symbol;
@@ -1,29 +1,10 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- function _export(target, all) {
6
- for(var name in all)Object.defineProperty(target, name, {
7
- enumerable: true,
8
- get: all[name]
9
- });
10
- }
11
- _export(exports, {
12
- PERMISSIONS_METADATA_KEY: function() {
13
- return PERMISSIONS_METADATA_KEY;
14
- },
15
- REQUIRE_AUTH_METADATA_KEY: function() {
16
- return REQUIRE_AUTH_METADATA_KEY;
17
- },
18
- ConfigurableModuleClass: function() {
19
- return ConfigurableModuleClass;
20
- },
21
- MODULE_OPTIONS_TOKEN: function() {
22
- return MODULE_OPTIONS_TOKEN;
23
- }
24
- });
25
- const _common = require("@nestjs/common");
26
- const _crypto = require("crypto");
27
- const PERMISSIONS_METADATA_KEY = (0, _crypto.randomUUID)();
28
- const REQUIRE_AUTH_METADATA_KEY = (0, _crypto.randomUUID)();
29
- const { ConfigurableModuleClass, MODULE_OPTIONS_TOKEN } = new _common.ConfigurableModuleBuilder().build();
2
+ var _a;
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.MODULE_OPTIONS_TOKEN = exports.ConfigurableModuleClass = exports.REQUIRE_AUTH_METADATA_KEY = exports.PERMISSIONS_METADATA_KEY = void 0;
5
+ const common_1 = require("@nestjs/common");
6
+ const crypto_1 = require("crypto");
7
+ exports.PERMISSIONS_METADATA_KEY = (0, crypto_1.randomUUID)();
8
+ exports.REQUIRE_AUTH_METADATA_KEY = (0, crypto_1.randomUUID)();
9
+ _a = new common_1.ConfigurableModuleBuilder().build(), exports.ConfigurableModuleClass = _a.ConfigurableModuleClass, exports.MODULE_OPTIONS_TOKEN = _a.MODULE_OPTIONS_TOKEN;
10
+ //# sourceMappingURL=auth.module-definition.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth.module-definition.js","sourceRoot":"","sources":["../src/auth.module-definition.ts"],"names":[],"mappings":";;;;AAAA,2CAA2D;AAC3D,mCAAoC;AAIvB,QAAA,wBAAwB,GAAG,IAAA,mBAAU,GAAE,CAAC;AAExC,QAAA,yBAAyB,GAAG,IAAA,mBAAU,GAAE,CAAC;AAEzC,KACX,IAAI,kCAAyB,EAAqB,CAAC,KAAK,EAAE,EAD7C,+BAAuB,+BAAE,4BAAoB,2BACC"}
@@ -0,0 +1,8 @@
1
+ import { type MiddlewareConsumer, type NestModule } from "@nestjs/common";
2
+ import { ConfigurableModuleClass } from "./auth.module-definition";
3
+ import { AuthModuleOptions } from "./interfaces";
4
+ export declare class AuthModule extends ConfigurableModuleClass implements NestModule {
5
+ private readonly options;
6
+ constructor(options: AuthModuleOptions);
7
+ configure(consumer: MiddlewareConsumer): void;
8
+ }
@@ -1,83 +1,56 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- Object.defineProperty(exports, "AuthModule", {
6
- enumerable: true,
7
- get: function() {
8
- return AuthModule;
9
- }
10
- });
11
- const _common = require("@nestjs/common");
12
- const _core = require("@nestjs/core");
13
- const _authguard = require("./auth.guard");
14
- const _authmiddleware = require("./auth.middleware");
15
- const _authmoduledefinition = require("./auth.module-definition");
16
- const _authservice = require("./auth.service");
17
- const _interfaces = require("./interfaces/index");
18
- function _define_property(obj, key, value) {
19
- if (key in obj) {
20
- Object.defineProperty(obj, key, {
21
- value: value,
22
- enumerable: true,
23
- configurable: true,
24
- writable: true
25
- });
26
- } else {
27
- obj[key] = value;
28
- }
29
- return obj;
30
- }
31
- function _ts_decorate(decorators, target, key, desc) {
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
32
3
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
33
4
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
34
- 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;
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
35
6
  return c > 3 && r && Object.defineProperty(target, key, r), r;
36
- }
37
- function _ts_metadata(k, v) {
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
38
9
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
39
- }
40
- function _ts_param(paramIndex, decorator) {
41
- return function(target, key) {
42
- decorator(target, key, paramIndex);
43
- };
44
- }
45
- let AuthModule = class AuthModule extends _authmoduledefinition.ConfigurableModuleClass {
10
+ };
11
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
12
+ return function (target, key) { decorator(target, key, paramIndex); }
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.AuthModule = void 0;
16
+ const common_1 = require("@nestjs/common");
17
+ const core_1 = require("@nestjs/core");
18
+ const auth_guard_1 = require("./auth.guard");
19
+ const auth_middleware_1 = require("./auth.middleware");
20
+ const auth_module_definition_1 = require("./auth.module-definition");
21
+ const auth_service_1 = require("./auth.service");
22
+ let AuthModule = exports.AuthModule = class AuthModule extends auth_module_definition_1.ConfigurableModuleClass {
23
+ constructor(options) {
24
+ super();
25
+ this.options = options;
26
+ }
46
27
  configure(consumer) {
47
- const middlewareConfigProxy = consumer.apply(_authmiddleware.AuthMiddleware);
28
+ const middlewareConfigProxy = consumer.apply(auth_middleware_1.AuthMiddleware);
48
29
  if (typeof this.options.excludeRoutes !== "undefined") {
49
30
  middlewareConfigProxy.exclude(...this.options.excludeRoutes);
50
31
  }
51
32
  if (typeof this.options.includeRoutes !== "undefined") {
52
33
  middlewareConfigProxy.forRoutes(...this.options.includeRoutes);
53
- } else {
34
+ }
35
+ else {
54
36
  middlewareConfigProxy.forRoutes("*");
55
37
  }
56
38
  }
57
- constructor(options){
58
- super();
59
- _define_property(this, "options", void 0);
60
- this.options = options;
61
- }
62
39
  };
63
- AuthModule = _ts_decorate([
64
- (0, _common.Global)(),
65
- (0, _common.Module)({
40
+ exports.AuthModule = AuthModule = __decorate([
41
+ (0, common_1.Global)(),
42
+ (0, common_1.Module)({
66
43
  providers: [
67
44
  {
68
- provide: _core.APP_GUARD,
69
- useClass: _authguard.AuthGuard
45
+ provide: core_1.APP_GUARD,
46
+ useClass: auth_guard_1.AuthGuard,
70
47
  },
71
- _authservice.AuthService,
72
- _core.Reflector
48
+ auth_service_1.AuthService,
49
+ core_1.Reflector,
73
50
  ],
74
- exports: [
75
- _authservice.AuthService
76
- ]
51
+ exports: [auth_service_1.AuthService],
77
52
  }),
78
- _ts_param(0, (0, _common.Inject)(_authmoduledefinition.MODULE_OPTIONS_TOKEN)),
79
- _ts_metadata("design:type", Function),
80
- _ts_metadata("design:paramtypes", [
81
- typeof _interfaces.AuthModuleOptions === "undefined" ? Object : _interfaces.AuthModuleOptions
82
- ])
53
+ __param(0, (0, common_1.Inject)(auth_module_definition_1.MODULE_OPTIONS_TOKEN)),
54
+ __metadata("design:paramtypes", [Object])
83
55
  ], AuthModule);
56
+ //# sourceMappingURL=auth.module.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth.module.js","sourceRoot":"","sources":["../src/auth.module.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAMwB;AACxB,uCAAoD;AAEpD,6CAAyC;AACzC,uDAAmD;AACnD,qEAGkC;AAClC,iDAA6C;AAetC,IAAM,UAAU,wBAAhB,MAAM,UAAW,SAAQ,gDAAuB;IACrD,YAEmB,OAA0B;QAE3C,KAAK,EAAE,CAAC;QAFS,YAAO,GAAP,OAAO,CAAmB;IAG7C,CAAC;IAED,SAAS,CAAC,QAA4B;QACpC,MAAM,qBAAqB,GAAG,QAAQ,CAAC,KAAK,CAAC,gCAAc,CAAC,CAAC;QAE7D,IAAI,OAAO,IAAI,CAAC,OAAO,CAAC,aAAa,KAAK,WAAW,EAAE;YACrD,qBAAqB,CAAC,OAAO,CAAC,GAAG,IAAI,CAAC,OAAO,CAAC,aAAa,CAAC,CAAC;SAC9D;QAED,IAAI,OAAO,IAAI,CAAC,OAAO,CAAC,aAAa,KAAK,WAAW,EAAE;YACrD,qBAAqB,CAAC,SAAS,CAAC,GAAG,IAAI,CAAC,OAAO,CAAC,aAAa,CAAC,CAAC;SAChE;aAAM;YACL,qBAAqB,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;SACtC;IACH,CAAC;CACF,CAAA;qBArBY,UAAU;IAZtB,IAAA,eAAM,GAAE;IACR,IAAA,eAAM,EAAC;QACN,SAAS,EAAE;YACT;gBACE,OAAO,EAAE,gBAAS;gBAClB,QAAQ,EAAE,sBAAS;aACpB;YACD,0BAAW;YACX,gBAAS;SACV;QACD,OAAO,EAAE,CAAC,0BAAW,CAAC;KACvB,CAAC;IAGG,WAAA,IAAA,eAAM,EAAC,6CAAoB,CAAC,CAAA;;GAFpB,UAAU,CAqBtB"}
@@ -0,0 +1,12 @@
1
+ import { type AnyEntity, EntityManager, type Loaded } from "@mikro-orm/core";
2
+ import { type AccessTokenInterface, AuthModuleOptions } from "./interfaces";
3
+ export declare class AuthService {
4
+ private readonly options;
5
+ private readonly entityManager;
6
+ private readonly accessTokenRepository;
7
+ constructor(options: AuthModuleOptions, entityManager: EntityManager);
8
+ createToken(entity: AnyEntity, name: string, expiresIn?: string | number): Promise<AccessTokenInterface>;
9
+ getToken(token?: string): Promise<Loaded<AccessTokenInterface> | null>;
10
+ verifyToken(token?: string): Promise<boolean>;
11
+ revokeToken(token?: string): Promise<void>;
12
+ }