This repository has been archived by the owner on Apr 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: export isAuthorizedByRole util function (#85)
- Loading branch information
Showing
4 changed files
with
73 additions
and
11 deletions.
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,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 | ||
} |
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,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)) | ||
}) |