|
| 1 | +import deepmerge from "deepmerge"; |
| 2 | + |
| 3 | +import { UX_MODE } from "../utils/enums"; |
| 4 | +import { broadcastChannelOptions, getTimeout } from "../utils/helpers"; |
| 5 | +import log from "../utils/loglevel"; |
| 6 | +import PopupHandler from "../utils/PopupHandler"; |
| 7 | +import AbstractLoginHandler from "./AbstractLoginHandler"; |
| 8 | +import { CreateHandlerParams, LoginWindowResponse, TorusVerifierResponse } from "./interfaces"; |
| 9 | + |
| 10 | +type PopupResponse = { |
| 11 | + result: { |
| 12 | + auth_date: number; |
| 13 | + first_name: string; |
| 14 | + hash: string; |
| 15 | + id: number; |
| 16 | + last_name: string; |
| 17 | + photo_url: string; |
| 18 | + username: string; |
| 19 | + }; |
| 20 | + origin: string; |
| 21 | + event: string; |
| 22 | +}; |
| 23 | + |
| 24 | +export default class TelegramHandler extends AbstractLoginHandler { |
| 25 | + private readonly RESPONSE_TYPE: string = "token"; |
| 26 | + |
| 27 | + private readonly SCOPE: string = "profile"; |
| 28 | + |
| 29 | + private readonly PROMPT: string = "select_account"; |
| 30 | + |
| 31 | + constructor(params: CreateHandlerParams) { |
| 32 | + super(params); |
| 33 | + this.setFinalUrl(); |
| 34 | + } |
| 35 | + |
| 36 | + setFinalUrl(): void { |
| 37 | + const finalUrl = new URL("https://oauth.telegram.org/auth"); |
| 38 | + const clonedParams = JSON.parse(JSON.stringify(this.params.jwtParams || {})); |
| 39 | + clonedParams.origin = `${this.params.redirect_uri}?state=${this.state}&nonce=${this.nonce}`; |
| 40 | + |
| 41 | + const finalJwtParams = deepmerge( |
| 42 | + { |
| 43 | + state: this.state, |
| 44 | + response_type: this.RESPONSE_TYPE, |
| 45 | + bot_id: this.params.clientId, |
| 46 | + prompt: this.PROMPT, |
| 47 | + redirect_uri: `${this.params.redirect_uri}?state=${this.state}&nonce=${this.nonce}`, |
| 48 | + scope: this.SCOPE, |
| 49 | + nonce: this.nonce, |
| 50 | + }, |
| 51 | + clonedParams |
| 52 | + ); |
| 53 | + Object.keys(finalJwtParams).forEach((key: string) => { |
| 54 | + const localKey = key as keyof typeof finalJwtParams; |
| 55 | + if (finalJwtParams[localKey]) finalUrl.searchParams.append(localKey, finalJwtParams[localKey]); |
| 56 | + }); |
| 57 | + this.finalURL = finalUrl; |
| 58 | + } |
| 59 | + |
| 60 | + objectToAuthDataMap(tgAuthenticationResulr: string) { |
| 61 | + return JSON.parse(atob(tgAuthenticationResulr)) as { first_name: string; last_name: string; photo_url: string; username: string }; |
| 62 | + } |
| 63 | + |
| 64 | + async getUserInfo(params: LoginWindowResponse): Promise<TorusVerifierResponse> { |
| 65 | + const { tgAuthResult } = params; |
| 66 | + const userInfo = this.objectToAuthDataMap(tgAuthResult); |
| 67 | + const { photo_url: profileImage = "", username = "", first_name = "", last_name = "" } = userInfo; |
| 68 | + return { |
| 69 | + email: "", // Telegram does not provide email |
| 70 | + name: `${first_name} ${last_name}`, |
| 71 | + profileImage, |
| 72 | + verifier: this.params.verifier, |
| 73 | + verifierId: username.toLowerCase(), |
| 74 | + typeOfLogin: this.params.typeOfLogin, |
| 75 | + }; |
| 76 | + } |
| 77 | + |
| 78 | + async handleLoginWindow(params: { locationReplaceOnRedirect?: boolean; popupFeatures?: string }): Promise<LoginWindowResponse> { |
| 79 | + const verifierWindow = new PopupHandler({ url: this.finalURL, features: params.popupFeatures, timeout: getTimeout(this.params.typeOfLogin) }); |
| 80 | + if (this.params.uxMode === UX_MODE.REDIRECT) { |
| 81 | + verifierWindow.redirect(params.locationReplaceOnRedirect); |
| 82 | + } else { |
| 83 | + const { BroadcastChannel } = await import("@toruslabs/broadcast-channel"); |
| 84 | + return new Promise<LoginWindowResponse>((resolve, reject) => { |
| 85 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 86 | + let bc: any; |
| 87 | + const handleData = async (ev: string) => { |
| 88 | + try { |
| 89 | + const { event, origin, result, ...rest } = (JSON.parse(ev) as PopupResponse) || {}; |
| 90 | + // 1. Parse URL |
| 91 | + const parsedUrl = new URL(origin); |
| 92 | + // 2. Get state param |
| 93 | + const stateParam = parsedUrl.searchParams.get("state"); |
| 94 | + |
| 95 | + if (event && event === "auth_result") { |
| 96 | + if (!this.params.redirectToOpener && bc) await bc.postMessage({ success: true }); |
| 97 | + // properly resolve the data |
| 98 | + resolve({ |
| 99 | + accessToken: "", |
| 100 | + tgAuthResult: btoa(JSON.stringify(result)) || "", |
| 101 | + ...rest, |
| 102 | + // State has to be last here otherwise it will be overwritten |
| 103 | + state: atob(stateParam) as unknown as { [key: string]: string }, |
| 104 | + }); |
| 105 | + } |
| 106 | + } catch (error) { |
| 107 | + log.error(error); |
| 108 | + reject(error); |
| 109 | + } |
| 110 | + }; |
| 111 | + |
| 112 | + if (!this.params.redirectToOpener) { |
| 113 | + bc = new BroadcastChannel<{ |
| 114 | + error: string; |
| 115 | + data: PopupResponse; |
| 116 | + }>(`redirect_channel_${this.nonce}`, broadcastChannelOptions); |
| 117 | + bc.addEventListener("message", async (ev: string) => { |
| 118 | + await handleData(ev); |
| 119 | + bc.close(); |
| 120 | + verifierWindow.close(); |
| 121 | + }); |
| 122 | + } |
| 123 | + const postMessageEventHandler = async (postMessageEvent: MessageEvent) => { |
| 124 | + if (!postMessageEvent.data) return; |
| 125 | + const ev = postMessageEvent.data; |
| 126 | + window.removeEventListener("message", postMessageEventHandler); |
| 127 | + handleData(ev); |
| 128 | + verifierWindow.close(); |
| 129 | + }; |
| 130 | + window.addEventListener("message", postMessageEventHandler); |
| 131 | + try { |
| 132 | + verifierWindow.open(); |
| 133 | + } catch (error) { |
| 134 | + log.error(error); |
| 135 | + reject(error); |
| 136 | + return; |
| 137 | + } |
| 138 | + verifierWindow.once("close", () => { |
| 139 | + if (bc) bc.close(); |
| 140 | + reject(new Error("user closed popup")); |
| 141 | + }); |
| 142 | + }); |
| 143 | + } |
| 144 | + return null; |
| 145 | + } |
| 146 | +} |
0 commit comments