-
Notifications
You must be signed in to change notification settings - Fork 20
feat(permissions): add permission service for reading scopes and permissions from RPT token #131
Changes from 2 commits
eecd81d
8d7ef43
a3b0844
5369a8f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
import { PermissionService, Permission } from './permission.service'; | ||
|
||
describe('Service: Permission Service', () => { | ||
// tslint:disable-next-line:max-line-length | ||
const fakeToken = 'eyJhbGciOiJSUzI1NiIsImtpZCI6ImFVR3Y4bVFBODVqZzRWMURVOFVrMVcwdUtzeG4xODdLUU9OQUdsNkFNdGMiLCJ0eXAiOiJKV1QifQ.eyJhY3IiOiIwIiwiYWxsb3dlZC1vcmlnaW5zIjpbImh0dHA6Ly9hdXRoLm9wZW5zaGlmdC5pbyIsImh0dHA6Ly9vcGVuc2hpZnQuaW8iXSwiYXBwcm92ZWQiOnRydWUsImF1ZCI6Imh0dHA6Ly9vcGVuc2hpZnQuaW8iLCJhdXRoX3RpbWUiOjE1MzU0MTQxNjAsImF6cCI6Imh0dHA6Ly9vcGVuc2hpZnQuaW8iLCJlbWFpbCI6IlRlc3RVc2VyLTUwZWRmZjE4LTZjODYtNDkxMC1iMDY5LTM3ZDY4ZjFjMDJjMUB0ZXN0LmNvbSIsImVtYWlsX3ZlcmlmaWVkIjpmYWxzZSwiZXhwIjoxNTM4MDA2MTYwLCJmYW1pbHlfbmFtZSI6IiIsImdpdmVuX25hbWUiOiJUZXN0VXNlci01MGVkZmYxOC02Yzg2LTQ5MTAtYjA2OS0zN2Q2OGYxYzAyYzEiLCJpYXQiOjE1MzU0MTQxNjAsImlzcyI6Imh0dHA6Ly9hdXRoLm9wZW5zaGlmdC5pbyIsImp0aSI6IjEwOWQwOWVkLTkxY2MtNDM5My04ZmExLWJjMzE4N2FhNDBiYSIsIm5hbWUiOiJUZXN0VXNlci01MGVkZmYxOC02Yzg2LTQ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; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
providers: [ | ||
PermissionService | ||
] | ||
}); | ||
service = TestBed.get(PermissionService); | ||
|
||
localStorage.setItem('auth_token', fakeToken); | ||
}); | ||
|
||
it('should decode the token', () => { | ||
const token = service.getDecodedToken(); | ||
expect(token.typ).toBe('Bearer'); | ||
}); | ||
|
||
it('should return permissions', () => { | ||
const permission: Permission = service.getPermission(fakeResourceId); | ||
expect(permission.resource_set_id).toBe(fakeResourceId); | ||
}); | ||
|
||
it('should check for scope', () => { | ||
expect(service.checkScope(fakeResourceId, 'lima')).toBe(true); | ||
expect(service.checkScope(fakeResourceId, 'bean')).toBe(false); | ||
}); | ||
|
||
it('should return all scopes', () => { | ||
const scopes = service.getAllScopes(fakeResourceId); | ||
expect(scopes.length).toBe(1); | ||
expect(scopes.includes('lima')).toBe(true); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { JwtHelperService } from '@auth0/angular-jwt'; | ||
|
||
export type Permission = { | ||
exp: number, | ||
resource_set_id: string, | ||
resource_set_name: string, | ||
scopes: Array<string> | ||
}; | ||
|
||
@Injectable() | ||
export class PermissionService { | ||
jwtHelper: JwtHelperService; | ||
constructor() { | ||
this.jwtHelper = new JwtHelperService(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move the assignment to the declaration. |
||
} | ||
|
||
/** | ||
* Returns all the scopes a user has for a specific resource. | ||
* @param resourceId ID of a specific resource such as a Space | ||
*/ | ||
getAllScopes(resourceId: string): Array<string> { | ||
const permissions = this.getPermission(resourceId); | ||
return permissions ? permissions.scopes : []; | ||
} | ||
|
||
/** | ||
* Checks if a user has a specific scope for a resource. | ||
* @param resourceId ID of a specific resource such as a Space | ||
* @param scope the scope you want to check for. Ex - `can edit` | ||
*/ | ||
checkScope(resourceId: string, scope: string): boolean { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A better name for this API might be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method will be used to check if a user has a specific scope for a resource or not. That way the names like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the clarification. I'm ok with |
||
const permissions = this.getPermission(resourceId); | ||
return permissions ? permissions.scopes.includes(scope) : false; | ||
} | ||
|
||
/** | ||
* Returns the permission for a specific resource. | ||
* @param resourceId ID of a specific resource such as a Space | ||
*/ | ||
getPermission(resourceId: string): Permission { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We really need to start using strict nulls so that we can have better type checking. Right now this can return |
||
const decodedToken = this.getDecodedToken(); | ||
if (this.isValidRPT(decodedToken)) { | ||
return decodedToken.permissions.find((permission: Permission) => permission.resource_set_id === resourceId); | ||
} | ||
} | ||
|
||
/** | ||
* Decodes the JWT token using JwtHelperService from `angular-jwt`. | ||
*/ | ||
getDecodedToken() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make private? I see it's used in a test but does it make sense for this to be API? |
||
const token = localStorage.getItem('auth_token'); | ||
return token ? this.jwtHelper.decodeToken(token) : ''; | ||
} | ||
|
||
/** | ||
* Checks if the decoded token is valid RPT by checking the permissions claim. | ||
* @param token Decoded JWT token. | ||
*/ | ||
isValidRPT(token: any) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make private? |
||
return token && token.permissions; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ | |
"emitDecoratorMetadata": true, | ||
"experimentalDecorators": true, | ||
"inlineSources": true, | ||
"lib": [ "dom", "es6" ], | ||
"lib": [ "dom", "es6", "es2017" ], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do you think this will need to be added to the UI? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be good to add it there as well but not necessary since build code from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI the monorepo will also include There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, i did check that |
||
"module": "es2015", | ||
"moduleResolution": "node", | ||
"noEmitHelpers": false, // Planner and demo won't run when true | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be private?