-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: jannyHou <[email protected]> Co-authored-by: Nora <[email protected]> Co-authored-by: Biniam <[email protected]> Co-authored-by: jannyHou <[email protected]>
- Loading branch information
1 parent
6700975
commit 8b2d6f7
Showing
14 changed files
with
354 additions
and
6,861 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const jwt = require('jsonwebtoken'); | ||
import {promisify} from 'util'; | ||
const verifyAsync = promisify(jwt.verify); | ||
// Consider turn it to a binding | ||
const SECRET = 'secretforjwt'; | ||
import {Request, HttpErrors} from '@loopback/rest'; | ||
import {UserProfile} from '@loopback/authentication'; | ||
import * as _ from 'lodash'; | ||
|
||
export class JWTStrategy { | ||
// tslint:disable-next-line:no-any | ||
async authenticate(request: Request): Promise<UserProfile | undefined> { | ||
// there is a discussion regarding how to retrieve the token, | ||
// see comment https://github.com/strongloop/loopback-next/issues/1997#issuecomment-451054806 | ||
const token = request.query.token || request.headers['authorization']; | ||
if (token) { | ||
try { | ||
const decoded = await verifyAsync(token, SECRET); | ||
return Promise.resolve(_.pick(decoded, ['id', 'email'])); | ||
} catch (err) { | ||
if (err) | ||
return Promise.reject( | ||
new HttpErrors.Unauthorized('Could not decode the JWT token!'), | ||
); | ||
} | ||
} else { | ||
return Promise.reject(new HttpErrors.Unauthorized('Token not found!')); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright IBM Corp. 2017,2018. All Rights Reserved. | ||
// Node module: @loopback/authentication | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
import {Getter, Provider, Setter, inject} from '@loopback/context'; | ||
import {Request} from '@loopback/rest'; | ||
import {AuthenticationBindings} from '@loopback/authentication'; | ||
import {AuthenticateFn, UserProfile} from '@loopback/authentication'; | ||
import {JWTStrategy} from '../authentication-strategies/JWT.strategy'; | ||
|
||
/** | ||
* @description Provider of a function which authenticates | ||
* @example `context.bind('authentication_key') | ||
* .toProvider(AuthenticateActionProvider)` | ||
*/ | ||
export class MyAuthenticateActionProvider implements Provider<AuthenticateFn> { | ||
constructor( | ||
// The provider is instantiated for Sequence constructor, | ||
// at which time we don't have information about the current | ||
// route yet. This information is needed to determine | ||
// what auth strategy should be used. | ||
// To solve this, we are injecting a getter function that will | ||
// defer resolution of the strategy until authenticate() action | ||
// is executed. | ||
@inject.getter(AuthenticationBindings.STRATEGY) | ||
readonly getStrategy: Getter<JWTStrategy>, | ||
@inject.setter(AuthenticationBindings.CURRENT_USER) | ||
readonly setCurrentUser: Setter<UserProfile>, | ||
) {} | ||
|
||
/** | ||
* @returns authenticateFn | ||
*/ | ||
value(): AuthenticateFn { | ||
return request => this.action(request); | ||
} | ||
|
||
/** | ||
* The implementation of authenticate() sequence action. | ||
* @param request The incoming request provided by the REST layer | ||
*/ | ||
async action(request: Request): Promise<UserProfile | undefined> { | ||
const strategy = await this.getStrategy(); | ||
if (!strategy) { | ||
// The invoked operation does not require authentication. | ||
return undefined; | ||
} | ||
if (!strategy.authenticate) { | ||
throw new Error('invalid strategy parameter'); | ||
} | ||
const user = await strategy.authenticate(request); | ||
if (user) this.setCurrentUser(user); | ||
return user; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './strategy.resolver.provider'; | ||
export * from './custom.authentication.provider'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import {Provider, ValueOrPromise} from '@loopback/core'; | ||
import {inject} from '@loopback/context'; | ||
import { | ||
AuthenticationBindings, | ||
AuthenticationMetadata, | ||
} from '@loopback/authentication'; | ||
import {JWTStrategy} from '../authentication-strategies/JWT.strategy'; | ||
// import {JWTStrategy} from '@loopback/authentication'; | ||
export class StrategyResolverProvider | ||
implements Provider<JWTStrategy | undefined> { | ||
constructor( | ||
@inject(AuthenticationBindings.METADATA) | ||
private metadata: AuthenticationMetadata, | ||
) {} | ||
value(): ValueOrPromise<JWTStrategy | undefined> { | ||
if (!this.metadata) { | ||
return Promise.resolve(undefined); | ||
} | ||
|
||
const name = this.metadata.strategy; | ||
if (name === 'jwt') { | ||
return new JWTStrategy(); | ||
} else { | ||
return Promise.reject(`The strategy ${name} is not available.`); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export type Credentials = { | ||
email: string; | ||
password: string; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './custom-types'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.