-
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
3 changed files
with
53 additions
and
18 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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright IBM Corp. 2019. All Rights Reserved. | ||
// Node module: @loopback/authorization | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
import {Principal, securityId, UserProfile} from '@loopback/security'; | ||
import {expect} from '@loopback/testlab'; | ||
import {userProfileToPrincipalBuilder} from '../../util'; | ||
|
||
describe('utils', () => { | ||
it('generates the correct principal', () => { | ||
const userProfile: UserProfile = { | ||
[securityId]: 'auser', | ||
email: '[email protected]', | ||
}; | ||
const expectedPrincipal: Principal = { | ||
[securityId]: 'auser', | ||
email: '[email protected]', | ||
name: 'auser', | ||
type: 'USER', | ||
}; | ||
expect(userProfileToPrincipalBuilder(userProfile)).to.deepEqual( | ||
expectedPrincipal, | ||
); | ||
}); | ||
}); |
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,24 @@ | ||
import { Principal, securityId, UserProfile } from '@loopback/security'; | ||
|
||
// This is a workaround before we specify `TypedPrincipal` instead of | ||
// `Principal` for the principals in the authorization context. | ||
|
||
/** | ||
* Module `@loopback/authentication` passes a user profile to | ||
* `@loopback/authorization` as the user identity. Authorization verifies | ||
* whether a user has access to a certain resource. | ||
* | ||
* The builder function: | ||
* - preserves all the fields from user profile | ||
* - specifies 'USER' as type | ||
* - assign the value of `securityId` to name if it's missing in the | ||
* user profile | ||
* @param user The user profile passed from `@loopback/authentication`. | ||
*/ | ||
export function createPrincipalFromUserProfile(user: UserProfile): Principal { | ||
return { | ||
...user, | ||
name: user.name || user[securityId], | ||
type: 'USER', | ||
}; | ||
} |