tachybase_todo/packages/core/auth/src/auth-manager.ts
YANG QIA 70d5b9e44b
feat: localization management (#2210)
* feat: init localization-management

* feat: resource api

* Merge branch 'main' into T-62

* chore: change name

* feat: basic feature

* feat: support filter & sync

* feat: support auto get texts afterSave

* Merge branch 'main' into T-62

* chore: upgrade

* fix: dependency

* fix: field type

* fix: type error

* chore: remove some translations

* feat: support extract text from menu

* chore: cache text keys

* chore: remove test key

* fix: issue of extracting menu titles

* feat: translate collections & fields name

* fix: remove unique of text

* refactor: improve cache

* chore: remove listeners after disable

* chore: translation

* fix: lang switch bug

* refactor: actions & filter

* fix: translation

* refactor: merge lang bundles at backend

* fix: style & field name

* fix: translate issues

* fix: cache bug

* fix: translation merge bug

* fix: translate issues

* fix: map translation

* fix: translation issues

* fix: card title bug

* feat: cover mobile client tabbar

* fix: menu title

* refactor: add locale plugin

* chore: merge locale plugin

* fix: map translation

* chore: remove no data

* style: change button style

* fix: sync bug

* docs: add README

* chore: change name

---------

Co-authored-by: chenos <chenlinxh@gmail.com>
2023-07-17 23:23:44 +08:00

114 lines
3.2 KiB
TypeScript

import { Context, Next } from '@nocobase/actions';
import { Model } from '@nocobase/database';
import { Registry } from '@nocobase/utils';
import { Auth, AuthExtend } from './auth';
import { JwtOptions, JwtService } from './base/jwt-service';
import { ITokenBlacklistService } from './base/token-blacklist-service';
type Storer = {
get: (name: string) => Promise<Model>;
};
type AuthManagerOptions = {
authKey: string;
default?: string;
jwt?: JwtOptions;
};
type AuthConfig = {
auth: AuthExtend<Auth>; // The authentication class.
};
export class AuthManager {
protected options: AuthManagerOptions;
protected authTypes: Registry<AuthConfig> = new Registry();
// authenticators collection manager.
protected storer: Storer;
jwt: JwtService;
constructor(options: AuthManagerOptions) {
this.options = options;
this.jwt = new JwtService(options.jwt);
}
setStorer(storer: Storer) {
this.storer = storer;
}
setTokenBlacklistService(service: ITokenBlacklistService) {
this.jwt.blacklist = service;
}
/**
* registerTypes
* @description Add a new authenticate type and the corresponding authenticator.
* The types will show in the authenticators list of the admin panel.
*
* @param {string} authType - The type of the authenticator. It is required to be unique.
* @param {AuthConfig} authConfig - Configurations of the kind of authenticator.
*/
registerTypes(authType: string, authConfig: AuthConfig) {
this.authTypes.register(authType, authConfig);
}
listTypes() {
return Array.from(this.authTypes.getKeys());
}
getAuthConfig(authType: string) {
return this.authTypes.get(authType);
}
/**
* get
* @description Get authenticator instance by name.
* @param {string} name - The name of the authenticator.
* @return {Promise<Auth>} authenticator instance.
*/
async get(name: string, ctx: Context) {
if (!this.storer) {
throw new Error('AuthManager.storer is not set.');
}
const authenticator = await this.storer.get(name);
if (!authenticator) {
throw new Error(`Authenticator [${name}] is not found.`);
}
const { auth } = this.authTypes.get(authenticator.authType);
if (!auth) {
throw new Error(`AuthType [${name}] is not found.`);
}
return new auth({ authenticator, options: authenticator.options, ctx });
}
/**
* middleware
* @description Auth middleware, used to check the authentication status.
*/
middleware() {
return async (ctx: Context & { auth: Auth }, next: Next) => {
const token = ctx.getBearerToken();
if (token && (await ctx.app.authManager.jwt.blacklist.has(token))) {
return ctx.throw(401, ctx.t('token is not available'));
}
const name = ctx.get(this.options.authKey) || this.options.default;
let authenticator: Auth;
try {
authenticator = await ctx.app.authManager.get(name, ctx);
ctx.auth = authenticator;
} catch (err) {
ctx.auth = {} as Auth;
ctx.app.logger.warn(`auth, ${err.message}`);
return next();
}
if (authenticator) {
const user = await ctx.auth.check();
if (user) {
ctx.auth.user = user;
}
}
await next();
};
}
}