Skip to content
This repository has been archived by the owner on May 7, 2021. It is now read-only.

feat(permissions): add permission service for reading scopes and permissions from RPT token #131

Merged
merged 4 commits into from
Oct 19, 2018

Conversation

rohitkrai03
Copy link
Contributor

@rohitkrai03 rohitkrai03 commented Oct 17, 2018

Fixes fabric8-services/fabric8-auth#691

This PR adds PermissionService which checks if the token is valid RPT token and adds various methods like -

  • getAllScopes: Returns all the scopes a user has for a specific resource.
  • checkScope: Checks if a user has a specific scope for a resource.
  • getPermission: Returns the permission for a specific resource.

Also,

  • Added auth0/angular-jwt instead of jwt-decode which is b eing used in fabric8-ui because jwt-decode doesn't have any type declarations since its not targetted for typescript or angular specifically which caused build to fail. We should probably make this move for fabric8-ui as well.
  • Added es2017 in tsconfig.json to use .includes().

@@ -14,7 +14,7 @@
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"inlineSources": true,
"lib": [ "dom", "es6" ],
"lib": [ "dom", "es6", "es2017" ],
Copy link
Contributor

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?

Copy link
Contributor Author

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 in fabric8-ui.

Copy link
Contributor

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 common tsconfig.

Copy link
Contributor Author

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 has es2017 along with es2015 and es2016. :)


@Injectable()
export class PermissionService {
jwtHelper: JwtHelperService;
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be private?

export class PermissionService {
jwtHelper: JwtHelperService;
constructor() {
this.jwtHelper = new JwtHelperService();
Copy link
Contributor

Choose a reason for hiding this comment

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

Move the assignment to the declaration.

* Checks if the decoded token is valid RPT by checking the permissions claim.
* @param token Decoded JWT token.
*/
isValidRPT(token: any) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Make private?

/**
* Decodes the JWT token using JwtHelperService from `angular-jwt`.
*/
getDecodedToken() {
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

* @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 {
Copy link
Contributor

Choose a reason for hiding this comment

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

A better name for this API might be isResourceInScope or isInScope to align with is functions returning a boolean.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 isResourceInScope or isInScope doesn't make sense here. Something like hasScopeForResource or hasScope might be more meaningful i think.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the clarification. I'm ok with hasScope.

* Returns the permission for a specific resource.
* @param resourceId ID of a specific resource such as a Space
*/
getPermission(resourceId: string): Permission {
Copy link
Contributor

Choose a reason for hiding this comment

The 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 undefined but that's not clear from the return type.
This is something we will improve with the monorepo linting / tsconfig.

@joshuawilson joshuawilson merged commit 72fda77 into fabric8-ui:master Oct 19, 2018
@fabric8cd
Copy link

🎉 This PR is included in version 2.6.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants