File

libs/oauth/pfe/src/lib/oauth-pfe-integration/oauth-pfe-integration.service.ts

Extends

PfeBaseActionConfig

Index

Properties

Properties

oauthConfigName (Optional)
Type string
import { PfeActionsService, PfeBaseActionConfig, PfeBusinessService } from '@allianz/ngx-pfe';
import { Injectable } from '@angular/core';
import { OauthService } from '@allianz/taly-oauth';

export const OauthGetTokenActionType = 'OAUTH_GET_TOKEN';
export const OauthAuthorizeActionType = 'OAUTH_AUTHORIZE';
export const OauthDecodeClientStateActionType = 'OAUTH_DECODE_CLIENT_STATE';
export const OauthLogoutActionType = 'OAUTH_LOGOUT';

export interface OauthGetTokenActionConfig extends PfeBaseActionConfig {
  oauthConfigName?: string;
}

export interface OauthAuthorizeActionConfig extends PfeBaseActionConfig {
  oauthConfigName?: string;
  clientStateExpression: string;
}

export interface OauthLogoutActionConfig extends PfeBaseActionConfig {
  oauthConfigName?: string;
}

export interface OauthDecodeClientStateActionConfig extends PfeBaseActionConfig {
  encodedClientStateExpression: string;
  clientStateDestinationExpression: string;
}

@Injectable()
export class OauthPfeIntegrationService {
  constructor(
    private oauthService: OauthService,
    private pfeBusinessService: PfeBusinessService,
    private pfeActionService: PfeActionsService
  ) {}

  registerActions() {
    this.pfeActionService.registerAction(
      OauthAuthorizeActionType,
      this.performAuthorizeAction.bind(this)
    );
    this.pfeActionService.registerAction(
      OauthGetTokenActionType,
      this.performGetTokenAction.bind(this)
    );
    this.pfeActionService.registerAction(
      OauthLogoutActionType,
      this.performLogoutAction.bind(this)
    );
    this.pfeActionService.registerAction(
      OauthDecodeClientStateActionType,
      this.performDecodeClientState.bind(this)
    );
  }

  private async performAuthorizeAction(actionConfig: OauthAuthorizeActionConfig): Promise<boolean> {
    let clientState = {};
    if (actionConfig.clientStateExpression) {
      clientState = this.pfeBusinessService.getValueByExpression(
        actionConfig.clientStateExpression
      );
    }
    this.oauthService.authorize(actionConfig.oauthConfigName, clientState);
    // return false to stop the (maybe) ongoing navigation
    return false;
  }

  private async performGetTokenAction(actionConfig: OauthGetTokenActionConfig): Promise<boolean> {
    try {
      const {
        access_token: accessToken,
        id_token: idToken,
        refresh_token: refreshToken,
        expires_in: expiresIn
      } = await this.oauthService.retrieveToken(actionConfig.oauthConfigName);
      this.pfeBusinessService.storeValue('oauth', {
        accessToken,
        idToken,
        refreshToken,
        expiresIn
      });
      this.oauthService.clearQueryParams();
      return true;
    } catch {
      this.pfeBusinessService.navigateToErrorPage();
      return false;
    }
  }

  private performLogoutAction(actionConfig: OauthLogoutActionConfig): Promise<void> {
    return this.oauthService.logout(actionConfig.oauthConfigName);
  }

  private async performDecodeClientState(
    actionConfig: OauthDecodeClientStateActionConfig
  ): Promise<void> {
    if (
      !actionConfig.encodedClientStateExpression ||
      !actionConfig.clientStateDestinationExpression
    ) {
      return;
    }
    const clientStateEncoded = this.pfeBusinessService.getValueByExpression(
      actionConfig.encodedClientStateExpression
    );
    const clientState = this.oauthService.decodeClientState(clientStateEncoded);
    this.pfeBusinessService.storeValueByExpression(
      actionConfig.clientStateDestinationExpression,
      clientState
    );
  }
}

results matching ""

    No results matching ""