Giter VIP home page Giter VIP logo

keycloak-backend's Introduction

keycloak-backend

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: http://www.keycloak.org/

Using the keycloak-backend module

Instantiating

const keycloak = require('keycloak-backend')({
    "realm": "your realm name",
    "auth-server-url": "http://keycloak.dev:8080",
    "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.isExpired());
//console.log(token.hasRealmRole('user'));
//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.isExpired());
//console.log(token.hasRealmRole('user'));
//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()

Then:

request.get('http://serviceb.com/v1/fetch/accounts', {
  '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 ?
);

Tests

WIP

keycloak-backend's People

Contributors

jkyberneees avatar monaka avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.