@nocobase/auth 2.1.0-beta.44 → 2.1.0-beta.46
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 +1 -0
- package/lib/auth-manager.d.ts +10 -0
- package/lib/auth-manager.js +31 -6
- package/lib/base/auth.js +2 -1
- package/lib/base/token-control-service.d.ts +3 -1
- package/package.json +7 -7
package/lib/actions.js
CHANGED
|
@@ -38,6 +38,7 @@ const actions = {
|
|
|
38
38
|
}, "signIn"),
|
|
39
39
|
signOut: /* @__PURE__ */ __name(async (ctx, next) => {
|
|
40
40
|
await ctx.auth.signOut();
|
|
41
|
+
await ctx.app.emitAsync("auth:signOut", { ctx, auth: ctx.auth });
|
|
41
42
|
await next();
|
|
42
43
|
}, "signOut"),
|
|
43
44
|
signUp: /* @__PURE__ */ __name(async (ctx, next) => {
|
package/lib/auth-manager.d.ts
CHANGED
|
@@ -17,6 +17,10 @@ export interface Authenticator {
|
|
|
17
17
|
options: Record<string, any>;
|
|
18
18
|
[key: string]: any;
|
|
19
19
|
}
|
|
20
|
+
export type BuiltInAuthenticator = Authenticator & {
|
|
21
|
+
name: string;
|
|
22
|
+
enabled?: boolean;
|
|
23
|
+
};
|
|
20
24
|
export interface Storer {
|
|
21
25
|
get: (name: string) => Promise<Authenticator>;
|
|
22
26
|
}
|
|
@@ -28,6 +32,7 @@ export type AuthManagerOptions = {
|
|
|
28
32
|
type AuthConfig = {
|
|
29
33
|
auth: AuthExtend<Auth>;
|
|
30
34
|
title?: string;
|
|
35
|
+
hidden?: boolean;
|
|
31
36
|
getPublicOptions?: (options: Record<string, any>) => Record<string, any>;
|
|
32
37
|
};
|
|
33
38
|
export declare class AuthManager {
|
|
@@ -39,8 +44,13 @@ export declare class AuthManager {
|
|
|
39
44
|
protected options: AuthManagerOptions;
|
|
40
45
|
protected authTypes: Registry<AuthConfig>;
|
|
41
46
|
protected storer: Storer;
|
|
47
|
+
protected builtInAuthenticators: Map<string, BuiltInAuthenticator>;
|
|
42
48
|
constructor(options: AuthManagerOptions);
|
|
43
49
|
setStorer(storer: Storer): void;
|
|
50
|
+
registerBuiltInAuthenticator(authenticator: BuiltInAuthenticator): void;
|
|
51
|
+
unregisterBuiltInAuthenticator(name: string): void;
|
|
52
|
+
getBuiltInAuthenticator(name: string): BuiltInAuthenticator;
|
|
53
|
+
private createAuth;
|
|
44
54
|
setTokenBlacklistService(service: ITokenBlacklistService): void;
|
|
45
55
|
setTokenControlService(service: ITokenControlService): void;
|
|
46
56
|
/**
|
package/lib/auth-manager.js
CHANGED
|
@@ -55,6 +55,7 @@ const _AuthManager = class _AuthManager {
|
|
|
55
55
|
authTypes = new import_utils.Registry();
|
|
56
56
|
// authenticators collection manager.
|
|
57
57
|
storer;
|
|
58
|
+
builtInAuthenticators = /* @__PURE__ */ new Map();
|
|
58
59
|
constructor(options) {
|
|
59
60
|
this.options = options;
|
|
60
61
|
const jwtOptions = options.jwt || {};
|
|
@@ -66,6 +67,30 @@ const _AuthManager = class _AuthManager {
|
|
|
66
67
|
setStorer(storer) {
|
|
67
68
|
this.storer = storer;
|
|
68
69
|
}
|
|
70
|
+
registerBuiltInAuthenticator(authenticator) {
|
|
71
|
+
this.builtInAuthenticators.set(authenticator.name, {
|
|
72
|
+
enabled: true,
|
|
73
|
+
options: {},
|
|
74
|
+
...authenticator
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
unregisterBuiltInAuthenticator(name) {
|
|
78
|
+
this.builtInAuthenticators.delete(name);
|
|
79
|
+
}
|
|
80
|
+
getBuiltInAuthenticator(name) {
|
|
81
|
+
const authenticator = this.builtInAuthenticators.get(name);
|
|
82
|
+
if (!(authenticator == null ? void 0 : authenticator.enabled)) {
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
return authenticator;
|
|
86
|
+
}
|
|
87
|
+
createAuth(authenticator, ctx) {
|
|
88
|
+
const { auth } = this.authTypes.get(authenticator.authType) || {};
|
|
89
|
+
if (!auth) {
|
|
90
|
+
throw new Error(`AuthType [${authenticator.authType}] is not found.`);
|
|
91
|
+
}
|
|
92
|
+
return new auth({ authenticator, options: authenticator.options, ctx });
|
|
93
|
+
}
|
|
69
94
|
setTokenBlacklistService(service) {
|
|
70
95
|
this.jwt.blacklist = service;
|
|
71
96
|
}
|
|
@@ -84,7 +109,7 @@ const _AuthManager = class _AuthManager {
|
|
|
84
109
|
this.authTypes.register(authType, authConfig);
|
|
85
110
|
}
|
|
86
111
|
listTypes() {
|
|
87
|
-
return Array.from(this.authTypes.getEntities()).map(([authType, authConfig]) => ({
|
|
112
|
+
return Array.from(this.authTypes.getEntities()).filter(([, authConfig]) => !authConfig.hidden).map(([authType, authConfig]) => ({
|
|
88
113
|
name: authType,
|
|
89
114
|
title: authConfig.title
|
|
90
115
|
}));
|
|
@@ -99,6 +124,10 @@ const _AuthManager = class _AuthManager {
|
|
|
99
124
|
* @return authenticator instance.
|
|
100
125
|
*/
|
|
101
126
|
async get(name, ctx) {
|
|
127
|
+
const builtInAuthenticator = this.getBuiltInAuthenticator(name);
|
|
128
|
+
if (builtInAuthenticator) {
|
|
129
|
+
return this.createAuth(builtInAuthenticator, ctx);
|
|
130
|
+
}
|
|
102
131
|
if (!this.storer) {
|
|
103
132
|
throw new Error("AuthManager.storer is not set.");
|
|
104
133
|
}
|
|
@@ -106,11 +135,7 @@ const _AuthManager = class _AuthManager {
|
|
|
106
135
|
if (!authenticator) {
|
|
107
136
|
throw new Error(`Authenticator [${name}] is not found.`);
|
|
108
137
|
}
|
|
109
|
-
|
|
110
|
-
if (!auth) {
|
|
111
|
-
throw new Error(`AuthType [${authenticator.authType}] is not found.`);
|
|
112
|
-
}
|
|
113
|
-
return new auth({ authenticator, options: authenticator.options, ctx });
|
|
138
|
+
return this.createAuth(authenticator, ctx);
|
|
114
139
|
}
|
|
115
140
|
/**
|
|
116
141
|
* middleware
|
package/lib/base/auth.js
CHANGED
|
@@ -241,7 +241,8 @@ const _BaseAuth = class _BaseAuth extends import_auth.Auth {
|
|
|
241
241
|
return null;
|
|
242
242
|
}
|
|
243
243
|
async signNewToken(userId) {
|
|
244
|
-
|
|
244
|
+
var _a;
|
|
245
|
+
const tokenInfo = await this.tokenController.add({ userId, authenticator: (_a = this.authenticator) == null ? void 0 : _a.name });
|
|
245
246
|
const expiresIn = Math.floor((await this.tokenController.getConfig()).tokenExpirationTime / 1e3);
|
|
246
247
|
const token = this.jwt.sign(
|
|
247
248
|
{
|
|
@@ -18,6 +18,7 @@ export type NumericTokenPolicyConfig = {
|
|
|
18
18
|
export type TokenInfo = {
|
|
19
19
|
jti: string;
|
|
20
20
|
userId: number;
|
|
21
|
+
authenticator?: string;
|
|
21
22
|
issuedTime: EpochTimeStamp;
|
|
22
23
|
signInTime: EpochTimeStamp;
|
|
23
24
|
renewed: boolean;
|
|
@@ -30,8 +31,9 @@ export interface ITokenControlService {
|
|
|
30
31
|
jti: string;
|
|
31
32
|
issuedTime: EpochTimeStamp;
|
|
32
33
|
}>;
|
|
33
|
-
add({ userId }: {
|
|
34
|
+
add({ userId, authenticator }: {
|
|
34
35
|
userId: number;
|
|
36
|
+
authenticator?: string;
|
|
35
37
|
}): Promise<TokenInfo>;
|
|
36
38
|
removeSessionExpiredTokens(userId: number): Promise<any>;
|
|
37
39
|
}
|
package/package.json
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nocobase/auth",
|
|
3
|
-
"version": "2.1.0-beta.
|
|
3
|
+
"version": "2.1.0-beta.46",
|
|
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": "2.1.0-beta.
|
|
10
|
-
"@nocobase/cache": "2.1.0-beta.
|
|
11
|
-
"@nocobase/database": "2.1.0-beta.
|
|
12
|
-
"@nocobase/resourcer": "2.1.0-beta.
|
|
13
|
-
"@nocobase/utils": "2.1.0-beta.
|
|
9
|
+
"@nocobase/actions": "2.1.0-beta.46",
|
|
10
|
+
"@nocobase/cache": "2.1.0-beta.46",
|
|
11
|
+
"@nocobase/database": "2.1.0-beta.46",
|
|
12
|
+
"@nocobase/resourcer": "2.1.0-beta.46",
|
|
13
|
+
"@nocobase/utils": "2.1.0-beta.46",
|
|
14
14
|
"@types/jsonwebtoken": "^9.0.9",
|
|
15
15
|
"jsonwebtoken": "^9.0.2"
|
|
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": "91fd3ab238a5ff58735bbfca04a8cb05d233b0af"
|
|
23
23
|
}
|