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

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.js +19 -5
  2. package/dist/auth.guard.js +89 -51
  3. package/dist/auth.middleware.js +64 -35
  4. package/dist/auth.module-definition.js +28 -9
  5. package/dist/auth.module.js +62 -34
  6. package/dist/auth.service.js +82 -38
  7. package/dist/decorators/can.decorator.js +12 -7
  8. package/dist/decorators/current-entity.decorator.js +23 -9
  9. package/dist/decorators/current-personal-access-token.decorator.js +14 -8
  10. package/dist/decorators/index.js +20 -20
  11. package/dist/decorators/require-auth.decorator.js +12 -7
  12. package/dist/index.js +24 -24
  13. package/dist/interfaces/access-token.interface.js +3 -2
  14. package/dist/interfaces/auth-module-options.interface.js +3 -2
  15. package/dist/interfaces/has-permission.interface.js +3 -2
  16. package/dist/interfaces/index.js +19 -19
  17. package/dist/utils/index.js +17 -17
  18. package/dist/utils/mixin-permissions.utils.js +30 -9
  19. package/package.json +19 -18
  20. package/dist/auth.constants.d.ts +0 -2
  21. package/dist/auth.constants.js.map +0 -1
  22. package/dist/auth.guard.d.ts +0 -13
  23. package/dist/auth.guard.js.map +0 -1
  24. package/dist/auth.middleware.d.ts +0 -13
  25. package/dist/auth.middleware.js.map +0 -1
  26. package/dist/auth.module-definition.d.ts +0 -4
  27. package/dist/auth.module-definition.js.map +0 -1
  28. package/dist/auth.module.d.ts +0 -8
  29. package/dist/auth.module.js.map +0 -1
  30. package/dist/auth.service.d.ts +0 -12
  31. package/dist/auth.service.js.map +0 -1
  32. package/dist/decorators/can.decorator.d.ts +0 -1
  33. package/dist/decorators/can.decorator.js.map +0 -1
  34. package/dist/decorators/current-entity.decorator.d.ts +0 -2
  35. package/dist/decorators/current-entity.decorator.js.map +0 -1
  36. package/dist/decorators/current-personal-access-token.decorator.d.ts +0 -1
  37. package/dist/decorators/current-personal-access-token.decorator.js.map +0 -1
  38. package/dist/decorators/index.d.ts +0 -4
  39. package/dist/decorators/index.js.map +0 -1
  40. package/dist/decorators/require-auth.decorator.d.ts +0 -1
  41. package/dist/decorators/require-auth.decorator.js.map +0 -1
  42. package/dist/index.d.ts +0 -8
  43. package/dist/index.js.map +0 -1
  44. package/dist/interfaces/access-token.interface.d.ts +0 -9
  45. package/dist/interfaces/access-token.interface.js.map +0 -1
  46. package/dist/interfaces/auth-module-options.interface.d.ts +0 -10
  47. package/dist/interfaces/auth-module-options.interface.js.map +0 -1
  48. package/dist/interfaces/has-permission.interface.d.ts +0 -3
  49. package/dist/interfaces/has-permission.interface.js.map +0 -1
  50. package/dist/interfaces/index.d.ts +0 -3
  51. package/dist/interfaces/index.js.map +0 -1
  52. package/dist/tsconfig.tsbuildinfo +0 -1
  53. package/dist/utils/index.d.ts +0 -1
  54. package/dist/utils/index.js.map +0 -1
  55. package/dist/utils/mixin-permissions.utils.d.ts +0 -3
  56. package/dist/utils/mixin-permissions.utils.js.map +0 -1
@@ -1,6 +1,20 @@
1
1
  "use strict";
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
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";
@@ -1,74 +1,100 @@
1
1
  "use strict";
