Skip to main content

Getting Started

Let's discover Keycloak Connector in less than 15 minutes.

🕒 Can't wait? Run it now with Docker Quickstart.

What you'll need​

Configure Keycloak​

Following FAPI guidance, several changes to your Keycloak configuration may be required.

info

Changing Realm configurations may impact existing Clients if they are not similarly configured.

Realm Configuration​

Sessions​

  • SSO Session Idle: 4 hours optional
  • SSO Session Max: 1 day optional

Tokens​

  • Default Signature Algorithm: PS256
  • Revoke Refresh Token: Enabled recommended
  • Refresh token max reuse: 0 recommended
  • Access Token Lifespan: 15 minutes optional

Client Configuration​

Settings​

  • Root URL http://localhost:5000* your backend server
  • Valid redirect URIs /auth/callback*
  • Web origins +
  • Admin URL /auth/k-admin-url*
  • Client authentication On
  • Authentication flow Standard flow
  • Front channel logout Off
Backchannel logout URL

When running Keycloak in a Docker instance and your NodeJs server on your host machine, try: http://host.docker.internal:5000/auth/k_logout

  • Backchannel logout URL http://localhost:5000/auth/k-logout
  • Backchannel logout session required On

Credentials​

warning

Clients must authenticate using Signed JWT (PS256) in production, Keycloak Connector will not start otherwise.

  • Client Authenticator Client Id and Secret dev only
  • Access token signature algorithm PS256
  • ID token signature algorithm PS256
  • User info signed response algorithm PS256
  • Request object signature algorithm PS256
  • Authorization response signature algorithm PS256
  • Proof Key for Code Exchange Code Challenge Method S256

Configure Webserver​

server.js
import express from 'express';
import {keycloakConnectorExpress, lock} from "@dapperduckling/keycloak-connector-server";
import cookieParser from "cookie-parser"

const serverPort = 5000;

// Grab express app
const app = express();

// Register the cookie parser
app.use(cookieParser());

// Initialize keycloak connector server
await keycloakConnectorExpress(app, {
realm: 'kcc-example',
clientId: 'example-express-app',
clientSecret: '***REPLACE WITH CLIENT SECRET FROM KEYCLOAK***', // Dev only
DANGEROUS_disableJwtClientAuthentication: true, // Dev only
fetchUserInfo: true,
serverOrigin: `http://localhost:${serverPort}`, // This server's origin
authServerUrl: 'http://localhost:8080/', // Your keycloak server here
});

// Register a public route on the app
app.get('/', (req, res) => {
res.send(`Public route`);
});

// Create a new router to secure all routes behind
const router = express.Router();

// Only authentication required route
router.get('/private', (req, res) => {
res.send(`Private route`);
});

// Lock all routes in the router behind a login page
app.use(lock(), router);

// Start the server
app.listen(serverPort, () => {
console.log(`express :: listening at http://localhost:${serverPort}`);
});

Launch the server​

node server.js

Test it!​

PurposeUrl
Public Routehttp://localhost:5000
Private Routehttp://localhost:5000/private
Loginhttp://localhost:5000/auth/login
Logouthttp://localhost:5000/auth/logout