@nocobase/auth 0.13.0-alpha.1 → 0.13.0-alpha.10
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/lib/actions.js +41 -48
- package/lib/auth-manager.js +73 -73
- package/lib/auth.js +37 -22
- package/lib/base/auth.js +76 -73
- package/lib/base/jwt-service.js +63 -53
- package/lib/base/token-blacklist-service.js +15 -5
- package/lib/index.js +28 -59
- package/package.json +6 -6
package/lib/actions.js
CHANGED
|
@@ -1,51 +1,44 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
Object.
|
|
4
|
-
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var actions_exports = {};
|
|
19
|
+
__export(actions_exports, {
|
|
20
|
+
actions: () => actions
|
|
5
21
|
});
|
|
6
|
-
exports
|
|
7
|
-
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
8
|
-
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
22
|
+
module.exports = __toCommonJS(actions_exports);
|
|
9
23
|
const actions = {
|
|
10
|
-
signIn:
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
return _signOut.apply(this, arguments);
|
|
27
|
-
}
|
|
28
|
-
return signOut;
|
|
29
|
-
}(),
|
|
30
|
-
signUp: function () {
|
|
31
|
-
var _signUp = _asyncToGenerator(function* (ctx, next) {
|
|
32
|
-
yield ctx.auth.signUp();
|
|
33
|
-
yield next();
|
|
34
|
-
});
|
|
35
|
-
function signUp(_x5, _x6) {
|
|
36
|
-
return _signUp.apply(this, arguments);
|
|
37
|
-
}
|
|
38
|
-
return signUp;
|
|
39
|
-
}(),
|
|
40
|
-
check: function () {
|
|
41
|
-
var _check = _asyncToGenerator(function* (ctx, next) {
|
|
42
|
-
ctx.body = ctx.auth.user || {};
|
|
43
|
-
yield next();
|
|
44
|
-
});
|
|
45
|
-
function check(_x7, _x8) {
|
|
46
|
-
return _check.apply(this, arguments);
|
|
47
|
-
}
|
|
48
|
-
return check;
|
|
49
|
-
}()
|
|
24
|
+
signIn: async (ctx, next) => {
|
|
25
|
+
ctx.body = await ctx.auth.signIn();
|
|
26
|
+
await next();
|
|
27
|
+
},
|
|
28
|
+
signOut: async (ctx, next) => {
|
|
29
|
+
await ctx.auth.signOut();
|
|
30
|
+
await next();
|
|
31
|
+
},
|
|
32
|
+
signUp: async (ctx, next) => {
|
|
33
|
+
await ctx.auth.signUp();
|
|
34
|
+
await next();
|
|
35
|
+
},
|
|
36
|
+
check: async (ctx, next) => {
|
|
37
|
+
ctx.body = ctx.auth.user || {};
|
|
38
|
+
await next();
|
|
39
|
+
}
|
|
50
40
|
};
|
|
51
|
-
|
|
41
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
42
|
+
0 && (module.exports = {
|
|
43
|
+
actions
|
|
44
|
+
});
|
package/lib/auth-manager.js
CHANGED
|
@@ -1,28 +1,37 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
Object.
|
|
4
|
-
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var auth_manager_exports = {};
|
|
20
|
+
__export(auth_manager_exports, {
|
|
21
|
+
AuthManager: () => AuthManager
|
|
5
22
|
});
|
|
6
|
-
exports
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
16
|
-
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
17
|
-
class AuthManager {
|
|
23
|
+
module.exports = __toCommonJS(auth_manager_exports);
|
|
24
|
+
var import_utils = require("@nocobase/utils");
|
|
25
|
+
var import_jwt_service = require("./base/jwt-service");
|
|
26
|
+
const _AuthManager = class _AuthManager {
|
|
27
|
+
options;
|
|
28
|
+
authTypes = new import_utils.Registry();
|
|
29
|
+
// authenticators collection manager.
|
|
30
|
+
storer;
|
|
31
|
+
jwt;
|
|
18
32
|
constructor(options) {
|
|
19
|
-
this.options = void 0;
|
|
20
|
-
this.authTypes = new (_utils().Registry)();
|
|
21
|
-
// authenticators collection manager.
|
|
22
|
-
this.storer = void 0;
|
|
23
|
-
this.jwt = void 0;
|
|
24
33
|
this.options = options;
|
|
25
|
-
this.jwt = new
|
|
34
|
+
this.jwt = new import_jwt_service.JwtService(options.jwt);
|
|
26
35
|
}
|
|
27
36
|
setStorer(storer) {
|
|
28
37
|
this.storer = storer;
|
|
@@ -56,62 +65,53 @@ class AuthManager {
|
|
|
56
65
|
* @param {string} name - The name of the authenticator.
|
|
57
66
|
* @return {Promise<Auth>} authenticator instance.
|
|
58
67
|
*/
|
|
59
|
-
get(name, ctx) {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
throw new Error(`AuthType [${name}] is not found.`);
|
|
73
|
-
}
|
|
74
|
-
return new auth({
|
|
75
|
-
authenticator,
|
|
76
|
-
options: authenticator.options,
|
|
77
|
-
ctx
|
|
78
|
-
});
|
|
79
|
-
})();
|
|
68
|
+
async get(name, ctx) {
|
|
69
|
+
if (!this.storer) {
|
|
70
|
+
throw new Error("AuthManager.storer is not set.");
|
|
71
|
+
}
|
|
72
|
+
const authenticator = await this.storer.get(name);
|
|
73
|
+
if (!authenticator) {
|
|
74
|
+
throw new Error(`Authenticator [${name}] is not found.`);
|
|
75
|
+
}
|
|
76
|
+
const { auth } = this.authTypes.get(authenticator.authType);
|
|
77
|
+
if (!auth) {
|
|
78
|
+
throw new Error(`AuthType [${name}] is not found.`);
|
|
79
|
+
}
|
|
80
|
+
return new auth({ authenticator, options: authenticator.options, ctx });
|
|
80
81
|
}
|
|
81
82
|
/**
|
|
82
83
|
* middleware
|
|
83
84
|
* @description Auth middleware, used to check the authentication status.
|
|
84
85
|
*/
|
|
85
86
|
middleware() {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
if (
|
|
104
|
-
|
|
105
|
-
if (user) {
|
|
106
|
-
ctx.auth.user = user;
|
|
107
|
-
}
|
|
87
|
+
return async (ctx, next) => {
|
|
88
|
+
const token = ctx.getBearerToken();
|
|
89
|
+
if (token && await ctx.app.authManager.jwt.blacklist.has(token)) {
|
|
90
|
+
return ctx.throw(401, ctx.t("token is not available"));
|
|
91
|
+
}
|
|
92
|
+
const name = ctx.get(this.options.authKey) || this.options.default;
|
|
93
|
+
let authenticator;
|
|
94
|
+
try {
|
|
95
|
+
authenticator = await ctx.app.authManager.get(name, ctx);
|
|
96
|
+
ctx.auth = authenticator;
|
|
97
|
+
} catch (err) {
|
|
98
|
+
ctx.auth = {};
|
|
99
|
+
ctx.app.logger.warn(`auth, ${err.message}`);
|
|
100
|
+
return next();
|
|
101
|
+
}
|
|
102
|
+
if (authenticator) {
|
|
103
|
+
const user = await ctx.auth.check();
|
|
104
|
+
if (user) {
|
|
105
|
+
ctx.auth.user = user;
|
|
108
106
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
return _ref.apply(this, arguments);
|
|
113
|
-
};
|
|
114
|
-
}();
|
|
107
|
+
}
|
|
108
|
+
await next();
|
|
109
|
+
};
|
|
115
110
|
}
|
|
116
|
-
}
|
|
117
|
-
|
|
111
|
+
};
|
|
112
|
+
__name(_AuthManager, "AuthManager");
|
|
113
|
+
let AuthManager = _AuthManager;
|
|
114
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
115
|
+
0 && (module.exports = {
|
|
116
|
+
AuthManager
|
|
117
|
+
});
|
package/lib/auth.js
CHANGED
|
@@ -1,32 +1,47 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
Object.
|
|
4
|
-
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var auth_exports = {};
|
|
20
|
+
__export(auth_exports, {
|
|
21
|
+
Auth: () => Auth
|
|
5
22
|
});
|
|
6
|
-
exports
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
23
|
+
module.exports = __toCommonJS(auth_exports);
|
|
24
|
+
const _Auth = class _Auth {
|
|
25
|
+
authenticator;
|
|
26
|
+
options;
|
|
27
|
+
ctx;
|
|
10
28
|
constructor(config) {
|
|
11
|
-
|
|
12
|
-
this.options = void 0;
|
|
13
|
-
this.ctx = void 0;
|
|
14
|
-
const authenticator = config.authenticator,
|
|
15
|
-
options = config.options,
|
|
16
|
-
ctx = config.ctx;
|
|
29
|
+
const { authenticator, options, ctx } = config;
|
|
17
30
|
this.authenticator = authenticator;
|
|
18
31
|
this.options = options;
|
|
19
32
|
this.ctx = ctx;
|
|
20
33
|
}
|
|
21
34
|
// The following methods are mainly designed for user authentications.
|
|
22
|
-
signIn() {
|
|
23
|
-
return _asyncToGenerator(function* () {})();
|
|
35
|
+
async signIn() {
|
|
24
36
|
}
|
|
25
|
-
signUp() {
|
|
26
|
-
return _asyncToGenerator(function* () {})();
|
|
37
|
+
async signUp() {
|
|
27
38
|
}
|
|
28
|
-
signOut() {
|
|
29
|
-
return _asyncToGenerator(function* () {})();
|
|
39
|
+
async signOut() {
|
|
30
40
|
}
|
|
31
|
-
}
|
|
32
|
-
|
|
41
|
+
};
|
|
42
|
+
__name(_Auth, "Auth");
|
|
43
|
+
let Auth = _Auth;
|
|
44
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
45
|
+
0 && (module.exports = {
|
|
46
|
+
Auth
|
|
47
|
+
});
|
package/lib/base/auth.js
CHANGED
|
@@ -1,21 +1,32 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
Object.
|
|
4
|
-
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var auth_exports = {};
|
|
20
|
+
__export(auth_exports, {
|
|
21
|
+
BaseAuth: () => BaseAuth
|
|
5
22
|
});
|
|
6
|
-
exports
|
|
7
|
-
var
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* BaseAuth
|
|
12
|
-
* @description A base class with jwt provide some common methods.
|
|
13
|
-
*/
|
|
14
|
-
class BaseAuth extends _auth.Auth {
|
|
23
|
+
module.exports = __toCommonJS(auth_exports);
|
|
24
|
+
var import_auth = require("../auth");
|
|
25
|
+
const _BaseAuth = class _BaseAuth extends import_auth.Auth {
|
|
26
|
+
userCollection;
|
|
15
27
|
constructor(config) {
|
|
16
|
-
const userCollection = config
|
|
28
|
+
const { userCollection } = config;
|
|
17
29
|
super(config);
|
|
18
|
-
this.userCollection = void 0;
|
|
19
30
|
this.userCollection = userCollection;
|
|
20
31
|
}
|
|
21
32
|
get userRepository() {
|
|
@@ -30,67 +41,59 @@ class BaseAuth extends _auth.Auth {
|
|
|
30
41
|
get user() {
|
|
31
42
|
return this.ctx.state.currentUser;
|
|
32
43
|
}
|
|
33
|
-
check() {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
44
|
+
async check() {
|
|
45
|
+
const token = this.ctx.getBearerToken();
|
|
46
|
+
if (!token) {
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
try {
|
|
50
|
+
const { userId, roleName } = await this.jwt.decode(token);
|
|
51
|
+
if (roleName) {
|
|
52
|
+
this.ctx.headers["x-role"] = roleName;
|
|
39
53
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
roleName = _yield$_this$jwt$deco.roleName;
|
|
44
|
-
if (roleName) {
|
|
45
|
-
_this.ctx.headers['x-role'] = roleName;
|
|
54
|
+
return await this.userRepository.findOne({
|
|
55
|
+
filter: {
|
|
56
|
+
id: userId
|
|
46
57
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
}
|
|
51
|
-
});
|
|
52
|
-
} catch (err) {
|
|
53
|
-
_this.ctx.logger.error(err);
|
|
54
|
-
return null;
|
|
55
|
-
}
|
|
56
|
-
})();
|
|
57
|
-
}
|
|
58
|
-
validate() {
|
|
59
|
-
return _asyncToGenerator(function* () {
|
|
58
|
+
});
|
|
59
|
+
} catch (err) {
|
|
60
|
+
this.ctx.logger.error(err);
|
|
60
61
|
return null;
|
|
61
|
-
}
|
|
62
|
+
}
|
|
62
63
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
return _asyncToGenerator(function* () {
|
|
66
|
-
let user;
|
|
67
|
-
try {
|
|
68
|
-
user = yield _this2.validate();
|
|
69
|
-
} catch (err) {
|
|
70
|
-
console.log(err);
|
|
71
|
-
_this2.ctx.throw(401, err.message);
|
|
72
|
-
}
|
|
73
|
-
if (!user) {
|
|
74
|
-
_this2.ctx.throw(401, 'Unauthorized');
|
|
75
|
-
}
|
|
76
|
-
const token = _this2.jwt.sign({
|
|
77
|
-
userId: user.id
|
|
78
|
-
});
|
|
79
|
-
return {
|
|
80
|
-
user,
|
|
81
|
-
token
|
|
82
|
-
};
|
|
83
|
-
})();
|
|
64
|
+
async validate() {
|
|
65
|
+
return null;
|
|
84
66
|
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
67
|
+
async signIn() {
|
|
68
|
+
let user;
|
|
69
|
+
try {
|
|
70
|
+
user = await this.validate();
|
|
71
|
+
} catch (err) {
|
|
72
|
+
console.log(err);
|
|
73
|
+
this.ctx.throw(401, err.message);
|
|
74
|
+
}
|
|
75
|
+
if (!user) {
|
|
76
|
+
this.ctx.throw(401, "Unauthorized");
|
|
77
|
+
}
|
|
78
|
+
const token = this.jwt.sign({
|
|
79
|
+
userId: user.id
|
|
80
|
+
});
|
|
81
|
+
return {
|
|
82
|
+
user,
|
|
83
|
+
token
|
|
84
|
+
};
|
|
94
85
|
}
|
|
95
|
-
|
|
96
|
-
|
|
86
|
+
async signOut() {
|
|
87
|
+
const token = this.ctx.getBearerToken();
|
|
88
|
+
if (!token) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
return await this.jwt.block(token);
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
__name(_BaseAuth, "BaseAuth");
|
|
95
|
+
let BaseAuth = _BaseAuth;
|
|
96
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
97
|
+
0 && (module.exports = {
|
|
98
|
+
BaseAuth
|
|
99
|
+
});
|
package/lib/base/jwt-service.js
CHANGED
|
@@ -1,38 +1,49 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
Object.
|
|
4
|
-
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var jwt_service_exports = {};
|
|
30
|
+
__export(jwt_service_exports, {
|
|
31
|
+
JwtService: () => JwtService
|
|
5
32
|
});
|
|
6
|
-
exports
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
_jsonwebtoken = function _jsonwebtoken() {
|
|
10
|
-
return data;
|
|
11
|
-
};
|
|
12
|
-
return data;
|
|
13
|
-
}
|
|
14
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
15
|
-
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
16
|
-
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
17
|
-
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
18
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
19
|
-
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
20
|
-
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
|
|
21
|
-
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
|
22
|
-
class JwtService {
|
|
33
|
+
module.exports = __toCommonJS(jwt_service_exports);
|
|
34
|
+
var import_jsonwebtoken = __toESM(require("jsonwebtoken"));
|
|
35
|
+
const _JwtService = class _JwtService {
|
|
23
36
|
constructor(options = {
|
|
24
37
|
secret: process.env.APP_KEY
|
|
25
38
|
}) {
|
|
26
|
-
this.options = void 0;
|
|
27
|
-
this.blacklist = void 0;
|
|
28
39
|
this.options = options;
|
|
29
|
-
const secret = options
|
|
30
|
-
expiresIn = options.expiresIn;
|
|
40
|
+
const { secret, expiresIn } = options;
|
|
31
41
|
this.options = {
|
|
32
|
-
secret
|
|
33
|
-
expiresIn: expiresIn || process.env.JWT_EXPIRES_IN ||
|
|
42
|
+
secret,
|
|
43
|
+
expiresIn: expiresIn || process.env.JWT_EXPIRES_IN || "7d"
|
|
34
44
|
};
|
|
35
45
|
}
|
|
46
|
+
blacklist;
|
|
36
47
|
expiresIn() {
|
|
37
48
|
return this.options.expiresIn;
|
|
38
49
|
}
|
|
@@ -40,17 +51,15 @@ class JwtService {
|
|
|
40
51
|
return this.options.secret;
|
|
41
52
|
}
|
|
42
53
|
sign(payload, options) {
|
|
43
|
-
const opt =
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
if (opt.expiresIn === 'never') {
|
|
47
|
-
opt.expiresIn = '1000y';
|
|
54
|
+
const opt = { expiresIn: this.expiresIn(), ...options };
|
|
55
|
+
if (opt.expiresIn === "never") {
|
|
56
|
+
opt.expiresIn = "1000y";
|
|
48
57
|
}
|
|
49
|
-
return
|
|
58
|
+
return import_jsonwebtoken.default.sign(payload, this.secret(), opt);
|
|
50
59
|
}
|
|
51
60
|
decode(token) {
|
|
52
61
|
return new Promise((resolve, reject) => {
|
|
53
|
-
|
|
62
|
+
import_jsonwebtoken.default.verify(token, this.secret(), (err, decoded) => {
|
|
54
63
|
if (err) {
|
|
55
64
|
return reject(err);
|
|
56
65
|
}
|
|
@@ -61,23 +70,24 @@ class JwtService {
|
|
|
61
70
|
/**
|
|
62
71
|
* @description Block a token so that this token can no longer be used
|
|
63
72
|
*/
|
|
64
|
-
block(token) {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
} catch (_unused) {
|
|
78
|
-
return null;
|
|
79
|
-
}
|
|
80
|
-
})();
|
|
73
|
+
async block(token) {
|
|
74
|
+
if (!this.blacklist) {
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
try {
|
|
78
|
+
const { exp } = await this.decode(token);
|
|
79
|
+
return this.blacklist.add({
|
|
80
|
+
token,
|
|
81
|
+
expiration: new Date(exp * 1e3).toString()
|
|
82
|
+
});
|
|
83
|
+
} catch {
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
81
86
|
}
|
|
82
|
-
}
|
|
83
|
-
|
|
87
|
+
};
|
|
88
|
+
__name(_JwtService, "JwtService");
|
|
89
|
+
let JwtService = _JwtService;
|
|
90
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
91
|
+
0 && (module.exports = {
|
|
92
|
+
JwtService
|
|
93
|
+
});
|
|
@@ -1,5 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
Object.
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __copyProps = (to, from, except, desc) => {
|
|
6
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
7
|
+
for (let key of __getOwnPropNames(from))
|
|
8
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
9
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
10
|
+
}
|
|
11
|
+
return to;
|
|
12
|
+
};
|
|
13
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
14
|
+
var token_blacklist_service_exports = {};
|
|
15
|
+
module.exports = __toCommonJS(token_blacklist_service_exports);
|
package/lib/index.js
CHANGED
|
@@ -1,60 +1,29 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
Object.
|
|
4
|
-
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __copyProps = (to, from, except, desc) => {
|
|
6
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
7
|
+
for (let key of __getOwnPropNames(from))
|
|
8
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
9
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
10
|
+
}
|
|
11
|
+
return to;
|
|
12
|
+
};
|
|
13
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
14
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
15
|
+
var src_exports = {};
|
|
16
|
+
module.exports = __toCommonJS(src_exports);
|
|
17
|
+
__reExport(src_exports, require("./actions"), module.exports);
|
|
18
|
+
__reExport(src_exports, require("./auth"), module.exports);
|
|
19
|
+
__reExport(src_exports, require("./auth-manager"), module.exports);
|
|
20
|
+
__reExport(src_exports, require("./base/auth"), module.exports);
|
|
21
|
+
__reExport(src_exports, require("./base/token-blacklist-service"), module.exports);
|
|
22
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
23
|
+
0 && (module.exports = {
|
|
24
|
+
...require("./actions"),
|
|
25
|
+
...require("./auth"),
|
|
26
|
+
...require("./auth-manager"),
|
|
27
|
+
...require("./base/auth"),
|
|
28
|
+
...require("./base/token-blacklist-service")
|
|
5
29
|
});
|
|
6
|
-
var _actions = require("./actions");
|
|
7
|
-
Object.keys(_actions).forEach(function (key) {
|
|
8
|
-
if (key === "default" || key === "__esModule") return;
|
|
9
|
-
if (key in exports && exports[key] === _actions[key]) return;
|
|
10
|
-
Object.defineProperty(exports, key, {
|
|
11
|
-
enumerable: true,
|
|
12
|
-
get: function get() {
|
|
13
|
-
return _actions[key];
|
|
14
|
-
}
|
|
15
|
-
});
|
|
16
|
-
});
|
|
17
|
-
var _auth = require("./auth");
|
|
18
|
-
Object.keys(_auth).forEach(function (key) {
|
|
19
|
-
if (key === "default" || key === "__esModule") return;
|
|
20
|
-
if (key in exports && exports[key] === _auth[key]) return;
|
|
21
|
-
Object.defineProperty(exports, key, {
|
|
22
|
-
enumerable: true,
|
|
23
|
-
get: function get() {
|
|
24
|
-
return _auth[key];
|
|
25
|
-
}
|
|
26
|
-
});
|
|
27
|
-
});
|
|
28
|
-
var _authManager = require("./auth-manager");
|
|
29
|
-
Object.keys(_authManager).forEach(function (key) {
|
|
30
|
-
if (key === "default" || key === "__esModule") return;
|
|
31
|
-
if (key in exports && exports[key] === _authManager[key]) return;
|
|
32
|
-
Object.defineProperty(exports, key, {
|
|
33
|
-
enumerable: true,
|
|
34
|
-
get: function get() {
|
|
35
|
-
return _authManager[key];
|
|
36
|
-
}
|
|
37
|
-
});
|
|
38
|
-
});
|
|
39
|
-
var _auth2 = require("./base/auth");
|
|
40
|
-
Object.keys(_auth2).forEach(function (key) {
|
|
41
|
-
if (key === "default" || key === "__esModule") return;
|
|
42
|
-
if (key in exports && exports[key] === _auth2[key]) return;
|
|
43
|
-
Object.defineProperty(exports, key, {
|
|
44
|
-
enumerable: true,
|
|
45
|
-
get: function get() {
|
|
46
|
-
return _auth2[key];
|
|
47
|
-
}
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
var _tokenBlacklistService = require("./base/token-blacklist-service");
|
|
51
|
-
Object.keys(_tokenBlacklistService).forEach(function (key) {
|
|
52
|
-
if (key === "default" || key === "__esModule") return;
|
|
53
|
-
if (key in exports && exports[key] === _tokenBlacklistService[key]) return;
|
|
54
|
-
Object.defineProperty(exports, key, {
|
|
55
|
-
enumerable: true,
|
|
56
|
-
get: function get() {
|
|
57
|
-
return _tokenBlacklistService[key];
|
|
58
|
-
}
|
|
59
|
-
});
|
|
60
|
-
});
|
package/package.json
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nocobase/auth",
|
|
3
|
-
"version": "0.13.0-alpha.
|
|
3
|
+
"version": "0.13.0-alpha.10",
|
|
4
4
|
"description": "",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"main": "./lib/index.js",
|
|
7
7
|
"types": "./lib/index.d.ts",
|
|
8
8
|
"dependencies": {
|
|
9
|
-
"@nocobase/actions": "0.13.0-alpha.
|
|
10
|
-
"@nocobase/database": "0.13.0-alpha.
|
|
11
|
-
"@nocobase/resourcer": "0.13.0-alpha.
|
|
12
|
-
"@nocobase/utils": "0.13.0-alpha.
|
|
9
|
+
"@nocobase/actions": "0.13.0-alpha.10",
|
|
10
|
+
"@nocobase/database": "0.13.0-alpha.10",
|
|
11
|
+
"@nocobase/resourcer": "0.13.0-alpha.10",
|
|
12
|
+
"@nocobase/utils": "0.13.0-alpha.10",
|
|
13
13
|
"@types/jsonwebtoken": "^8.5.8",
|
|
14
14
|
"jsonwebtoken": "^8.5.1"
|
|
15
15
|
},
|
|
@@ -18,5 +18,5 @@
|
|
|
18
18
|
"url": "git+https://github.com/nocobase/nocobase.git",
|
|
19
19
|
"directory": "packages/auth"
|
|
20
20
|
},
|
|
21
|
-
"gitHead": "
|
|
21
|
+
"gitHead": "5360ed81650f6895f3ed39aede2706467d55862c"
|
|
22
22
|
}
|