Skip to content

Commit

Permalink
feat(authentication): add a middleware for authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed May 13, 2020
1 parent e457c83 commit 9463ac2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 12 deletions.
19 changes: 9 additions & 10 deletions packages/authentication/src/authentication.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,22 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {bind, Component, ContextTags, ProviderMap} from '@loopback/core';
import {bind, Component, ContextTags} from '@loopback/core';
import {AuthenticationBindings} from './keys';
import {
AuthenticateActionProvider,
AuthenticationMiddlewareProvider,
AuthenticationStrategyProvider,
AuthMetadataProvider,
} from './providers';

@bind({tags: {[ContextTags.KEY]: AuthenticationBindings.COMPONENT}})
export class AuthenticationComponent implements Component {
providers?: ProviderMap;

constructor() {
this.providers = {
[AuthenticationBindings.AUTH_ACTION.key]: AuthenticateActionProvider,
[AuthenticationBindings.STRATEGY.key]: AuthenticationStrategyProvider,
[AuthenticationBindings.METADATA.key]: AuthMetadataProvider,
};
}
providers = {
[AuthenticationBindings.AUTH_ACTION.key]: AuthenticateActionProvider,
[AuthenticationBindings.STRATEGY.key]: AuthenticationStrategyProvider,
[AuthenticationBindings.METADATA.key]: AuthMetadataProvider,
[AuthenticationBindings.AUTHENTICATION_MIDDLEWARE
.key]: AuthenticationMiddlewareProvider,
};
}
8 changes: 8 additions & 0 deletions packages/authentication/src/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import {BindingKey} from '@loopback/context';
import {MetadataAccessor} from '@loopback/metadata';
import {Middleware} from '@loopback/rest';
import {SecurityBindings} from '@loopback/security';
import {AuthenticationComponent} from './authentication.component';
import {
Expand Down Expand Up @@ -89,6 +90,13 @@ export namespace AuthenticationBindings {
'authentication.actions.authenticate',
);

/**
* Binding key for AUTHENTICATION_MIDDLEWARE
*/
export const AUTHENTICATION_MIDDLEWARE = BindingKey.create<Middleware>(
'middleware.authentication',
);

/**
* Key used to inject authentication metadata, which is used to determine
* whether a request requires authentication or not.
Expand Down
32 changes: 30 additions & 2 deletions packages/authentication/src/providers/auth-action.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {Getter, inject, Provider, Setter} from '@loopback/context';
import {Request, RedirectRoute} from '@loopback/rest';
import {bind, Getter, inject, Provider, Setter} from '@loopback/context';
import {
asMiddleware,
Middleware,
RedirectRoute,
Request,
RestTags,
} from '@loopback/rest';
import {SecurityBindings, UserProfile} from '@loopback/security';
import {AuthenticationBindings} from '../keys';
import {
Expand Down Expand Up @@ -80,3 +86,25 @@ export class AuthenticateActionProvider implements Provider<AuthenticateFn> {
}
}
}

@bind(
asMiddleware({
chain: RestTags.REST_MIDDLEWARE_CHAIN,
group: 'authentication',
requestDependencies: 'cors',
responseDependencies: 'findRoute',
}),
)
export class AuthenticationMiddlewareProvider implements Provider<Middleware> {
constructor(
@inject(AuthenticationBindings.AUTH_ACTION)
private authenticate: AuthenticateFn,
) {}

value(): Middleware {
return async (ctx, next) => {
await this.authenticate(ctx.request);
return next();
};
}
}

0 comments on commit 9463ac2

Please sign in to comment.