@tachybase/auth 1.3.55-alpha.1 → 1.6.0-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/auth-manager.d.ts +0 -3
- package/lib/auth-manager.js +0 -3
- package/lib/auth.d.ts +0 -1
- package/lib/auth.js +1 -2
- package/lib/base/auth.d.ts +0 -8
- package/lib/base/auth.js +6 -45
- package/lib/index.d.ts +0 -1
- package/lib/index.js +1 -3
- package/package.json +5 -5
- package/lib/base/user-status-service.d.ts +0 -77
- package/lib/base/user-status-service.js +0 -15
package/lib/auth-manager.d.ts
CHANGED
|
@@ -4,7 +4,6 @@ import { Auth, AuthExtend } from './auth';
|
|
|
4
4
|
import { JwtOptions, JwtService } from './base/jwt-service';
|
|
5
5
|
import { ITokenBlacklistService } from './base/token-blacklist-service';
|
|
6
6
|
import { ITokenControlService } from './base/token-control-service';
|
|
7
|
-
import { IUserStatusService } from './base/user-status-service';
|
|
8
7
|
export interface Authenticator {
|
|
9
8
|
authType: string;
|
|
10
9
|
options: Record<string, any>;
|
|
@@ -26,7 +25,6 @@ type AuthConfig = {
|
|
|
26
25
|
export declare class AuthManager {
|
|
27
26
|
jwt: JwtService;
|
|
28
27
|
tokenController: ITokenControlService;
|
|
29
|
-
userStatusService: IUserStatusService;
|
|
30
28
|
protected options: AuthManagerOptions;
|
|
31
29
|
protected authTypes: Registry<AuthConfig>;
|
|
32
30
|
protected storer: Storer;
|
|
@@ -34,7 +32,6 @@ export declare class AuthManager {
|
|
|
34
32
|
setStorer(storer: Storer): void;
|
|
35
33
|
setTokenBlacklistService(service: ITokenBlacklistService): void;
|
|
36
34
|
setTokenControlService(service: ITokenControlService): void;
|
|
37
|
-
setUserStatusService(service: IUserStatusService): void;
|
|
38
35
|
/**
|
|
39
36
|
* registerTypes
|
|
40
37
|
* @description Add a new authenticate type and the corresponding authenticator.
|
package/lib/auth-manager.js
CHANGED
|
@@ -38,9 +38,6 @@ const _AuthManager = class _AuthManager {
|
|
|
38
38
|
setTokenControlService(service) {
|
|
39
39
|
this.tokenController = service;
|
|
40
40
|
}
|
|
41
|
-
setUserStatusService(service) {
|
|
42
|
-
this.userStatusService = service;
|
|
43
|
-
}
|
|
44
41
|
/**
|
|
45
42
|
* registerTypes
|
|
46
43
|
* @description Add a new authenticate type and the corresponding authenticator.
|
package/lib/auth.d.ts
CHANGED
|
@@ -17,7 +17,6 @@ export declare const AuthErrorCode: {
|
|
|
17
17
|
EXPIRED_SESSION: "EXPIRED_SESSION";
|
|
18
18
|
NOT_EXIST_USER: "NOT_EXIST_USER";
|
|
19
19
|
SKIP_TOKEN_RENEW: "SKIP_TOKEN_RENEW";
|
|
20
|
-
USER_STATUS_NOT_ALLOW_LOGIN: "USER_STATUS_NOT_ALLOW_LOGIN";
|
|
21
20
|
};
|
|
22
21
|
export type AuthErrorType = keyof typeof AuthErrorCode;
|
|
23
22
|
export declare class AuthError extends Error {
|
package/lib/auth.js
CHANGED
|
@@ -31,8 +31,7 @@ const AuthErrorCode = {
|
|
|
31
31
|
BLOCKED_TOKEN: "BLOCKED_TOKEN",
|
|
32
32
|
EXPIRED_SESSION: "EXPIRED_SESSION",
|
|
33
33
|
NOT_EXIST_USER: "NOT_EXIST_USER",
|
|
34
|
-
SKIP_TOKEN_RENEW: "SKIP_TOKEN_RENEW"
|
|
35
|
-
USER_STATUS_NOT_ALLOW_LOGIN: "USER_STATUS_NOT_ALLOW_LOGIN"
|
|
34
|
+
SKIP_TOKEN_RENEW: "SKIP_TOKEN_RENEW"
|
|
36
35
|
};
|
|
37
36
|
const _AuthError = class _AuthError extends Error {
|
|
38
37
|
constructor(options) {
|
package/lib/base/auth.d.ts
CHANGED
|
@@ -2,7 +2,6 @@ import { Collection, Model } from '@tachybase/database';
|
|
|
2
2
|
import { Auth, AuthConfig } from '../auth';
|
|
3
3
|
import { JwtService } from './jwt-service';
|
|
4
4
|
import { ITokenControlService } from './token-control-service';
|
|
5
|
-
import { IUserStatusService } from './user-status-service';
|
|
6
5
|
/**
|
|
7
6
|
* BaseAuth
|
|
8
7
|
* @description A base class with jwt provide some common methods.
|
|
@@ -15,7 +14,6 @@ export declare class BaseAuth extends Auth {
|
|
|
15
14
|
get userRepository(): import("@tachybase/database").Repository<any, any>;
|
|
16
15
|
get jwt(): JwtService;
|
|
17
16
|
get tokenController(): ITokenControlService;
|
|
18
|
-
get userStatusService(): IUserStatusService;
|
|
19
17
|
set user(user: Model);
|
|
20
18
|
get user(): Model;
|
|
21
19
|
getCacheKey(userId: number): string;
|
|
@@ -23,7 +21,6 @@ export declare class BaseAuth extends Auth {
|
|
|
23
21
|
checkToken(): Promise<{
|
|
24
22
|
tokenStatus: 'valid' | 'expired' | 'invalid';
|
|
25
23
|
user: Awaited<ReturnType<Auth['check']>>;
|
|
26
|
-
userStatus: string;
|
|
27
24
|
jti?: string;
|
|
28
25
|
temp: any;
|
|
29
26
|
roleName?: any;
|
|
@@ -31,11 +28,6 @@ export declare class BaseAuth extends Auth {
|
|
|
31
28
|
}>;
|
|
32
29
|
check(): ReturnType<Auth['check']>;
|
|
33
30
|
validate(): Promise<Model>;
|
|
34
|
-
/**
|
|
35
|
-
* 签新 token
|
|
36
|
-
* @param userId 用户 ID
|
|
37
|
-
* @returns 新 token
|
|
38
|
-
*/
|
|
39
31
|
signNewToken(userId: number): Promise<string>;
|
|
40
32
|
signIn(): Promise<{
|
|
41
33
|
user: Model<any, any>;
|
package/lib/base/auth.js
CHANGED
|
@@ -49,9 +49,6 @@ const _BaseAuth = class _BaseAuth extends import_auth.Auth {
|
|
|
49
49
|
get tokenController() {
|
|
50
50
|
return this.ctx.tego.authManager.tokenController;
|
|
51
51
|
}
|
|
52
|
-
get userStatusService() {
|
|
53
|
-
return this.ctx.tego.authManager.userStatusService;
|
|
54
|
-
}
|
|
55
52
|
set user(user) {
|
|
56
53
|
this.ctx.state.currentUser = user;
|
|
57
54
|
}
|
|
@@ -90,7 +87,7 @@ const _BaseAuth = class _BaseAuth extends import_auth.Auth {
|
|
|
90
87
|
});
|
|
91
88
|
}
|
|
92
89
|
}
|
|
93
|
-
const { userId,
|
|
90
|
+
const { userId, roleName, iat, temp, jti, exp, signInTime } = payload ?? {};
|
|
94
91
|
const user = userId ? await this.ctx.tego.cache.wrap(
|
|
95
92
|
this.getCacheKey(userId),
|
|
96
93
|
() => this.userRepository.findOne({
|
|
@@ -106,19 +103,6 @@ const _BaseAuth = class _BaseAuth extends import_auth.Auth {
|
|
|
106
103
|
code: import_auth.AuthErrorCode.NOT_EXIST_USER
|
|
107
104
|
});
|
|
108
105
|
}
|
|
109
|
-
const statusCheckResult = await this.userStatusService.checkUserStatus(user.id);
|
|
110
|
-
if (!statusCheckResult.allowed) {
|
|
111
|
-
this.ctx.throw(401, {
|
|
112
|
-
message: this.ctx.t(statusCheckResult.errorMessage, { ns: localeNamespace }),
|
|
113
|
-
code: import_auth.AuthErrorCode.USER_STATUS_NOT_ALLOW_LOGIN
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
if (statusCheckResult.status !== userStatus) {
|
|
117
|
-
this.ctx.throw(401, {
|
|
118
|
-
message: this.ctx.t("Your account status has changed. Please sign in again.", { ns: localeNamespace }),
|
|
119
|
-
code: import_auth.AuthErrorCode.INVALID_TOKEN
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
106
|
if (roleName) {
|
|
123
107
|
this.ctx.headers["x-role"] = roleName;
|
|
124
108
|
}
|
|
@@ -131,7 +115,7 @@ const _BaseAuth = class _BaseAuth extends import_auth.Auth {
|
|
|
131
115
|
}
|
|
132
116
|
if (!temp) {
|
|
133
117
|
if (tokenStatus === "valid") {
|
|
134
|
-
return { tokenStatus, user,
|
|
118
|
+
return { tokenStatus, user, temp };
|
|
135
119
|
} else {
|
|
136
120
|
this.ctx.throw(401, {
|
|
137
121
|
message: this.ctx.t("Your session has expired. Please sign in again.", { ns: localeNamespace }),
|
|
@@ -174,13 +158,13 @@ const _BaseAuth = class _BaseAuth extends import_auth.Auth {
|
|
|
174
158
|
code: import_auth.AuthErrorCode.INVALID_TOKEN
|
|
175
159
|
});
|
|
176
160
|
}
|
|
177
|
-
return { tokenStatus, user,
|
|
161
|
+
return { tokenStatus, user, jti, signInTime, temp };
|
|
178
162
|
}
|
|
179
|
-
return { tokenStatus, user,
|
|
163
|
+
return { tokenStatus, user, jti, signInTime, temp };
|
|
180
164
|
}
|
|
181
165
|
async check() {
|
|
182
166
|
var _a, _b, _c;
|
|
183
|
-
const { tokenStatus, user,
|
|
167
|
+
const { tokenStatus, user, jti, temp, signInTime, roleName } = await this.checkToken();
|
|
184
168
|
if (tokenStatus === "expired") {
|
|
185
169
|
const tokenPolicy = await this.tokenController.getConfig();
|
|
186
170
|
try {
|
|
@@ -209,7 +193,7 @@ const _BaseAuth = class _BaseAuth extends import_auth.Auth {
|
|
|
209
193
|
});
|
|
210
194
|
const expiresIn = Math.floor(tokenPolicy.tokenExpirationTime / 1e3);
|
|
211
195
|
const newToken = this.jwt.sign(
|
|
212
|
-
{ userId: user.id,
|
|
196
|
+
{ userId: user.id, roleName, temp, signInTime, iat: Math.floor(renewedResult.issuedTime / 1e3) },
|
|
213
197
|
{ jwtid: renewedResult.jti, expiresIn }
|
|
214
198
|
);
|
|
215
199
|
this.ctx.res.setHeader("x-new-token", newToken);
|
|
@@ -230,28 +214,12 @@ const _BaseAuth = class _BaseAuth extends import_auth.Auth {
|
|
|
230
214
|
async validate() {
|
|
231
215
|
return null;
|
|
232
216
|
}
|
|
233
|
-
/**
|
|
234
|
-
* 签新 token
|
|
235
|
-
* @param userId 用户 ID
|
|
236
|
-
* @returns 新 token
|
|
237
|
-
*/
|
|
238
217
|
async signNewToken(userId) {
|
|
239
|
-
const user = await this.userRepository.findOne({
|
|
240
|
-
filter: { id: userId },
|
|
241
|
-
fields: ["id", "status"]
|
|
242
|
-
});
|
|
243
|
-
if (!user) {
|
|
244
|
-
this.ctx.throw(401, {
|
|
245
|
-
message: this.ctx.t("User not found. Please sign in again to continue.", { ns: localeNamespace }),
|
|
246
|
-
code: import_auth.AuthErrorCode.NOT_EXIST_USER
|
|
247
|
-
});
|
|
248
|
-
}
|
|
249
218
|
const tokenInfo = await this.tokenController.add({ userId });
|
|
250
219
|
const expiresIn = Math.floor((await this.tokenController.getConfig()).tokenExpirationTime / 1e3);
|
|
251
220
|
const token = this.jwt.sign(
|
|
252
221
|
{
|
|
253
222
|
userId,
|
|
254
|
-
userStatus: user.status,
|
|
255
223
|
temp: true,
|
|
256
224
|
iat: Math.floor(tokenInfo.issuedTime / 1e3),
|
|
257
225
|
signInTime: tokenInfo.signInTime
|
|
@@ -278,13 +246,6 @@ const _BaseAuth = class _BaseAuth extends import_auth.Auth {
|
|
|
278
246
|
code: import_auth.AuthErrorCode.NOT_EXIST_USER
|
|
279
247
|
});
|
|
280
248
|
}
|
|
281
|
-
const statusCheckResult = await this.userStatusService.checkUserStatus(user.id);
|
|
282
|
-
if (!statusCheckResult.allowed) {
|
|
283
|
-
this.ctx.throw(401, {
|
|
284
|
-
message: this.ctx.t(statusCheckResult.errorMessage, { ns: localeNamespace }),
|
|
285
|
-
code: import_auth.AuthErrorCode.USER_STATUS_NOT_ALLOW_LOGIN
|
|
286
|
-
});
|
|
287
|
-
}
|
|
288
249
|
const token = await this.signNewToken(user.id);
|
|
289
250
|
return {
|
|
290
251
|
user,
|
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -20,7 +20,6 @@ __reExport(index_exports, require("./auth-manager"), module.exports);
|
|
|
20
20
|
__reExport(index_exports, require("./base/auth"), module.exports);
|
|
21
21
|
__reExport(index_exports, require("./base/token-blacklist-service"), module.exports);
|
|
22
22
|
__reExport(index_exports, require("./base/token-control-service"), module.exports);
|
|
23
|
-
__reExport(index_exports, require("./base/user-status-service"), module.exports);
|
|
24
23
|
// Annotate the CommonJS export names for ESM import in node:
|
|
25
24
|
0 && (module.exports = {
|
|
26
25
|
...require("./actions"),
|
|
@@ -28,6 +27,5 @@ __reExport(index_exports, require("./base/user-status-service"), module.exports)
|
|
|
28
27
|
...require("./auth-manager"),
|
|
29
28
|
...require("./base/auth"),
|
|
30
29
|
...require("./base/token-blacklist-service"),
|
|
31
|
-
...require("./base/token-control-service")
|
|
32
|
-
...require("./base/user-status-service")
|
|
30
|
+
...require("./base/token-control-service")
|
|
33
31
|
});
|
package/package.json
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tachybase/auth",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.0-alpha.2",
|
|
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
9
|
"jsonwebtoken": "^8.5.1",
|
|
10
|
-
"@tachybase/actions": "1.
|
|
11
|
-
"@tachybase/database": "1.
|
|
12
|
-
"@tachybase/resourcer": "1.
|
|
13
|
-
"@tachybase/utils": "1.
|
|
10
|
+
"@tachybase/actions": "1.6.0-alpha.2",
|
|
11
|
+
"@tachybase/database": "1.6.0-alpha.2",
|
|
12
|
+
"@tachybase/resourcer": "1.6.0-alpha.2",
|
|
13
|
+
"@tachybase/utils": "1.6.0-alpha.2"
|
|
14
14
|
},
|
|
15
15
|
"devDependencies": {
|
|
16
16
|
"@types/jsonwebtoken": "^8.5.9",
|
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 用户状态检查结果
|
|
3
|
-
*/
|
|
4
|
-
export interface UserStatusCheckResult {
|
|
5
|
-
allowed: boolean;
|
|
6
|
-
status: string;
|
|
7
|
-
statusInfo: {
|
|
8
|
-
title: string;
|
|
9
|
-
color: string;
|
|
10
|
-
allowLogin: boolean;
|
|
11
|
-
};
|
|
12
|
-
errorMessage: string;
|
|
13
|
-
isExpired: boolean;
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* 用户状态缓存数据
|
|
17
|
-
*/
|
|
18
|
-
export interface UserStatusCache {
|
|
19
|
-
userId: number;
|
|
20
|
-
status: string;
|
|
21
|
-
expireAt: Date | null;
|
|
22
|
-
previousStatus: string | null;
|
|
23
|
-
lastChecked: Date;
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* 用户状态服务接口
|
|
27
|
-
*/
|
|
28
|
-
export interface IUserStatusService {
|
|
29
|
-
/**
|
|
30
|
-
* 检查用户状态是否允许登录
|
|
31
|
-
* @param userId 用户ID
|
|
32
|
-
* @returns 检查结果
|
|
33
|
-
*/
|
|
34
|
-
checkUserStatus(userId: number): Promise<UserStatusCheckResult>;
|
|
35
|
-
/**
|
|
36
|
-
* 设置用户状态缓存
|
|
37
|
-
* @param userId 用户ID
|
|
38
|
-
* @param data 缓存数据
|
|
39
|
-
*/
|
|
40
|
-
setUserStatusCache(userId: number, data: UserStatusCache): Promise<void>;
|
|
41
|
-
/**
|
|
42
|
-
* 从缓存获取用户状态
|
|
43
|
-
* @param userId 用户ID
|
|
44
|
-
* @returns 缓存数据或 null
|
|
45
|
-
*/
|
|
46
|
-
getUserStatusFromCache(userId: number): Promise<UserStatusCache | null>;
|
|
47
|
-
/**
|
|
48
|
-
* 获取用户状态缓存键
|
|
49
|
-
* @param userId 用户ID
|
|
50
|
-
* @returns 缓存键
|
|
51
|
-
*/
|
|
52
|
-
getUserStatusCacheKey(userId: number): string;
|
|
53
|
-
/**
|
|
54
|
-
* 恢复过期的用户状态
|
|
55
|
-
* @param userId 用户ID
|
|
56
|
-
*/
|
|
57
|
-
restoreUserStatus(userId: number): Promise<void>;
|
|
58
|
-
/**
|
|
59
|
-
* 清除用户状态缓存
|
|
60
|
-
* @param userId 用户ID
|
|
61
|
-
*/
|
|
62
|
-
clearUserStatusCache(userId: number): Promise<void>;
|
|
63
|
-
/**
|
|
64
|
-
* 记录状态变更历史(如果不存在相同记录)
|
|
65
|
-
* @param params 状态变更参数
|
|
66
|
-
*/
|
|
67
|
-
recordStatusHistoryIfNotExists(params: {
|
|
68
|
-
userId: number;
|
|
69
|
-
fromStatus: string;
|
|
70
|
-
toStatus: string;
|
|
71
|
-
reason: string | null;
|
|
72
|
-
expireAt: Date | null;
|
|
73
|
-
operationType: 'manual' | 'auto' | 'system';
|
|
74
|
-
createdBy: number | null;
|
|
75
|
-
transaction?: any;
|
|
76
|
-
}): Promise<void>;
|
|
77
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
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 user_status_service_exports = {};
|
|
15
|
-
module.exports = __toCommonJS(user_status_service_exports);
|