nocobase/packages/core/auth/src/auth-manager.ts
Dunqing 6cfd586175
feat(plugin-api-keys): support fetch api via api-keys (#2136)
* feat(plugin-api-keys): support fetch api via api-keys

* feat: full support

* fix: missing parseToken in auth

* feat: add created at column

* feat: configure snippet

* fix: remove unused code

* fix: revert

* chore: update deps

* feat: improve role

* fix: avoid create api key without not exist role

* feat: improve select roles

* refactor: when no X-Role is found, roles should not be randomly assigned

* feat: improve code

* feat: improve current role

* fix: revert

* fix: revert apilicent

* fix: revert auth

* feat: improve currentRole logic

* feat: use resourcer.use instead it

* refactor: remove api-keys-auth

* fix: type

* refactor: move jwt to authManager

* refactor: remove unused code

* refactor: remove protected

* Revert "refactor: remove unused code"

This reverts commit 8f81535ab7.

* feat: remove unused code

* feat: improve code

* fix: test error

* test: update test

* test: add test cases

* docs: update

* chore: update X-Role

* fix: token's roleName not work

* docs: update usage

* fix: i18n Add APi key

* docs: update capital

* docs: update

* feat: clean

* Update package.json

* Update roles.ts

* fix: api key

---------

Co-authored-by: chenos <chenlinxh@gmail.com>
2023-06-30 11:20:35 +08:00

104 lines
2.9 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';
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;
}
/**
* 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 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}, ${err.stack}`);
return next();
}
if (authenticator) {
const user = await ctx.auth.check();
if (user) {
ctx.auth.user = user;
}
}
await next();
};
}
}