From acc20a4ea4e4dfe3c60e712d1a15ded52f96d0d3 Mon Sep 17 00:00:00 2001 From: yubintw Date: Sun, 22 May 2022 22:42:32 +0800 Subject: [PATCH] add scope to KeycloakOptions --- README.md | 2 ++ package.json | 4 ++-- src/keycloak.ts | 4 +++- src/tests/server.test.ts | 1 - 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 731269b..82145c1 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,8 @@ server.register(keycloak, opts) - `clientSecret` client secret (required) +- `scope` client scope of keycloak (optional, string[], defaults to `['openid']`) + - `logoutEndpoint` route path of doing logout (optional, defaults to `/logout`) - `excludedPatterns` string array for non-authorized urls (optional, support `?`, `*` and `**` wildcards) diff --git a/package.json b/package.json index f60115b..37b4749 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "fastify-keycloak-adapter", "author": "Yubin, Hsu", - "version": "0.5.1", + "version": "0.6.0", "main": "dist/keycloak", "types": "dist/keycloak", "keywords": [ @@ -25,7 +25,7 @@ "scripts": { "build": "tsc", "test": "jest --coverage --runInBand --detectOpenHandles", - "fix-prettier": "prettier --write \"./{src,test}/**/*.ts\"" + "fix-prettier": "prettier --write \"./src/**/*.ts\"" }, "dependencies": { "@fastify/cookie": "^6.0.0", diff --git a/src/keycloak.ts b/src/keycloak.ts index 92b32c6..8f2c7ac 100644 --- a/src/keycloak.ts +++ b/src/keycloak.ts @@ -47,10 +47,12 @@ export type KeycloakOptions = { clientSecret?: string logoutEndpoint?: string excludedPatterns?: Array + scope?: Array userPayloadMapper?: (userPayload: UserInfo) => {} } export default fastifyPlugin(async (fastify: FastifyInstance, opts: KeycloakOptions) => { + console.log(`opts = ${inspect(opts, false, null)}`) function getWellKnownConfiguration(url: string) { return TE.tryCatch( () => axios.get(url), @@ -101,7 +103,7 @@ export default fastifyPlugin(async (fastify: FastifyInstance, opts: KeycloakOpti authorize_url: config.authorization_endpoint, access_url: config.token_endpoint, callback: '/', - scope: ['openid'], + scope: opts.scope ?? ['openid'], nonce: true } }) diff --git a/src/tests/server.test.ts b/src/tests/server.test.ts index f084add..019c8c9 100644 --- a/src/tests/server.test.ts +++ b/src/tests/server.test.ts @@ -1,4 +1,3 @@ -import axios from 'axios' import { FastifyInstance } from 'fastify' import { KeycloakContainer, StartedKeycloakContainer } from 'testcontainers-keycloak' import { KeycloakOptions } from '../keycloak'