Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
feat: export isAuthorizedByRole util function (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
darahayes committed May 13, 2020
1 parent a6247a9 commit 752e426
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 11 deletions.
14 changes: 3 additions & 11 deletions src/directives/directiveResolvers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { CONTEXT_KEY } from '../KeycloakContext'
import { isAuthorizedByRole } from './utils'

/**
*
Expand Down Expand Up @@ -104,17 +105,8 @@ export const hasRole = (roles: Array<string>) => (next: Function) => (root: any,
if (typeof roles === 'string') {
roles = [roles]
}

let foundRole = null // this will be the role the user was successfully authorized on

for (let role of roles) {
if (context[CONTEXT_KEY].hasRole(role)) {
foundRole = role
break
}
}

if (!foundRole) {

if (!isAuthorizedByRole(roles, context)) {
const error: any = new Error(`User is not authorized. Must have one of the following roles: [${roles}]`);
error.code = "FORBIDDEN"
throw error
Expand Down
21 changes: 21 additions & 0 deletions src/directives/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { CONTEXT_KEY } from '../KeycloakContext'

/**
*
* @param roles the list of roles the user should match against
* @param context the graphql context that contains the user info
*/
export function isAuthorizedByRole(roles: string[], context?: any) {
if (!(context && context[CONTEXT_KEY])) {
console.error(`context.${CONTEXT_KEY} is missing. Keycloak integration is probably misconfigured`)
return false
}

for (const role of roles) {
if (context[CONTEXT_KEY].hasRole(role)) {
return true
}
}

return false
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './KeycloakSubscriptionHandler'
export * from './KeycloakContext'
export * from './directives'
export * from './api'
export { isAuthorizedByRole } from './directives/utils'
48 changes: 48 additions & 0 deletions test/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import test from 'ava'
import { isAuthorizedByRole } from '../src/directives/utils'
import Keycloak from 'keycloak-connect'
import { KeycloakContextBase } from '../src/KeycloakContext'

test('isAuthorizedByRole returns the result of token.hasRole', (t) => {
t.plan(4)
const token = {
hasRole: (role: string) => {
t.pass()
return role === 'c'
},
isExpired: () => {
return false
}
} as Keycloak.Token

const context = { kauth: new KeycloakContextBase(token) }

t.truthy(isAuthorizedByRole(['a', 'b', 'c'], context))
})

test('isAuthorizedByRole returns false if hasRole returns false', (t) => {
t.plan(4)
const token = {
hasRole: (role: string) => {
t.pass()
return false
},
isExpired: () => {
return false
}
} as Keycloak.Token

const context = { kauth: new KeycloakContextBase(token) }

t.falsy(isAuthorizedByRole(['a', 'b', 'c'], context))
})

test('isAuthorizedByRole returns false if context is empty', (t) => {
const context = { }
t.falsy(isAuthorizedByRole(['a', 'b', 'c'], context))
})

test('isAuthorizedByRole returns false if context undefined', (t) => {
const context = { }
t.falsy(isAuthorizedByRole(['a', 'b', 'c'], undefined))
})

0 comments on commit 752e426

Please sign in to comment.