summaryrefslogtreecommitdiffhomepage
path: root/packages/sdk/js/src/gen/core/auth.ts
blob: e496d4557a2028fb1caea934bc5c44b0f4c86f25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
export type AuthToken = string | undefined

export interface Auth {
  /**
   * Which part of the request do we use to send the auth?
   *
   * @default 'header'
   */
  in?: "header" | "query" | "cookie"
  /**
   * Header or query parameter name.
   *
   * @default 'Authorization'
   */
  name?: string
  scheme?: "basic" | "bearer"
  type: "apiKey" | "http"
}

export const getAuthToken = async (
  auth: Auth,
  callback: ((auth: Auth) => Promise<AuthToken> | AuthToken) | AuthToken,
): Promise<string | undefined> => {
  const token = typeof callback === "function" ? await callback(auth) : callback

  if (!token) {
    return
  }

  if (auth.scheme === "bearer") {
    return `Bearer ${token}`
  }

  if (auth.scheme === "basic") {
    return `Basic ${btoa(token)}`
  }

  return token
}