Skip to content

Commit

Permalink
feat(authentication): Add setup method for auth strategies
Browse files Browse the repository at this point in the history
  • Loading branch information
vonagam committed Oct 27, 2021
1 parent 51ed7c7 commit 4a5b7ff
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
26 changes: 23 additions & 3 deletions packages/authentication/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ export interface AuthenticationStrategy {
* and throw an error if it is invalid.
*/
verifyConfiguration? (): void;
/**
* Implement this method to setup this strategy
* @param auth The AuthenticationService
* @param name The name of the strategy
*/
setup? (auth: AuthenticationBase, name: string): Promise<void>;
/**
* Authenticate an authentication request with this strategy.
* Should throw an error if the strategy did not succeed.
Expand Down Expand Up @@ -77,10 +83,9 @@ export interface JwtVerifyOptions extends VerifyOptions {
*/
export class AuthenticationBase {
app: Application;
strategies: { [key: string]: AuthenticationStrategy };
configKey: string;
strategies: {
[key: string]: AuthenticationStrategy;
};
isReady: boolean;

/**
* Create a new authentication service.
Expand All @@ -97,6 +102,7 @@ export class AuthenticationBase {
this.app = app;
this.strategies = {};
this.configKey = configKey;
this.isReady = false;

app.set('defaultAuthentication', app.get('defaultAuthentication') || configKey);
app.set(configKey, merge({}, app.get(configKey), options));
Expand Down Expand Up @@ -143,6 +149,10 @@ export class AuthenticationBase {

// Register strategy as name
this.strategies[name] = strategy;

if (this.isReady) {
strategy.setup?.(this, name);
}
}

/**
Expand Down Expand Up @@ -275,4 +285,14 @@ export class AuthenticationBase {

return null;
}

async setup () {
this.isReady = true;

for (const name of Object.keys(this.strategies)) {
const strategy = this.strategies[name];

await strategy.setup?.(this, name);
}
}
}
2 changes: 2 additions & 0 deletions packages/authentication/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ export class AuthenticationService extends AuthenticationBase implements Partial
* Validates the service configuration.
*/
async setup () {
await super.setup();

// The setup method checks for valid settings and registers the
// connection and event (login, logout) hooks
const { secret, service, entity, entityId } = this.configuration;
Expand Down

0 comments on commit 4a5b7ff

Please sign in to comment.