Skip to content

Commit

Permalink
fix: update types & check for WebCrypto
Browse files Browse the repository at this point in the history
  • Loading branch information
kangmingtay committed Jan 29, 2025
1 parent 545e610 commit a26e5dc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
12 changes: 5 additions & 7 deletions src/GoTrueClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ import type {
AuthMFAEnrollTOTPResponse,
AuthMFAEnrollPhoneResponse,
JWK,
JwtPayload,
} from './lib/types'
import { stringToUint8Array } from './lib/base64url'

Expand Down Expand Up @@ -2636,7 +2637,7 @@ export default class GoTrueClient {
jwks: { keys: JWK[] } = { keys: [] }
): Promise<
| {
data: { claims: { [key: string]: any } }
data: { claims: JwtPayload }
error: null
}
| { data: null; error: AuthError }
Expand All @@ -2662,8 +2663,8 @@ export default class GoTrueClient {
// Reject expired JWTs
validateExp(payload.exp)

// If symmetric algorithm, fallback to getUser()
if (header.alg === 'HS256') {
// If symmetric algorithm or WebCrypto API is unavailable, fallback to getUser()
if (header.alg === 'HS256' || !('crypto' in globalThis && 'subtle' in globalThis.crypto)) {
const { error } = await this.getUser(token)
if (error) {
throw error
Expand Down Expand Up @@ -2712,10 +2713,7 @@ export default class GoTrueClient {
if (isAuthError(error)) {
return { data: null, error }
}
return {
data: null,
error: new AuthUnknownError('Unknown error occurred while getting claims', error),
}
throw error
}
}
}
10 changes: 7 additions & 3 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1200,20 +1200,24 @@ export type AuthMFAEnrollPhoneResponse =
error: AuthError
}

export interface JwtHeader {
export type JwtHeader = {
alg: 'RS256' | 'ES256' | 'HS256'
kid: string
typ: string
}

export interface JwtPayload {
export type RequiredClaims = {
iss: string
sub: string
aud: string | string[]
exp: number
iat: number
role: string
aal: AuthenticatorAssuranceLevels
session_id: string
}

// any other non-standard claim in the payload
export type JwtPayload = RequiredClaims & {
[key: string]: any
}

Expand Down

0 comments on commit a26e5dc

Please sign in to comment.