# angular-oauth2-oidc **Repository Path**: mirrors_maxisam/angular-oauth2-oidc ## Basic Information - **Project Name**: angular-oauth2-oidc - **Description**: Support for OAuth 2 and OpenId Connect (OIDC) in Angular. - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-08-09 - **Last Updated**: 2026-08-08 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # angular-oauth2-oidc Support for OAuth 2 and OpenId Connect (OIDC) in Angular.  ## Credits - [generator-angular2-library](https://github.com/jvandemo/generator-angular2-library) for scaffolding a angular library - [jsrasign](https://kjur.github.io/jsrsasign/) for validating token signature and for hashing - [Identity Server](https://github.com/identityserver) (used for Testing with an .NET/.NET Core Backend) - [Keycloak (Redhad)](http://www.keycloak.org/) for Testing with Java ## Resources - Sources and Sample: https://github.com/manfredsteyer/angular-oauth2-oidc - Source Code Documentation https://manfredsteyer.github.io/angular-oauth2-oidc/angular-oauth2-oidc/docs/ ## Tested Environment Successfully tested with the Angular 2 and 4 and its Router, PathLocationStrategy as well as HashLocationStrategy and CommonJS-Bundling via webpack. At server side we've used IdentityServer (.NET/ .NET Core) and Redhat's Keycloak (Java). ## New Features in Version 2.1 - New Config API (the original one is still supported) - New convenience methods in OAuthService to streamline default tasks: - ``setupAutomaticSilentRefresh()`` - ``loadDiscoveryDocumentAndTryLogin()`` - Single Sign out through Session Status Change Notification according to the OpenID Connect Session Management specs. This means, you can be notified when the user logs out using at the login provider. - Possibility to define the ValidationHandler, the Config as well as the OAuthStorage via DI - Better structured documentation ## New Features in Version 2 - Token Refresh for Implicit Flow by implementing "silent refresh" - Validating the signature of the received id_token - Providing Events via the observable ``events``. - The event ``token_expires`` can be used togehter with a silent refresh to automatically refresh a token when/ before it expires (see also property ``timeoutFactor``). ## Additional Features - Logging in via OAuth2 and OpenId Connect (OIDC) Implicit Flow (where user is redirected to Identity Provider) - "Logging in" via Password Flow (where user enters his/her password into the client) - Token Refresh for Password Flow by using a Refresh Token - Automatically refreshing a token when/ some time before it expires - Querying Userinfo Endpoint - Querying Discovery Document to ease configuration - Validating claims of the id_token regarding the specs - Hook for further custom validations - Single-Sign-Out by redirecting to the auth-server's logout-endpoint ## Breaking Changes in Version 2 - The property ``oidc`` defaults to ``true``. - If you are just using oauth2, you have to set ``oidc`` to ``false``. Otherwise, the validation of the user profile will fail! - By default, ``sessionStorage`` is used. To use ``localStorage`` call method setStorage - Demands using https as OIDC and OAuth2 relay on it. This rule can be relaxed using the property ``requireHttps``, e. g. for local testing. - Demands that every url provided by the discovery document starts with the issuer's url. This can be relaxed by using the property ``strictDiscoveryDocumentValidation``. ## Sample-Auth-Server You can use the OIDC-Sample-Server mentioned in the samples for Testing. It assumes, that your Web-App runns on http://localhost:8080. Username/Password: max/geheim *clientIds:* - spa-demo (implicit flow) - demo-resource-owner (resource owner password flow) *redirectUris:* - localhost:[8080-8089|4200-4202] - localhost:[8080-8089|4200-4202]/index.html - localhost:[8080-8089|4200-4202]/silent-refresh.html ## Installing ``` npm i angular-oauth2-oidc --save ``` ## Importing the NgModule ```TypeScript import { OAuthModule } from 'angular-oauth2-oidc'; [...] @NgModule({ imports: [ [...] HttpModule, OAuthModule.forRoot() ], declarations: [ AppComponent, HomeComponent, [...] ], bootstrap: [ AppComponent ] }) export class AppModule { } ``` ## Configuring for Implicit Flow This section shows how to implement login leveraging implicit flow. This is the OAuth2/OIDC flow best suitable for Single Page Application. It sends the user to the Identity Provider's login page. After logging in, the SPA gets tokens. This also allows for single sign on as well as single sign off. To configure the library the following sample uses the new configuration API introduced with Version 2.1. Hence, The original API is still supported. ```TypeScript import { AuthConfig } from 'angular-oauth2-oidc'; export const authConfig: AuthConfig = { // Url of the Identity Provider issuer: 'https://steyer-identity-server.azurewebsites.net/identity', // URL of the SPA to redirect the user to after login redirectUri: window.location.origin + '/index.html', // The SPA's id. The SPA is registerd with this id at the auth-server clientId: 'spa-demo', // set the scope for the permissions the client should request // The first three are defined by OIDC. The 4th is a usecase-specific one scope: 'openid profile email voucher', } ``` Configure the OAuthService with this config object when the application starts up: ```TypeScript import { OAuthService } from 'angular-oauth2-oidc'; import { JwksValidationHandler } from 'angular-oauth2-oidc'; import { authConfig } from './auth.config'; import { Component } from '@angular/core'; @Component({ selector: 'flight-app', templateUrl: './app.component.html' }) export class AppComponent { constructor(private oauthService: OAuthService) { this.configureWithNewConfigApi(); } private configureWithNewConfigApi() { this.oauthService.configure(authConfig); this.oauthService.tokenValidationHandler = new JwksValidationHandler(); this.oauthService.loadDiscoveryDocumentAndTryLogin(); } } ``` ### Implementing a Login Form After you've configured the library, you just have to call ``initImplicitFlow`` to login using OAuth2/ OIDC. ```TypeScript import { Component } from '@angular/core'; import { OAuthService } from 'angular-oauth2-oidc'; @Component({ templateUrl: "app/home.html" }) export class HomeComponent { constructor(private oauthService: OAuthService) { } public login() { this.oauthService.initImplicitFlow(); } public logoff() { this.oauthService.logOut(); } public get name() { let claims = this.oAuthService.getIdentityClaims(); if (!claims) return null; return claims.given_name; } } ``` The following snippet contains the template for the login page: ```HTML