Skip to content

Commit

Permalink
feat(authentication): Add setup method for auth strategies (#1611)
Browse files Browse the repository at this point in the history
  • Loading branch information
vonagam committed Mar 30, 2022
1 parent 1a166f3 commit a3c3581
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
26 changes: 23 additions & 3 deletions packages/authentication/src/core.ts
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
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
4 changes: 2 additions & 2 deletions packages/feathers/src/application.ts
Expand Up @@ -106,14 +106,14 @@ export class Feathers<Services, Settings> extends EventEmitter implements Feathe
// Add all the mixins
this.mixins.forEach(fn => fn.call(this, protoService, location, serviceOptions));

this.services[location] = protoService;

// If we ran setup already, set this service up explicitly, this will not `await`
if (this._isSetup && typeof protoService.setup === 'function') {
debug(`Setting up service for \`${location}\``);
protoService.setup(this, location);
}

this.services[location] = protoService;

return this;
}

Expand Down

0 comments on commit a3c3581

Please sign in to comment.