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

Commit

Permalink
Fix issues with typings (#73)
Browse files Browse the repository at this point in the history
* fix: add yarn lock to git ignore

* fix: Make onSubscriptionConnect parameters optional as in example app

* fix: Use granted request from code

* fix: Use peer dependencies for keycloak and graphql-tools

* fix: Unit tests type mappings
  • Loading branch information
wtrocki authored Feb 27, 2020
1 parent 7b34d97 commit b60c900
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 28 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ dist
coverage
.nyc_output
.vscode
yarn.lock
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,19 @@ Based on the [keycloak-connect](https://github.com/keycloak/keycloak-nodejs-conn

## Getting Started

Install library
```bash
npm install keycloak-connect-graphql
npm install --save keycloak-connect-graphql
```

Install required dependencies:
```bash
npm install --save graphql graphql-tools keycloak-connect
```

Install one of the Apollo Server libraries
```bash
npm install --save apollo-server-express
```

There are 3 steps to set up `keycloak-connect-graphql` in your application.
Expand Down Expand Up @@ -300,6 +311,9 @@ The `examples` folder contains runnable examples that demonstrate the various wa
* `subscriptions` - Shows basic subscriptions setup, requiring all subscriptions to be authenticated.
* `subscriptionsAdvanced` - Shows subscriptions that use the `auth` and `hasRole` middlewares directly on subscription resolvers.
> NOTE: Examples using unrelased code that needs to be compiled before use.
Please run `npm run compile` to compile source code before running examples.
## Setting up the Examples
Prerequisites:
Expand Down
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "keycloak-connect-graphql",
"version": "0.2.4",
"version": "0.3.0",
"description": "Add Keycloak authentication and authorization to your GraphQL server.",
"keywords": [
"graphql",
Expand Down Expand Up @@ -34,9 +34,6 @@
"examples:seed": "node scripts/initKeycloak.js"
},
"dependencies": {
"apollo-server-express": "2.9.16",
"graphql-tools": "4.0.6",
"keycloak-connect": "8.0.1"
},
"devDependencies": {
"@types/express-session": "1.15.16",
Expand All @@ -58,10 +55,15 @@
"subscriptions-transport-ws": "0.9.16",
"ts-node": "8.6.2",
"tslint": "5.20.1",
"typescript": "3.7.5"
"typescript": "3.7.5",
"apollo-server-express": "2.9.16",
"graphql-tools": "4.0.6",
"keycloak-connect": "8.0.1"
},
"peerDependencies": {
"graphql": "^0.12.0 || ^0.13.0 || ^14.0.0"
"graphql": "^0.12.0 || ^0.13.0 || ^14.0.0",
"graphql-tools": ">=4.0.0",
"keycloak-connect": ">=7.0.0"
},
"nyc": {
"extension": [
Expand Down
9 changes: 7 additions & 2 deletions src/KeycloakContext.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { AuthContextProvider } from './api'
import Keycloak from 'keycloak-connect'
import { Grant } from 'keycloak-connect'

export interface GrantedRequest extends Request {
kauth: { grant?: Grant };
}

export const CONTEXT_KEY = 'kauth'

Expand Down Expand Up @@ -66,10 +71,10 @@ export class KeycloakContextBase implements AuthContextProvider {
*
*/
export class KeycloakContext extends KeycloakContextBase implements AuthContextProvider {
public readonly request: Keycloak.GrantedRequest
public readonly request: GrantedRequest
public readonly accessToken: Keycloak.Token | undefined

constructor ({ req }: { req: Keycloak.GrantedRequest }) {
constructor ({ req }: { req: GrantedRequest }) {
const token = (req && req.kauth && req.kauth.grant) ? req.kauth.grant.access_token : undefined
super(token)
this.request = req
Expand Down
2 changes: 1 addition & 1 deletion src/KeycloakSubscriptionHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class KeycloakSubscriptionHandler {
* @param webSocket
* @param context
*/
public async onSubscriptionConnect(connectionParams: any, webSocket: any, context: any): Promise<Keycloak.Token | undefined> {
public async onSubscriptionConnect(connectionParams: any, webSocket?: any, context?: any): Promise<Keycloak.Token | undefined> {
if (!connectionParams || typeof connectionParams !== 'object') {
if (this.protect === true) {
throw new Error('Access Denied - missing connection parameters for Authentication')
Expand Down
12 changes: 6 additions & 6 deletions test/KeycloakContext.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import test from 'ava'
import Keycloak from 'keycloak-connect'

import { KeycloakContext, KeycloakContextBase, KeycloakSubscriptionContext } from '../src/KeycloakContext'
import { KeycloakContext, KeycloakContextBase, KeycloakSubscriptionContext, GrantedRequest } from '../src/KeycloakContext'

test('KeycloakContextBase accessToken is the access_token in req.kauth', (t) => {
const token = {
Expand Down Expand Up @@ -47,7 +47,7 @@ test('KeycloakContext accessToken is the access_token in req.kauth', (t) => {
}
}
}
} as Keycloak.GrantedRequest
} as GrantedRequest

const provider = new KeycloakContext({ req })
const token = req.kauth.grant && req.kauth.grant.access_token ? req.kauth.grant.access_token : undefined
Expand All @@ -70,7 +70,7 @@ test('KeycloakContext hasRole calls hasRole in the access_token', (t) => {
}
}
}
} as Keycloak.GrantedRequest
} as GrantedRequest

const provider = new KeycloakContext({ req })
t.truthy(provider.hasRole(''))
Expand All @@ -90,7 +90,7 @@ test('KeycloakContext.isAuthenticated is true when token is defined and isExpire
}
}
}
} as Keycloak.GrantedRequest
} as GrantedRequest

const provider = new KeycloakContext({ req })
t.truthy(provider.isAuthenticated())
Expand All @@ -110,7 +110,7 @@ test('KeycloakContext.isAuthenticated is false when token is defined but isExpir
}
}
}
} as Keycloak.GrantedRequest
} as GrantedRequest

const provider = new KeycloakContext({ req })
t.false(provider.isAuthenticated())
Expand All @@ -130,7 +130,7 @@ test('KeycloakContext.hasRole is false if token is expired', (t) => {
}
}
}
} as Keycloak.GrantedRequest
} as GrantedRequest

