|
| 1 | +// Code originally generated by Speakeasy (https://www.speakeasyapi.dev). |
| 2 | + |
| 3 | +import * as z from "zod"; |
| 4 | +import { stringToBase64 } from "../lib/base64.js"; |
| 5 | +import { env } from "../lib/env.js"; |
| 6 | +import { HTTPClient } from "../lib/http.js"; |
| 7 | +import { parse } from "../lib/schemas.js"; |
| 8 | +import * as components from "../models/components/index.js"; |
| 9 | +import { |
| 10 | + AfterErrorContext, |
| 11 | + AfterErrorHook, |
| 12 | + BeforeRequestContext, |
| 13 | + BeforeRequestHook, |
| 14 | + SDKInitHook, |
| 15 | + SDKInitOptions, |
| 16 | +} from "./types.js"; |
| 17 | + |
| 18 | +type Credentials = { |
| 19 | + clientID: string; |
| 20 | + clientSecret: string; |
| 21 | + tokenURL: string | undefined; |
| 22 | +}; |
| 23 | + |
| 24 | +type Session = { |
| 25 | + credentials: Credentials; |
| 26 | + token: string; |
| 27 | + expiresAt?: number; |
| 28 | + scopes: string[]; |
| 29 | +}; |
| 30 | + |
| 31 | +export class ClientCredentialsHook |
| 32 | + implements SDKInitHook, BeforeRequestHook, AfterErrorHook |
| 33 | +{ |
| 34 | + private baseURL?: URL | null; |
| 35 | + private client?: HTTPClient; |
| 36 | + private sessions: Record<string, Session> = {}; |
| 37 | + |
| 38 | + sdkInit(opts: SDKInitOptions): SDKInitOptions { |
| 39 | + this.baseURL = opts.baseURL; |
| 40 | + this.client = opts.client; |
| 41 | + |
| 42 | + return opts; |
| 43 | + } |
| 44 | + |
| 45 | + async beforeRequest( |
| 46 | + hookCtx: BeforeRequestContext, |
| 47 | + request: Request |
| 48 | + ): Promise<Request> { |
| 49 | + if (!hookCtx.oAuth2Scopes) { |
| 50 | + // OAuth2 not in use |
| 51 | + return request; |
| 52 | + } |
| 53 | + |
| 54 | + const credentials = await this.getCredentials(hookCtx.securitySource); |
| 55 | + if (!credentials) { |
| 56 | + return request; |
| 57 | + } |
| 58 | + |
| 59 | + const sessionKey = this.getSessionKey(credentials); |
| 60 | + |
| 61 | + let session = this.sessions[sessionKey]; |
| 62 | + if ( |
| 63 | + !session || |
| 64 | + !this.hasRequiredScopes(session.scopes, hookCtx.oAuth2Scopes) || |
| 65 | + this.hasTokenExpired(session.expiresAt) |
| 66 | + ) { |
| 67 | + session = await this.doTokenRequest( |
| 68 | + credentials, |
| 69 | + this.getScopes(hookCtx.oAuth2Scopes, session) |
| 70 | + ); |
| 71 | + |
| 72 | + this.sessions[sessionKey] = session; |
| 73 | + } |
| 74 | + |
| 75 | + request.headers.set("Authorization", `Bearer ${session.token}`); |
| 76 | + |
| 77 | + return request; |
| 78 | + } |
| 79 | + |
| 80 | + async afterError( |
| 81 | + hookCtx: AfterErrorContext, |
| 82 | + response: Response | null, |
| 83 | + error: unknown |
| 84 | + ): Promise<{ response: Response | null; error: unknown }> { |
| 85 | + if (!hookCtx.oAuth2Scopes) { |
| 86 | + // OAuth2 not in use |
| 87 | + return { response, error }; |
| 88 | + } |
| 89 | + |
| 90 | + if (error) { |
| 91 | + return { response, error }; |
| 92 | + } |
| 93 | + |
| 94 | + const credentials = await this.getCredentials(hookCtx.securitySource); |
| 95 | + if (!credentials) { |
| 96 | + return { response, error }; |
| 97 | + } |
| 98 | + |
| 99 | + if (response && response?.status === 401) { |
| 100 | + const sessionKey = this.getSessionKey(credentials); |
| 101 | + delete this.sessions[sessionKey]; |
| 102 | + } |
| 103 | + |
| 104 | + return { response, error }; |
| 105 | + } |
| 106 | + |
| 107 | + private async doTokenRequest( |
| 108 | + credentials: Credentials, |
| 109 | + scopes: string[] |
| 110 | + ): Promise<Session> { |
| 111 | + const formData = new URLSearchParams(); |
| 112 | + formData.append("grant_type", "system_access"); |
| 113 | + formData.append("client_id", credentials.clientID); |
| 114 | + formData.append("client_secret", credentials.clientSecret); |
| 115 | + |
| 116 | + if (scopes.length > 0) { |
| 117 | + formData.append("scope", scopes.join(" ")); |
| 118 | + } |
| 119 | + |
| 120 | + const tokenURL = new URL(credentials.tokenURL ?? "", this.baseURL ?? ""); |
| 121 | + |
| 122 | + const request = new Request(tokenURL, { |
| 123 | + method: "POST", |
| 124 | + headers: { |
| 125 | + "Content-Type": "application/x-www-form-urlencoded", |
| 126 | + }, |
| 127 | + body: formData, |
| 128 | + }); |
| 129 | + |
| 130 | + const res = await this.client?.request(request); |
| 131 | + if (!res) { |
| 132 | + throw new Error("Failed to fetch token"); |
| 133 | + } |
| 134 | + |
| 135 | + if (res.status < 200 || res.status >= 300) { |
| 136 | + throw new Error("Unexpected status code"); |
| 137 | + } |
| 138 | + |
| 139 | + const data = await res.json(); |
| 140 | + if (!data || typeof data !== "object") { |
| 141 | + throw new Error("Unexpected response format"); |
| 142 | + } |
| 143 | + |
| 144 | + if (data.token_type !== "Bearer") { |
| 145 | + throw new Error("Unexpected token type"); |
| 146 | + } |
| 147 | + |
| 148 | + let expiresAt: number | undefined; |
| 149 | + if (data.expires_in) { |
| 150 | + expiresAt = Date.now() + data.expires_in * 1000; |
| 151 | + } |
| 152 | + |
| 153 | + const sess: Session = { |
| 154 | + credentials, |
| 155 | + token: data.access_token, |
| 156 | + scopes, |
| 157 | + }; |
| 158 | + |
| 159 | + if (expiresAt !== undefined) { |
| 160 | + sess.expiresAt = expiresAt; |
| 161 | + } |
| 162 | + |
| 163 | + return sess; |
| 164 | + } |
| 165 | + |
| 166 | + private async getCredentials( |
| 167 | + source?: unknown | (() => Promise<unknown>) |
| 168 | + ): Promise<Credentials | null> { |
| 169 | + if (!source) { |
| 170 | + return null; |
| 171 | + } |
| 172 | + |
| 173 | + let security = source; |
| 174 | + if (typeof source === "function") { |
| 175 | + security = await source(); |
| 176 | + } |
| 177 | + const out = parse( |
| 178 | + security, |
| 179 | + (val) => z.lazy(() => components.Security$outboundSchema).parse(val), |
| 180 | + "unexpected security type" |
| 181 | + ); |
| 182 | + |
| 183 | + return { |
| 184 | + clientID: out?.clientID ?? env().GUSTOEMBEDDED_CLIENT_ID ?? "", |
| 185 | + clientSecret: |
| 186 | + out?.clientSecret ?? env().GUSTOEMBEDDED_CLIENT_SECRET ?? "", |
| 187 | + tokenURL: out?.tokenURL ?? env().GUSTOEMBEDDED_TOKEN_URL ?? "", |
| 188 | + }; |
| 189 | + } |
| 190 | + |
| 191 | + private getSessionKey(credentials: Credentials): string { |
| 192 | + const key = `${credentials.clientID}:${credentials.clientSecret}`; |
| 193 | + return stringToBase64(key); |
| 194 | + } |
| 195 | + |
| 196 | + private hasRequiredScopes( |
| 197 | + scopes: string[], |
| 198 | + requiredScopes: string[] |
| 199 | + ): boolean { |
| 200 | + return requiredScopes.every((scope) => scopes.includes(scope)); |
| 201 | + } |
| 202 | + |
| 203 | + private getScopes(requiredScopes: string[], sess?: Session): string[] { |
| 204 | + return [...new Set((sess?.scopes ?? []).concat(requiredScopes))]; |
| 205 | + } |
| 206 | + |
| 207 | + private hasTokenExpired(expiresAt?: number): boolean { |
| 208 | + return !expiresAt || Date.now() + 60000 > expiresAt; |
| 209 | + } |
| 210 | +} |
0 commit comments