Skip to content

Commit 385f714

Browse files
authored
refactor: replace custom JsonWebKeySet with JSONWebKeySet (#91)
1 parent 744105b commit 385f714

5 files changed

Lines changed: 21 additions & 24 deletions

File tree

src/core/resolve-env.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { afterEach, describe, expect, it, vi } from 'vitest'
22

33
import { resolveEnv } from './resolve-env.js'
44
import { MissingSupabaseURLError } from '../errors.js'
5-
import type { JsonWebKeySet } from '../types.js'
5+
import type { JSONWebKeySet } from 'jose'
66

77
describe('resolveEnv', () => {
88
afterEach(() => {
@@ -244,7 +244,7 @@ describe('resolveEnv', () => {
244244
default: 'sb_secret_fake_default_key_val',
245245
internal: 'sb_secret_fake_internal_key',
246246
})
247-
const jwks = result.data!.jwks as JsonWebKeySet
247+
const jwks = result.data!.jwks as JSONWebKeySet
248248
expect(jwks.keys).toHaveLength(2)
249249
expect((jwks.keys[0] as Record<string, unknown>).kid).toBe(
250250
'cb770052-bdd3-4f5e-8d6f-8836046b7c93',

src/core/resolve-env.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { EnvError, Errors, MissingSupabaseURLError } from '../errors.js'
2-
import type { JsonWebKeySet, SupabaseEnv } from '../types.js'
2+
import type { JSONWebKeySet } from 'jose'
3+
4+
import type { SupabaseEnv } from '../types.js'
35

46
/**
57
* Reads an environment variable from the current runtime (Deno, Node.js, or Bun).
@@ -64,13 +66,13 @@ function resolveKeys(
6466
*
6567
* @internal
6668
*/
67-
function parseJwks(raw: string | undefined): JsonWebKeySet | null {
69+
function parseJwks(raw: string | undefined): JSONWebKeySet | null {
6870
if (!raw) return null
6971
try {
7072
const parsed = JSON.parse(raw)
7173
if (Array.isArray(parsed)) return { keys: parsed }
7274
if (parsed?.keys && Array.isArray(parsed.keys))
73-
return parsed as JsonWebKeySet
75+
return parsed as JSONWebKeySet
7476
return null
7577
} catch {
7678
return null
@@ -124,7 +126,7 @@ function parseJwksUrl(raw: string | undefined): URL | null {
124126
*
125127
* @internal
126128
*/
127-
function resolveJwks(): JsonWebKeySet | URL | null {
129+
function resolveJwks(): JSONWebKeySet | URL | null {
128130
const rawJwks = getEnvVar('SUPABASE_JWKS')
129131
if (rawJwks && rawJwks.trim()) {
130132
return parseJwks(rawJwks)

src/core/verify-credentials.test.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import {
99
vi,
1010
} from 'vitest'
1111

12-
import type { Credentials, JsonWebKeySet, SupabaseEnv } from '../types.js'
12+
import type { JSONWebKeySet } from 'jose'
13+
14+
import type { Credentials, SupabaseEnv } from '../types.js'
1315
import { verifyCredentials } from './verify-credentials.js'
1416
import { _resetAllowDeprecationWarned } from './utils/deprecation.js'
1517
import { InvalidCredentialsError } from '../errors.js'
@@ -276,7 +278,7 @@ describe('verifyCredentials', () => {
276278
})
277279

278280
describe('user mode', () => {
279-
let jwks: JsonWebKeySet
281+
let jwks: JSONWebKeySet
280282
let validTokens: string[]
281283

282284
beforeAll(async () => {
@@ -380,7 +382,7 @@ describe('verifyCredentials', () => {
380382
})
381383

382384
describe('user mode with remote JWKS URL', () => {
383-
let jwks: JsonWebKeySet
385+
let jwks: JSONWebKeySet
384386
let validTokens: string[]
385387
let fetchMock: ReturnType<typeof vi.fn>
386388

@@ -516,7 +518,7 @@ describe('verifyCredentials', () => {
516518
publicJwkB.alg = 'RS256'
517519
publicJwkB.use = 'sig'
518520
publicJwkB.kid = 'remote-key-b'
519-
const jwksB: JsonWebKeySet = { keys: [publicJwkB] }
521+
const jwksB: JSONWebKeySet = { keys: [publicJwkB] }
520522
const tokenB = await new SignJWT({
521523
sub: 'user-remote-b',
522524
role: 'authenticated',
@@ -628,7 +630,7 @@ describe('verifyCredentials', () => {
628630
})
629631

630632
describe('invalid credential rejection (no silent fallthrough)', () => {
631-
let jwks: JsonWebKeySet
633+
let jwks: JSONWebKeySet
632634

633635
beforeAll(async () => {
634636
const { publicKey } = await generateKeyPair('RS256')

src/core/verify-credentials.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import type {
1515
AuthModeWithKey,
1616
AuthResult,
1717
Credentials,
18-
JsonWebKeySet,
1918
JWTClaims,
2019
SupabaseEnv,
2120
UserClaims,
@@ -111,7 +110,7 @@ let remoteJwksResolver: { url: string; resolver: JwksResolver } | undefined =
111110
*
112111
* @internal
113112
*/
114-
function getJwksResolver(jwks: JsonWebKeySet | URL): JwksResolver {
113+
function getJwksResolver(jwks: JSONWebKeySet | URL): JwksResolver {
115114
if (jwks instanceof URL) {
116115
const url = jwks.toString()
117116
if (remoteJwksResolver?.url !== url) {

src/types.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
import type { JSONWebKeySet } from 'jose'
2+
13
import type {
24
SupabaseClient,
35
SupabaseClientOptions,
46
} from '@supabase/supabase-js'
57

8+
export type { JSONWebKeySet }
9+
610
/**
711
* Authentication mode that determines what credentials a request must provide.
812
*
@@ -100,17 +104,7 @@ export interface SupabaseEnv {
100104
* Each env var is authoritative when set: a malformed value resolves to
101105
* `null` rather than falling through to the other variable.
102106
*/
103-
jwks: JsonWebKeySet | URL | null
104-
}
105-
106-
/**
107-
* A JSON Web Key Set as defined by RFC 7517.
108-
*
109-
* @see https://datatracker.ietf.org/doc/html/rfc7517
110-
*/
111-
export interface JsonWebKeySet {
112-
/** Array of JSON Web Keys. */
113-
keys: JsonWebKey[]
107+
jwks: JSONWebKeySet | URL | null
114108
}
115109

116110
/**

0 commit comments

Comments
 (0)