const provider = new KeycloakContext({ req })
t.false(provider.hasRole(''))
Expand Down
10 changes: 5 additions & 5 deletions test/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { GraphQLSchema } from 'graphql'
import { VisitableSchemaType } from 'graphql-tools/dist/schemaVisitor'
import { AuthDirective } from '../src/directives/schemaDirectiveVisitors'

import { KeycloakContext } from '../src/KeycloakContext'
import { KeycloakContext, GrantedRequest } from '../src/KeycloakContext'

const createHasRoleDirective = () => {
return new AuthDirective({
Expand Down Expand Up @@ -43,7 +43,7 @@ test('happy path: context.kauth.isAuthenticated() is called, then original resol
}
}
}
} as Keycloak.GrantedRequest
} as GrantedRequest

const context = {
request: req,
Expand Down Expand Up @@ -85,7 +85,7 @@ test('context.kauth.isAuthenticated() is called, even if field has no resolver',
}
}
}
} as Keycloak.GrantedRequest
} as GrantedRequest

const context = {
request: req,
Expand Down Expand Up @@ -130,7 +130,7 @@ test('resolver will throw if context.kauth is not present', async (t) => {
}
}
}
} as Keycloak.GrantedRequest
} as GrantedRequest

const context = {
request: req
Expand Down Expand Up @@ -161,7 +161,7 @@ test('resolver will throw if context.kauth present but context.kauth.isAuthentic

const root = {}
const args = {}
const req = {} as Keycloak.GrantedRequest
const req = {} as GrantedRequest

const context = {
request: req,
Expand Down
14 changes: 7 additions & 7 deletions test/hasRole.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Keycloak from 'keycloak-connect'
import { GraphQLSchema } from 'graphql'
import { VisitableSchemaType } from 'graphql-tools/dist/schemaVisitor'
import { HasRoleDirective } from '../src/directives/schemaDirectiveVisitors'
import { KeycloakContext } from '../src/KeycloakContext'
import { KeycloakContext, GrantedRequest } from '../src/KeycloakContext'

const createHasRoleDirective = (directiveArgs: any) => {
return new HasRoleDirective({
Expand Down Expand Up @@ -51,7 +51,7 @@ test('context.auth.hasRole() is called', async (t) => {
}
}
}
} as Keycloak.GrantedRequest
} as GrantedRequest

const context = {
request: req,
Expand Down Expand Up @@ -98,7 +98,7 @@ test('hasRole works on fields that have no resolvers. context.auth.hasRole() is
}
}
}
} as Keycloak.GrantedRequest
} as GrantedRequest

const context = {
request: req,
Expand Down Expand Up @@ -148,7 +148,7 @@ test('visitFieldDefinition accepts an array of roles', async (t) => {
}
}
}
} as Keycloak.GrantedRequest
} as GrantedRequest

const context = {
request: req,
Expand Down Expand Up @@ -185,7 +185,7 @@ test('if there is no authentication, then an error is returned and the original

const root = {}
const args = {}
const req = {} as Keycloak.GrantedRequest
const req = {} as GrantedRequest
const context = {
request: req,
kauth: new KeycloakContext({ req })
Expand Down Expand Up @@ -236,7 +236,7 @@ test('if token does not have the required role, then an error is returned and th
}
}
}
} as Keycloak.GrantedRequest
} as GrantedRequest

const context = {
request: req,
Expand Down Expand Up @@ -395,7 +395,7 @@ test('context.auth.hasRole() works even if request is not supplied in context',
}
}
}
} as Keycloak.GrantedRequest
} as GrantedRequest

const context = {
kauth: new KeycloakContext({ req })
Expand Down

0 comments on commit b60c900

Please sign in to comment.