Need private packages and team management tools?Check out npm Teams »


2.0.0 • Public • Published


Keycloak Node.js minimalist connector for backend services integration. It aims to serve as base for high performance authorization middlewares.

Note: Version 2.x uses jsonwebtoken 8.x

Keycloak Introduction

The awesome open-source Identity and Access Management solution develop by RedHat. Keycloak support those very nice features you are looking for:

  • Single-Sign On
  • LDAP and Active Directory
  • Standard Protocols
  • Social Login
  • Clustering
  • Custom Themes
  • Centralized Management
  • Identity Brokering
  • Extensible
  • Adapters
  • High Performance
  • Password Policies

More about Keycloak:

Using the keycloak-backend module


const keycloak = require('keycloak-connector')({
    "realm": "your realm name",
    "auth-server-url": "",
    "client_id": "your client name",
    "client_secret": "c88a2c21-9d1a-4f83-a18d-66d75c4d8020", // if required
    "username": "your service username",
    "password": "your service password"

Validating access tokens

Online validation:

This method requires online connection to the Keycloak service to validate the access token. It is highly secure since it also check for the possible token invalidation. The disadvantage is that a request to the Keycloak service happens on every validation attempt.

let token = await keycloak.jwt.verify(someAccessToken);
//console.log(token.hasApplicationRole('app-client-name', 'some-role'));

Offline validation:

This method perform offline JWT verification against the access token using the Keycloak Realm public key. Performance is higher compared to the online method, the disadvantage is that access token invalidation will not work until the token is expired.

let cert = fs.readFileSync('public_cert.pem');
token = await keycloak.jwt.verifyOffline(someAccessToken, cert);
//console.log(token.hasApplicationRole('app-client-name', 'some-role'));

Generating service access token

Efficiently maintaining a valid access token can be hard. Get it easy by using:

let accessToken = await keycloak.accessToken.get()


request.get('', {
  'auth': {
    'bearer': await keycloak.accessToken.get()

For this feature, the authentication details described in the configuration options are used.

Retrieve users information by id

Sometimes backend services only have a target user identifier to digg for details, in such cases, you can contact the Keycloak service by:

Retrieve user details by id

let details = await keycloak.users.details(uid);

Retrieve user roles by id

let details = await keycloak.users.roles(uid, [
    // clients id here for roles retrieval
  true // include realm roles ?




npm i keycloak-backend

DownloadsWeekly Downloads






Unpacked Size

13.2 kB

Total Files


Last publish


  • avatar