-
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
1 parent
2ec12dc
commit 02cf3b4
Showing
9 changed files
with
211 additions
and
43 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 |
---|---|---|
@@ -1,20 +1,12 @@ | ||
{ | ||
"ids": { | ||
"User": 3, | ||
"UserIdentity": 1005, | ||
"UserCredentials": 2 | ||
"User": 8, | ||
"UserIdentity": 1009, | ||
"UserCredentials": 4 | ||
}, | ||
"models": { | ||
"User": { | ||
"1": "{\"name\":\"Test User\",\"username\":\"[email protected]\",\"email\":\"[email protected]\",\"id\":1}", | ||
"2": "{\"name\":\"\\\"tinkerbell\\\"\",\"username\":\"[email protected]\",\"email\":\"[email protected]\",\"id\":2}" | ||
}, | ||
"UserIdentity": { | ||
"1001": "{\"id\":\"1001\",\"provider\":\"custom-oauth2\",\"profile\":{\"emails\":[{\"value\":\"[email protected]\"}]},\"authScheme\":\"custom-oauth2\",\"created\":\"2020-04-14T18:55:47.991Z\",\"userId\":2}", | ||
"1003": "{\"id\":\"1003\",\"provider\":\"custom-oauth2\",\"profile\":{\"emails\":[{\"value\":\"[email protected]\"}]},\"authScheme\":\"custom-oauth2\",\"created\":\"2020-04-14T18:55:47.412Z\",\"userId\":1}" | ||
}, | ||
"UserCredentials": { | ||
"[email protected]": "{\"id\":\"[email protected]\",\"password\":\"password\",\"userId\":1}" | ||
} | ||
"User": {}, | ||
"UserIdentity": {}, | ||
"UserCredentials": {} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,18 @@ describe('example-passport-login acceptance test', () => { | |
client = supertest('http://127.0.0.1:3000'); | ||
}); | ||
|
||
before(async function clearTestData() { | ||
await supertest('') | ||
.delete('http://localhost:3000/api/clear') | ||
.auth('admin', 'password', {type: 'basic'}); | ||
}); | ||
|
||
after(async function clearTestData() { | ||
await supertest('') | ||
.delete('http://localhost:3000/api/clear') | ||
.auth('admin', 'password', {type: 'basic'}); | ||
}); | ||
|
||
after(async function closeApplication() { | ||
await server.stop(); | ||
}); | ||
|
@@ -63,7 +75,7 @@ describe('example-passport-login acceptance test', () => { | |
* Test case 2: login as the new user with email id | ||
* Test case 3: logout | ||
*/ | ||
context('Scenario 1. Signing up as a NEW user', () => { | ||
context('Scenario 1: Signing up as a NEW user', () => { | ||
/** | ||
* create a local account in the loopback app with the following profile | ||
* username: [email protected] | ||
|
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
129 changes: 129 additions & 0 deletions
129
examples/passport-login/src/authentication-strategies/basic.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,129 @@ | ||
// Copyright IBM Corp. 2020. All Rights Reserved. | ||
// Node module: @loopback/example-passport-login | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
import {AuthenticationStrategy, asAuthStrategy} from '@loopback/authentication'; | ||
import {StrategyAdapter} from '@loopback/authentication-passport'; | ||
import {Request, RedirectRoute} from '@loopback/rest'; | ||
import {UserProfile, securityId} from '@loopback/security'; | ||
import {User} from '../models'; | ||
import {bind} from '@loopback/context'; | ||
import {BasicStrategy as Strategy} from 'passport-http'; | ||
import {repository} from '@loopback/repository'; | ||
import {UserRepository} from '../repositories'; | ||
|
||
/** | ||
* basic passport strategy | ||
*/ | ||
@bind(asAuthStrategy) | ||
export class BasicStrategy implements AuthenticationStrategy { | ||
name = 'basic'; | ||
passportstrategy: Strategy; | ||
strategy: StrategyAdapter<User>; | ||
|
||
constructor( | ||
@repository(UserRepository) | ||
public userRepository: UserRepository, | ||
) { | ||
/** | ||
* create a basic passport strategy with verify function to validate credentials | ||
*/ | ||
this.passportstrategy = new Strategy(this.verify.bind(this)); | ||
/** | ||
* wrap the passport strategy instance with an adapter to plugin to LoopBack authentication | ||
*/ | ||
this.strategy = new StrategyAdapter( | ||
this.passportstrategy, | ||
this.name, | ||
this.mapProfile.bind(this), | ||
); | ||
} | ||
|
||
/** | ||
* authenticate a request | ||
* @param request | ||
*/ | ||
async authenticate(request: Request): Promise<UserProfile | RedirectRoute> { | ||
return this.strategy.authenticate(request); | ||
} | ||
|
||
/** | ||
* authenticate user with provided username and password | ||
* | ||
* @param username | ||
* @param password | ||
* @param done | ||
* | ||
* @returns User model | ||
*/ | ||
verify( | ||
username: string, | ||
password: string, | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
done: (error: any, user?: any) => void, | ||
): void { | ||
/** | ||
* A dummy admin user is required for ease of testing purposes. | ||
* TODO: | ||
* enable roles and authorization, add user with admin roles in the | ||
* beginning of the tests | ||
*/ | ||
if (username === 'admin' && password === 'password') { | ||
return done(null, { | ||
id: 999, | ||
name: 'admin', | ||
username: 'admin', | ||
email: '[email protected]', | ||
}); | ||
} | ||
|
||
this.userRepository | ||
.find({ | ||
where: { | ||
email: username, | ||
}, | ||
include: [ | ||
{ | ||
relation: 'profiles', | ||
}, | ||
{ | ||
relation: 'credentials', | ||
}, | ||
], | ||
}) | ||
.then((users: User[]) => { | ||
if (!users || !users.length) { | ||
return done(null, false); | ||
} | ||
const user = users[0]; | ||
if (!user.credentials || user.credentials.password !== password) { | ||
return done(null, false); | ||
} | ||
// Authentication passed, return user profile | ||
done(null, user); | ||
}) | ||
.catch(err => { | ||
/** | ||
* Error occurred in authenticating process. | ||
* Does not necessarily mean an unauthorized user. | ||
*/ | ||
done(err); | ||
}); | ||
} | ||
|
||
/** | ||
* maps returned User model from verify function to UserProfile | ||
* | ||
* @param user | ||
*/ | ||
mapProfile(user: User): UserProfile { | ||
const userProfile: UserProfile = { | ||
[securityId]: '' + user.id, | ||
profile: { | ||
...user, | ||
}, | ||
}; | ||
return userProfile; | ||
} | ||
} |
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.