Keycloak Angular
Easy Keycloak setup for Angular applications.
About
This library helps you to use keycloak-js in Angular > v4.3 applications providing the following features:
- A Keycloak Service which wraps the keycloak-js methods to be used in Angular, giving extra functionalities to the original functions and adding new methods to make it easier to be consumed by Angular applications.
- Generic AuthGuard implementation, so you can customize your own AuthGuard logic inheriting the authentication logic and the roles load.
- A HttpClient interceptor that adds the authorization header to all HttpClient requests. It is also possible to disable this interceptor or exclude routes from having the authorization header.
- This documentation also assists you to configure the keycloak in your Angular applications and with the client setup in the admin console of your keycloak installation.
Install
keycloak-angular
npm i --save keycloak-angular
keycloak-js
Since keycloak-angular v.7.0.0, the keycloak-js dependency became a peer dependency. This change allows greater flexibility for choosing the keycloak-js adapter version and follows the project documentation recommendation.
npm i --save keycloak-js@version
Choosing the keycloak-js version
The keycloak-js adapter documentation recommends to use the same version of your Keycloak / RH-SSO (Red Hat Single Sign On) installation.
A best practice is to load the JavaScript adapter directly from Keycloak Server as it will automatically be updated when you upgrade the server. If you copy the adapter to your web application instead, make sure you upgrade the adapter only after you have upgraded the server.
Setup
Angular
The following topics explain two different ways to bootstrap the library and configure keycloak-angular. The first one is by using the APP_INITIALIZER token and the second option uses ngDoBootstrap. You will have to choose one of them.
Using APP_INITIALIZER
The KeycloakService can be initialized during the application loading, using the APP_INITIALIZER token.
AppModule
;;; @ {}
Initializer Function
This function can be named and placed in the way you think is most appropriate. In the
underneath example it was placed in a separate file app-init.ts
and the function was called
initializer
.
; : () => Promise<any> { return : Promise<any> keycloak;}
Using ngDoBootstrap
The KeycloakService can be initialized before the application loading. When the Keycloak initialization is successful the application is bootstrapped.
This has two major benefits.
- This is faster because the application isn't fully bootstrapped and
- It prevents a moment when you see the application without having the authorization.
AppModule
;; const keycloakService = ; @ implements DoBootstrap { keycloakService ; }
Keycloak
Besides configuring the keycloak lib in your application it is also necessary to setup the access - scope for the account client.
In this documentation we assume that you already installed and configured your Keycloak instance, as well created the client app.
Hint: If you need to create an environment for testing purposes, try out the Keycloak demo or the official keycloak docker image.
Client configuration
When requesting the method to get the User's Profile, the client app should have the scope and access to the account view-profile role. To do it, access Clients ➡️ My-app ➡️ Scope. Select the account app in Client Roles and assign the view-profile role.
Please be aware that when accessing the https://keycloak/auth/realms/REALM/account
endpoint, if this role is not configured, a misleading No 'Access-Control-Allow-Origin'
error might show up. If that happens, this role should be enough to fix it, no changes in the Web Origin of the account client is needed.
AuthGuard
A generic AuthGuard, KeycloakAuthGuard
, was created to help you bootstrap your security configuration and avoid duplicate code. This class already checks if the user is logged in and get the list of roles from the authenticated user, provided by the keycloak instance. In your implementation you just need to implement the desired security logic.
Example:
;;; @ implements CanActivate { superrouter keycloakAngular; } : Promise<boolean> return { if !thisauthenticated thiskeycloakAngular ; return ; const requiredRoles: string = routedataroles; if !requiredRoles || requiredRoleslength === 0 return ; else if !thisroles || thisroleslength === 0 ; ; };
HttpClient Interceptor
By default all HttpClient requests will add the Authorization header in the format of: Authorization: Bearer TOKEN.
There is also the possibility to exclude a list of URLs that should not have the authorization header. The excluded list must be informed in the keycloak initialization. For example:
try await keycloak; ; catch error {}
Contributors
Mauricio Gemelli Vigolo |
Jon Koops |
Frederik Prijck |
Jonathan Share |
jmparra |
Marcel Német |
Raphael Alex Silva Abreu |
---|
If you want to contribute to the project, please check out the contributing document.
License
keycloak-angular is licensed under the MIT.
keycloak-js is licensed under the Apache 2.0.