@tachybase/module-auth 0.23.8
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/.turbo/turbo-build.log +14 -0
- package/README.md +117 -0
- package/client.d.ts +2 -0
- package/client.js +65 -0
- package/dist/client/AuthProvider.d.ts +2 -0
- package/dist/client/authenticator.d.ts +12 -0
- package/dist/client/basic/Options.d.ts +2 -0
- package/dist/client/basic/SignInForm.d.ts +9 -0
- package/dist/client/basic/SignUpForm.d.ts +13 -0
- package/dist/client/basic/index.d.ts +3 -0
- package/dist/client/bind/AuthenticatorBind.d.ts +2 -0
- package/dist/client/bind/BindForm.d.ts +5 -0
- package/dist/client/bind/schemas/authenticators.d.ts +2 -0
- package/dist/client/index.d.ts +30 -0
- package/dist/client/index.js +22 -0
- package/dist/client/locale/index.d.ts +2 -0
- package/dist/client/pages/AuthLayout.d.ts +2 -0
- package/dist/client/pages/SignInPage.d.ts +13 -0
- package/dist/client/pages/SignUpPage.d.ts +20 -0
- package/dist/client/pages/index.d.ts +3 -0
- package/dist/client/settings/Authenticator.d.ts +2 -0
- package/dist/client/settings/Options.d.ts +4 -0
- package/dist/client/settings/authType.d.ts +15 -0
- package/dist/client/settings/schemas/authenticators.d.ts +3 -0
- package/dist/externalVersion.js +16 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +40 -0
- package/dist/locale/en-US.json +31 -0
- package/dist/locale/ko_KR.json +26 -0
- package/dist/locale/zh-CN.json +31 -0
- package/dist/node_modules/cron/LICENSE +8 -0
- package/dist/node_modules/cron/dist/constants.d.ts +74 -0
- package/dist/node_modules/cron/dist/constants.js +78 -0
- package/dist/node_modules/cron/dist/errors.d.ts +5 -0
- package/dist/node_modules/cron/dist/errors.js +13 -0
- package/dist/node_modules/cron/dist/index.d.ts +6 -0
- package/dist/node_modules/cron/dist/index.js +1 -0
- package/dist/node_modules/cron/dist/job.d.ts +30 -0
- package/dist/node_modules/cron/dist/job.js +222 -0
- package/dist/node_modules/cron/dist/time.d.ts +33 -0
- package/dist/node_modules/cron/dist/time.js +444 -0
- package/dist/node_modules/cron/dist/types/cron.types.d.ts +50 -0
- package/dist/node_modules/cron/dist/types/cron.types.js +3 -0
- package/dist/node_modules/cron/dist/types/utils.d.ts +3 -0
- package/dist/node_modules/cron/dist/types/utils.js +3 -0
- package/dist/node_modules/cron/dist/utils.d.ts +2 -0
- package/dist/node_modules/cron/dist/utils.js +8 -0
- package/dist/node_modules/cron/package.json +1 -0
- package/dist/preset.d.ts +3 -0
- package/dist/preset.js +34 -0
- package/dist/server/actions/auth.d.ts +8 -0
- package/dist/server/actions/auth.js +40 -0
- package/dist/server/actions/authenticators.d.ts +10 -0
- package/dist/server/actions/authenticators.js +153 -0
- package/dist/server/basic-auth.d.ts +10 -0
- package/dist/server/basic-auth.js +176 -0
- package/dist/server/collections/authenticators.d.ts +5 -0
- package/dist/server/collections/authenticators.js +117 -0
- package/dist/server/collections/token-blacklist.d.ts +2 -0
- package/dist/server/collections/token-blacklist.js +42 -0
- package/dist/server/collections/users-authenticators.d.ts +6 -0
- package/dist/server/collections/users-authenticators.js +85 -0
- package/dist/server/index.d.ts +3 -0
- package/dist/server/index.js +42 -0
- package/dist/server/locale/en-US.d.ts +9 -0
- package/dist/server/locale/en-US.js +30 -0
- package/dist/server/locale/fr-FR.d.ts +9 -0
- package/dist/server/locale/fr-FR.js +30 -0
- package/dist/server/locale/index.d.ts +3 -0
- package/dist/server/locale/index.js +43 -0
- package/dist/server/locale/ja-JP.d.ts +5 -0
- package/dist/server/locale/ja-JP.js +26 -0
- package/dist/server/locale/pt-BR.d.ts +9 -0
- package/dist/server/locale/pt-BR.js +30 -0
- package/dist/server/locale/zh-CN.d.ts +11 -0
- package/dist/server/locale/zh-CN.js +32 -0
- package/dist/server/migrations/20230506152253-basic-authenticator.d.ts +6 -0
- package/dist/server/migrations/20230506152253-basic-authenticator.js +44 -0
- package/dist/server/migrations/20230607174500-update-basic.d.ts +6 -0
- package/dist/server/migrations/20230607174500-update-basic.js +47 -0
- package/dist/server/migrations/20231218132032-fix-allow-signup.d.ts +6 -0
- package/dist/server/migrations/20231218132032-fix-allow-signup.js +55 -0
- package/dist/server/model/authenticator.d.ts +10 -0
- package/dist/server/model/authenticator.js +81 -0
- package/dist/server/plugin.d.ts +11 -0
- package/dist/server/plugin.js +132 -0
- package/dist/server/storer.d.ts +16 -0
- package/dist/server/storer.js +69 -0
- package/dist/server/token-blacklist.d.ts +17 -0
- package/dist/server/token-blacklist.js +86 -0
- package/dist/swagger/index.d.ts +754 -0
- package/dist/swagger/index.js +787 -0
- package/package.json +34 -0
- package/server.d.ts +2 -0
- package/server.js +65 -0
|
@@ -0,0 +1,81 @@
|
|
|
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 authenticator_exports = {};
|
|
19
|
+
__export(authenticator_exports, {
|
|
20
|
+
AuthModel: () => AuthModel
|
|
21
|
+
});
|
|
22
|
+
module.exports = __toCommonJS(authenticator_exports);
|
|
23
|
+
var import_database = require("@tachybase/database");
|
|
24
|
+
class AuthModel extends import_database.Model {
|
|
25
|
+
async findUser(uuid) {
|
|
26
|
+
let user;
|
|
27
|
+
const users = await this.getUsers({
|
|
28
|
+
through: {
|
|
29
|
+
where: { uuid }
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
if (users.length) {
|
|
33
|
+
user = users[0];
|
|
34
|
+
return user;
|
|
35
|
+
}
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
async newUser(uuid, userValues) {
|
|
39
|
+
let user;
|
|
40
|
+
const db = this.constructor.database;
|
|
41
|
+
await this.sequelize.transaction(async (transaction) => {
|
|
42
|
+
user = await this.createUser(
|
|
43
|
+
userValues || {
|
|
44
|
+
nickname: uuid
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
through: {
|
|
48
|
+
uuid
|
|
49
|
+
},
|
|
50
|
+
transaction
|
|
51
|
+
}
|
|
52
|
+
);
|
|
53
|
+
await db.emitAsync(`users.afterCreateWithAssociations`, user, {
|
|
54
|
+
transaction
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
return user;
|
|
58
|
+
}
|
|
59
|
+
async findOrCreateUser(uuid, userValues) {
|
|
60
|
+
const user = await this.findUser(uuid);
|
|
61
|
+
if (user) {
|
|
62
|
+
return user;
|
|
63
|
+
}
|
|
64
|
+
return await this.newUser(uuid, userValues);
|
|
65
|
+
}
|
|
66
|
+
async bindUser(userId, uuid, info) {
|
|
67
|
+
await this.sequelize.transaction(async (transaction) => {
|
|
68
|
+
await this.addUser(userId, {
|
|
69
|
+
through: {
|
|
70
|
+
uuid,
|
|
71
|
+
...info
|
|
72
|
+
},
|
|
73
|
+
transaction
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
79
|
+
0 && (module.exports = {
|
|
80
|
+
AuthModel
|
|
81
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Cache } from '@tachybase/cache';
|
|
2
|
+
import { InstallOptions, Plugin } from '@tachybase/server';
|
|
3
|
+
export declare class PluginAuthServer extends Plugin {
|
|
4
|
+
cache: Cache;
|
|
5
|
+
afterAdd(): void;
|
|
6
|
+
beforeLoad(): Promise<void>;
|
|
7
|
+
load(): Promise<void>;
|
|
8
|
+
install(options?: InstallOptions): Promise<void>;
|
|
9
|
+
remove(): Promise<void>;
|
|
10
|
+
}
|
|
11
|
+
export default PluginAuthServer;
|
|
@@ -0,0 +1,132 @@
|
|
|
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 __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
var plugin_exports = {};
|
|
29
|
+
__export(plugin_exports, {
|
|
30
|
+
PluginAuthServer: () => PluginAuthServer,
|
|
31
|
+
default: () => plugin_default
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(plugin_exports);
|
|
34
|
+
var import_path = require("path");
|
|
35
|
+
var import_server = require("@tachybase/server");
|
|
36
|
+
var import_preset = require("../preset");
|
|
37
|
+
var import_auth = __toESM(require("./actions/auth"));
|
|
38
|
+
var import_authenticators = __toESM(require("./actions/authenticators"));
|
|
39
|
+
var import_basic_auth = require("./basic-auth");
|
|
40
|
+
var import_locale = require("./locale");
|
|
41
|
+
var import_authenticator = require("./model/authenticator");
|
|
42
|
+
var import_storer = require("./storer");
|
|
43
|
+
var import_token_blacklist = require("./token-blacklist");
|
|
44
|
+
class PluginAuthServer extends import_server.Plugin {
|
|
45
|
+
cache;
|
|
46
|
+
afterAdd() {
|
|
47
|
+
}
|
|
48
|
+
async beforeLoad() {
|
|
49
|
+
this.app.i18n.addResources("zh-CN", import_preset.namespace, import_locale.zhCN);
|
|
50
|
+
this.app.i18n.addResources("en-US", import_preset.namespace, import_locale.enUS);
|
|
51
|
+
this.app.db.registerModels({ AuthModel: import_authenticator.AuthModel });
|
|
52
|
+
}
|
|
53
|
+
async load() {
|
|
54
|
+
await this.importCollections((0, import_path.resolve)(__dirname, "collections"));
|
|
55
|
+
this.db.addMigrations({
|
|
56
|
+
namespace: "auth",
|
|
57
|
+
directory: (0, import_path.resolve)(__dirname, "migrations"),
|
|
58
|
+
context: {
|
|
59
|
+
plugin: this
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
this.cache = await this.app.cacheManager.createCache({
|
|
63
|
+
name: "auth",
|
|
64
|
+
prefix: "auth",
|
|
65
|
+
store: "memory"
|
|
66
|
+
});
|
|
67
|
+
const storer = new import_storer.Storer({
|
|
68
|
+
db: this.db,
|
|
69
|
+
cache: this.cache
|
|
70
|
+
});
|
|
71
|
+
this.app.authManager.setStorer(storer);
|
|
72
|
+
if (!this.app.authManager.jwt.blacklist) {
|
|
73
|
+
this.app.authManager.setTokenBlacklistService(new import_token_blacklist.TokenBlacklistService(this));
|
|
74
|
+
}
|
|
75
|
+
this.app.authManager.registerTypes(import_preset.presetAuthType, {
|
|
76
|
+
auth: import_basic_auth.BasicAuth,
|
|
77
|
+
title: "Password"
|
|
78
|
+
});
|
|
79
|
+
Object.entries(import_auth.default).forEach(
|
|
80
|
+
([action, handler]) => {
|
|
81
|
+
var _a;
|
|
82
|
+
return (_a = this.app.resourcer.getResource("auth")) == null ? void 0 : _a.addAction(action, handler);
|
|
83
|
+
}
|
|
84
|
+
);
|
|
85
|
+
Object.entries(import_authenticators.default).forEach(
|
|
86
|
+
([action, handler]) => this.app.resourcer.registerAction(`authenticators:${action}`, handler)
|
|
87
|
+
);
|
|
88
|
+
["check", "signIn", "signUp"].forEach((action) => this.app.acl.allow("auth", action));
|
|
89
|
+
["signOut", "changePassword"].forEach((action) => this.app.acl.allow("auth", action, "loggedIn"));
|
|
90
|
+
this.app.acl.allow("authenticators", "publicList");
|
|
91
|
+
this.app.acl.allow("authenticators", "bindTypes", "loggedIn");
|
|
92
|
+
this.app.acl.registerSnippet({
|
|
93
|
+
name: `pm.${this.name}.authenticators`,
|
|
94
|
+
actions: ["authenticators:*"]
|
|
95
|
+
});
|
|
96
|
+
this.app.db.on("users.afterSave", async (user) => {
|
|
97
|
+
const cache = this.app.cache;
|
|
98
|
+
await cache.set(`auth:${user.id}`, user.toJSON());
|
|
99
|
+
});
|
|
100
|
+
this.app.db.on("users.afterDestroy", async (user) => {
|
|
101
|
+
const cache = this.app.cache;
|
|
102
|
+
await cache.del(`auth:${user.id}`);
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
async install(options) {
|
|
106
|
+
const repository = this.db.getRepository("authenticators");
|
|
107
|
+
const exist = await repository.findOne({ filter: { name: import_preset.presetAuthenticator } });
|
|
108
|
+
if (exist) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
await repository.create({
|
|
112
|
+
values: {
|
|
113
|
+
name: import_preset.presetAuthenticator,
|
|
114
|
+
authType: import_preset.presetAuthType,
|
|
115
|
+
description: "Sign in with username/email.",
|
|
116
|
+
enabled: true,
|
|
117
|
+
options: {
|
|
118
|
+
public: {
|
|
119
|
+
allowSignUp: true
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
async remove() {
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
var plugin_default = PluginAuthServer;
|
|
129
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
130
|
+
0 && (module.exports = {
|
|
131
|
+
PluginAuthServer
|
|
132
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Storer as IStorer } from '@tachybase/auth';
|
|
2
|
+
import { Cache } from '@tachybase/cache';
|
|
3
|
+
import { Database } from '@tachybase/database';
|
|
4
|
+
import { AuthModel } from './model/authenticator';
|
|
5
|
+
export declare class Storer implements IStorer {
|
|
6
|
+
db: Database;
|
|
7
|
+
cache: Cache;
|
|
8
|
+
key: string;
|
|
9
|
+
constructor({ db, cache }: {
|
|
10
|
+
db: Database;
|
|
11
|
+
cache: Cache;
|
|
12
|
+
});
|
|
13
|
+
getCache(): Promise<AuthModel[]>;
|
|
14
|
+
setCache(authenticators: AuthModel[]): Promise<void>;
|
|
15
|
+
get(name: string): Promise<AuthModel>;
|
|
16
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
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 storer_exports = {};
|
|
19
|
+
__export(storer_exports, {
|
|
20
|
+
Storer: () => Storer
|
|
21
|
+
});
|
|
22
|
+
module.exports = __toCommonJS(storer_exports);
|
|
23
|
+
class Storer {
|
|
24
|
+
db;
|
|
25
|
+
cache;
|
|
26
|
+
key = "authenticators";
|
|
27
|
+
constructor({ db, cache }) {
|
|
28
|
+
this.db = db;
|
|
29
|
+
this.cache = cache;
|
|
30
|
+
this.db.on("authenticators.afterSave", async (model) => {
|
|
31
|
+
if (!model.enabled) {
|
|
32
|
+
await this.cache.delValueInObject(this.key, model.name);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
await this.cache.setValueInObject(this.key, model.name, model);
|
|
36
|
+
});
|
|
37
|
+
this.db.on("authenticators.afterDestroy", async (model) => {
|
|
38
|
+
await this.cache.delValueInObject(this.key, model.name);
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
async getCache() {
|
|
42
|
+
const authenticators = await this.cache.get(this.key);
|
|
43
|
+
if (!authenticators) {
|
|
44
|
+
return [];
|
|
45
|
+
}
|
|
46
|
+
return Object.values(authenticators);
|
|
47
|
+
}
|
|
48
|
+
async setCache(authenticators) {
|
|
49
|
+
const obj = authenticators.reduce((obj2, authenticator) => {
|
|
50
|
+
obj2[authenticator.name] = authenticator;
|
|
51
|
+
return obj2;
|
|
52
|
+
}, {});
|
|
53
|
+
await this.cache.set(this.key, obj);
|
|
54
|
+
}
|
|
55
|
+
async get(name) {
|
|
56
|
+
let authenticators = await this.getCache();
|
|
57
|
+
if (!authenticators.length) {
|
|
58
|
+
const repo = this.db.getRepository("authenticators");
|
|
59
|
+
authenticators = await repo.find({ filter: { enabled: true } });
|
|
60
|
+
await this.setCache(authenticators);
|
|
61
|
+
}
|
|
62
|
+
const authenticator = authenticators.find((authenticator2) => authenticator2.name === name);
|
|
63
|
+
return authenticator || authenticators[0];
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
67
|
+
0 && (module.exports = {
|
|
68
|
+
Storer
|
|
69
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { ITokenBlacklistService } from '@tachybase/auth';
|
|
2
|
+
import { BloomFilter } from '@tachybase/cache';
|
|
3
|
+
import { Repository } from '@tachybase/database';
|
|
4
|
+
import { CronJob } from 'cron';
|
|
5
|
+
import AuthPlugin from './plugin';
|
|
6
|
+
export declare class TokenBlacklistService implements ITokenBlacklistService {
|
|
7
|
+
protected plugin: AuthPlugin;
|
|
8
|
+
repo: Repository;
|
|
9
|
+
cronJob: CronJob;
|
|
10
|
+
bloomFilter: BloomFilter;
|
|
11
|
+
cacheKey: string;
|
|
12
|
+
constructor(plugin: AuthPlugin);
|
|
13
|
+
get app(): import("packages/server/lib").default<import("packages/server/lib").DefaultState, import("packages/server/lib").DefaultContext>;
|
|
14
|
+
has(token: string): Promise<boolean>;
|
|
15
|
+
add(values: any): Promise<[import("@tachybase/database").Model<any, any>, boolean]>;
|
|
16
|
+
deleteExpiredTokens(): Promise<any>;
|
|
17
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
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 token_blacklist_exports = {};
|
|
19
|
+
__export(token_blacklist_exports, {
|
|
20
|
+
TokenBlacklistService: () => TokenBlacklistService
|
|
21
|
+
});
|
|
22
|
+
module.exports = __toCommonJS(token_blacklist_exports);
|
|
23
|
+
class TokenBlacklistService {
|
|
24
|
+
constructor(plugin) {
|
|
25
|
+
this.plugin = plugin;
|
|
26
|
+
this.repo = plugin.db.getRepository("tokenBlacklist");
|
|
27
|
+
plugin.app.on("beforeStart", async () => {
|
|
28
|
+
try {
|
|
29
|
+
this.bloomFilter = await plugin.app.cacheManager.createBloomFilter();
|
|
30
|
+
await this.bloomFilter.reserve(this.cacheKey, 1e-3, 1e6);
|
|
31
|
+
const data = await this.repo.find({ fields: ["token"], raw: true });
|
|
32
|
+
const tokens = data.map((item) => item.token);
|
|
33
|
+
await this.bloomFilter.mAdd(this.cacheKey, tokens);
|
|
34
|
+
} catch (error) {
|
|
35
|
+
plugin.app.logger.error("token-blacklist: create bloom filter failed", error);
|
|
36
|
+
this.bloomFilter = null;
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
repo;
|
|
41
|
+
cronJob;
|
|
42
|
+
bloomFilter;
|
|
43
|
+
cacheKey = "token-black-list";
|
|
44
|
+
get app() {
|
|
45
|
+
return this.plugin.app;
|
|
46
|
+
}
|
|
47
|
+
async has(token) {
|
|
48
|
+
if (this.bloomFilter) {
|
|
49
|
+
const exists = await this.bloomFilter.exists(this.cacheKey, token);
|
|
50
|
+
if (!exists) {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return !!await this.repo.findOne({
|
|
55
|
+
filter: {
|
|
56
|
+
token
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
async add(values) {
|
|
61
|
+
await this.deleteExpiredTokens();
|
|
62
|
+
const { token } = values;
|
|
63
|
+
if (this.bloomFilter) {
|
|
64
|
+
await this.bloomFilter.add(this.cacheKey, token);
|
|
65
|
+
}
|
|
66
|
+
return this.repo.model.findOrCreate({
|
|
67
|
+
defaults: values,
|
|
68
|
+
where: {
|
|
69
|
+
token
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
async deleteExpiredTokens() {
|
|
74
|
+
return this.repo.destroy({
|
|
75
|
+
filter: {
|
|
76
|
+
expiration: {
|
|
77
|
+
$dateNotAfter: /* @__PURE__ */ new Date()
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
84
|
+
0 && (module.exports = {
|
|
85
|
+
TokenBlacklistService
|
|
86
|
+
});
|