Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: user profile to principal #3807

Merged
merged 1 commit into from
Sep 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions packages/authorization/src/__tests__/unit/util.test.ts
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 {createPrincipalFromUserProfile} from '../../util';

describe('utils', () => {
it('generates the correct principal', () => {
const userProfile: UserProfile = {
[securityId]: 'auser',
email: 'test@fake.com',
};
const expectedPrincipal: Principal = {
[securityId]: 'auser',
email: 'test@fake.com',
name: 'auser',
type: 'USER',
};
expect(createPrincipalFromUserProfile(userProfile)).to.deepEqual(
expectedPrincipal,
);
});
});
21 changes: 3 additions & 18 deletions packages/authorization/src/authorize-interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@ import {
Next,
Provider,
} from '@loopback/context';
import {
Principal,
SecurityBindings,
securityId,
UserProfile,
} from '@loopback/security';
import {SecurityBindings, UserProfile} from '@loopback/security';
import * as debugFactory from 'debug';
import {getAuthorizationMetadata} from './decorators/authorize';
import {AuthorizationBindings, AuthorizationTags} from './keys';
Expand All @@ -33,6 +28,7 @@ import {
AuthorizationOptions,
Authorizer,
} from './types';
import {createPrincipalFromUserProfile} from './util';

const debug = debugFactory('loopback:authorization:interceptor');

Expand Down Expand Up @@ -83,7 +79,7 @@ export class AuthorizationInterceptor implements Provider<Interceptor> {
debug('Current user', user);

const authorizationCtx: AuthorizationContext = {
principals: user ? [userToPrinciple(user)] : [],
principals: user ? [createPrincipalFromUserProfile(user)] : [],
roles: [],
scopes: [],
resource: invocationCtx.targetName,
Expand Down Expand Up @@ -151,14 +147,3 @@ async function loadAuthorizers(
}
return authorizerFunctions;
}

// This is a workaround before we extract a common layer
// for authentication and authorization.
function userToPrinciple(user: UserProfile): Principal {
return {
name: user.name || user[securityId],
[securityId]: user.id,
email: user.email,
type: 'USER',
};
}
24 changes: 24 additions & 0 deletions packages/authorization/src/util.ts
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.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change will be addressed as part of #2246 or as a follow-up story after the spike done.


/**
* 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',
};
}