This repository has been archived by the owner on May 7, 2021. It is now read-only.
-
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
Merged
joshuawilson
merged 4 commits into
fabric8-ui:master
from
rohitkrai03:permission-service
Oct 19, 2018
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
eecd81d
feat(permissions): add permission service for reading scopes and perm…
rohitkrai03 8d7ef43
chore(comments): add comments for methods
rohitkrai03 a3b0844
fix(permissions): strict null check, make private methods and other f…
rohitkrai03 5369a8f
fix(permissions): checkScope() -> hasScope()
rohitkrai03 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,36 @@ | ||
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.eyJhY3IiOiIwIiwiYWxsb3dlZC1vcmlnaW5zIjpbImh0dHA6Ly9hdXRoLm9wZW5zaGlmdC5pbyIsImh0dHA6Ly9vcGVuc2hpZnQuaW8iXSwiYXBwcm92ZWQiOnRydWUsImF1ZCI6Imh0dHA6Ly9vcGVuc2hpZnQuaW8iLCJhdXRoX3RpbWUiOjE1MzU0MTQxNjAsImF6cCI6Imh0dHA6Ly9vcGVuc2hpZnQuaW8iLCJl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J0eXAiOiJCZWFyZXIifQ.xbY2neM56yeHRwhXnaKLp67o6ine38MkJb4Yhe-guQ2nN0-aLrXkqxYF7Jgqb-8w1TfDfdUuKQGWUK1Ye-Xh10biZq-Cl7amPIRQwZ8bLsoII9KFXTjkUQbCxOjNxMl89PuliIP_rO3OXydATnL2KAoU36qKbkBiUTKpQNUOXkcb8wtID_SXE1lssHHNeHNVU358kJjMJUqYE0K59C8csddupR1vpEYJknoLW7nKxxWtAJYGYTOjCey8BkVom6bOgOXz0AiEq2aYdjcaRdwz4IeiLGeFIyvT_sIDyPgYFSR2YCN4_N3CSQPfQYdrQhDGKM7fKLBKnYqAwfUe2OeibQ'; | ||
const fakeResourceId = 'c0ee2b94-aee3-4c41-9e15-6fa330ce8e0b'; | ||
let service: PermissionService; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
providers: [ | ||
PermissionService | ||
] | ||
}); | ||
service = TestBed.get(PermissionService); | ||
|
||
localStorage.setItem('auth_token', fakeToken); | ||
}); | ||
|
||
it('should return permission for a resource', () => { | ||
const permission: Permission = service.getPermission(fakeResourceId); | ||
expect(permission.resource_set_id).toBe(fakeResourceId); | ||
}); | ||
|
||
it('should check for scope for a resource', () => { | ||
expect(service.hasScope(fakeResourceId, 'lima')).toBe(true); | ||
expect(service.hasScope(fakeResourceId, 'bean')).toBe(false); | ||
}); | ||
|
||
it('should return all scopes for a resource', () => { | ||
const scopes = service.getAllScopes(fakeResourceId); | ||
expect(scopes.length).toBe(1); | ||
expect(scopes.includes('lima')).toBe(true); | ||
}); | ||
}); |
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,61 @@ | ||
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 { | ||
private jwtHelper: JwtHelperService = new JwtHelperService(); | ||
|
||
/** | ||
* 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` | ||
*/ | ||
hasScope(resourceId: string, scope: string): boolean { | ||
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 | null { | ||
const decodedToken = this.getDecodedToken(); | ||
if (this.isValidRPT(decodedToken)) { | ||
return decodedToken.permissions.find((permission: Permission) => permission.resource_set_id === resourceId); | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* Decodes the JWT token using JwtHelperService from `angular-jwt`. | ||
*/ | ||
private getDecodedToken(): any { | ||
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. | ||
*/ | ||
private isValidRPT(token: any) { | ||
return token && token.permissions; | ||
} | ||
} |
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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 comment
The 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
ngx-login-client
will be used infabric8-ui
.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.
FYI the monorepo will also include
es2017
as part of the commontsconfig
.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.
Yes, i did check that
tsconfig
in talamer hases2017
along withes2015
andes2016
. :)