|
| 1 | +--- |
| 2 | +id: keycloak |
| 3 | +title: Out of the box Keycloak based authentication |
| 4 | +sidebar_label: Keycloak Auth |
| 5 | +--- |
| 6 | + |
| 7 | +## Graphback Keycloak Authz |
| 8 | + |
| 9 | +Graphback Keycloak Authz enables [Keycloak](https://www.keycloak.org/) integration in [Graphback](https://graphback.dev) based applications. This enables you to declaratively add authorization capabilities like role based access on top of the CRUD model that is used within Graphback. |
| 10 | + |
| 11 | + |
| 12 | +This package is designed to work with [`keycloak-connect`](https://www.npmjs.com/package/keycloak-connect) and [`keycloak-connect-graphql`](https://www.npmjs.com/package/keycloak-connect-graphql). `keycloak-connect` is the official Keycloak middleware for Express applications. `keycloak-connect-graphql` provides deeper Keycloak integration into GraphQL servers. |
| 13 | + |
| 14 | +> NOTE: This package is an early alpha and not officially supported by Graphback |
| 15 | +
|
| 16 | +## Getting Started |
| 17 | + |
| 18 | +This module requires you to install the following dependencies into your application. |
| 19 | + |
| 20 | +```bash |
| 21 | +npm install keycloak-connect |
| 22 | +npm install keycloak-connect-graphql |
| 23 | +``` |
| 24 | + |
| 25 | +Then follow the [Getting started instructions for `keycloak-connect-graphql`](https://github.com/aerogear/keycloak-connect-graphql#getting-started) |
| 26 | + |
| 27 | +Once the getting started instructions are covered, you must create a configuration that defines the authorization rules for each model within your Graphback application. |
| 28 | + |
| 29 | +Here is an example configuration. |
| 30 | + |
| 31 | +```ts |
| 32 | +const authConfig = { |
| 33 | + Task: { |
| 34 | + create: {}, |
| 35 | + read: {}, |
| 36 | + update: { roles: ['admin'] }, |
| 37 | + delete: { roles: ['admin'] }, |
| 38 | + }, |
| 39 | + Report: { |
| 40 | + create: { roles: ['admin'] }, |
| 41 | + read: {}, |
| 42 | + update: { roles: ['admin'] }, |
| 43 | + delete: { roles: ['admin'] }, |
| 44 | + }, |
| 45 | +}; |
| 46 | +``` |
| 47 | + |
| 48 | +With this configuration the following rules are in place. |
| 49 | + |
| 50 | +- All users can create and read `Task` types but only admins can update and delete them. |
| 51 | +- Admin users can create, update and delete `Report` types, and all users can read them. |
| 52 | + |
| 53 | +## Example with Graphback Runtime |
| 54 | + |
| 55 | +During server initialization, use the `createKeycloakCRUDService` function to initialize the KeycloakCrudService instances for each model. |
| 56 | + |
| 57 | +The following example shows just the necessary parts to set up the runtime services in Graphback. |
| 58 | + |
| 59 | +```ts |
| 60 | +import { ApolloServer } from 'apollo-server-express' |
| 61 | +import { createKeycloakCRUDService } from '@graphback/keycloak-authz' |
| 62 | +import { KnexDbDataProvider } from '@graphback/runtime-knex' |
| 63 | +import { PubSub } from 'graphql-subscriptions' |
| 64 | +import * as Knex from 'knex' |
| 65 | +import { buildGraphbackAPI, createCRUDService } from 'graphback' |
| 66 | + |
| 67 | +// the application model |
| 68 | +const model = ` |
| 69 | +""" |
| 70 | +@model |
| 71 | +""" |
| 72 | +type Task { |
| 73 | + id: ID! |
| 74 | + title: String! |
| 75 | + description: String! |
| 76 | +}` |
| 77 | + |
| 78 | +// the auth rules for the application |
| 79 | +const authConfig = { |
| 80 | + Task: { |
| 81 | + create: {}, |
| 82 | + read: {}, |
| 83 | + update: { roles: ["admin"] }, |
| 84 | + delete: { roles: ["admin"] } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +// set up the Knex database client |
| 89 | +const db = Knex({...}) |
| 90 | + |
| 91 | +// standard Graphback runtime setup |
| 92 | +const pubSub = new PubSub() |
| 93 | + |
| 94 | +const keycloakService = createKeycloakCRUDService(authConfig, createCRUDService({ |
| 95 | + pubSub: new PubSub() |
| 96 | +})); |
| 97 | +const { typeDefs, resolvers, contextCreator } = buildGraphbackAPI(modelDefs, { |
| 98 | + serviceCreator: keycloakService, |
| 99 | + dataProviderCreator: createKnexDbProvider(db) |
| 100 | +}); |
| 101 | + |
| 102 | +const server = new ApolloServer({ |
| 103 | + typeDefs, |
| 104 | + resolvers, |
| 105 | + context: (context) => { |
| 106 | + return { |
| 107 | + ...contextCreator(context), |
| 108 | + kauth: new KeycloakContext({ req: context.req }) |
| 109 | + } |
| 110 | + } |
| 111 | +}) |
| 112 | +``` |
| 113 | + |
| 114 | +The above example shows runtime set up using the KnexDbDataProvider, but other data providers such as the `MongoDBDataProvider` can also be passed. |
0 commit comments