-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
204 additions
and
82 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -3,33 +3,35 @@ | |
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
import {inject, Provider, ValueOrPromise} from '@loopback/context'; | ||
import {Application} from '@loopback/core'; | ||
import {anOpenApiSpec} from '@loopback/openapi-spec-builder'; | ||
import {api, get} from '@loopback/openapi-v3'; | ||
import { | ||
RestBindings, | ||
ParseParams, | ||
FindRoute, | ||
InvokeMethod, | ||
Send, | ||
ParseParams, | ||
Reject, | ||
SequenceHandler, | ||
RestServer, | ||
RestComponent, | ||
RequestContext, | ||
RestBindings, | ||
RestComponent, | ||
RestServer, | ||
Send, | ||
SequenceHandler, | ||
} from '@loopback/rest'; | ||
import {api, get} from '@loopback/openapi-v3'; | ||
import {Client, createClientForHandler} from '@loopback/testlab'; | ||
import {anOpenApiSpec} from '@loopback/openapi-spec-builder'; | ||
import {inject, Provider, ValueOrPromise} from '@loopback/context'; | ||
import {BasicStrategy} from 'passport-http'; | ||
import { | ||
authenticate, | ||
UserProfile, | ||
AuthenticationBindings, | ||
AuthenticateFn, | ||
AuthenticationMetadata, | ||
AuthenticationBindings, | ||
AuthenticationComponent, | ||
AuthenticationMetadata, | ||
AuthenticationStrategy, | ||
StrategyAdapter, | ||
UserProfile, | ||
} from '../..'; | ||
import {Strategy} from 'passport'; | ||
import {BasicStrategy} from 'passport-http'; | ||
import {MockStrategy} from '../unit/fixtures/mock-strategy'; | ||
|
||
const SequenceActions = RestBindings.SequenceActions; | ||
|
||
|
@@ -115,7 +117,7 @@ describe('Basic Authentication', () => { | |
@inject(AuthenticationBindings.CURRENT_USER) private user: UserProfile, | ||
) {} | ||
|
||
@authenticate('BasicStrategy') | ||
@authenticate('BasicStrategy', {isPassportStrategy: true}) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
jannyHou
Author
Contributor
|
||
async whoAmI(): Promise<string> { | ||
return this.user.id; | ||
} | ||
|
@@ -159,31 +161,67 @@ describe('Basic Authentication', () => { | |
} | ||
|
||
function givenProviders() { | ||
class MyPassportStrategyProvider implements Provider<Strategy | undefined> { | ||
class MyPassportStrategyProvider | ||
implements Provider<AuthenticationStrategy | undefined> { | ||
constructor( | ||
@inject(AuthenticationBindings.METADATA) | ||
private metadata: AuthenticationMetadata, | ||
) {} | ||
value(): ValueOrPromise<Strategy | undefined> { | ||
value(): ValueOrPromise<AuthenticationStrategy | undefined> { | ||
if (!this.metadata) { | ||
return undefined; | ||
} | ||
const isPassportStrategy = | ||
this.metadata.options && this.metadata.options.isPassportStrategy; | ||
if (!isPassportStrategy) { | ||
return undefined; | ||
} | ||
|
||
const name = this.metadata.strategy; | ||
if (name === 'BasicStrategy') { | ||
return new BasicStrategy(this.verify); | ||
const basicStrategy = new BasicStrategy(verify); | ||
return new StrategyAdapter(basicStrategy, 'basic'); | ||
} else { | ||
return Promise.reject(`The strategy ${name} is not available.`); | ||
} | ||
} | ||
// callback method for BasicStrategy | ||
verify(username: string, password: string, cb: Function) { | ||
process.nextTick(() => { | ||
users.find(username, password, cb); | ||
}); | ||
} | ||
class MyStrategyProvider | ||
implements Provider<AuthenticationStrategy | undefined> { | ||
constructor( | ||
@inject(AuthenticationBindings.METADATA) | ||
private metadata: AuthenticationMetadata, | ||
@inject(AuthenticationBindings.PASSPORT_STRATEGY) | ||
private passportStrategy?: AuthenticationStrategy, | ||
) {} | ||
value(): ValueOrPromise<AuthenticationStrategy | undefined> { | ||
if (!this.metadata) { | ||
return undefined; | ||
} | ||
const isPassportStrategy = | ||
this.metadata.options && this.metadata.options.isPassportStrategy; | ||
if (isPassportStrategy) { | ||
return this.passportStrategy; | ||
} | ||
|
||
const name = this.metadata.strategy; | ||
if (name === 'BasicStrategy') { | ||
return new MockStrategy(); | ||
} else { | ||
return Promise.reject(`The strategy ${name} is not available.`); | ||
} | ||
} | ||
} | ||
|
||
// callback method for BasicStrategy | ||
function verify(username: string, password: string, cb: Function) { | ||
process.nextTick(() => { | ||
users.find(username, password, cb); | ||
}); | ||
} | ||
server.bind(AuthenticationBindings.STRATEGY).toProvider(MyStrategyProvider); | ||
server | ||
.bind(AuthenticationBindings.STRATEGY) | ||
.bind(AuthenticationBindings.PASSPORT_STRATEGY) | ||
.toProvider(MyPassportStrategyProvider); | ||
} | ||
|
||
|
67 changes: 67 additions & 0 deletions
67
packages/authentication/src/__tests__/unit/fixtures/mock-strategy-passport.ts
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,67 @@ | ||
// Copyright IBM Corp. 2019. 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 {Request} from 'express'; | ||
import {AuthenticateOptions, Strategy} from 'passport'; | ||
import {UserProfile} from '../../../types'; | ||
|
||
/** | ||
* Test fixture for a mock asynchronous passport-strategy | ||
*/ | ||
export class MockPassportStrategy extends Strategy { | ||
// user to return for successful authentication | ||
private mockUser: UserProfile; | ||
|
||
setMockUser(userObj: UserProfile) { | ||
this.mockUser = userObj; | ||
} | ||
|
||
/** | ||
* authenticate() function similar to passport-strategy packages | ||
* @param req | ||
*/ | ||
async authenticate(req: Request, options?: AuthenticateOptions) { | ||
await this.verify(req); | ||
} | ||
/** | ||
* @param req | ||
* mock verification function; usually passed in as constructor argument for | ||
* passport-strategy | ||
* | ||
* For the purpose of mock tests we have this here | ||
* pass req.query.testState = 'fail' to mock failed authorization | ||
* pass req.query.testState = 'error' to mock unexpected error | ||
*/ | ||
async verify(request: Request) { | ||
if ( | ||
request.headers && | ||
request.headers.testState && | ||
request.headers.testState === 'fail' | ||
) { | ||
this.returnUnauthorized('authorization failed'); | ||
return; | ||
} else if ( | ||
request.headers && | ||
request.headers.testState && | ||
request.headers.testState === 'error' | ||
) { | ||
this.returnError('unexpected error'); | ||
return; | ||
} | ||
process.nextTick(this.returnMockUser.bind(this)); | ||
} | ||
|
||
returnMockUser() { | ||
this.success(this.mockUser); | ||
} | ||
|
||
returnUnauthorized(challenge?: string | number, status?: number) { | ||
this.fail(challenge, status); | ||
} | ||
|
||
returnError(err: string) { | ||
this.error(err); | ||
} | ||
} |
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
1 change: 1 addition & 0 deletions
1
...ges/authentication/src/__tests__/unit/providers/authentication.provider(passport).unit.ts
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 @@ | ||
// TBD |
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
Oops, something went wrong.
I don't think
isPassportStrategy
flag should be exposed to application developers. It should be part of implementation details. We can register two different strategies, one implemented in LB4, another one in Passport.IMO, here is the extension point structure: