-
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.
feat: add extension point for passport
- Loading branch information
Showing
10 changed files
with
207 additions
and
87 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
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
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.