npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

passport-keycloak-oauth2-oidc

v1.0.5

Published

A Passport.js strategy for authenticating with Keycloak using the OAuth2/OIDC API

Downloads

20,204

Readme

passport-keycloak-oauth2-oidc

Passport strategy for authenticating with Keycloak using the OAuth2/OIDC API.

This module lets you authenticate using Keycloak in your Node.js applications. By plugging into Passport, Keycloak authentication can be easily and unobtrusively integrated into any application or framework that supports Connect-style middleware, including Express.

Install

$ npm install passport-keycloak-oauth2-oidc

Usage

Create an Application

Before using passport-keycloak-oauth2-oidc, you must create a realm and client with your Keycloak.

Configure Strategy

The Keycloak authentication strategy authenticates requests by delegating to your Keycloak server using the OpenID Connect (OIDC/OAuth 2.0) protocol.

When using this strategy, it's AuthorizationURL and TokenURL options are generated based on the authServerURL and realm options. You can find these two option values from the Applications->Installation section, or from the OAuth Clients->Installation section in your keycloak realm.

Applications must supply a verify callback which accepts an accessToken, refreshToken and service-specific profile, and then calls the done callback supplying a user, which should be set to false if the credentials are not valid. If an exception occured, err should be set.

Options:

  • realm Name of your KeyCloak realm (set to master by default).
  • authServerURL Base URL for you Realm authorization endpoint.
  • publicClient If your Keycloak client's Access Type is set to public (publicClient set to true by default).
  • clientID This will match your Application Name, resource or OAuth Client Name.
  • clientSecret If your Keycloak client's Access Type is set to confidential this is required (publicClient set to false).
  • callbackURL URL to which KeyCloak will redirect the user after granting authentication.
  • sslRequired requires SSL for (all|external|none) requests (set to external by default).

Examples:

  var KeyCloakStrategy = require('passport-keycloak-oauth2-oidc').Strategy;
  passport.use(new KeyCloakStrategy({
      clientID: 'myOauthClient',
      realm: 'MyKeyCloakRealm',
      publicClient: 'false',
      clientSecret: '6ee0f303-faef-42d7-ba8e-00cdec755c42',
      sslRequired: 'external',
      authServerURL: 'https://keycloak.example.com/auth',
      callbackURL: 'https://www.example.com/keycloak/callback'
    },
    function(accessToken, refreshToken, profile, done) {
      User.findOrCreate(..., function err, user) {
        done(err, user);
      });
    }
  });

Authenticate Requests

Use passport.authenticate(), specifying the 'keycloak' strategy, to authenticate requests.

For example, as route middleware in an Express application:

app.get('/auth/keycloak',
  passport.authenticate('keycloak', { scope: ['profile'] }));

app.get('/auth/keycloak/callback', 
  passport.authenticate('keycloak', { failureRedirect: '/login' }),
  function(req, res) {
    // Successful authentication, redirect home.
    res.redirect('/');
  });

How to get Roles

By default, Keycloak returns Roles information within AccessToken.

If you are wondering to fetch Roles (e.g. realm_access roles, resource_access roles etc) within UserInfo endpoint, please make sure that Keycloak returns those claims .

To add these claims to the UserInfo endpoint, edit the roles settings in the Client Scopes:

Clients Scopes -> roles -> settings:

  • name: roles
  • Include In Token Scope: enable

and in the client roles mappers settings, an example mapping :

  • name: client roles
  • mapper type: user client role
  • multivalued: on
  • token claim name: roles.resource_access.${client_id}.roles
  • claim JSON type: string
  • add to userinfo: enabled

License

The MIT License