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

Commit

Permalink
Forward all directiveResolver args. Closes #108
Browse files Browse the repository at this point in the history
  • Loading branch information
augustusnaz authored and wtrocki committed Sep 1, 2020
1 parent 1799cf7 commit 3222bda
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 28 deletions.
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "keycloak-connect-graphql",
"version": "0.6.0",
"version": "0.6.1",
"description": "Add Keycloak authentication and authorization to your GraphQL server.",
"keywords": [
"graphql",
Expand Down
56 changes: 29 additions & 27 deletions src/directives/directiveResolvers.ts
Expand Up @@ -2,30 +2,30 @@ import { CONTEXT_KEY } from '../KeycloakContext'
import { isAuthorizedByRole } from './utils'

/**
*
*
* @param next - The resolver function you want to wrap with the auth resolver
*
*
* Checks if the incoming request to the GraphQL server is authenticated.
* Does this by checking that `context.kauth` is present and that the token is valid.
* The keycloak middleware must be set up on your GraphQL endpoint.
*
*
* Example usage:
*
*
* ```javascript
* const { auth } = require('keycloak-connect-graphql')
*
*
* const typeDefs = gql`
* type Query {
* hello: String
* }
* `
*
*
* const hello = (root, args, context, info) => 'Hello World'
*
*
* const resolvers = {
* hello: auth(hello)
* }
*
*
* const server = new ApolloServer({
* typeDefs,
* resolvers,
Expand All @@ -37,52 +37,53 @@ import { isAuthorizedByRole } from './utils'
* }
* })
* ```
*
*
*/
export const auth = (next: Function) => (root: any, args: any, context: any, info: any) => {
export const auth = (next: Function) => (...params: any[]) => {
let context = params[2]
if (!context[CONTEXT_KEY] || !context[CONTEXT_KEY].isAuthenticated()) {
const error: any = new Error(`User not Authenticated`);
error.code = "UNAUTHENTICATED"
throw error
}
return next(root, args, context, info)
return next.apply( null, params )
}

/**
*
*
* @param roles - The role or array of roles you want to authorize the user against.
*
*
* Checks if the authenticated keycloak user has the role.
* If the user has the role, the next resolver is called.
* If the user does not have the role, an error is thrown.
*
*
* If an array of roles is passed, it checks that the user has at least one of the roles
*
*
* By default, hasRole checks for keycloak client roles.
* Example: `hasRole('admin')` will check the logged in user has the client role named admin.
*
*
* It also is possible to check for realm roles and application roles.
* * `hasRole('realm:admin')` will check the logged in user has the admin realm role
* * `hasRole('some-other-app:admin')` will check the loged in user has the admin realm role in a different application
*
*
*
*
* Example usage:
*
*
* ```javascript
* const { hasRole } = require('keycloak-connect-graphql')
*
*
* const typeDefs = gql`
* type Query {
* hello: String
* }
* `
*
*
* const hello = (root, args, context, info) => 'Hello World'
*
*
* const resolvers = {
* hello: hasRole('admin')(hello)
* }
*
* }
*
* const server = new ApolloServer({
* typeDefs,
* resolvers,
Expand All @@ -95,7 +96,8 @@ export const auth = (next: Function) => (root: any, args: any, context: any, inf
* })
* ```
*/
export const hasRole = (roles: Array<string>) => (next: Function) => (root: any, args: any, context: any, info: any) => {
export const hasRole = (roles: Array<string>) => (next: Function) => (...params: any[]) => {
let context = params[2]
if (!context[CONTEXT_KEY] || !context[CONTEXT_KEY].isAuthenticated()) {
const error: any = new Error(`User not Authenticated`);
error.code = "UNAUTHENTICATED"
Expand All @@ -105,12 +107,12 @@ export const hasRole = (roles: Array<string>) => (next: Function) => (root: any,
if (typeof roles === 'string') {
roles = [roles]
}

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
}

return next(root, args, context, info)
return next.apply( null, params )
}

0 comments on commit 3222bda

Please sign in to comment.