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

@webundsoehne/nestjs-keycloak

v3.2.3

Published

Keycloak modules for token verification and administration of Keycloak.

Downloads

353

Readme


@webundsoehne/nestjs-keycloak

Version Downloads/week Dependencies semantic-release

Description

This package includes Keycloak integration for NestJS.


Installation

Keycloak library versions are tightly coupled with the Keycloak server itself, therefore all the Keycloak related dependencies are peer dependencies. Please be sure to check out the compatibility table.

npm i @webundsoehne/nestjs-keycloak @keycloak/keycloak-admin-client keycloak-connect

Keycloak Admin Module

KeycloakAdminModule provides Keycloak REST API client through an administration user. This user can either be a superuser in the master realm or a realm-management user in a specific realm.

  • Add it to your specific module with the Keycloak client options.

    @Module({
      imports: [KeycloakAdminModule.register(ConfigService.get('keycloak.admin'))]
    })
    export class SomeModule {}
  • Inject it to services through @InjectKeycloak() decorator.

Keycloak Connect Module

KeycloakConnectModule provides a utility REST API to access Keycloak through connecting a private Keycloak client with a secret.

This enables us to validate user tokens and fetch more information about the user and decode the token through Keycloak. So it is intended for authentication checks.

  • Add it to your specific module with the Keycloak client options.

    @Module({
      imports: [KeycloakConnectModule.register(ConfigService.get('keycloak.connect'))]
    })
    export class SomeModule {}
  • Utilize it in your custom authentication guard by injecting it through decorators @InjectKeycloakConnect() and InjectKeycloakConnectOptions().

Compatibility

This library is compatible with RESTFUL and GraphQL APIs. But to avoid missing dependency errors, since they do require different additional dependencies.

Anything neutral is exported from the index of this library where RESTFUL API dependent tools are exported from ./dist/restful and GraphQL dependent tools are exported from ./dist/graphql.

Guards

This repository also has a neutral guard that can be configured through the options. This guard fetches the roles, groups, and scopes through Keycloak properly.

Since fetching the request is different for both RESTFUL APIs and GraphQL based APIs, they are exported from the respective specific endpoint.

Extending The Base Guards

Let us assume that we are in a GraphQL application and we do want to extend the default guard with some specific logic. Since in most of the cases we do want to verify the user is on Keycloak and fetch its properties, we can call the default guard first, then extend canActivate in the guard itself.

import { CanActivate, ExecutionContext, Inject, Injectable, UnauthorizedException } from '@nestjs/common'
import { Reflector } from '@nestjs/core'
import { Keycloak } from 'keycloak-connect'
import { Connection } from 'typeorm'

import { KeycloakConnectOptions } from '@webundsoehne/nestjs-keycloak'
import { AuthGuard as BaseAuthGuard } from '@webundsoehne/nestjs-keycloak/dist/graphql'

@Injectable()
export class AuthGuard extends BaseAuthGuard implements CanActivate {
  constructor(@InjectKeycloakConnect() keycloak: Keycloak, @InjectKeycloakConnectOptions() keycloakOptions: KeycloakConnectOptions, @Inject(Reflector) reflector: Reflector) {
    super(keycloak, keycloakOptions, reflector)
  }

  async canActivate(context: ExecutionContext): Promise<boolean> {
    await super.canActivate(context)

    const request = super.getRequest(context)

    if (!customLogic) {
      throw new UnauthorizedException(`Custom logic failed.`)
    }

    return true
  }
}