Skip to content

Commit 3359275

Browse files
committed
further simplify code
1 parent 1234c1b commit 3359275

File tree

2 files changed

+22
-42
lines changed

2 files changed

+22
-42
lines changed

src/GoTrueClient.ts

Lines changed: 19 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3066,12 +3066,13 @@ export default class GoTrueClient {
30663066
validateExp(payload.exp)
30673067
}
30683068

3069+
const signingKey =
3070+
!header.alg || header.alg.startsWith('HS') || !header.kid
3071+
? null
3072+
: await this.fetchJwk(header.kid, options?.keys ? { keys: options.keys } : options?.jwks)
3073+
30693074
// If symmetric algorithm or WebCrypto API is unavailable, fallback to getUser()
3070-
if (
3071-
!header.kid ||
3072-
header.alg === 'HS256' ||
3073-
!('crypto' in globalThis && 'subtle' in globalThis.crypto)
3074-
) {
3075+
if (!signingKey || !('crypto' in globalThis && 'subtle' in globalThis.crypto)) {
30753076
const { error } = await this.getUser(token)
30763077
if (error) {
30773078
throw error
@@ -3088,45 +3089,22 @@ export default class GoTrueClient {
30883089
}
30893090

30903091
const algorithm = getAlgorithm(header.alg)
3091-
const signingKey = await this.fetchJwk(
3092-
header.kid,
3093-
options?.keys ? { keys: options.keys } : options?.jwks
3094-
)
30953092

3096-
if (signingKey) {
3097-
// Convert JWK to CryptoKey
3098-
const publicKey = await crypto.subtle.importKey('jwk', signingKey, algorithm, true, [
3099-
'verify',
3100-
])
3101-
3102-
// Verify the signature
3103-
const isValid = await crypto.subtle.verify(
3104-
algorithm,
3105-
publicKey,
3106-
signature,
3107-
stringToUint8Array(`${rawHeader}.${rawPayload}`)
3108-
)
3093+
// Convert JWK to CryptoKey
3094+
const publicKey = await crypto.subtle.importKey('jwk', signingKey, algorithm, true, [
3095+
'verify',
3096+
])
31093097

3110-
if (!isValid) {
3111-
throw new AuthInvalidJwtError('Invalid JWT signature')
3112-
}
3113-
} else {
3114-
// no signing key found in the JWKS, this might mean that the developer rotated the JWT signing key too fast without waiting for all caches to be purged
3115-
// in this case, validate the JWT directly with the Auth server
3098+
// Verify the signature
3099+
const isValid = await crypto.subtle.verify(
3100+
algorithm,
3101+
publicKey,
3102+
signature,
3103+
stringToUint8Array(`${rawHeader}.${rawPayload}`)
3104+
)
31163105

3117-
const { error } = await this.getUser(token)
3118-
if (error) {
3119-
throw error
3120-
}
3121-
// getUser succeeds so the claims in the JWT can be trusted
3122-
return {
3123-
data: {
3124-
claims: payload,
3125-
header,
3126-
signature,
3127-
},
3128-
error: null,
3129-
}
3106+
if (!isValid) {
3107+
throw new AuthInvalidJwtError('Invalid JWT signature')
31303108
}
31313109

31323110
// If verification succeeds, decode and return claims

src/lib/helpers.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,9 @@ export function validateExp(exp: number) {
340340
}
341341
}
342342

343-
export function getAlgorithm(alg: 'RS256' | 'ES256'): RsaHashedImportParams | EcKeyImportParams {
343+
export function getAlgorithm(
344+
alg: 'HS256' | 'RS256' | 'ES256'
345+
): RsaHashedImportParams | EcKeyImportParams {
344346
switch (alg) {
345347
case 'RS256':
346348
return {

0 commit comments

Comments
 (0)