Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(authentication): Add setup method for auth strategies #1611

Merged
merged 3 commits into from
Mar 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 2 additions & 2 deletions packages/feathers/src/application.ts
Original file line number Diff line number Diff line change
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