@depup/supabase__auth-js 2.99.2-depup.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (195) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +25 -0
  3. package/changes.json +5 -0
  4. package/dist/main/AuthAdminApi.d.ts +4 -0
  5. package/dist/main/AuthAdminApi.d.ts.map +1 -0
  6. package/dist/main/AuthAdminApi.js +7 -0
  7. package/dist/main/AuthAdminApi.js.map +1 -0
  8. package/dist/main/AuthClient.d.ts +4 -0
  9. package/dist/main/AuthClient.d.ts.map +1 -0
  10. package/dist/main/AuthClient.js +7 -0
  11. package/dist/main/AuthClient.js.map +1 -0
  12. package/dist/main/GoTrueAdminApi.d.ts +227 -0
  13. package/dist/main/GoTrueAdminApi.d.ts.map +1 -0
  14. package/dist/main/GoTrueAdminApi.js +596 -0
  15. package/dist/main/GoTrueAdminApi.js.map +1 -0
  16. package/dist/main/GoTrueClient.d.ts +783 -0
  17. package/dist/main/GoTrueClient.d.ts.map +1 -0
  18. package/dist/main/GoTrueClient.js +3029 -0
  19. package/dist/main/GoTrueClient.js.map +1 -0
  20. package/dist/main/index.d.ts +9 -0
  21. package/dist/main/index.d.ts.map +1 -0
  22. package/dist/main/index.js +20 -0
  23. package/dist/main/index.js.map +1 -0
  24. package/dist/main/lib/base64url.d.ts +76 -0
  25. package/dist/main/lib/base64url.d.ts.map +1 -0
  26. package/dist/main/lib/base64url.js +269 -0
  27. package/dist/main/lib/base64url.js.map +1 -0
  28. package/dist/main/lib/constants.d.ts +26 -0
  29. package/dist/main/lib/constants.d.ts.map +1 -0
  30. package/dist/main/lib/constants.js +31 -0
  31. package/dist/main/lib/constants.js.map +1 -0
  32. package/dist/main/lib/error-codes.d.ts +7 -0
  33. package/dist/main/lib/error-codes.d.ts.map +1 -0
  34. package/dist/main/lib/error-codes.js +3 -0
  35. package/dist/main/lib/error-codes.js.map +1 -0
  36. package/dist/main/lib/errors.d.ts +243 -0
  37. package/dist/main/lib/errors.d.ts.map +1 -0
  38. package/dist/main/lib/errors.js +289 -0
  39. package/dist/main/lib/errors.js.map +1 -0
  40. package/dist/main/lib/fetch.d.ts +34 -0
  41. package/dist/main/lib/fetch.d.ts.map +1 -0
  42. package/dist/main/lib/fetch.js +184 -0
  43. package/dist/main/lib/fetch.js.map +1 -0
  44. package/dist/main/lib/helpers.d.ts +91 -0
  45. package/dist/main/lib/helpers.d.ts.map +1 -0
  46. package/dist/main/lib/helpers.js +395 -0
  47. package/dist/main/lib/helpers.js.map +1 -0
  48. package/dist/main/lib/local-storage.d.ts +9 -0
  49. package/dist/main/lib/local-storage.d.ts.map +1 -0
  50. package/dist/main/lib/local-storage.js +21 -0
  51. package/dist/main/lib/local-storage.js.map +1 -0
  52. package/dist/main/lib/locks.d.ts +107 -0
  53. package/dist/main/lib/locks.d.ts.map +1 -0
  54. package/dist/main/lib/locks.js +314 -0
  55. package/dist/main/lib/locks.js.map +1 -0
  56. package/dist/main/lib/polyfills.d.ts +5 -0
  57. package/dist/main/lib/polyfills.d.ts.map +1 -0
  58. package/dist/main/lib/polyfills.js +29 -0
  59. package/dist/main/lib/polyfills.js.map +1 -0
  60. package/dist/main/lib/types.d.ts +1861 -0
  61. package/dist/main/lib/types.d.ts.map +1 -0
  62. package/dist/main/lib/types.js +23 -0
  63. package/dist/main/lib/types.js.map +1 -0
  64. package/dist/main/lib/version.d.ts +2 -0
  65. package/dist/main/lib/version.d.ts.map +1 -0
  66. package/dist/main/lib/version.js +11 -0
  67. package/dist/main/lib/version.js.map +1 -0
  68. package/dist/main/lib/web3/ethereum.d.ts +96 -0
  69. package/dist/main/lib/web3/ethereum.d.ts.map +1 -0
  70. package/dist/main/lib/web3/ethereum.js +66 -0
  71. package/dist/main/lib/web3/ethereum.js.map +1 -0
  72. package/dist/main/lib/web3/solana.d.ts +160 -0
  73. package/dist/main/lib/web3/solana.d.ts.map +1 -0
  74. package/dist/main/lib/web3/solana.js +4 -0
  75. package/dist/main/lib/web3/solana.js.map +1 -0
  76. package/dist/main/lib/webauthn.d.ts +276 -0
  77. package/dist/main/lib/webauthn.d.ts.map +1 -0
  78. package/dist/main/lib/webauthn.dom.d.ts +583 -0
  79. package/dist/main/lib/webauthn.dom.d.ts.map +1 -0
  80. package/dist/main/lib/webauthn.dom.js +4 -0
  81. package/dist/main/lib/webauthn.dom.js.map +1 -0
  82. package/dist/main/lib/webauthn.errors.d.ts +80 -0
  83. package/dist/main/lib/webauthn.errors.d.ts.map +1 -0
  84. package/dist/main/lib/webauthn.errors.js +265 -0
  85. package/dist/main/lib/webauthn.errors.js.map +1 -0
  86. package/dist/main/lib/webauthn.js +706 -0
  87. package/dist/main/lib/webauthn.js.map +1 -0
  88. package/dist/module/AuthAdminApi.d.ts +4 -0
  89. package/dist/module/AuthAdminApi.d.ts.map +1 -0
  90. package/dist/module/AuthAdminApi.js +4 -0
  91. package/dist/module/AuthAdminApi.js.map +1 -0
  92. package/dist/module/AuthClient.d.ts +4 -0
  93. package/dist/module/AuthClient.d.ts.map +1 -0
  94. package/dist/module/AuthClient.js +4 -0
  95. package/dist/module/AuthClient.js.map +1 -0
  96. package/dist/module/GoTrueAdminApi.d.ts +227 -0
  97. package/dist/module/GoTrueAdminApi.d.ts.map +1 -0
  98. package/dist/module/GoTrueAdminApi.js +593 -0
  99. package/dist/module/GoTrueAdminApi.js.map +1 -0
  100. package/dist/module/GoTrueClient.d.ts +783 -0
  101. package/dist/module/GoTrueClient.d.ts.map +1 -0
  102. package/dist/module/GoTrueClient.js +3026 -0
  103. package/dist/module/GoTrueClient.js.map +1 -0
  104. package/dist/module/index.d.ts +9 -0
  105. package/dist/module/index.d.ts.map +1 -0
  106. package/dist/module/index.js +9 -0
  107. package/dist/module/index.js.map +1 -0
  108. package/dist/module/lib/base64url.d.ts +76 -0
  109. package/dist/module/lib/base64url.d.ts.map +1 -0
  110. package/dist/module/lib/base64url.js +257 -0
  111. package/dist/module/lib/base64url.js.map +1 -0
  112. package/dist/module/lib/constants.d.ts +26 -0
  113. package/dist/module/lib/constants.d.ts.map +1 -0
  114. package/dist/module/lib/constants.js +28 -0
  115. package/dist/module/lib/constants.js.map +1 -0
  116. package/dist/module/lib/error-codes.d.ts +7 -0
  117. package/dist/module/lib/error-codes.d.ts.map +1 -0
  118. package/dist/module/lib/error-codes.js +2 -0
  119. package/dist/module/lib/error-codes.js.map +1 -0
  120. package/dist/module/lib/errors.d.ts +243 -0
  121. package/dist/module/lib/errors.d.ts.map +1 -0
  122. package/dist/module/lib/errors.js +266 -0
  123. package/dist/module/lib/errors.js.map +1 -0
  124. package/dist/module/lib/fetch.d.ts +34 -0
  125. package/dist/module/lib/fetch.d.ts.map +1 -0
  126. package/dist/module/lib/fetch.js +174 -0
  127. package/dist/module/lib/fetch.js.map +1 -0
  128. package/dist/module/lib/helpers.d.ts +91 -0
  129. package/dist/module/lib/helpers.d.ts.map +1 -0
  130. package/dist/module/lib/helpers.js +368 -0
  131. package/dist/module/lib/helpers.js.map +1 -0
  132. package/dist/module/lib/local-storage.d.ts +9 -0
  133. package/dist/module/lib/local-storage.d.ts.map +1 -0
  134. package/dist/module/lib/local-storage.js +18 -0
  135. package/dist/module/lib/local-storage.js.map +1 -0
  136. package/dist/module/lib/locks.d.ts +107 -0
  137. package/dist/module/lib/locks.d.ts.map +1 -0
  138. package/dist/module/lib/locks.js +306 -0
  139. package/dist/module/lib/locks.js.map +1 -0
  140. package/dist/module/lib/polyfills.d.ts +5 -0
  141. package/dist/module/lib/polyfills.d.ts.map +1 -0
  142. package/dist/module/lib/polyfills.js +26 -0
  143. package/dist/module/lib/polyfills.js.map +1 -0
  144. package/dist/module/lib/types.d.ts +1861 -0
  145. package/dist/module/lib/types.d.ts.map +1 -0
  146. package/dist/module/lib/types.js +20 -0
  147. package/dist/module/lib/types.js.map +1 -0
  148. package/dist/module/lib/version.d.ts +2 -0
  149. package/dist/module/lib/version.d.ts.map +1 -0
  150. package/dist/module/lib/version.js +8 -0
  151. package/dist/module/lib/version.js.map +1 -0
  152. package/dist/module/lib/web3/ethereum.d.ts +96 -0
  153. package/dist/module/lib/web3/ethereum.d.ts.map +1 -0
  154. package/dist/module/lib/web3/ethereum.js +60 -0
  155. package/dist/module/lib/web3/ethereum.js.map +1 -0
  156. package/dist/module/lib/web3/solana.d.ts +160 -0
  157. package/dist/module/lib/web3/solana.d.ts.map +1 -0
  158. package/dist/module/lib/web3/solana.js +3 -0
  159. package/dist/module/lib/web3/solana.js.map +1 -0
  160. package/dist/module/lib/webauthn.d.ts +276 -0
  161. package/dist/module/lib/webauthn.d.ts.map +1 -0
  162. package/dist/module/lib/webauthn.dom.d.ts +583 -0
  163. package/dist/module/lib/webauthn.dom.d.ts.map +1 -0
  164. package/dist/module/lib/webauthn.dom.js +3 -0
  165. package/dist/module/lib/webauthn.dom.js.map +1 -0
  166. package/dist/module/lib/webauthn.errors.d.ts +80 -0
  167. package/dist/module/lib/webauthn.errors.d.ts.map +1 -0
  168. package/dist/module/lib/webauthn.errors.js +257 -0
  169. package/dist/module/lib/webauthn.errors.js.map +1 -0
  170. package/dist/module/lib/webauthn.js +689 -0
  171. package/dist/module/lib/webauthn.js.map +1 -0
  172. package/dist/tsconfig.module.tsbuildinfo +1 -0
  173. package/dist/tsconfig.tsbuildinfo +1 -0
  174. package/package.json +56 -0
  175. package/src/AuthAdminApi.ts +5 -0
  176. package/src/AuthClient.ts +5 -0
  177. package/src/GoTrueAdminApi.ts +723 -0
  178. package/src/GoTrueClient.ts +4078 -0
  179. package/src/index.ts +13 -0
  180. package/src/lib/base64url.ts +308 -0
  181. package/src/lib/constants.ts +34 -0
  182. package/src/lib/error-codes.ts +90 -0
  183. package/src/lib/errors.ts +324 -0
  184. package/src/lib/fetch.ts +283 -0
  185. package/src/lib/helpers.ts +463 -0
  186. package/src/lib/local-storage.ts +21 -0
  187. package/src/lib/locks.ts +375 -0
  188. package/src/lib/polyfills.ts +23 -0
  189. package/src/lib/types.ts +2229 -0
  190. package/src/lib/version.ts +7 -0
  191. package/src/lib/web3/ethereum.ts +184 -0
  192. package/src/lib/web3/solana.ts +186 -0
  193. package/src/lib/webauthn.dom.ts +636 -0
  194. package/src/lib/webauthn.errors.ts +317 -0
  195. package/src/lib/webauthn.ts +946 -0
