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

100% Statements 51/51
100% Branches 4/4
100% Functions 13/13
100% Lines 46/46
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 97 98 99 100 101 102 103 104 105                                                                                                               10× 10×        
import { autoinject } from 'aurelia-framework';
import { NavigationInstruction } from 'aurelia-router';
import { UserManager } from 'oidc-client';
import OpenIdConnectConfigurationManager from './open-id-connect-configuration-manager';
import { LoginRedirectKey } from './open-id-connect-constants';
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> {
 
    let redirectRoute = this.openIdConnectConfiguration.loginRedirectRoute;
 
    const callbackHandler = async () => {
      const args: any = {};
      const user = await this.userManager.signinRedirectCallback(args);
 
      // The state is not persisted with the rest of the user.
      // This callback is the only place we will be able to capture the state.
      if (user.state && user.state[LoginRedirectKey]) {
        redirectRoute = user.state[LoginRedirectKey];
      }
    };
 
    const navigationInstruction = () =>
      this.redirectAfterCallback(instruction, redirectRoute);
 
    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;
 
      // TODO: Consider redirecting the parent window
      // to the loginRedirectRoute when the silent sign in completes.
    };
 
    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.redirectAfterCallback(instruction, this.openIdConnectConfiguration.logoutRedirectRoute);
 
    return this.runHandlerAndCompleteNavigationInstruction(
      callbackHandler,
      navigationInstruction);
  }
 
  // Redirect to the specified route AND ensure that a page refresh does not
  // load the OIDC redirect callback url.
  // See https://github.com/aurelia-contrib/aurelia-open-id-connect/issues/46
  // See https://github.com/aurelia-contrib/aurelia-open-id-connect/issues/47
  private redirectAfterCallback(instruction: NavigationInstruction, route: string) {
    this.$window.history.pushState({}, '', route);
    instruction.config.redirect = route;
  }
 
  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;
    }
  }
}