Skip to content

Commit

Permalink
feat: keep only one extension point
Browse files Browse the repository at this point in the history
  • Loading branch information
jannyHou committed May 7, 2019
1 parent cc2d82a commit 881fe37
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ import {
SequenceHandler,
} from '@loopback/rest';
import {Client, createClientForHandler} from '@loopback/testlab';
import {BasicStrategy} from 'passport-http';
import {BasicStrategy, BasicVerifyFunction} from 'passport-http';
import {
authenticate,
AuthenticateFn,
AuthenticationBindings,
AuthenticationComponent,
UserProfile,
} from '../..';
import {StrategyAdapter} from '../../strategy-adapter';
import {AuthenticationStrategy} from '../../types';
const SequenceActions = RestBindings.SequenceActions;

describe('Basic Authentication', () => {
Expand Down Expand Up @@ -88,9 +90,18 @@ describe('Basic Authentication', () => {
// class as extension directly, we need to wrap it as a strategy provider,
// then add the provider class as the extension.
// See Line 89 in the function `givenAServer`
class PassportBasicAuthProvider implements Provider<BasicStrategy> {
value(): BasicStrategy {
return new BasicStrategy(verify);
class PassportBasicAuthProvider implements Provider<AuthenticationStrategy> {
value(): AuthenticationStrategy {
const basicStrategy = this.configuratedBasicStrategy(verify);
return this.convertToAuthStrategy(basicStrategy);
}

configuratedBasicStrategy(verifyFn: BasicVerifyFunction): BasicStrategy {
return new BasicStrategy(verifyFn);
}

convertToAuthStrategy(basic: BasicStrategy): AuthenticationStrategy {
return new StrategyAdapter(basic, 'basic');
}
}

Expand All @@ -106,11 +117,11 @@ describe('Basic Authentication', () => {
app.component(RestComponent);
addExtension(
app,
AuthenticationBindings.PASSPORT_STRATEGY_EXTENSION_POINT_NAME,
AuthenticationBindings.AUTHENTICATION_STRATEGY_EXTENSION_POINT_NAME,
PassportBasicAuthProvider,
{
namespace:
AuthenticationBindings.PASSPORT_STRATEGY_EXTENSION_POINT_NAME,
AuthenticationBindings.AUTHENTICATION_STRATEGY_EXTENSION_POINT_NAME,
},
);
server = await app.getServer(RestServer);
Expand Down
2 changes: 0 additions & 2 deletions packages/authentication/src/authentication.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
AuthenticationStrategyProvider,
AuthMetadataProvider,
} from './providers';
import {PassportStrategyProvider} from './providers/passport-auth-strategy.provider';

export class AuthenticationComponent implements Component {
providers?: ProviderMap;
Expand All @@ -19,7 +18,6 @@ export class AuthenticationComponent implements Component {
this.providers = {
[AuthenticationBindings.AUTH_ACTION.key]: AuthenticateActionProvider,
[AuthenticationBindings.STRATEGY.key]: AuthenticationStrategyProvider,
[AuthenticationBindings.PASSPORT_STRATEGY.key]: PassportStrategyProvider,
[AuthenticationBindings.METADATA.key]: AuthMetadataProvider,
};
}
Expand Down
6 changes: 0 additions & 6 deletions packages/authentication/src/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import {BindingKey} from '@loopback/context';
import {MetadataAccessor} from '@loopback/metadata';
import {Strategy} from 'passport';
import {AuthenticationMetadata} from './decorators';
import {AuthenticateFn, AuthenticationStrategy, UserProfile} from './types';

Expand All @@ -27,10 +26,6 @@ export namespace AuthenticationBindings {
'authentication.strategy',
);

export const PASSPORT_STRATEGY = BindingKey.create<Strategy | undefined>(
'authentication.passport.strategy',
);

/**
* Key used to inject the authentication function into the sequence.
*
Expand Down Expand Up @@ -107,7 +102,6 @@ export namespace AuthenticationBindings {

export const AUTHENTICATION_STRATEGY_EXTENSION_POINT_NAME =
'authentication-strategies';
export const PASSPORT_STRATEGY_EXTENSION_POINT_NAME = 'passport-strategies';
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,13 @@ export class AuthenticationStrategyProvider
private metadata: AuthenticationMetadata,
@extensions()
private authenticationStrategies: Getter<AuthenticationStrategy[]>,
@inject(AuthenticationBindings.PASSPORT_STRATEGY, {optional: true})
private passportStrategy: AuthenticationStrategy,
) {}
async value(): Promise<AuthenticationStrategy | undefined> {
if (!this.metadata) {
return undefined;
}
const name = this.metadata.strategy;
const strategy =
(await this.findAuthenticationStrategy(name)) || this.passportStrategy;
const strategy = await this.findAuthenticationStrategy(name);

if (!strategy) {
// important not to throw a non-protocol-specific error here
Expand Down

This file was deleted.

0 comments on commit 881fe37

Please sign in to comment.