all files / src/ open-id-connect-configuration-manager.ts

100% Statements 35/35
100% Branches 10/10
100% Functions 13/13
100% Lines 35/35
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96                                                                44×                 10×         10×                     14×              
import { UserManagerSettings } from 'oidc-client';
import { OpenIdConnectConfiguration } from './index';
 
const defaultClientUri = 'https://localhost:9000';
 
export default class {
 
  [key: string]: any;
 
  // tslint:disable-next-line:variable-name
  private _loginRedirectRoute: string = '/';
  // tslint:disable-next-line:variable-name
  private _logoutRedirectRoute: string = '/';
  // tslint:disable-next-line:variable-name
  private _unauthorizedRedirectRoute: '/';
  // tslint:disable-next-line:variable-name
  private _logLevel: 0;
  // tslint:disable-next-line:variable-name
  private _userManagerSettings: UserManagerSettings = {
    authority: 'https://localhost:5000',
    client_id: 'Aurelia.OpenIdConnect',
    loadUserInfo: true,
    post_logout_redirect_uri: `${defaultClientUri}/signout-oidc`,
    redirect_uri: `${defaultClientUri}/signin-oidc`,
    response_type: 'id_token token',
    scope: 'openid email roles profile',
    silent_redirect_uri: `${defaultClientUri}/signin-oidc`,
  };
 
  public get loginRedirectRoute(): string {
    return this._loginRedirectRoute;
  }
 
  public get logoutRedirectRoute(): string {
    return this._logoutRedirectRoute;
  }
 
  public get unauthorizedRedirectRoute(): string {
    return this._unauthorizedRedirectRoute;
  }
 
  public get logLevel(): number {
    return this._logLevel;
  }
 
  public get userManagerSettings(): UserManagerSettings {
    return this._userManagerSettings;
  }
 
  // This convenience property adheres to the Law of Demeter.
  public get redirectUri(): string {
    return this._userManagerSettings.redirect_uri as string;
  }
 
  // This convenience property adheres to the Law of Demeter.
  public get postLogoutRedirectUri(): string {
    return this._userManagerSettings.post_logout_redirect_uri as string;
  }
 
  constructor(dto?: OpenIdConnectConfiguration) {
 
    // populate top level fields
    if (!dto) {
      return;
    }
 
    Object.keys(dto)
      .filter((key) => dto[key] !== undefined && dto[key] !== null)
      .forEach((key) => {
 
        this.ensureRouteValueBeginsWithSlash(key, dto[key]);
 
        this['_' + key] = dto[key];
      });
 
    // populate user manager properties
    if (!dto.userManagerSettings) {
      return;
    }
 
    Object.keys(dto.userManagerSettings).forEach((k) => {
      // Use a type assertion to suppress the implicty any error.
      // Element implicitly has an 'any' type because type 'UserManagerSettings' has no index signature.
      (this.userManagerSettings as any)[k] = (dto.userManagerSettings as any)[k];
    });
  }
 
  // Ensure that relative paths (routes) start with a forward slash.
  private ensureRouteValueBeginsWithSlash(key: string, val: string) {
    if (key.endsWith('Route') && !val.startsWith('/')) {
      const message = `The configured "${key}" must begin with a slash`;
      throw new RangeError(message);
    }
  }
}