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

100% Statements 58/58
85% Branches 17/20
100% Functions 11/11
100% Lines 49/49
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                                                                      
import { autoinject } from 'aurelia-framework';
import { Router, RouterConfiguration } from 'aurelia-router';
import { User, UserManager, UserManagerEvents } from 'oidc-client';
import { UserManagerEventHandler, UserManagerEventsAction } from './internal-types';
import OpenIdConnectConfigurationManager from './open-id-connect-configuration-manager';
import { LoginRedirectKey } from './open-id-connect-constants';
import OpenIdConnectLogger from './open-id-connect-logger';
import OpenIdConnectRouting from './open-id-connect-routing';
 
@autoinject
export default class OpenIdConnect {
 
  constructor(
    private openIdConnectRouting: OpenIdConnectRouting,
    private router: Router,
    private configuration: OpenIdConnectConfigurationManager,
    public logger: OpenIdConnectLogger,
    public userManager: UserManager) { }
 
  public configure(routerConfiguration: RouterConfiguration) {
 
    if (typeof routerConfiguration === 'undefined' || routerConfiguration === null) {
      throw new Error('routerConfiguration parameter must not be undefined or null');
    }
 
    this.openIdConnectRouting.configureRouter(routerConfiguration);
  }
 
  public async login(Eargs: any = {}): Promise<void> {
    if (this.router.currentInstruction) {
      const loginRedirectValue = this.router.currentInstruction.queryParams[LoginRedirectKey];
      if (loginRedirectValue) {
        args.data = { ...args.data };
        args.data[LoginRedirectKey] = loginRedirectValue;
      }
    }
 
    await this.userManager.signinRedirect(args);
  }
 
  public async logout(Eargs: any = {}): Promise<void> {
    try {
      await this.userManager.signoutRedirect(args);
    } catch (err) {
      if (err.message === 'no end session endpoint') {
        this.logger.debug(err);
        this.logger.debug('The user remains logged in at the authorization server.');
        this.router.navigate(this.configuration.logoutRedirectRoute);
      } else {
        throw err;
      }
    }
  }
 
  public loginSilent(Eargs: any = {}): Promise<User> {
    return this.userManager.signinSilent(args);
  }
 
  public getUser(): Promise<User> {
    return this.userManager.getUser();
  }
 
  public addOrRemoveHandler(
    key: keyof UserManagerEvents,
    handler: UserManagerEventHandler) {
 
    if (!key.startsWith('add') && !key.startsWith('remove')) {
      let message = 'The \'addOrRemoveHandlers\' method expects a \'key\' argument ';
      message += 'that starts with either \'add\' or \'remove\'. Instead we ';
      message += 'recevied ' + key;
      throw new TypeError(message);
    }
 
    const addOrRemove = this.userManager.events[key] as UserManagerEventsAction;
    addOrRemove.call(this.userManager.events, handler);
  }
 
  public observeUser(callback: (user: User) => void): Promise<void> {
    this.addOrRemoveHandler('addUserLoaded', () => this.getUser().then(callback));
    this.addOrRemoveHandler('addUserUnloaded', () => this.getUser().then(callback));
    return this.getUser().then(callback);
  }
}