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

@universis/keycloak

v3.1.2

Published

Keycloak Client Service for Universis Api Server

Downloads

190

Readme

Keycloak Client Service for Universis Api Server

@universis/keycloak allows Universis Api Server to use Keycloak as backend authorization server.

Usage

Install @universis/keycloak as dependency of Universis Api Server:

npm i @universis/keycloak

Service Configuration

Use KeycloakClientService as service strategy of OAuth2ClientService of Universis Api Server

server/config/app.production.json

{
  "services": [
    {
      "serviceType": "./services/oauth2-client-service#OAuth2ClientService",
      "strategyType": "@universis/keycloak#KeycloakClientService"
    }
  ]
}

Note: Check @themost/express version and update to ^1.3.5.

npm update @themost/express

Application Configuration

server_uri

Configure Universis Api Server to use Keycloak endpoints to validate user access:

{
    "settings": {
      "auth": {
        "server_uri": "https://<keycloak server>/auth/realms/<keycloak realm>/protocol/openid-connect/",
        "client_id": "universis-client",
        "client_secret": "secret"
      }
    }
}
issuer_uri

The issuer_uri attribute is the base URL of the Keycloak server.

{
    "settings": {
      "auth": {
        "issuer_uri": "https://<keycloak server>/auth/realms/<keycloak realm>/",
        "client_id": "universis-client",
        "client_secret": "secret"
      }
    }
}

This setting will be used to validate and get well known configuration of the Keycloak server and identity the endpoints that are going to be used by KeycloakClientService. The usage of server_uri is optional and may be omitted if issuer_uri is defined.

client_id

The client_id attribute is the client identifier of the Universis Api Server.

client_secret

The client_secret attribute is the client secret of the Universis Api Server. You should enable confidential access type for a keycloak client to get a client secret.

admin_uri

The admin_uri attribute is the base URL of the Keycloak server REST API. If admin_uri is not defined, the issuer_uri will be used to construct the admin URI.

{
    "settings": {
      "auth": {
        "issuer_uri": "https://<keycloak server>/auth/realms/<keycloak realm>/",
        "client_id": "universis-client",
        "client_secret": "secret",
        "admin_uri": "https://<keycloak server>/auth/admin/realms/<keycloak realm>/"
      }
    }
}

User management

KeycloakClientService offers operations for managing users. If you want to use keycloak api inside your application extend application configuration:

{
  "settings": {
    "auth": {
      "server_uri": "https://<keycloak server>/auth/realms/<keycloak realm>/protocol/openid-connect/",
      "client_id": "universis-client",
      "client_secret": "secret",
      "admin_uri": "https://<keycloak server>/auth/admin/realms/<keycloak realm>/",
      "adminAccount": {
        "client_id": "admin-cli",
        "grant_type": "password",
        "username": "<a user who has query-users and manage-users roles>",
        "password": "<user password>"
      }
    }
  }
}

Client Application Configuration

Following Universis Api Server configuration update, configure each client application respectively:

e.g. change settings#auth section of universis-students configuration

"auth": {
    "authorizeURL":"https://<keycloak server>/auth/realms/<keycloak realm>/protocol/openid-connect/auth",
    "logoutURL":"https://<keycloak server>/auth/realms/<keycloak realm>/protocol/openid-connect/logout?redirect_uri=http://localhost:7001/#/auth/login",
    "userProfileURL":"https://<api server>/api/users/me",
    "oauth2": {
        "clientID": "universis-client",
        "callbackURL": "http://localhost:7001/auth/callback/index.html",
        "scope": [
            "students"
        ]
    }
}

Token introspection

Token introspection is the process of sending a token to the authorization server to determine the active state of the token and to find out other information such as token type scope etc.

@universis/keycloak^3.0.0 implements 2 different mechanisms for token introspection:

  1. HttpTokenIntrospectionStrategy which uses HTTP POST request to introspect a token.
  2. JwtTokenIntrospectionStrategy which uses JWT token introspection mechanism.

Both strategies are enabled by registering them at services section of application configuration. The HttpTokenIntrospectionStrategy is used by default.

{
  "services": [
    {
      "serviceType": "@universis/keycloak#KeycloakTokenIntrospectionStrategy",
      "strategyType": "@universis/keycloak#HttpTokenIntrospectionStrategy"
    }
  ]
}

or

{
  "services": [
    {
      "serviceType": "@universis/keycloak#KeycloakTokenIntrospectionStrategy",
      "strategyType": "@universis/keycloak#HttpTokenIntrospectionStrategy"
    }
  ]
}

HttpTokenIntrospectionStrategy may use caching while introspecting a token against Keycloak server. Use settings/auth/introspectionLifespan attribute which defines the absolute expiration timeout in seconds.

{
  "settings": {
    "auth": {
      "introspectionLifespan": 120
    }
  }
}