3.2.3 • Published 7 months ago

@webundsoehne/nestjs-keycloak v3.2.3

Weekly downloads
-
License
ISC
Repository
github
Last release
7 months ago

@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
  }
}
3.2.3

7 months ago

3.2.2

1 year ago

3.0.0-beta.1

2 years ago

3.0.0-beta.3

2 years ago

3.0.0-beta.2

2 years ago

2.3.0

2 years ago

2.3.0-beta.1

2 years ago

2.3.2

2 years ago

2.3.1

2 years ago

3.0.4

2 years ago

3.2.1

1 year ago

3.0.3

2 years ago

3.2.0

1 year ago

3.1.1

1 year ago

3.0.2

2 years ago

3.1.0

1 year ago

3.0.1

2 years ago

3.0.6

1 year ago

3.0.5

1 year ago

3.0.0

2 years ago

2.2.0

2 years ago

2.2.0-beta.2

2 years ago

2.2.0-beta.1

2 years ago

2.1.0

2 years ago

2.0.2

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.0.13-beta.1

2 years ago

1.0.12-beta.4

2 years ago

1.0.12

2 years ago

1.0.12-beta.3

2 years ago

1.0.12-beta.2

2 years ago

1.0.12-beta.1

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago