@nocobase/auth 1.2.12-alpha → 1.3.0-alpha.20240710084543
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/package.json +7 -7
- package/lib/actions.d.ts +0 -10
- package/lib/actions.js +0 -54
- package/lib/auth-manager.d.ts +0 -71
- package/lib/auth-manager.js +0 -130
- package/lib/auth.d.ts +0 -40
- package/lib/auth.js +0 -56
- package/lib/base/auth.d.ts +0 -43
- package/lib/base/auth.js +0 -130
- package/lib/base/jwt-service.d.ts +0 -28
- package/lib/base/jwt-service.js +0 -104
- package/lib/base/token-blacklist-service.d.ts +0 -15
- package/lib/base/token-blacklist-service.js +0 -24
- package/lib/index.d.ts +0 -13
- package/lib/index.js +0 -38
package/package.json
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nocobase/auth",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0-alpha.20240710084543",
|
|
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.
|
|
10
|
-
"@nocobase/cache": "1.
|
|
11
|
-
"@nocobase/database": "1.
|
|
12
|
-
"@nocobase/resourcer": "1.
|
|
13
|
-
"@nocobase/utils": "1.
|
|
9
|
+
"@nocobase/actions": "1.3.0-alpha.20240710084543",
|
|
10
|
+
"@nocobase/cache": "1.3.0-alpha.20240710084543",
|
|
11
|
+
"@nocobase/database": "1.3.0-alpha.20240710084543",
|
|
12
|
+
"@nocobase/resourcer": "1.3.0-alpha.20240710084543",
|
|
13
|
+
"@nocobase/utils": "1.3.0-alpha.20240710084543",
|
|
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": "07a8b596fc64a9779a194cb9b0dc2ca7570ed9d4"
|
|
23
23
|
}
|
package/lib/actions.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
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
|
-
import { Handlers } from '@nocobase/resourcer';
|
|
10
|
-
export declare const actions: Handlers;
|
package/lib/actions.js
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
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 actions_exports = {};
|
|
28
|
-
__export(actions_exports, {
|
|
29
|
-
actions: () => actions
|
|
30
|
-
});
|
|
31
|
-
module.exports = __toCommonJS(actions_exports);
|
|
32
|
-
/* istanbul ignore file -- @preserve */
|
|
33
|
-
const actions = {
|
|
34
|
-
signIn: async (ctx, next) => {
|
|
35
|
-
ctx.body = await ctx.auth.signIn();
|
|
36
|
-
await next();
|
|
37
|
-
},
|
|
38
|
-
signOut: async (ctx, next) => {
|
|
39
|
-
await ctx.auth.signOut();
|
|
40
|
-
await next();
|
|
41
|
-
},
|
|
42
|
-
signUp: async (ctx, next) => {
|
|
43
|
-
await ctx.auth.signUp();
|
|
44
|
-
await next();
|
|
45
|
-
},
|
|
46
|
-
check: async (ctx, next) => {
|
|
47
|
-
ctx.body = ctx.auth.user || {};
|
|
48
|
-
await next();
|
|
49
|
-
}
|
|
50
|
-
};
|
|
51
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
52
|
-
0 && (module.exports = {
|
|
53
|
-
actions
|
|
54
|
-
});
|
package/lib/auth-manager.d.ts
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
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
|
-
import { Context, Next } from '@nocobase/actions';
|
|
10
|
-
import { Registry } from '@nocobase/utils';
|
|
11
|
-
import { Auth, AuthExtend } from './auth';
|
|
12
|
-
import { JwtOptions, JwtService } from './base/jwt-service';
|
|
13
|
-
import { ITokenBlacklistService } from './base/token-blacklist-service';
|
|
14
|
-
export interface Authenticator {
|
|
15
|
-
authType: string;
|
|
16
|
-
options: Record<string, any>;
|
|
17
|
-
[key: string]: any;
|
|
18
|
-
}
|
|
19
|
-
export interface Storer {
|
|
20
|
-
get: (name: string) => Promise<Authenticator>;
|
|
21
|
-
}
|
|
22
|
-
export type AuthManagerOptions = {
|
|
23
|
-
authKey: string;
|
|
24
|
-
default?: string;
|
|
25
|
-
jwt?: JwtOptions;
|
|
26
|
-
};
|
|
27
|
-
type AuthConfig = {
|
|
28
|
-
auth: AuthExtend<Auth>;
|
|
29
|
-
title?: string;
|
|
30
|
-
};
|
|
31
|
-
export declare class AuthManager {
|
|
32
|
-
/**
|
|
33
|
-
* @internal
|
|
34
|
-
*/
|
|
35
|
-
jwt: JwtService;
|
|
36
|
-
protected options: AuthManagerOptions;
|
|
37
|
-
protected authTypes: Registry<AuthConfig>;
|
|
38
|
-
protected storer: Storer;
|
|
39
|
-
constructor(options: AuthManagerOptions);
|
|
40
|
-
setStorer(storer: Storer): void;
|
|
41
|
-
setTokenBlacklistService(service: ITokenBlacklistService): void;
|
|
42
|
-
/**
|
|
43
|
-
* registerTypes
|
|
44
|
-
* @description Add a new authenticate type and the corresponding authenticator.
|
|
45
|
-
* The types will show in the authenticators list of the admin panel.
|
|
46
|
-
*
|
|
47
|
-
* @param authType - The type of the authenticator. It is required to be unique.
|
|
48
|
-
* @param authConfig - Configurations of the kind of authenticator.
|
|
49
|
-
*/
|
|
50
|
-
registerTypes(authType: string, authConfig: AuthConfig): void;
|
|
51
|
-
listTypes(): {
|
|
52
|
-
name: string;
|
|
53
|
-
title: string;
|
|
54
|
-
}[];
|
|
55
|
-
getAuthConfig(authType: string): AuthConfig;
|
|
56
|
-
/**
|
|
57
|
-
* get
|
|
58
|
-
* @description Get authenticator instance by name.
|
|
59
|
-
* @param name - The name of the authenticator.
|
|
60
|
-
* @return authenticator instance.
|
|
61
|
-
*/
|
|
62
|
-
get(name: string, ctx: Context): Promise<Auth>;
|
|
63
|
-
/**
|
|
64
|
-
* middleware
|
|
65
|
-
* @description Auth middleware, used to check the authentication status.
|
|
66
|
-
*/
|
|
67
|
-
middleware(): (ctx: Context & {
|
|
68
|
-
auth: Auth;
|
|
69
|
-
}, next: Next) => Promise<any>;
|
|
70
|
-
}
|
|
71
|
-
export {};
|
package/lib/auth-manager.js
DELETED
|
@@ -1,130 +0,0 @@
|
|
|
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 __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
15
|
-
var __export = (target, all) => {
|
|
16
|
-
for (var name in all)
|
|
17
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
18
|
-
};
|
|
19
|
-
var __copyProps = (to, from, except, desc) => {
|
|
20
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
21
|
-
for (let key of __getOwnPropNames(from))
|
|
22
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
23
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
24
|
-
}
|
|
25
|
-
return to;
|
|
26
|
-
};
|
|
27
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
-
var auth_manager_exports = {};
|
|
29
|
-
__export(auth_manager_exports, {
|
|
30
|
-
AuthManager: () => AuthManager
|
|
31
|
-
});
|
|
32
|
-
module.exports = __toCommonJS(auth_manager_exports);
|
|
33
|
-
var import_utils = require("@nocobase/utils");
|
|
34
|
-
var import_jwt_service = require("./base/jwt-service");
|
|
35
|
-
const _AuthManager = class _AuthManager {
|
|
36
|
-
/**
|
|
37
|
-
* @internal
|
|
38
|
-
*/
|
|
39
|
-
jwt;
|
|
40
|
-
options;
|
|
41
|
-
authTypes = new import_utils.Registry();
|
|
42
|
-
// authenticators collection manager.
|
|
43
|
-
storer;
|
|
44
|
-
constructor(options) {
|
|
45
|
-
this.options = options;
|
|
46
|
-
this.jwt = new import_jwt_service.JwtService(options.jwt);
|
|
47
|
-
}
|
|
48
|
-
setStorer(storer) {
|
|
49
|
-
this.storer = storer;
|
|
50
|
-
}
|
|
51
|
-
setTokenBlacklistService(service) {
|
|
52
|
-
this.jwt.blacklist = service;
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* registerTypes
|
|
56
|
-
* @description Add a new authenticate type and the corresponding authenticator.
|
|
57
|
-
* The types will show in the authenticators list of the admin panel.
|
|
58
|
-
*
|
|
59
|
-
* @param authType - The type of the authenticator. It is required to be unique.
|
|
60
|
-
* @param authConfig - Configurations of the kind of authenticator.
|
|
61
|
-
*/
|
|
62
|
-
registerTypes(authType, authConfig) {
|
|
63
|
-
this.authTypes.register(authType, authConfig);
|
|
64
|
-
}
|
|
65
|
-
listTypes() {
|
|
66
|
-
return Array.from(this.authTypes.getEntities()).map(([authType, authConfig]) => ({
|
|
67
|
-
name: authType,
|
|
68
|
-
title: authConfig.title
|
|
69
|
-
}));
|
|
70
|
-
}
|
|
71
|
-
getAuthConfig(authType) {
|
|
72
|
-
return this.authTypes.get(authType);
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* get
|
|
76
|
-
* @description Get authenticator instance by name.
|
|
77
|
-
* @param name - The name of the authenticator.
|
|
78
|
-
* @return authenticator instance.
|
|
79
|
-
*/
|
|
80
|
-
async get(name, ctx) {
|
|
81
|
-
if (!this.storer) {
|
|
82
|
-
throw new Error("AuthManager.storer is not set.");
|
|
83
|
-
}
|
|
84
|
-
const authenticator = await this.storer.get(name);
|
|
85
|
-
if (!authenticator) {
|
|
86
|
-
throw new Error(`Authenticator [${name}] is not found.`);
|
|
87
|
-
}
|
|
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
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
* middleware
|
|
96
|
-
* @description Auth middleware, used to check the authentication status.
|
|
97
|
-
*/
|
|
98
|
-
middleware() {
|
|
99
|
-
return async (ctx, next) => {
|
|
100
|
-
var _a;
|
|
101
|
-
const token = ctx.getBearerToken();
|
|
102
|
-
if (token && await ((_a = ctx.app.authManager.jwt.blacklist) == null ? void 0 : _a.has(token))) {
|
|
103
|
-
return ctx.throw(401, ctx.t("token is not available"));
|
|
104
|
-
}
|
|
105
|
-
const name = ctx.get(this.options.authKey) || this.options.default;
|
|
106
|
-
let authenticator;
|
|
107
|
-
try {
|
|
108
|
-
authenticator = await ctx.app.authManager.get(name, ctx);
|
|
109
|
-
ctx.auth = authenticator;
|
|
110
|
-
} catch (err) {
|
|
111
|
-
ctx.auth = {};
|
|
112
|
-
ctx.logger.warn(err.message, { method: "check", authenticator: name });
|
|
113
|
-
return next();
|
|
114
|
-
}
|
|
115
|
-
if (authenticator) {
|
|
116
|
-
const user = await ctx.auth.check();
|
|
117
|
-
if (user) {
|
|
118
|
-
ctx.auth.user = user;
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
await next();
|
|
122
|
-
};
|
|
123
|
-
}
|
|
124
|
-
};
|
|
125
|
-
__name(_AuthManager, "AuthManager");
|
|
126
|
-
let AuthManager = _AuthManager;
|
|
127
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
128
|
-
0 && (module.exports = {
|
|
129
|
-
AuthManager
|
|
130
|
-
});
|
package/lib/auth.d.ts
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
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
|
-
import { Context } from '@nocobase/actions';
|
|
10
|
-
import { Model } from '@nocobase/database';
|
|
11
|
-
import { Authenticator } from './auth-manager';
|
|
12
|
-
export type AuthConfig = {
|
|
13
|
-
authenticator: Authenticator;
|
|
14
|
-
options: {
|
|
15
|
-
[key: string]: any;
|
|
16
|
-
};
|
|
17
|
-
ctx: Context;
|
|
18
|
-
};
|
|
19
|
-
export type AuthExtend<T extends Auth> = new (config: AuthConfig) => T;
|
|
20
|
-
interface IAuth {
|
|
21
|
-
user: Model;
|
|
22
|
-
check(): Promise<Model>;
|
|
23
|
-
signIn(): Promise<any>;
|
|
24
|
-
signUp(): Promise<any>;
|
|
25
|
-
signOut(): Promise<any>;
|
|
26
|
-
}
|
|
27
|
-
export declare abstract class Auth implements IAuth {
|
|
28
|
-
abstract user: Model;
|
|
29
|
-
protected authenticator: Authenticator;
|
|
30
|
-
protected options: {
|
|
31
|
-
[key: string]: any;
|
|
32
|
-
};
|
|
33
|
-
protected ctx: Context;
|
|
34
|
-
constructor(config: AuthConfig);
|
|
35
|
-
abstract check(): Promise<Model>;
|
|
36
|
-
signIn(): Promise<any>;
|
|
37
|
-
signUp(): Promise<any>;
|
|
38
|
-
signOut(): Promise<any>;
|
|
39
|
-
}
|
|
40
|
-
export {};
|
package/lib/auth.js
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
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 __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
15
|
-
var __export = (target, all) => {
|
|
16
|
-
for (var name in all)
|
|
17
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
18
|
-
};
|
|
19
|
-
var __copyProps = (to, from, except, desc) => {
|
|
20
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
21
|
-
for (let key of __getOwnPropNames(from))
|
|
22
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
23
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
24
|
-
}
|
|
25
|
-
return to;
|
|
26
|
-
};
|
|
27
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
-
var auth_exports = {};
|
|
29
|
-
__export(auth_exports, {
|
|
30
|
-
Auth: () => Auth
|
|
31
|
-
});
|
|
32
|
-
module.exports = __toCommonJS(auth_exports);
|
|
33
|
-
const _Auth = class _Auth {
|
|
34
|
-
authenticator;
|
|
35
|
-
options;
|
|
36
|
-
ctx;
|
|
37
|
-
constructor(config) {
|
|
38
|
-
const { authenticator, options, ctx } = config;
|
|
39
|
-
this.authenticator = authenticator;
|
|
40
|
-
this.options = options;
|
|
41
|
-
this.ctx = ctx;
|
|
42
|
-
}
|
|
43
|
-
// The following methods are mainly designed for user authentications.
|
|
44
|
-
async signIn() {
|
|
45
|
-
}
|
|
46
|
-
async signUp() {
|
|
47
|
-
}
|
|
48
|
-
async signOut() {
|
|
49
|
-
}
|
|
50
|
-
};
|
|
51
|
-
__name(_Auth, "Auth");
|
|
52
|
-
let Auth = _Auth;
|
|
53
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
54
|
-
0 && (module.exports = {
|
|
55
|
-
Auth
|
|
56
|
-
});
|
package/lib/base/auth.d.ts
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
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
|
-
import { Collection, Model } from '@nocobase/database';
|
|
10
|
-
import { Auth, AuthConfig } from '../auth';
|
|
11
|
-
import { JwtService } from './jwt-service';
|
|
12
|
-
/**
|
|
13
|
-
* BaseAuth
|
|
14
|
-
* @description A base class with jwt provide some common methods.
|
|
15
|
-
*/
|
|
16
|
-
export declare class BaseAuth extends Auth {
|
|
17
|
-
protected userCollection: Collection;
|
|
18
|
-
constructor(config: AuthConfig & {
|
|
19
|
-
userCollection: Collection;
|
|
20
|
-
});
|
|
21
|
-
get userRepository(): import("@nocobase/database").Repository<any, any>;
|
|
22
|
-
/**
|
|
23
|
-
* @internal
|
|
24
|
-
*/
|
|
25
|
-
get jwt(): JwtService;
|
|
26
|
-
set user(user: Model);
|
|
27
|
-
get user(): Model;
|
|
28
|
-
/**
|
|
29
|
-
* @internal
|
|
30
|
-
*/
|
|
31
|
-
getCacheKey(userId: number): string;
|
|
32
|
-
/**
|
|
33
|
-
* @internal
|
|
34
|
-
*/
|
|
35
|
-
validateUsername(username: string): boolean;
|
|
36
|
-
check(): Promise<any>;
|
|
37
|
-
validate(): Promise<Model>;
|
|
38
|
-
signIn(): Promise<{
|
|
39
|
-
user: Model<any, any>;
|
|
40
|
-
token: string;
|
|
41
|
-
}>;
|
|
42
|
-
signOut(): Promise<any>;
|
|
43
|
-
}
|
package/lib/base/auth.js
DELETED
|
@@ -1,130 +0,0 @@
|
|
|
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 __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
15
|
-
var __export = (target, all) => {
|
|
16
|
-
for (var name in all)
|
|
17
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
18
|
-
};
|
|
19
|
-
var __copyProps = (to, from, except, desc) => {
|
|
20
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
21
|
-
for (let key of __getOwnPropNames(from))
|
|
22
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
23
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
24
|
-
}
|
|
25
|
-
return to;
|
|
26
|
-
};
|
|
27
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
-
var auth_exports = {};
|
|
29
|
-
__export(auth_exports, {
|
|
30
|
-
BaseAuth: () => BaseAuth
|
|
31
|
-
});
|
|
32
|
-
module.exports = __toCommonJS(auth_exports);
|
|
33
|
-
var import_auth = require("../auth");
|
|
34
|
-
const _BaseAuth = class _BaseAuth extends import_auth.Auth {
|
|
35
|
-
userCollection;
|
|
36
|
-
constructor(config) {
|
|
37
|
-
const { userCollection } = config;
|
|
38
|
-
super(config);
|
|
39
|
-
this.userCollection = userCollection;
|
|
40
|
-
}
|
|
41
|
-
get userRepository() {
|
|
42
|
-
return this.userCollection.repository;
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* @internal
|
|
46
|
-
*/
|
|
47
|
-
get jwt() {
|
|
48
|
-
return this.ctx.app.authManager.jwt;
|
|
49
|
-
}
|
|
50
|
-
set user(user) {
|
|
51
|
-
this.ctx.state.currentUser = user;
|
|
52
|
-
}
|
|
53
|
-
get user() {
|
|
54
|
-
return this.ctx.state.currentUser;
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* @internal
|
|
58
|
-
*/
|
|
59
|
-
getCacheKey(userId) {
|
|
60
|
-
return `auth:${userId}`;
|
|
61
|
-
}
|
|
62
|
-
/**
|
|
63
|
-
* @internal
|
|
64
|
-
*/
|
|
65
|
-
validateUsername(username) {
|
|
66
|
-
return /^[^@.<>"'/]{1,50}$/.test(username);
|
|
67
|
-
}
|
|
68
|
-
async check() {
|
|
69
|
-
const token = this.ctx.getBearerToken();
|
|
70
|
-
if (!token) {
|
|
71
|
-
return null;
|
|
72
|
-
}
|
|
73
|
-
try {
|
|
74
|
-
const { userId, roleName } = await this.jwt.decode(token);
|
|
75
|
-
if (roleName) {
|
|
76
|
-
this.ctx.headers["x-role"] = roleName;
|
|
77
|
-
}
|
|
78
|
-
const cache = this.ctx.cache;
|
|
79
|
-
return await cache.wrap(
|
|
80
|
-
this.getCacheKey(userId),
|
|
81
|
-
() => this.userRepository.findOne({
|
|
82
|
-
filter: {
|
|
83
|
-
id: userId
|
|
84
|
-
},
|
|
85
|
-
raw: true
|
|
86
|
-
})
|
|
87
|
-
);
|
|
88
|
-
} catch (err) {
|
|
89
|
-
this.ctx.logger.error(err, { method: "check" });
|
|
90
|
-
return null;
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
async validate() {
|
|
94
|
-
return null;
|
|
95
|
-
}
|
|
96
|
-
async signIn() {
|
|
97
|
-
let user;
|
|
98
|
-
try {
|
|
99
|
-
user = await this.validate();
|
|
100
|
-
} catch (err) {
|
|
101
|
-
this.ctx.throw(err.status || 401, err.message);
|
|
102
|
-
}
|
|
103
|
-
if (!user) {
|
|
104
|
-
this.ctx.throw(401, "Unauthorized");
|
|
105
|
-
}
|
|
106
|
-
const token = this.jwt.sign({
|
|
107
|
-
userId: user.id
|
|
108
|
-
});
|
|
109
|
-
return {
|
|
110
|
-
user,
|
|
111
|
-
token
|
|
112
|
-
};
|
|
113
|
-
}
|
|
114
|
-
async signOut() {
|
|
115
|
-
const token = this.ctx.getBearerToken();
|
|
116
|
-
if (!token) {
|
|
117
|
-
return;
|
|
118
|
-
}
|
|
119
|
-
const { userId } = await this.jwt.decode(token);
|
|
120
|
-
await this.ctx.app.emitAsync("beforeSignOut", { userId });
|
|
121
|
-
await this.ctx.cache.del(this.getCacheKey(userId));
|
|
122
|
-
return await this.jwt.block(token);
|
|
123
|
-
}
|
|
124
|
-
};
|
|
125
|
-
__name(_BaseAuth, "BaseAuth");
|
|
126
|
-
let BaseAuth = _BaseAuth;
|
|
127
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
128
|
-
0 && (module.exports = {
|
|
129
|
-
BaseAuth
|
|
130
|
-
});
|
|
@@ -1,28 +0,0 @@
|
|
|
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
|
-
import jwt, { SignOptions } from 'jsonwebtoken';
|
|
10
|
-
import { ITokenBlacklistService } from './token-blacklist-service';
|
|
11
|
-
export interface JwtOptions {
|
|
12
|
-
secret: string;
|
|
13
|
-
expiresIn?: string;
|
|
14
|
-
}
|
|
15
|
-
export type SignPayload = Parameters<typeof jwt.sign>[0];
|
|
16
|
-
export declare class JwtService {
|
|
17
|
-
protected options: JwtOptions;
|
|
18
|
-
constructor(options?: JwtOptions);
|
|
19
|
-
blacklist: ITokenBlacklistService;
|
|
20
|
-
private expiresIn;
|
|
21
|
-
private secret;
|
|
22
|
-
sign(payload: SignPayload, options?: SignOptions): string;
|
|
23
|
-
decode(token: string): Promise<any>;
|
|
24
|
-
/**
|
|
25
|
-
* @description Block a token so that this token can no longer be used
|
|
26
|
-
*/
|
|
27
|
-
block(token: string): Promise<any>;
|
|
28
|
-
}
|
package/lib/base/jwt-service.js
DELETED
|
@@ -1,104 +0,0 @@
|
|
|
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 __create = Object.create;
|
|
11
|
-
var __defProp = Object.defineProperty;
|
|
12
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
13
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
14
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
15
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
16
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
17
|
-
var __export = (target, all) => {
|
|
18
|
-
for (var name in all)
|
|
19
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
20
|
-
};
|
|
21
|
-
var __copyProps = (to, from, except, desc) => {
|
|
22
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
23
|
-
for (let key of __getOwnPropNames(from))
|
|
24
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
25
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
26
|
-
}
|
|
27
|
-
return to;
|
|
28
|
-
};
|
|
29
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
30
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
31
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
32
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
33
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
34
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
35
|
-
mod
|
|
36
|
-
));
|
|
37
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
38
|
-
var jwt_service_exports = {};
|
|
39
|
-
__export(jwt_service_exports, {
|
|
40
|
-
JwtService: () => JwtService
|
|
41
|
-
});
|
|
42
|
-
module.exports = __toCommonJS(jwt_service_exports);
|
|
43
|
-
var import_jsonwebtoken = __toESM(require("jsonwebtoken"));
|
|
44
|
-
const _JwtService = class _JwtService {
|
|
45
|
-
constructor(options = {
|
|
46
|
-
secret: process.env.APP_KEY
|
|
47
|
-
}) {
|
|
48
|
-
this.options = options;
|
|
49
|
-
const { secret, expiresIn } = options;
|
|
50
|
-
this.options = {
|
|
51
|
-
secret,
|
|
52
|
-
expiresIn: expiresIn || process.env.JWT_EXPIRES_IN || "7d"
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
blacklist;
|
|
56
|
-
expiresIn() {
|
|
57
|
-
return this.options.expiresIn;
|
|
58
|
-
}
|
|
59
|
-
secret() {
|
|
60
|
-
return this.options.secret;
|
|
61
|
-
}
|
|
62
|
-
/* istanbul ignore next -- @preserve */
|
|
63
|
-
sign(payload, options) {
|
|
64
|
-
const opt = { expiresIn: this.expiresIn(), ...options };
|
|
65
|
-
if (opt.expiresIn === "never") {
|
|
66
|
-
opt.expiresIn = "1000y";
|
|
67
|
-
}
|
|
68
|
-
return import_jsonwebtoken.default.sign(payload, this.secret(), opt);
|
|
69
|
-
}
|
|
70
|
-
/* istanbul ignore next -- @preserve */
|
|
71
|
-
decode(token) {
|
|
72
|
-
return new Promise((resolve, reject) => {
|
|
73
|
-
import_jsonwebtoken.default.verify(token, this.secret(), (err, decoded) => {
|
|
74
|
-
if (err) {
|
|
75
|
-
return reject(err);
|
|
76
|
-
}
|
|
77
|
-
resolve(decoded);
|
|
78
|
-
});
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
/**
|
|
82
|
-
* @description Block a token so that this token can no longer be used
|
|
83
|
-
*/
|
|
84
|
-
async block(token) {
|
|
85
|
-
if (!this.blacklist) {
|
|
86
|
-
return null;
|
|
87
|
-
}
|
|
88
|
-
try {
|
|
89
|
-
const { exp } = await this.decode(token);
|
|
90
|
-
return this.blacklist.add({
|
|
91
|
-
token,
|
|
92
|
-
expiration: new Date(exp * 1e3).toString()
|
|
93
|
-
});
|
|
94
|
-
} catch {
|
|
95
|
-
return null;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
};
|
|
99
|
-
__name(_JwtService, "JwtService");
|
|
100
|
-
let JwtService = _JwtService;
|
|
101
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
102
|
-
0 && (module.exports = {
|
|
103
|
-
JwtService
|
|
104
|
-
});
|
|
@@ -1,15 +0,0 @@
|
|
|
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 interface ITokenBlacklistService {
|
|
10
|
-
has(token: string): Promise<boolean>;
|
|
11
|
-
add(values: {
|
|
12
|
-
token: string;
|
|
13
|
-
expiration: string | Date;
|
|
14
|
-
}): Promise<any>;
|
|
15
|
-
}
|
|
@@ -1,24 +0,0 @@
|
|
|
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 __copyProps = (to, from, except, desc) => {
|
|
15
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
16
|
-
for (let key of __getOwnPropNames(from))
|
|
17
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
18
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
19
|
-
}
|
|
20
|
-
return to;
|
|
21
|
-
};
|
|
22
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
23
|
-
var token_blacklist_service_exports = {};
|
|
24
|
-
module.exports = __toCommonJS(token_blacklist_service_exports);
|
package/lib/index.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
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 * from './actions';
|
|
10
|
-
export * from './auth';
|
|
11
|
-
export * from './auth-manager';
|
|
12
|
-
export * from './base/auth';
|
|
13
|
-
export * from './base/token-blacklist-service';
|
package/lib/index.js
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
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 __copyProps = (to, from, except, desc) => {
|
|
15
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
16
|
-
for (let key of __getOwnPropNames(from))
|
|
17
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
18
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
19
|
-
}
|
|
20
|
-
return to;
|
|
21
|
-
};
|
|
22
|
-
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
23
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
24
|
-
var src_exports = {};
|
|
25
|
-
module.exports = __toCommonJS(src_exports);
|
|
26
|
-
__reExport(src_exports, require("./actions"), module.exports);
|
|
27
|
-
__reExport(src_exports, require("./auth"), module.exports);
|
|
28
|
-
__reExport(src_exports, require("./auth-manager"), module.exports);
|
|
29
|
-
__reExport(src_exports, require("./base/auth"), module.exports);
|
|
30
|
-
__reExport(src_exports, require("./base/token-blacklist-service"), module.exports);
|
|
31
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
32
|
-
0 && (module.exports = {
|
|
33
|
-
...require("./actions"),
|
|
34
|
-
...require("./auth"),
|
|
35
|
-
...require("./auth-manager"),
|
|
36
|
-
...require("./base/auth"),
|
|
37
|
-
...require("./base/token-blacklist-service")
|
|
38
|
-
});
|