@nest-boot/auth 4.4.0 → 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.
- package/dist/auth.constants.js +19 -5
- package/dist/auth.guard.js +89 -51
- package/dist/auth.middleware.js +64 -35
- package/dist/auth.module-definition.js +28 -9
- package/dist/auth.module.js +62 -34
- package/dist/auth.service.js +82 -38
- package/dist/decorators/can.decorator.js +12 -7
- package/dist/decorators/current-entity.decorator.js +23 -9
- package/dist/decorators/current-personal-access-token.decorator.js +14 -8
- package/dist/decorators/index.js +20 -20
- package/dist/decorators/require-auth.decorator.js +12 -7
- package/dist/index.js +24 -24
- package/dist/interfaces/access-token.interface.js +3 -2
- package/dist/interfaces/auth-module-options.interface.js +3 -2
- package/dist/interfaces/has-permission.interface.js +3 -2
- package/dist/interfaces/index.js +19 -19
- package/dist/utils/index.js +17 -17
- package/dist/utils/mixin-permissions.utils.js +30 -9
- package/package.json +19 -18
- package/dist/auth.constants.d.ts +0 -2
- package/dist/auth.constants.js.map +0 -1
- package/dist/auth.guard.d.ts +0 -13
- package/dist/auth.guard.js.map +0 -1
- package/dist/auth.middleware.d.ts +0 -13
- package/dist/auth.middleware.js.map +0 -1
- package/dist/auth.module-definition.d.ts +0 -4
- package/dist/auth.module-definition.js.map +0 -1
- package/dist/auth.module.d.ts +0 -8
- package/dist/auth.module.js.map +0 -1
- package/dist/auth.service.d.ts +0 -12
- package/dist/auth.service.js.map +0 -1
- package/dist/decorators/can.decorator.d.ts +0 -1
- package/dist/decorators/can.decorator.js.map +0 -1
- package/dist/decorators/current-entity.decorator.d.ts +0 -2
- package/dist/decorators/current-entity.decorator.js.map +0 -1
- package/dist/decorators/current-personal-access-token.decorator.d.ts +0 -1
- package/dist/decorators/current-personal-access-token.decorator.js.map +0 -1
- package/dist/decorators/index.d.ts +0 -4
- package/dist/decorators/index.js.map +0 -1
- package/dist/decorators/require-auth.decorator.d.ts +0 -1
- package/dist/decorators/require-auth.decorator.js.map +0 -1
- package/dist/index.d.ts +0 -8
- package/dist/index.js.map +0 -1
- package/dist/interfaces/access-token.interface.d.ts +0 -9
- package/dist/interfaces/access-token.interface.js.map +0 -1
- package/dist/interfaces/auth-module-options.interface.d.ts +0 -10
- package/dist/interfaces/auth-module-options.interface.js.map +0 -1
- package/dist/interfaces/has-permission.interface.d.ts +0 -3
- package/dist/interfaces/has-permission.interface.js.map +0 -1
- package/dist/interfaces/index.d.ts +0 -3
- package/dist/interfaces/index.js.map +0 -1
- package/dist/tsconfig.tsbuildinfo +0 -1
- package/dist/utils/index.d.ts +0 -1
- package/dist/utils/index.js.map +0 -1
- package/dist/utils/mixin-permissions.utils.d.ts +0 -3
- package/dist/utils/mixin-permissions.utils.js.map +0 -1
package/dist/auth.constants.js
CHANGED
|
@@ -1,6 +1,20 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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";
|
package/dist/auth.guard.js
CHANGED
|
@@ -1,74 +1,100 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
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
|
|
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
|
-
|
|
43
|
+
}
|
|
44
|
+
function _ts_metadata(k, v) {
|
|
9
45
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
return function
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|
-
|
|
36
|
-
|
|
54
|
+
return _requestcontext.RequestContext.get(_i18n.I18N)?.t(key, {
|
|
55
|
+
ns: "auth"
|
|
56
|
+
}) ?? key;
|
|
37
57
|
}
|
|
38
58
|
async canActivate(executionContext) {
|
|
39
|
-
|
|
40
|
-
|
|
59
|
+
if (![
|
|
60
|
+
"http",
|
|
61
|
+
"graphql"
|
|
62
|
+
].includes(executionContext.getType())) {
|
|
41
63
|
return true;
|
|
42
64
|
}
|
|
43
|
-
if (typeof
|
|
65
|
+
if (typeof _requestcontext.RequestContext.get(_requestcontext.REQUEST)?.authMiddlewareUsed === "undefined") {
|
|
44
66
|
return true;
|
|
45
67
|
}
|
|
46
|
-
|
|
47
|
-
|
|
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 =
|
|
74
|
+
const accessToken = _requestcontext.RequestContext.get(_authconstants.AUTH_ACCESS_TOKEN);
|
|
75
|
+
// 如果上下文中没有认证信息拒绝访问
|
|
51
76
|
if (typeof accessToken === "undefined") {
|
|
52
|
-
throw new
|
|
77
|
+
throw new _common.UnauthorizedException(this.t("The access token is invalid."));
|
|
53
78
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
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
|
|
88
|
+
throw new _common.ForbiddenException(this.t("Permission denied."));
|
|
62
89
|
}
|
|
63
90
|
async getPermissions() {
|
|
64
91
|
let permissions = [];
|
|
65
|
-
const 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
|
-
|
|
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
|
-
|
|
81
|
-
(0,
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
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
|
package/dist/auth.middleware.js
CHANGED
|
@@ -1,49 +1,65 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
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
|
|
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
|
-
|
|
36
|
+
}
|
|
37
|
+
function _ts_metadata(k, v) {
|
|
9
38
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
return function
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
-
|
|
35
|
-
const entity = await this.entityManager
|
|
36
|
-
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
61
|
-
(0,
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
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
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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();
|
package/dist/auth.module.js
CHANGED
|
@@ -1,55 +1,83 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
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
|
|
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
|
-
|
|
36
|
+
}
|
|
37
|
+
function _ts_metadata(k, v) {
|
|
9
38
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
return function
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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(
|
|
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
|
-
|
|
41
|
-
(0,
|
|
42
|
-
(0,
|
|
63
|
+
AuthModule = _ts_decorate([
|
|
64
|
+
(0, _common.Global)(),
|
|
65
|
+
(0, _common.Module)({
|
|
43
66
|
providers: [
|
|
44
67
|
{
|
|
45
|
-
provide:
|
|
46
|
-
useClass:
|
|
68
|
+
provide: _core.APP_GUARD,
|
|
69
|
+
useClass: _authguard.AuthGuard
|
|
47
70
|
},
|
|
48
|
-
|
|
71
|
+
_authservice.AuthService,
|
|
72
|
+
_core.Reflector
|
|
49
73
|
],
|
|
50
|
-
exports: [
|
|
74
|
+
exports: [
|
|
75
|
+
_authservice.AuthService
|
|
76
|
+
]
|
|
51
77
|
}),
|
|
52
|
-
|
|
53
|
-
|
|
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
|
package/dist/auth.service.js
CHANGED
|
@@ -1,65 +1,109 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
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
|
|
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
|
-
|
|
36
|
+
}
|
|
37
|
+
function _ts_metadata(k, v) {
|
|
9
38
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
return function
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
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
|
-
|
|
44
|
-
|
|
66
|
+
/**
|
|
67
|
+
* 获取个人访问令牌
|
|
68
|
+
* @param token 令牌,如果不传则从上下文中获取
|
|
69
|
+
*/ async getToken(token) {
|
|
45
70
|
if (typeof token === "undefined") {
|
|
46
|
-
return
|
|
71
|
+
return _requestcontext.RequestContext.get(_authconstants.AUTH_ACCESS_TOKEN) ?? null;
|
|
47
72
|
}
|
|
48
|
-
return await this.accessTokenRepository.findOne({
|
|
73
|
+
return await this.accessTokenRepository.findOne({
|
|
74
|
+
token
|
|
75
|
+
});
|
|
49
76
|
}
|
|
50
|
-
|
|
51
|
-
|
|
77
|
+
/**
|
|
78
|
+
* 验证访问令牌
|
|
79
|
+
* @param token 令牌,如果不传则从上下文中获取
|
|
80
|
+
*/ async verifyToken(token) {
|
|
81
|
+
return await this.getToken(token) !== null;
|
|
52
82
|
}
|
|
53
|
-
|
|
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
|
-
|
|
61
|
-
(0,
|
|
62
|
-
|
|
63
|
-
|
|
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
|