2
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
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) {
3
39
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
40
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
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;
6
42
  return c > 3 && r && Object.defineProperty(target, key, r), r;
7
- };
8
- var __metadata = (this && this.__metadata) || function (k, v) {
43
+ }
44
+ function _ts_metadata(k, v) {
9
45
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
- };
11
- var __param = (this && this.__param) || function (paramIndex, decorator) {
12
- return function (target, key) { decorator(target, key, paramIndex); }
13
- };
14
- var __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
- }
46
+ }
47
+ function _ts_param(paramIndex, decorator) {
48
+ return function(target, key) {
49
+ decorator(target, key, paramIndex);
50
+ };
51
+ }
52
+ let AuthGuard = class AuthGuard {
34
53
  t(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;
54
+ return _requestcontext.RequestContext.get(_i18n.I18N)?.t(key, {
55
+ ns: "auth"
56
+ }) ?? key;
37
57
  }
38
58
  async canActivate(executionContext) {
39
- var _a, _b, _c;
40
- if (!["http", "graphql"].includes(executionContext.getType())) {
59
+ if (![
60
+ "http",
61
+ "graphql"
62
+ ].includes(executionContext.getType())) {
41
63
  return true;
42
64
  }
43
- if (typeof ((_a = request_context_1.RequestContext.get(request_context_1.REQUEST)) === null || _a === void 0 ? void 0 : _a.authMiddlewareUsed) === "undefined") {
65
+ if (typeof _requestcontext.RequestContext.get(_requestcontext.REQUEST)?.authMiddlewareUsed === "undefined") {
44
66
  return true;
45
67
  }
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)) {
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)) {
48
72
  return true;
49
73
  }
50
- const accessToken = request_context_1.RequestContext.get(auth_constants_1.AUTH_ACCESS_TOKEN);
74
+ const accessToken = _requestcontext.RequestContext.get(_authconstants.AUTH_ACCESS_TOKEN);
75
+ // 如果上下文中没有认证信息拒绝访问
51
76
  if (typeof accessToken === "undefined") {
52
- throw new common_1.UnauthorizedException(this.t("The access token is invalid."));
77
+ throw new _common.UnauthorizedException(this.t("The access token is invalid."));
53
78
  }
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) {
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) {
57
84
  accessToken.lastUsedAt = new Date();
58
85
  await this.entityManager.flush();
59
86
  return true;
60
87
  }
61
- throw new common_1.ForbiddenException(this.t("Permission denied."));
88
+ throw new _common.ForbiddenException(this.t("Permission denied."));
62
89
  }
63
90
  async getPermissions() {
64
91
  let permissions = [];
65
- const entity = request_context_1.RequestContext.get(auth_constants_1.AUTH_ENTITY);
92
+ const entity = _requestcontext.RequestContext.get(_authconstants.AUTH_ENTITY);
66
93
  if (typeof entity !== "undefined") {
67
94
  if (typeof entity.permissions !== "undefined") {
68
95
  permissions = permissions.concat(entity.permissions);
69
- }
70
- else {
71
- const newPermissions = await core_1.Reference.create(entity).load("permissions");
96
+ } else {
97
+ const newPermissions = await _core.Reference.create(entity).load("permissions");
72
98
  if (typeof newPermissions !== "undefined") {
73
99
  permissions = permissions.concat(newPermissions);
74
100
  }
@@ -76,11 +102,23 @@ let AuthGuard = exports.AuthGuard = class AuthGuard {
76
102
  }
77
103
  return permissions;
78
104
  }
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
+ }
79
114
  };
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])
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
+ ])
85
124
  ], AuthGuard);
86
- //# sourceMappingURL=auth.guard.js.map
@@ -1,49 +1,65 @@
1
1
  "use strict";
2
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
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) {
3
32
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
33
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
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;
6
35
  return c > 3 && r && Object.defineProperty(target, key, r), r;
7
- };
8
- var __metadata = (this && this.__metadata) || function (k, v) {
36
+ }
37
+ function _ts_metadata(k, v) {
9
38
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
- };
11
- var __param = (this && this.__param) || function (paramIndex, decorator) {
12
- return function (target, key) { decorator(target, key, paramIndex); }
13
- };
14
- 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
- }
39
+ }
40
+ function _ts_param(paramIndex, decorator) {
41
+ return function(target, key) {
42
+ decorator(target, key, paramIndex);
43
+ };
44
+ }
45
+ let AuthMiddleware = class AuthMiddleware {
28
46
  async use(req, res, next) {
29
47
  req.authMiddlewareUsed = true;
30
48
  const accessTokenString = this.extractAccessToken(req);
31
49
  if (accessTokenString !== null) {
32
50
  const accessToken = await this.authService.getToken(accessTokenString);
33
51
  if (accessToken !== null) {
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);
52
+ _requestcontext.RequestContext.set(_authconstants.AUTH_ACCESS_TOKEN, accessToken);
53
+ const entity = await this.entityManager.getRepository(accessToken.entityName).findOne(accessToken.entityId);
54
+ // 设置访问令牌和用户到运行上下文
38
55
  if (entity !== null) {
39
- request_context_1.RequestContext.set(auth_constants_1.AUTH_ENTITY, entity);
56
+ _requestcontext.RequestContext.set(_authconstants.AUTH_ENTITY, entity);
40
57
  }
41
58
  }
42
59
  }
43
60
  next();
44
61
  }
