|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2020 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +import { AuthEvent, AuthEventConsumer, EventManager } from '../../model/popup_redirect'; |
| 19 | + |
| 20 | +export class AuthEventManager implements EventManager { |
| 21 | + private readonly consumers: Set<AuthEventConsumer> = new Set(); |
| 22 | + |
| 23 | + registerConsumer(authEventConsumer: AuthEventConsumer): void { |
| 24 | + this.consumers.add(authEventConsumer); |
| 25 | + } |
| 26 | + |
| 27 | + unregisterConsumer(authEventConsumer: AuthEventConsumer): void { |
| 28 | + this.consumers.delete(authEventConsumer); |
| 29 | + } |
| 30 | + |
| 31 | + onEvent(event: AuthEvent): void { |
| 32 | + this.consumers.forEach(consumer => { |
| 33 | + if (consumer.filter === event.type && consumer.isMatchingEvent(event.eventId)) { |
| 34 | + consumer.onAuthEvent(event); |
| 35 | + } |
| 36 | + }); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +// export abstract class Abstract |
| 41 | + |
| 42 | +// export abstract class AbstractAuthEventManager { |
| 43 | +// // private readonly redirectOutcomeHandler = new RedirectManager(); |
| 44 | +// private readonly popupOutcomeHandler = new PopupResultManager(); |
| 45 | + |
| 46 | +// constructor() {} |
| 47 | + |
| 48 | +// abstract openPopup( |
| 49 | +// provider: AuthProvider, |
| 50 | +// authType: AuthEventType, |
| 51 | +// eventId?: string |
| 52 | +// ): Promise<AuthPopup>; |
| 53 | + |
| 54 | +// abstract processRedirect( |
| 55 | +// auth: Auth, |
| 56 | +// provider: AuthProvider, |
| 57 | +// authType: AuthEventType |
| 58 | +// ): Promise<never>; |
| 59 | + |
| 60 | +// abstract initializeAndWait(auth: Auth): Promise<void>; |
| 61 | +// abstract isInitialized(): boolean; |
| 62 | + |
| 63 | +// async onEvent(event: AuthEvent): Promise<boolean> { |
| 64 | +// if (event.error && event.type !== AuthEventType.UNKNOWN) { |
| 65 | +// this.getOutcomeHandler(event.type).broadcastResult(null, event.error); |
| 66 | +// return true; |
| 67 | +// } |
| 68 | + |
| 69 | +// const potentialUser = this.userForEvent(event.eventId); |
| 70 | + |
| 71 | +// switch (event.type) { |
| 72 | +// case AuthEventType.SIGN_IN_VIA_POPUP: |
| 73 | +// if (!this.popupOutcomeHandler.isMatchingEvent(event.eventId)) { |
| 74 | +// break; |
| 75 | +// } |
| 76 | +// // Fallthrough |
| 77 | +// case AuthEventType.SIGN_IN_VIA_REDIRECT: |
| 78 | +// this.execIdpTask(event, idp.signIn); |
| 79 | +// break; |
| 80 | +// case AuthEventType.LINK_VIA_POPUP: |
| 81 | +// case AuthEventType.LINK_VIA_REDIRECT: |
| 82 | +// if (potentialUser) { |
| 83 | +// this.execIdpTask(event, idp.link, potentialUser); |
| 84 | +// } |
| 85 | +// break; |
| 86 | +// case AuthEventType.REAUTH_VIA_POPUP: |
| 87 | +// case AuthEventType.REAUTH_VIA_REDIRECT: |
| 88 | +// if (potentialUser) { |
| 89 | +// this.execIdpTask(event, idp.reauth, potentialUser); |
| 90 | +// } |
| 91 | +// break; |
| 92 | +// } |
| 93 | + |
| 94 | +// // Always resolve with the iframe |
| 95 | +// return true; |
| 96 | +// } |
| 97 | + |
| 98 | +// processPopup( |
| 99 | +// auth: Auth, |
| 100 | +// provider: AuthProvider, |
| 101 | +// authType: AuthEventType, |
| 102 | +// eventId?: string |
| 103 | +// ): Promise<UserCredential | null> { |
| 104 | +// // TODO: Fix the dirty hack |
| 105 | +// this.auth = auth; |
| 106 | + |
| 107 | +// return this.popupOutcomeHandler.getNewPendingPromise(async () => { |
| 108 | +// if (!this.isInitialized()) { |
| 109 | +// await this.initializeAndWait(auth); |
| 110 | +// } |
| 111 | + |
| 112 | +// const win = await this.openPopup(auth, provider, authType, eventId); |
| 113 | +// win.associatedEvent = eventId || null; |
| 114 | +// return win; |
| 115 | +// }); |
| 116 | +// } |
| 117 | + |
| 118 | +// getRedirectResult(auth: Auth): Promise<UserCredential | null> { |
| 119 | +// // TODO: Fix this dirty hack |
| 120 | +// this.auth = auth; |
| 121 | +// return this.redirectOutcomeHandler.getRedirectPromiseOrInit(() => { |
| 122 | +// if (!this.isInitialized()) { |
| 123 | +// this.initializeAndWait(auth); |
| 124 | +// } |
| 125 | +// }); |
| 126 | +// } |
| 127 | + |
| 128 | +// private userForEvent(id: string | null): User | undefined { |
| 129 | +// return this.auth |
| 130 | +// .getPotentialRedirectUsers_() |
| 131 | +// .find(u => u.redirectEventId_ === id); |
| 132 | +// } |
| 133 | + |
| 134 | +// private getOutcomeHandler( |
| 135 | +// eventType: AuthEventType |
| 136 | +// ): PopupRedirectOutcomeHandler { |
| 137 | +// switch (eventType) { |
| 138 | +// case AuthEventType.SIGN_IN_VIA_POPUP: |
| 139 | +// case AuthEventType.LINK_VIA_POPUP: |
| 140 | +// case AuthEventType.REAUTH_VIA_POPUP: |
| 141 | +// return this.popupOutcomeHandler; |
| 142 | +// case AuthEventType.SIGN_IN_VIA_REDIRECT: |
| 143 | +// case AuthEventType.LINK_VIA_REDIRECT: |
| 144 | +// case AuthEventType.REAUTH_VIA_REDIRECT: |
| 145 | +// return this.redirectOutcomeHandler; |
| 146 | +// default: |
| 147 | +// throw AUTH_ERROR_FACTORY.create(AuthErrorCode.INTERNAL_ERROR, { |
| 148 | +// appName: 'TODO' |
| 149 | +// }); |
| 150 | +// } |
| 151 | +// } |
| 152 | + |
| 153 | +// private async execIdpTask(event: AuthEvent, task: idp.IdpTask, user?: User) { |
| 154 | +// const { urlResponse, sessionId, postBody, tenantId } = event; |
| 155 | +// const params: idp.IdpTaskParams = { |
| 156 | +// requestUri: urlResponse!, |
| 157 | +// sessionId: sessionId!, |
| 158 | +// auth: this.auth, |
| 159 | +// tenantId: tenantId || undefined, |
| 160 | +// postBody: postBody || undefined, |
| 161 | +// user |
| 162 | +// }; |
| 163 | + |
| 164 | +// const outcomeHandler = this.getOutcomeHandler(event.type); |
| 165 | + |
| 166 | +// try { |
| 167 | +// const cred = await task(params); |
| 168 | +// outcomeHandler.broadcastResult(cred); |
| 169 | +// } catch (e) { |
| 170 | +// outcomeHandler.broadcastResult(null, e); |
| 171 | +// } |
| 172 | +// } |
| 173 | +// } |
0 commit comments