0.0.6 • Published 2 years ago

@glamtime/oauth-oidc-client v0.0.6

Weekly downloads
-
License
GPL
Repository
-
Last release
2 years ago

Angular Lib for OpenID Connect & OAuth2

Secure your Angular app using the latest standards for OpenID Connect & OAuth2. Provides support for token refresh, all modern OIDC Identity Providers and more.

Implemented specs & features

The following client/RP features from OpenID Connect/OAuth2.0 specifications are implemented by openid-client.

Updates to draft specifications (DPoP, JARM, etc) are released as MINOR library versions, if you utilize these specification implementations consider using the tilde ~ operator in your package.json since breaking changes may be introduced as part of these version updates.

Certification


This library has certified that openid-client conforms to the following profiles of the OpenID Connect™ protocol

  • Basic, Implicit, Hybrid, Config, Dynamic, and Form Post RP
  • FAPI 1.0 Advanced RP

Features

Installation

Npm / Yarn

Navigate to the level of your package.json and type

 npm i @glamtime/oauth-oidc-client

or with yarn

 yarn add @glamtime/oauth-oidc-client

Configuration

Import the AuthModule in your module.

import {NgModule} from '@angular/core';
import {OAuthModule} from '@glamtime/oauth-oidc-client';

// ...

@NgModule({
  // ...
  imports: [
    // ...
    OAuthModule.forRoot({
      issuer: '<your authority address here>',
      clientId: '<your clientId>',
      clientSecret: '<your clientSecret>'
    }),
  ],
  // ...
})
export class AppModule {
}

And call the method checkAuth() from your app.component.ts. The method checkAuth() is needed to process the redirect from your Security Token Service and set the correct states. This method must be used to ensure the correct functioning of the library.

import {Component, OnInit} from '@angular/core';
import {OAuthService} from '@glamtime/oauth-oidc-client';

@Component({
  /*...*/
})
export class AppComponent implements OnInit {
  constructor(public authService: OAuthService) {
  }

  ngOnInit() {
    oauthService.checkAuth().subscribe();
  }

  onLogin() {
    this.oauthService.login().subscribe();
  }

  onLogout() {
    this.oauthService.logout().subscribe();
  }
}

Http Interceptor

The HttpClient allows you to implement HTTP interceptors to tap into requests and responses. A common use case would be to intercept any outgoing HTTP request and add an authorization header.

Note: Do not send the access token with requests for which the access token is not intended!

You can configure the resource servers that you want to send a token with in the configuration:

OAuthModule.forRoot({
  config: {
    // ...
    resourceServers: ['https://my-secure-url.com/', 'https://my-second-secure-url.com/'],
  },
}),

The lib provides an own interceptor implementation which you can register like any other HTTP interceptor: and use the interceptor the lib provides you

import {OAuthInterceptor, OAuthModule} from 'angular-auth-oidc-client';

@NgModule({
  // ...
  imports: [
    // ...
    OAuthModule.forRoot(),
    HttpClientModule,
  ],
  providers: [
    {provide: HTTP_INTERCEPTORS, useClass: OAuthInterceptor, multi: true},
    // ...
  ],
})
export class AppModule {
}

If you configured a route to be protected, every child route underneath is protected, too. So if you configure https://example.org/api the token is also added to a request to the route https://example.org/api/users.

In case you are running multiple configurations all the configured routes over all configurations are collected and compared against the currently requested route. If a match is made, the token for the configuration you added the secure route to is being taken and applied in the Authorization header.

Keep in mind that you always can implement your own interceptor as described in the Angular documentation.