45
62
  extractAccessToken(req) {
46
- var _a;
47
63
  const authorizationHeader = req.get("authorization");
48
64
  if (typeof authorizationHeader === "string") {
49
65
  const matched = authorizationHeader.match(/(\S+)\s+(\S+)/);
@@ -51,16 +67,29 @@ let AuthMiddleware = exports.AuthMiddleware = class AuthMiddleware {
51
67
  return matched[2];
52
68
  }
53
69
  }
54
- if (typeof ((_a = req.cookies) === null || _a === void 0 ? void 0 : _a.token) === "string") {
70
+ if (typeof req.cookies?.token === "string") {
55
71
  return req.cookies.token;
56
72
  }
57
73
  return null;
58
74
  }
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
+ }
59
83
  };
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])
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
+ ])
65
95
  ], AuthMiddleware);
66
- //# sourceMappingURL=auth.middleware.js.map
@@ -1,10 +1,29 @@
1
1
  "use strict";
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
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();
@@ -1,55 +1,83 @@
1
1
  "use strict";
2
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
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) {
3
32
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
33
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
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;
6
35
  return c > 3 && r && Object.defineProperty(target, key, r), r;
7
- };
8
- var __metadata = (this && this.__metadata) || function (k, v) {
36
+ }
37
+ function _ts_metadata(k, v) {
9
38
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
- };
11
- var __param = (this && this.__param) || function (paramIndex, decorator) {
12
- return function (target, key) { decorator(target, key, paramIndex); }
13
- };
14
- 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
- }
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 {
27
46
  configure(consumer) {
28
- const middlewareConfigProxy = consumer.apply(auth_middleware_1.AuthMiddleware);
47
+ const middlewareConfigProxy = consumer.apply(_authmiddleware.AuthMiddleware);
29
48
  if (typeof this.options.excludeRoutes !== "undefined") {
30
49
  middlewareConfigProxy.exclude(...this.options.excludeRoutes);
31
50
  }
32
51
  if (typeof this.options.includeRoutes !== "undefined") {
33
52
  middlewareConfigProxy.forRoutes(...this.options.includeRoutes);
34
- }
35
- else {
53
+ } else {
36
54
  middlewareConfigProxy.forRoutes("*");
37
55
  }
38
56
  }
57
+ constructor(options){
58
+ super();
59
+ _define_property(this, "options", void 0);
60
+ this.options = options;
61
+ }
39
62
  };
40
- exports.AuthModule = AuthModule = __decorate([
41
- (0, common_1.Global)(),
42
- (0, common_1.Module)({
63
+ AuthModule = _ts_decorate([
64
+ (0, _common.Global)(),
65
+ (0, _common.Module)({
43
66
  providers: [
44
67
  {
45
- provide: core_1.APP_GUARD,
46
- useClass: auth_guard_1.AuthGuard,
68
+ provide: _core.APP_GUARD,
69
+ useClass: _authguard.AuthGuard
47
70
  },
48
- auth_service_1.AuthService,
71
+ _authservice.AuthService,
72
+ _core.Reflector
49
73
  ],
50
- exports: [auth_service_1.AuthService],
74
+ exports: [
75
+ _authservice.AuthService
76
+ ]
51
77
  }),
52
- __param(0, (0, common_1.Inject)(auth_module_definition_1.MODULE_OPTIONS_TOKEN)),
53
- __metadata("design:paramtypes", [Object])
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
+ ])
54
83
  ], AuthModule);
55
- //# sourceMappingURL=auth.module.js.map
@@ -1,65 +1,109 @@
1
1
  "use strict";
2
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "AuthService", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return AuthService;
9
+ }
10
+ });
11
+ const _core = require("@mikro-orm/core");
12
+ const _common = require("@nestjs/common");
13
+ const _crypto = require("crypto");
14
+ const _authmoduledefinition = require("./auth.module-definition");
15
+ const _interfaces = require("./interfaces/index");
16
+ const _requestcontext = require("@nest-boot/request-context");
17
+ const _authconstants = require("./auth.constants");
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) {
3
32
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
33
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
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;
6
35
  return c > 3 && r && Object.defineProperty(target, key, r), r;
7
- };
8
- var __metadata = (this && this.__metadata) || function (k, v) {
36
+ }
37
+ function _ts_metadata(k, v) {
9
38
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
- };
11
- var __param = (this && this.__param) || function (paramIndex, decorator) {
12
- return function (target, key) { decorator(target, key, paramIndex); }
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.AuthService = void 0;
16
- const core_1 = require("@mikro-orm/core");
17
- const common_1 = require("@nestjs/common");
18
- const crypto_1 = require("crypto");
19
- const auth_module_definition_1 = require("./auth.module-definition");
39
+ }
40
+ function _ts_param(paramIndex, decorator) {
41
+ return function(target, key) {
42
+ decorator(target, key, paramIndex);
43
+ };
44
+ }
20
45
  const ms = require("ms");
