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

100% Statements 43/43
100% Branches 0/0
100% Functions 12/12
100% Lines 39/39
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                                                                                                      
import { autoinject } from 'aurelia-framework';
import { NavigationInstruction } from 'aurelia-router';
import { UserManager } from 'oidc-client';
import OpenIdConnectConfigurationManager from './open-id-connect-configuration-manager';
import OpenIdConnectLogger from './open-id-connect-logger';
 
// TODO: Move some of the route-definition logic from
// the open-id-connect-routing.ts file into this file instead.
// The current file, for instance, could define the
// { name, navigationStrategy, route } object instead of defining only
// the navigationStrategy implementation.
@autoinject
export default class OpenIdConnectNavigationStrategies {
 
  constructor(
    // @ts-ignore
    private logger: OpenIdConnectLogger,
    private openIdConnectConfiguration: OpenIdConnectConfigurationManager,
    private userManager: UserManager,
    private $window: Window) { }
 
  public async signInRedirectCallback(instruction: NavigationInstruction): Promise<any> {
    const callbackHandler = async () => {
      const args: any = {};
      return this.userManager.signinRedirectCallback(args);
    };
 
    const navigationInstruction = () => {
      // Use location.assign not instruction.config.redirect,
      // because the former adds the route to the web browser's history,
      // and that controls what will load on a page refresh.
      // See https://github.com/aurelia-contrib/aurelia-open-id-connect/issues/46
      this.$window.location.assign(this.openIdConnectConfiguration.loginRedirectRoute);
    };
 
    return this.runHandlerAndCompleteNavigationInstruction(
      callbackHandler,
      navigationInstruction);
  }
 
  public silentSignInCallback(instruction: NavigationInstruction): Promise<any> {
 
    const callbackHandler = async () => {
      return this.userManager.signinSilentCallback();
    };
 
    const navigationInstruction = () => {
      // This happens in a child iframe.
      instruction.config.redirect =
        this.openIdConnectConfiguration.loginRedirectRoute;
    };
 
    return this.runHandlerAndCompleteNavigationInstruction(
      callbackHandler,
      navigationInstruction);
  }
 
  public signOutRedirectCallback(instruction: NavigationInstruction): Promise<any> {
 
    const callbackHandler = async () => {
      const args: any = {};
      return this.userManager.signoutRedirectCallback(args);
    };
 
    const navigationInstruction = () => {
      this.$window.location.assign(this.openIdConnectConfiguration.logoutRedirectRoute);
    };
 
    return this.runHandlerAndCompleteNavigationInstruction(
      callbackHandler,
      navigationInstruction);
  }
 
  private async runHandlerAndCompleteNavigationInstruction(
    callbackHandler: () => Promise<any>,
    navigationInstruction: () => void): Promise<any> {
 
    try {
      this.logger.debug('Handling the response from the Identity Provider');
      await callbackHandler();
      this.logger.debug('Redirecting on authorization success');
      navigationInstruction();
    } catch (err) {
      this.logger.debug('Redirecting on authorization error');
      navigationInstruction();
      throw err;
    }
  }
}