@@ -0,0 +1,463 @@
1
+ import { API_VERSION_HEADER_NAME, BASE64URL_REGEX } from './constants'
2
+ import { AuthInvalidJwtError } from './errors'
3
+ import { base64UrlToUint8Array, stringFromBase64URL } from './base64url'
4
+ import { JwtHeader, JwtPayload, SupportedStorage, User } from './types'
5
+ import { Uint8Array_ } from './webauthn.dom'
6
+
7
+ export function expiresAt(expiresIn: number) {
8
+ const timeNow = Math.round(Date.now() / 1000)
9
+ return timeNow + expiresIn
10
+ }
11
+
12
+ /**
13
+ * Generates a unique identifier for internal callback subscriptions.
14
+ *
15
+ * This function uses JavaScript Symbols to create guaranteed-unique identifiers
16
+ * for auth state change callbacks. Symbols are ideal for this use case because:
17
+ * - They are guaranteed unique by the JavaScript runtime
18
+ * - They work in all environments (browser, SSR, Node.js)
19
+ * - They avoid issues with Next.js 16 deterministic rendering requirements
20
+ * - They are perfect for internal, non-serializable identifiers
21
+ *
22
+ * Note: This function is only used for internal subscription management,
23
+ * not for security-critical operations like session tokens.
24
+ */
25
+ export function generateCallbackId(): symbol {
26
+ return Symbol('auth-callback')
27
+ }
28
+
29
+ export const isBrowser = () => typeof window !== 'undefined' && typeof document !== 'undefined'
30
+
31
+ const localStorageWriteTests = {
32
+ tested: false,
33
+ writable: false,
34
+ }
35
+
36
+ /**
37
+ * Checks whether localStorage is supported on this browser.
38
+ */
39
+ export const supportsLocalStorage = () => {
40
+ if (!isBrowser()) {
41
+ return false
42
+ }
43
+
44
+ try {
45
+ if (typeof globalThis.localStorage !== 'object') {
46
+ return false
47
+ }
48
+ } catch (e) {
49
+ // DOM exception when accessing `localStorage`
50
+ return false
51
+ }
52
+
53
+ if (localStorageWriteTests.tested) {
54
+ return localStorageWriteTests.writable
55
+ }
56
+
57
+ const randomKey = `lswt-${Math.random()}${Math.random()}`
58
+
59
+ try {
60
+ globalThis.localStorage.setItem(randomKey, randomKey)
61
+ globalThis.localStorage.removeItem(randomKey)
62
+
63
+ localStorageWriteTests.tested = true
64
+ localStorageWriteTests.writable = true
65
+ } catch (e) {
66
+ // localStorage can't be written to
67
+ // https://www.chromium.org/for-testers/bug-reporting-guidelines/uncaught-securityerror-failed-to-read-the-localstorage-property-from-window-access-is-denied-for-this-document
68
+
69
+ localStorageWriteTests.tested = true
70
+ localStorageWriteTests.writable = false
71
+ }
72
+
73
+ return localStorageWriteTests.writable
74
+ }
75
+
76
+ /**
77
+ * Extracts parameters encoded in the URL both in the query and fragment.
78
+ */
79
+ export function parseParametersFromURL(href: string) {
80
+ const result: { [parameter: string]: string } = {}
81
+
82
+ const url = new URL(href)
83
+
84
+ if (url.hash && url.hash[0] === '#') {
85
+ try {
86
+ const hashSearchParams = new URLSearchParams(url.hash.substring(1))
87
+ hashSearchParams.forEach((value, key) => {
88
+ result[key] = value
89
+ })
90
+ } catch (e: any) {
91
+ // hash is not a query string
92
+ }
93
+ }
94
+
95
+ // search parameters take precedence over hash parameters
96
+ url.searchParams.forEach((value, key) => {
97
+ result[key] = value
98
+ })
99
+
100
+ return result
101
+ }
102
+
103
+ type Fetch = typeof fetch
104
+
105
+ export const resolveFetch = (customFetch?: Fetch): Fetch => {
106
+ if (customFetch) {
107
+ return (...args) => customFetch(...args)
108
+ }
109
+ return (...args) => fetch(...args)
110
+ }
111
+
112
+ export const looksLikeFetchResponse = (maybeResponse: unknown): maybeResponse is Response => {
113
+ return (
114
+ typeof maybeResponse === 'object' &&
115
+ maybeResponse !== null &&
116
+ 'status' in maybeResponse &&
117
+ 'ok' in maybeResponse &&
118
+ 'json' in maybeResponse &&
119
+ typeof (maybeResponse as any).json === 'function'
120
+ )
121
+ }
122
+
123
+ // Storage helpers
124
+ export const setItemAsync = async (
125
+ storage: SupportedStorage,
126
+ key: string,
127
+ data: any
128
+ ): Promise<void> => {
129
+ await storage.setItem(key, JSON.stringify(data))
130
+ }
131
+
132
+ export const getItemAsync = async (storage: SupportedStorage, key: string): Promise<unknown> => {
133
+ const value = await storage.getItem(key)
134
+
135
+ if (!value) {
136
+ return null
137
+ }
138
+
139
+ try {
140
+ return JSON.parse(value)
141
+ } catch {
142
+ return value
143
+ }
144
+ }
145
+
146
+ export const removeItemAsync = async (storage: SupportedStorage, key: string): Promise<void> => {
147
+ await storage.removeItem(key)
148
+ }
149
+
150
+ /**
151
+ * A deferred represents some asynchronous work that is not yet finished, which
152
+ * may or may not culminate in a value.
153
+ * Taken from: https://github.com/mike-north/types/blob/master/src/async.ts
154
+ */
155
+ export class Deferred<T = any> {
156
+ public static promiseConstructor: PromiseConstructor = Promise
157
+
158
+ public readonly promise!: PromiseLike<T>
159
+
160
+ public readonly resolve!: (value?: T | PromiseLike<T>) => void
161
+
162
+ public readonly reject!: (reason?: any) => any
163
+
164
+ public constructor() {
165
+ // eslint-disable-next-line @typescript-eslint/no-extra-semi
166
+ ;(this as any).promise = new Deferred.promiseConstructor((res, rej) => {
167
+ // eslint-disable-next-line @typescript-eslint/no-extra-semi
168
+ ;(this as any).resolve = res
169
+ // eslint-disable-next-line @typescript-eslint/no-extra-semi
170
+ ;(this as any).reject = rej
171
+ })
172
+ }
173
+ }
174
+
175
+ export function decodeJWT(token: string): {
176
+ header: JwtHeader
177
+ payload: JwtPayload
178
+ signature: Uint8Array_
179
+ raw: {
180
+ header: string
181
+ payload: string
182
+ }
183
+ } {
184
+ const parts = token.split('.')
185
+
186
+ if (parts.length !== 3) {
187
+ throw new AuthInvalidJwtError('Invalid JWT structure')
188
+ }
189
+
190
+ // Regex checks for base64url format
191
+ for (let i = 0; i < parts.length; i++) {
192
+ if (!BASE64URL_REGEX.test(parts[i] as string)) {
193
+ throw new AuthInvalidJwtError('JWT not in base64url format')
194
+ }
195
+ }
196
+ const data = {
197
+ // using base64url lib
198
+ header: JSON.parse(stringFromBase64URL(parts[0])),
199
+ payload: JSON.parse(stringFromBase64URL(parts[1])),
200
+ signature: base64UrlToUint8Array(parts[2]),
201
+ raw: {
202
+ header: parts[0],
203
+ payload: parts[1],
204
+ },
205
+ }
206
+ return data
207
+ }
208
+
209
+ /**
210
+ * Creates a promise that resolves to null after some time.
211
+ */
212
+ export async function sleep(time: number): Promise<null> {
213
+ return await new Promise((accept) => {
214
+ setTimeout(() => accept(null), time)
215
+ })
216
+ }
217
+
218
+ /**
219
+ * Converts the provided async function into a retryable function. Each result
220
+ * or thrown error is sent to the isRetryable function which should return true
221
+ * if the function should run again.
222
+ */
223
+ export function retryable<T>(
224
+ fn: (attempt: number) => Promise<T>,
225
+ isRetryable: (attempt: number, error: any | null, result?: T) => boolean
226
+ ): Promise<T> {
227
+ const promise = new Promise<T>((accept, reject) => {
228
+ // eslint-disable-next-line @typescript-eslint/no-extra-semi
229
+ ;(async () => {
230
+ for (let attempt = 0; attempt < Infinity; attempt++) {
231
+ try {
232
+ const result = await fn(attempt)
233
+
234
+ if (!isRetryable(attempt, null, result)) {
235
+ accept(result)
236
+ return
237
+ }
238
+ } catch (e: any) {
239
+ if (!isRetryable(attempt, e)) {
240
+ reject(e)
241
+ return
242
+ }
243
+ }
244
+ }
245
+ })()
246
+ })
247
+
248
+ return promise
249
+ }
250
+
251
+ function dec2hex(dec: number) {
252
+ return ('0' + dec.toString(16)).substr(-2)
253
+ }
254
+
255
+ // Functions below taken from: https://stackoverflow.com/questions/63309409/creating-a-code-verifier-and-challenge-for-pkce-auth-on-spotify-api-in-reactjs
256
+ export function generatePKCEVerifier() {
257
+ const verifierLength = 56
258
+ const array = new Uint32Array(verifierLength)
259
+ if (typeof crypto === 'undefined') {
260
+ const charSet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~'
261
+ const charSetLen = charSet.length
262
+ let verifier = ''
263
+ for (let i = 0; i < verifierLength; i++) {
264
+ verifier += charSet.charAt(Math.floor(Math.random() * charSetLen))
265
+ }
266
+ return verifier
267
+ }
268
+ crypto.getRandomValues(array)
269
+ return Array.from(array, dec2hex).join('')
270
+ }
271
+
272
+ async function sha256(randomString: string) {
273
+ const encoder = new TextEncoder()
274
+ const encodedData = encoder.encode(randomString)
275
+ const hash = await crypto.subtle.digest('SHA-256', encodedData)
276
+ const bytes = new Uint8Array(hash)
277
+
278
+ return Array.from(bytes)
279
+ .map((c) => String.fromCharCode(c))
280
+ .join('')
281
+ }
282
+
283
+ export async function generatePKCEChallenge(verifier: string) {
284
+ const hasCryptoSupport =
285
+ typeof crypto !== 'undefined' &&
286
+ typeof crypto.subtle !== 'undefined' &&
287
+ typeof TextEncoder !== 'undefined'
288
+
289
+ if (!hasCryptoSupport) {
290
+ console.warn(
291
+ 'WebCrypto API is not supported. Code challenge method will default to use plain instead of sha256.'
292
+ )
293
+ return verifier
294
+ }
295
+ const hashed = await sha256(verifier)
296
+ return btoa(hashed).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '')
297
+ }
298
+
299
+ export async function getCodeChallengeAndMethod(
300
+ storage: SupportedStorage,
301
+ storageKey: string,
302
+ isPasswordRecovery = false
303
+ ) {
304
+ const codeVerifier = generatePKCEVerifier()
305
+ let storedCodeVerifier = codeVerifier
306
+ if (isPasswordRecovery) {
307
+ storedCodeVerifier += '/PASSWORD_RECOVERY'
308
+ }
309
+ await setItemAsync(storage, `${storageKey}-code-verifier`, storedCodeVerifier)
310
+ const codeChallenge = await generatePKCEChallenge(codeVerifier)
311
+ const codeChallengeMethod = codeVerifier === codeChallenge ? 'plain' : 's256'
312
+ return [codeChallenge, codeChallengeMethod]
313
+ }
314
+
315
+ /** Parses the API version which is 2YYY-MM-DD. */
316
+ const API_VERSION_REGEX = /^2[0-9]{3}-(0[1-9]|1[0-2])-(0[1-9]|1[0-9]|2[0-9]|3[0-1])$/i
317
+
318
+ export function parseResponseAPIVersion(response: Response) {
319
+ const apiVersion = response.headers.get(API_VERSION_HEADER_NAME)
320
+
321
+ if (!apiVersion) {
322
+ return null
323
+ }
324
+
325
+ if (!apiVersion.match(API_VERSION_REGEX)) {
326
+ return null
327
+ }
328
+
329
+ try {
330
+ const date = new Date(`${apiVersion}T00:00:00.0Z`)
331
+ return date
332
+ } catch (e: any) {
333
+ return null
334
+ }
335
+ }
336
+
337
+ export function validateExp(exp: number) {
338
+ if (!exp) {
339
+ throw new Error('Missing exp claim')
340
+ }
341
+ const timeNow = Math.floor(Date.now() / 1000)
342
+ if (exp <= timeNow) {
343
+ throw new Error('JWT has expired')
344
+ }
345
+ }
346
+
347
+ export function getAlgorithm(
348
+ alg: 'HS256' | 'RS256' | 'ES256'
349
+ ): RsaHashedImportParams | EcKeyImportParams {
350
+ switch (alg) {
351
+ case 'RS256':
352
+ return {
353
+ name: 'RSASSA-PKCS1-v1_5',
354
+ hash: { name: 'SHA-256' },
355
+ }
356
+ case 'ES256':
357
+ return {
358
+ name: 'ECDSA',
359
+ namedCurve: 'P-256',
360
+ hash: { name: 'SHA-256' },
361
+ }
362
+ default:
363
+ throw new Error('Invalid alg claim')
364
+ }
365
+ }
366
+
367
+ const UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/
368
+
369
+ export function validateUUID(str: string) {
370
+ if (!UUID_REGEX.test(str)) {
371
+ throw new Error('@supabase/auth-js: Expected parameter to be UUID but is not')
372
+ }
373
+ }
374
+
375
+ export function userNotAvailableProxy(): User {
376
+ const proxyTarget = {} as User
377
+
378
+ return new Proxy(proxyTarget, {
379
+ get: (target: any, prop: string) => {
380
+ if (prop === '__isUserNotAvailableProxy') {
381
+ return true
382
+ }
383
+ // Preventative check for common problematic symbols during cloning/inspection
384
+ // These symbols might be accessed by structuredClone or other internal mechanisms.
385
+ if (typeof prop === 'symbol') {
386
+ const sProp = (prop as symbol).toString()
387
+ if (
388
+ sProp === 'Symbol(Symbol.toPrimitive)' ||
389
+ sProp === 'Symbol(Symbol.toStringTag)' ||
390
+ sProp === 'Symbol(util.inspect.custom)'
391
+ ) {
392
+ // Node.js util.inspect
393
+ return undefined
394
+ }
395
+ }
396
+ throw new Error(
397
+ `@supabase/auth-js: client was created with userStorage option and there was no user stored in the user storage. Accessing the "${prop}" property of the session object is not supported. Please use getUser() instead.`
398
+ )
399
+ },
400
+ set: (_target: any, prop: string) => {
401
+ throw new Error(
402
+ `@supabase/auth-js: client was created with userStorage option and there was no user stored in the user storage. Setting the "${prop}" property of the session object is not supported. Please use getUser() to fetch a user object you can manipulate.`
403
+ )
404
+ },
405
+ deleteProperty: (_target: any, prop: string) => {
406
+ throw new Error(
407
+ `@supabase/auth-js: client was created with userStorage option and there was no user stored in the user storage. Deleting the "${prop}" property of the session object is not supported. Please use getUser() to fetch a user object you can manipulate.`
408
+ )
409
+ },
410
+ })
411
+ }
412
+
413
+ /**
414
+ * Creates a proxy around a user object that warns when properties are accessed on the server.
415
+ * This is used to alert developers that using user data from getSession() on the server is insecure.
416
+ *
417
+ * @param user The actual user object to wrap
418
+ * @param suppressWarningRef An object with a 'value' property that controls warning suppression
419
+ * @returns A proxied user object that warns on property access
420
+ */
421
+ export function insecureUserWarningProxy(user: User, suppressWarningRef: { value: boolean }): User {
422
+ return new Proxy(user, {
423
+ get: (target: any, prop: string | symbol, receiver: any) => {
424
+ // Allow internal checks without warning
425
+ if (prop === '__isInsecureUserWarningProxy') {
426
+ return true
427
+ }
428
+
429
+ // Preventative check for common problematic symbols during cloning/inspection
430
+ // These symbols might be accessed by structuredClone or other internal mechanisms
431
+ if (typeof prop === 'symbol') {
432
+ const sProp = prop.toString()
433
+ if (
434
+ sProp === 'Symbol(Symbol.toPrimitive)' ||
435
+ sProp === 'Symbol(Symbol.toStringTag)' ||
436
+ sProp === 'Symbol(util.inspect.custom)' ||
437
+ sProp === 'Symbol(nodejs.util.inspect.custom)'
438
+ ) {
439
+ // Return the actual value for these symbols to allow proper inspection
440
+ return Reflect.get(target, prop, receiver)
441
+ }
442
+ }
443
+
444
+ // Emit warning on first property access
445
+ if (!suppressWarningRef.value && typeof prop === 'string') {
446
+ console.warn(
447
+ 'Using the user object as returned from supabase.auth.getSession() or from some supabase.auth.onAuthStateChange() events could be insecure! This value comes directly from the storage medium (usually cookies on the server) and may not be authentic. Use supabase.auth.getUser() instead which authenticates the data by contacting the Supabase Auth server.'
448
+ )
449
+ suppressWarningRef.value = true
450
+ }
451
+
452
+ return Reflect.get(target, prop, receiver)
453
+ },
454
+ })
455
+ }
456
+
457
+ /**
458
+ * Deep clones a JSON-serializable object using JSON.parse(JSON.stringify(obj)).
459
+ * Note: Only works for JSON-safe data.
460
+ */
461
+ export function deepClone<T>(obj: T): T {
462
+ return JSON.parse(JSON.stringify(obj))
463
+ }
@@ -0,0 +1,21 @@
1
+ import { SupportedStorage } from './types'
2
+
3
+ /**
4
+ * Returns a localStorage-like object that stores the key-value pairs in
5
+ * memory.
6
+ */
7
+ export function memoryLocalStorageAdapter(store: { [key: string]: string } = {}): SupportedStorage {
8
+ return {
9
+ getItem: (key) => {
10
+ return store[key] || null
11
+ },
12
+
13
+ setItem: (key, value) => {
14
+ store[key] = value
15
+ },
16
+
17
+ removeItem: (key) => {
18
+ delete store[key]
19
+ },
20
+ }
21
+ }