Angular Lib for OpenID Connect Code Flow with PKCE and Implicit Flow
OpenID Code Flow with PKCE, OpenID Connect Implicit Flow
OpenID Certification
This library is certified by OpenID Foundation. (RP Implicit and Config RP)
Features
- version 4.1.0 Angular 4 to Angular 5.2.10, Version 6.0.0, Angular 6 onwards
- Supports OpenID Connect Code Flow with PKCE
- Supports OpenID Connect Implicit Flow http://openid.net/specs/openid-connect-implicit-1_0.html
- Complete client side validation for REQUIRED features
- OpenID Connect Session Management 1.0 http://openid.net/specs/openid-connect-session-1_0.html
- AOT build
- Can be lazy loaded
Documentation : Quickstart | API Documentation | Changelog
Using the package
Navigate to the level of your package.json and type
npm install angular-auth-oidc-client
or with yarn
yarn add angular-auth-oidc-client
or you can add the npm package to your package.json
"angular-auth-oidc-client": "^10.0.10"
and type
npm install
Configuration
APP_INITIALIZER
Approach 1: Import the module and services in your module.
The OidcSecurityService
has a dependency on the HttpClientModule
which needs to be imported. The angular-auth-oidc-client module supports all versions of Angular 4.3 onwards.
Loading the configuration from the server
;;;;;; ;// if your config is on server side// const oidc_configuration = ${window.location.origin}/api/ClientAppSettings
assets/auth.clientConfiguration.json
See Auth documentation for the detail of each field.
At present only the 'code' with PKCE, 'id_token token' or the 'id_token' flows are supported:
"response_type": ["code" | "id_token token" | "id_token" ]
Note the configuration json must have a property stsServer for this to work.
Configuration without APP_INITIALIZER
Approach 2.
Custom STS server well known configuration
Sometimes it is required to load custom .well-known/openid-configuration. The load_using_custom_stsServer can be used for this.
Usage
Code Flow with PKCE
It is recomended flow in SPA applications, see SECURELY USING THE OIDC AUTHORIZATION CODE FLOW AND A PUBLIC CLIENT WITH SINGLE PAGE APPLICATIONS.
Not all security service providers and servers support it yet.
Create the login, logout component and use the oidcSecurityService
;;;
Implicit Flow (Not recommended)
This flow is no longer recommended, but some servers support this flow only, and not the Code flow with PKCE.
Create the login, logout component and use the oidcSecurityService
private doCallbackLogicIfRequired
And a simple template for the component.
button *ngIf="!isAuthenticated" ="login()"Login</button>button *ngIf="isAuthenticated" ="logout()"Logout</button> hr / Is Authenticated: br /br /
Silent Renew
When silent renew is enabled, a DOM event will be automatically installed in the application's host window.
The event oidc-silent-renew-message
accepts a CustomEvent
instance with the token returned from the OAuth server
in its detail
field.
The event handler will send this token to the authorization callback and complete the validation.
Point the silent_renew_url
property to an HTML file which contains the following script element to enable authorization.
Code Flow with PKCE
<script> window { /* The parent window hosts the Angular application */ var parent = windowparent; /* Send the id_token information to the oidc message handler */ var event = 'oidc-silent-renew-message' detail: windowlocation ; parent; };</script>
Silent Renew Angular-CLI
Add the silent-renew.html file to the angular.json assets configuration
"assets": "projects/sample-code-flow/src/silent-renew.html" "projects/sample-code-flow/src/favicon.ico" "projects/sample-code-flow/src/assets"
Using the access_token
In the http services, add the token to the header using the oidcSecurityService
private setHeaders
Using Guards
;;;; ;
Note about Guards
The guard should only be applied to protected URLs. The guard should not be active on the default route, where the authorization request is processed.
Adding custom parameters to the authorization request
Custom parameters can be added to the auth request by using the setCustomRequestParameters method. Here you could add ui_locale, acr or whatever you request for your token server.
this.oidcSecurityService.setCustomRequestParameters;
Custom Storage
If you need, you can create a custom storage (for example to use cookies).
Implement OidcSecurityStorage
class-interface and the read
and write
methods:
Then provide the class in the module:
See also oidc.security.storage.ts
for an example.
Http Interceptor
The HttpClient allows you to write interceptors. A common usecase would be to intercept any outgoing HTTP request and add an authorization header. Keep in mind that injecting OidcSecurityService into the interceptor via the constructor results in a cyclic dependency. To avoid this use the injector instead.
Authorizing in a popup or iframe
You can call the Provider's authorization endpoint in a popup or iframe instead of navigating to it in the app's parent window. This allows you to have the Provider's consent prompt display in a popup window to avoid unloading and reloading the app, or to authorize the user silently by loading the endpoint in a hidden iframe if that supported by the Provider.
To get the fully-formed authorization URL, pass a handler function to OidcSecurityService.authorize
(this will also prevent the default behavior of loading the authorization endpoint in the current window):
login
Silent Renew Implicit Flow
<script> window { /* The parent window hosts the Angular application */ var parent = windowparent; /* Send the id_token information to the oidc message handler */ var event = 'oidc-silent-renew-message' detail: windowlocationhash ; parent;};</script>
When silent renew is enabled, getIsAuthorized()
will attempt to perform a renew before returning the authorization state.
This allows the application to authorize a user, that is already authenticated, without redirects.
Silent renew requires CSP configuration, see next section.
X-Frame-Options / CSP ancestor / different domains
If deploying the client application and the STS server application with 2 different domains, the X-Frame-Options HTTPS header needs to allow all iframes. Then use the CSP HTTPS header to only allow the required domains. The silent renew requires this.
Add this header to responses from the server that serves your SPA:
Content-Security-Policy: script-src 'self' 'unsafe-inline';style-src 'self' 'unsafe-inline';img-src 'self' data:;font-src 'self';frame-ancestors 'self' https://localhost:44318;block-all-mixed-content
where https://localhost:44318
is the address of your STS server.
e.g. if you use NginX to serve your Angular application, it would be
http {
server {
...
add_header Content-Security-Policy "script-src 'self' 'unsafe-inline';style-src 'self' 'unsafe-inline';img-src 'self' data:;font-src 'self';frame-ancestors 'self' https://localhost:44318;block-all-mixed-content";
Examples using:
https://github.com/damienbod/AspNetCoreAngularSignalRSecurity
https://github.com/damienbod/dotnet-template-angular
https://github.com/damienbod/angular-auth-oidc-sample-google-openid
https://github.com/HWouters/ad-b2c-oidc-angular
Using src code directly:
https://github.com/damienbod/AspNet5IdentityServerAngularImplicitFlow
License
MIT