21
- const request_context_1 = require("@nest-boot/request-context");
22
- const auth_constants_1 = require("./auth.constants");
23
- let AuthService = exports.AuthService = class AuthService {
24
- constructor(options, entityManager) {
25
- this.options = options;
26
- this.entityManager = entityManager;
27
- this.accessTokenRepository = this.entityManager.getRepository(this.options.accessTokenEntityClass);
28
- }
29
- async createToken(entity, name, expiresIn) {
30
- const token = (0, crypto_1.randomBytes)(32).toString("hex");
31
- const _expiresIn = expiresIn !== null && expiresIn !== void 0 ? expiresIn : this.options.expiresIn;
46
+ let AuthService = class AuthService {
47
+ /**
48
+ * 生成访问令牌
49
+ * @param entity
50
+ * @param name
51
+ * @param expiresIn
52
+ */ async createToken(entity, name, expiresIn) {
53
+ const token = (0, _crypto.randomBytes)(32).toString("hex");
54
+ const _expiresIn = expiresIn ?? this.options.expiresIn;
32
55
  const _expiresInMs = typeof _expiresIn === "string" ? ms(_expiresIn) : _expiresIn;
33
56
  const accessToken = this.accessTokenRepository.create({
34
57
  name,
35
58
  token,
36
59
  entityId: entity.id,
37
60
  entityName: entity.constructor.name,
38
- expiresAt: _expiresInMs != null ? new Date(Date.now() + _expiresInMs) : undefined,
61
+ expiresAt: _expiresInMs != null ? new Date(Date.now() + _expiresInMs) : undefined
39
62
  });
40
63
  await this.accessTokenRepository.persistAndFlush(accessToken);
41
64
  return accessToken;
42
65
  }
43
- async getToken(token) {
44
- var _a;
66
+ /**
67
+ * 获取个人访问令牌
68
+ * @param token 令牌,如果不传则从上下文中获取
69
+ */ async getToken(token) {
45
70
  if (typeof token === "undefined") {
46
- return ((_a = request_context_1.RequestContext.get(auth_constants_1.AUTH_ACCESS_TOKEN)) !== null && _a !== void 0 ? _a : null);
71
+ return _requestcontext.RequestContext.get(_authconstants.AUTH_ACCESS_TOKEN) ?? null;
47
72
  }
48
- return await this.accessTokenRepository.findOne({ token });
73
+ return await this.accessTokenRepository.findOne({
74
+ token
75
+ });
49
76
  }
50
- async verifyToken(token) {
51
- return (await this.getToken(token)) !== null;
77
+ /**
78
+ * 验证访问令牌
79
+ * @param token 令牌,如果不传则从上下文中获取
80
+ */ async verifyToken(token) {
81
+ return await this.getToken(token) !== null;
52
82
  }
53
- async revokeToken(token) {
83
+ /**
84
+ * 撤销访问令牌
85
+ * @param token 令牌,如果不传则从上下文中获取
86
+ */ async revokeToken(token) {
54
87
  const accessToken = await this.getToken(token);
55
88
  if (accessToken !== null) {
56
89
  await this.accessTokenRepository.remove(accessToken).flush();
57
90
  }
58
91
  }
92
+ constructor(options, entityManager){
93
+ _define_property(this, "options", void 0);
94
+ _define_property(this, "entityManager", void 0);
95
+ _define_property(this, "accessTokenRepository", void 0);
96
+ this.options = options;
97
+ this.entityManager = entityManager;
98
+ this.accessTokenRepository = this.entityManager.getRepository(this.options.accessTokenEntityClass);
99
+ }
59
100
  };
60
- exports.AuthService = AuthService = __decorate([
61
- (0, common_1.Injectable)(),
62
- __param(0, (0, common_1.Inject)(auth_module_definition_1.MODULE_OPTIONS_TOKEN)),
63
- __metadata("design:paramtypes", [Object, core_1.EntityManager])
101
+ AuthService = _ts_decorate([
102
+ (0, _common.Injectable)(),
103
+ _ts_param(0, (0, _common.Inject)(_authmoduledefinition.MODULE_OPTIONS_TOKEN)),
104
+ _ts_metadata("design:type", Function),
105
+ _ts_metadata("design:paramtypes", [
106
+ typeof _interfaces.AuthModuleOptions === "undefined" ? Object : _interfaces.AuthModuleOptions,
107
+ typeof _core.EntityManager === "undefined" ? Object : _core.EntityManager
108
+ ])
64
109
  ], AuthService);
65
- //# sourceMappingURL=auth.service.js.map