|
| 1 | +/* |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. |
| 4 | + */ |
| 5 | +import { version } from "../../package.json"; |
| 6 | +import { BrokerAuthenticationResult, ServerTelemetryManager, AuthorizationCodeClient, BrokerAuthorizationCodeClient } from "@azure/msal-common"; |
| 7 | +import { BrokerMessage } from "./BrokerMessage"; |
| 8 | +import { BrokerMessageType, InteractionType } from "../utils/BrowserConstants"; |
| 9 | +import { Configuration } from "../config/Configuration"; |
| 10 | +import { BrokerHandshakeRequest } from "./BrokerHandshakeRequest"; |
| 11 | +import { BrokerHandshakeResponse } from "./BrokerHandshakeResponse"; |
| 12 | +import { BrokerAuthRequest } from "./BrokerAuthRequest"; |
| 13 | +import { BrokerRedirectResponse } from "./BrokerRedirectResponse"; |
| 14 | +import { RedirectRequest } from "../request/RedirectRequest"; |
| 15 | +import { BrokerAuthResult } from "./BrokerAuthResult"; |
| 16 | +import { ClientApplication } from "../app/ClientApplication"; |
| 17 | +import { PopupRequest } from "../request/PopupRequest"; |
| 18 | + |
| 19 | +/** |
| 20 | + * Broker Application class to manage brokered requests. |
| 21 | + */ |
| 22 | +export class BrokerClientApplication extends ClientApplication { |
| 23 | + |
| 24 | + constructor(configuration: Configuration) { |
| 25 | + super(configuration); |
| 26 | + this.logger.verbose("Acting as Broker"); |
| 27 | + this.listenForBrokerMessage(); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * |
| 32 | + */ |
| 33 | + private listenForBrokerMessage(): void { |
| 34 | + window.addEventListener("message", this.handleBrokerMessage.bind(this)); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * |
| 39 | + * @param message |
| 40 | + */ |
| 41 | + private async handleBrokerMessage(message: MessageEvent): Promise<void> { |
| 42 | + // Check that message is a BrokerHandshakeRequest |
| 43 | + const clientMessage = BrokerMessage.validateMessage(message); |
| 44 | + if (clientMessage) { |
| 45 | + switch (clientMessage.data.messageType) { |
| 46 | + case BrokerMessageType.HANDSHAKE_REQUEST: |
| 47 | + this.logger.verbose("Broker handshake request received"); |
| 48 | + return await this.handleBrokerHandshake(clientMessage); |
| 49 | + case BrokerMessageType.AUTH_REQUEST: |
| 50 | + this.logger.verbose("Broker auth request received"); |
| 51 | + return await this.handleBrokerAuthRequest(clientMessage); |
| 52 | + default: |
| 53 | + return; |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /* eslint-disable */ |
| 59 | + /** |
| 60 | + * Handle a broker handshake request from a child. |
| 61 | + * @param clientMessage |
| 62 | + */ |
| 63 | + private async handleBrokerHandshake(clientMessage: MessageEvent): Promise<void> { |
| 64 | + const validMessage = BrokerHandshakeRequest.validate(clientMessage); |
| 65 | + this.logger.verbose(`Broker handshake validated: ${validMessage}`); |
| 66 | + const brokerHandshakeResponse = new BrokerHandshakeResponse(version); |
| 67 | + |
| 68 | + // @ts-ignore |
| 69 | + clientMessage.source.postMessage(brokerHandshakeResponse, clientMessage.origin); |
| 70 | + this.logger.info(`Sending handshake response: ${brokerHandshakeResponse}`); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Handle a brokered auth request from the child. |
| 75 | + * @param clientMessage |
| 76 | + */ |
| 77 | + private async handleBrokerAuthRequest(clientMessage: MessageEvent): Promise<void> { |
| 78 | + const validMessage = BrokerAuthRequest.validate(clientMessage); |
| 79 | + if (validMessage) { |
| 80 | + this.logger.verbose(`Broker auth request validated: ${validMessage}`); |
| 81 | + switch (validMessage.interactionType) { |
| 82 | + case InteractionType.REDIRECT: |
| 83 | + return this.brokeredRedirectRequest(validMessage, clientMessage.ports[0]); |
| 84 | + case InteractionType.POPUP: |
| 85 | + return this.brokeredPopupRequest(validMessage, clientMessage.ports[0]); |
| 86 | + default: |
| 87 | + return; |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private async brokeredRedirectRequest(validMessage: BrokerAuthRequest, clientPort: MessagePort): Promise<void> { |
| 93 | + const brokerRedirectResp = new BrokerRedirectResponse(); |
| 94 | + // @ts-ignore |
| 95 | + clientPort.postMessage(brokerRedirectResp); |
| 96 | + this.logger.info(`Sending redirect response: ${brokerRedirectResp}`); |
| 97 | + |
| 98 | + // Call loginRedirect |
| 99 | + this.acquireTokenRedirect(validMessage.request as RedirectRequest); |
| 100 | + } |
| 101 | + |
| 102 | + private async brokeredPopupRequest(validMessage: BrokerAuthRequest, clientPort: MessagePort): Promise<void> { |
| 103 | + try { |
| 104 | + const response: BrokerAuthenticationResult = (await this.acquireTokenPopup(validMessage.request as PopupRequest)) as BrokerAuthenticationResult; |
| 105 | + const brokerAuthResponse: BrokerAuthResult = new BrokerAuthResult(InteractionType.POPUP, response); |
| 106 | + this.logger.info(`Sending auth response: ${brokerAuthResponse}`); |
| 107 | + clientPort.postMessage(brokerAuthResponse); |
| 108 | + } catch (err) { |
| 109 | + const brokerAuthResponse = new BrokerAuthResult(InteractionType.POPUP, null, err); |
| 110 | + this.logger.info(`Found auth error: ${err}`); |
| 111 | + clientPort.postMessage(brokerAuthResponse); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Creates an Broker Authorization Code Client with the given authority, or the default authority. |
| 117 | + * @param authorityUrl |
| 118 | + */ |
| 119 | + protected async createAuthCodeClient(serverTelemetryManager: ServerTelemetryManager, authorityUrl?: string): Promise<AuthorizationCodeClient> { |
| 120 | + // Create auth module. |
| 121 | + const clientConfig = await this.getClientConfiguration(serverTelemetryManager, authorityUrl); |
| 122 | + |
| 123 | + return new BrokerAuthorizationCodeClient(clientConfig); |
| 124 | + } |
| 125 | +} |
0 commit comments