@nocobase/auth 1.6.0-alpha.20 → 1.6.0-alpha.22
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/auth.d.ts +1 -0
- package/lib/auth.js +2 -1
- package/lib/base/auth.js +36 -9
- package/lib/client.d.ts +9 -0
- package/lib/client.js +36 -0
- package/package.json +7 -7
package/lib/auth.d.ts
CHANGED
|
@@ -24,6 +24,7 @@ export declare const AuthErrorCode: {
|
|
|
24
24
|
BLOCKED_TOKEN: "BLOCKED_TOKEN";
|
|
25
25
|
EXPIRED_SESSION: "EXPIRED_SESSION";
|
|
26
26
|
NOT_EXIST_USER: "NOT_EXIST_USER";
|
|
27
|
+
SKIP_TOKEN_RENEW: "SKIP_TOKEN_RENEW";
|
|
27
28
|
};
|
|
28
29
|
export type AuthErrorType = keyof typeof AuthErrorCode;
|
|
29
30
|
export declare class AuthError extends Error {
|
package/lib/auth.js
CHANGED
|
@@ -41,7 +41,8 @@ const AuthErrorCode = {
|
|
|
41
41
|
TOKEN_RENEW_FAILED: "TOKEN_RENEW_FAILED",
|
|
42
42
|
BLOCKED_TOKEN: "BLOCKED_TOKEN",
|
|
43
43
|
EXPIRED_SESSION: "EXPIRED_SESSION",
|
|
44
|
-
NOT_EXIST_USER: "NOT_EXIST_USER"
|
|
44
|
+
NOT_EXIST_USER: "NOT_EXIST_USER",
|
|
45
|
+
SKIP_TOKEN_RENEW: "SKIP_TOKEN_RENEW"
|
|
45
46
|
};
|
|
46
47
|
const _AuthError = class _AuthError extends Error {
|
|
47
48
|
code;
|
package/lib/base/auth.js
CHANGED
|
@@ -81,6 +81,7 @@ const _BaseAuth = class _BaseAuth extends import_auth.Auth {
|
|
|
81
81
|
return /^[^@.<>"'/]{1,50}$/.test(username);
|
|
82
82
|
}
|
|
83
83
|
async check() {
|
|
84
|
+
var _a, _b, _c, _d, _e, _f;
|
|
84
85
|
const token = this.ctx.getBearerToken();
|
|
85
86
|
if (!token) {
|
|
86
87
|
this.ctx.throw(401, {
|
|
@@ -106,6 +107,16 @@ const _BaseAuth = class _BaseAuth extends import_auth.Auth {
|
|
|
106
107
|
}
|
|
107
108
|
}
|
|
108
109
|
const { userId, roleName, iat, temp, jti, exp, signInTime } = payload ?? {};
|
|
110
|
+
const tokenPolicy = await this.tokenController.getConfig();
|
|
111
|
+
if (!signInTime || Date.now() - signInTime > tokenPolicy.sessionExpirationTime) {
|
|
112
|
+
this.ctx.throw(401, {
|
|
113
|
+
message: this.ctx.t("Your session has expired. Please sign in again.", { ns: localeNamespace }),
|
|
114
|
+
code: import_auth.AuthErrorCode.EXPIRED_SESSION
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
if (tokenStatus === "valid" && Date.now() - iat * 1e3 > tokenPolicy.tokenExpirationTime) {
|
|
118
|
+
tokenStatus = "expired";
|
|
119
|
+
}
|
|
109
120
|
const blocked = await this.jwt.blacklist.has(jti ?? token);
|
|
110
121
|
if (blocked) {
|
|
111
122
|
this.ctx.throw(401, {
|
|
@@ -139,13 +150,6 @@ const _BaseAuth = class _BaseAuth extends import_auth.Auth {
|
|
|
139
150
|
});
|
|
140
151
|
}
|
|
141
152
|
if (tokenStatus === "expired") {
|
|
142
|
-
const tokenPolicy = await this.tokenController.getConfig();
|
|
143
|
-
if (!signInTime || Date.now() - signInTime > tokenPolicy.sessionExpirationTime) {
|
|
144
|
-
this.ctx.throw(401, {
|
|
145
|
-
message: this.ctx.t("Your session has expired. Please sign in again.", { ns: localeNamespace }),
|
|
146
|
-
code: import_auth.AuthErrorCode.EXPIRED_SESSION
|
|
147
|
-
});
|
|
148
|
-
}
|
|
149
153
|
if (tokenPolicy.expiredTokenRenewLimit > 0 && Date.now() - exp * 1e3 > tokenPolicy.expiredTokenRenewLimit) {
|
|
150
154
|
this.ctx.throw(401, {
|
|
151
155
|
message: this.ctx.t("Your session has expired. Please sign in again.", { ns: localeNamespace }),
|
|
@@ -153,16 +157,39 @@ const _BaseAuth = class _BaseAuth extends import_auth.Auth {
|
|
|
153
157
|
});
|
|
154
158
|
}
|
|
155
159
|
try {
|
|
160
|
+
this.ctx.logger.info("token renewing", {
|
|
161
|
+
method: "auth.check",
|
|
162
|
+
url: this.ctx.originalUrl,
|
|
163
|
+
headers: JSON.stringify((_b = (_a = this.ctx) == null ? void 0 : _a.req) == null ? void 0 : _b.headers)
|
|
164
|
+
});
|
|
165
|
+
const isStreamRequest = this.ctx.req.headers["accept"] === "text/event-stream";
|
|
166
|
+
if (isStreamRequest) {
|
|
167
|
+
this.ctx.throw(401, {
|
|
168
|
+
message: "Stream api not allow renew token.",
|
|
169
|
+
code: import_auth.AuthErrorCode.SKIP_TOKEN_RENEW
|
|
170
|
+
});
|
|
171
|
+
}
|
|
156
172
|
const renewedResult = await this.tokenController.renew(jti);
|
|
173
|
+
this.ctx.logger.info("token renewed", {
|
|
174
|
+
method: "auth.check",
|
|
175
|
+
url: this.ctx.originalUrl,
|
|
176
|
+
headers: JSON.stringify((_d = (_c = this.ctx) == null ? void 0 : _c.req) == null ? void 0 : _d.headers)
|
|
177
|
+
});
|
|
157
178
|
const expiresIn = Math.floor(tokenPolicy.tokenExpirationTime / 1e3);
|
|
158
179
|
const newToken = this.jwt.sign({ userId, roleName, temp, signInTime }, { jwtid: renewedResult.jti, expiresIn });
|
|
159
180
|
this.ctx.res.setHeader("x-new-token", newToken);
|
|
160
181
|
return user;
|
|
161
182
|
} catch (err) {
|
|
162
|
-
|
|
183
|
+
this.ctx.logger.info("token renew failed", {
|
|
184
|
+
method: "auth.check",
|
|
185
|
+
url: this.ctx.originalUrl,
|
|
186
|
+
err,
|
|
187
|
+
headers: JSON.stringify((_f = (_e = this.ctx) == null ? void 0 : _e.req) == null ? void 0 : _f.headers)
|
|
188
|
+
});
|
|
189
|
+
const options = err instanceof import_auth.AuthError ? { code: err.code, message: err.message } : { message: err.message, code: err.code ?? import_auth.AuthErrorCode.INVALID_TOKEN };
|
|
163
190
|
this.ctx.throw(401, {
|
|
164
191
|
message: this.ctx.t(options.message, { ns: localeNamespace }),
|
|
165
|
-
code: options.
|
|
192
|
+
code: options.code
|
|
166
193
|
});
|
|
167
194
|
}
|
|
168
195
|
}
|
package/lib/client.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
export { AuthErrorCode } from './auth';
|
package/lib/client.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
var __defProp = Object.defineProperty;
|
|
11
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
12
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
13
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
14
|
+
var __export = (target, all) => {
|
|
15
|
+
for (var name in all)
|
|
16
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
17
|
+
};
|
|
18
|
+
var __copyProps = (to, from, except, desc) => {
|
|
19
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
20
|
+
for (let key of __getOwnPropNames(from))
|
|
21
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
22
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
23
|
+
}
|
|
24
|
+
return to;
|
|
25
|
+
};
|
|
26
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
27
|
+
var client_exports = {};
|
|
28
|
+
__export(client_exports, {
|
|
29
|
+
AuthErrorCode: () => import_auth.AuthErrorCode
|
|
30
|
+
});
|
|
31
|
+
module.exports = __toCommonJS(client_exports);
|
|
32
|
+
var import_auth = require("./auth");
|
|
33
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
34
|
+
0 && (module.exports = {
|
|
35
|
+
AuthErrorCode
|
|
36
|
+
});
|
package/package.json
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nocobase/auth",
|
|
3
|
-
"version": "1.6.0-alpha.
|
|
3
|
+
"version": "1.6.0-alpha.22",
|
|
4
4
|
"description": "",
|
|
5
5
|
"license": "AGPL-3.0",
|
|
6
6
|
"main": "./lib/index.js",
|
|
7
7
|
"types": "./lib/index.d.ts",
|
|
8
8
|
"dependencies": {
|
|
9
|
-
"@nocobase/actions": "1.6.0-alpha.
|
|
10
|
-
"@nocobase/cache": "1.6.0-alpha.
|
|
11
|
-
"@nocobase/database": "1.6.0-alpha.
|
|
12
|
-
"@nocobase/resourcer": "1.6.0-alpha.
|
|
13
|
-
"@nocobase/utils": "1.6.0-alpha.
|
|
9
|
+
"@nocobase/actions": "1.6.0-alpha.22",
|
|
10
|
+
"@nocobase/cache": "1.6.0-alpha.22",
|
|
11
|
+
"@nocobase/database": "1.6.0-alpha.22",
|
|
12
|
+
"@nocobase/resourcer": "1.6.0-alpha.22",
|
|
13
|
+
"@nocobase/utils": "1.6.0-alpha.22",
|
|
14
14
|
"@types/jsonwebtoken": "^8.5.8",
|
|
15
15
|
"jsonwebtoken": "^8.5.1"
|
|
16
16
|
},
|
|
@@ -19,5 +19,5 @@
|
|
|
19
19
|
"url": "git+https://github.com/nocobase/nocobase.git",
|
|
20
20
|
"directory": "packages/auth"
|
|
21
21
|
},
|
|
22
|
-
"gitHead": "
|
|
22
|
+
"gitHead": "ff8764febe306023bd6d56f0d9612d7a03170006"
|
|
23
23
|
}
|