@powersync/service-core 0.0.0-dev-20250724111728 → 0.0.0-dev-20250729121434

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.
@@ -4,7 +4,7 @@ import secs from '../util/secs.js';
4
4
  import { JwtPayload } from './JwtPayload.js';
5
5
  import { KeyCollector } from './KeyCollector.js';
6
6
  import { KeyOptions, KeySpec, SUPPORTED_ALGORITHMS } from './KeySpec.js';
7
- import { mapAuthError } from './utils.js';
7
+ import { debugKeyNotFound, mapAuthError, SupabaseAuthDetails, tokenDebugDetails } from './utils.js';
8
8
 
9
9
  /**
10
10
  * KeyStore to get keys and verify tokens.
@@ -39,6 +39,29 @@ export class KeyStore<Collector extends KeyCollector = KeyCollector> {
39
39
  */
40
40
  collector: Collector;
41
41
 
42
+ /**
43
+ * For debug purposes only.
44
+ *
45
+ * This is very Supabase-specific, but we need the info on this level. For example,
46
+ * we want to detect cases where a Supabase token is used, but Supabase auth is not enabled
47
+ * (no Supabase collector configured).
48
+ */
49
+ supabaseAuthDebug: {
50
+ /**
51
+ * This can be populated without jwksEnabled, but not the other way around.
52
+ */
53
+ jwksDetails: SupabaseAuthDetails | null;
54
+ jwksEnabled: boolean;
55
+ /**
56
+ * This can be enabled without jwksDetails populated.
57
+ */
58
+ sharedSecretEnabled: boolean;
59
+ } = {
60
+ jwksDetails: null,
61
+ jwksEnabled: false,
62
+ sharedSecretEnabled: false
63
+ };
64
+
42
65
  constructor(collector: Collector) {
43
66
  this.collector = collector;
44
67
  }
@@ -131,7 +154,7 @@ export class KeyStore<Collector extends KeyCollector = KeyCollector> {
131
154
  if (!key.matchesAlgorithm(header.alg)) {
132
155
  throw new AuthorizationError(ErrorCode.PSYNC_S2101, `Unexpected token algorithm ${header.alg}`, {
133
156
  configurationDetails: `Key kid: ${key.source.kid}, alg: ${key.source.alg}, kty: ${key.source.kty}`
134
- // Token details automatically populated elsewhere
157
+ // tokenDetails automatically populated higher up the stack
135
158
  });
136
159
  }
137
160
  return key;
@@ -165,12 +188,13 @@ export class KeyStore<Collector extends KeyCollector = KeyCollector> {
165
188
  logger.error(`Failed to refresh keys`, e);
166
189
  });
167
190
 
191
+ const details = debugKeyNotFound(this, keys, token);
192
+
168
193
  throw new AuthorizationError(
169
194
  ErrorCode.PSYNC_S2101,
170
195
  'Could not find an appropriate key in the keystore. The key is missing or no key matched the token KID',
171
196
  {
172
- configurationDetails: `Known keys: ${keys.map((key) => key.description).join(', ')}`
173
- // tokenDetails automatically populated later
197
+ ...details
174
198
  }
175
199
  );
176
200
  }
@@ -12,10 +12,11 @@ import {
12
12
  ServiceError
13
13
  } from '@powersync/lib-services-framework';
14
14
  import { KeyCollector, KeyResult } from './KeyCollector.js';
15
- import { KeySpec } from './KeySpec.js';
15
+ import { KeyOptions, KeySpec } from './KeySpec.js';
16
16
 
17
17
  export type RemoteJWKSCollectorOptions = {
18
18
  lookupOptions?: LookupOptions;
19
+ keyOptions?: KeyOptions;
19
20
  };
20
21
 
21
22
  /**
@@ -24,6 +25,7 @@ export type RemoteJWKSCollectorOptions = {
24
25
  export class RemoteJWKSCollector implements KeyCollector {
25
26
  private url: URL;
26
27
  private agent: http.Agent;
28
+ private keyOptions: KeyOptions;
27
29
 
28
30
  constructor(
29
31
  url: string,
@@ -34,6 +36,7 @@ export class RemoteJWKSCollector implements KeyCollector {
34
36
  } catch (e: any) {
35
37
  throw new ServiceError(ErrorCode.PSYNC_S3102, `Invalid jwks_uri: ${JSON.stringify(url)} Details: ${e.message}`);
36
38
  }
39
+ this.keyOptions = options?.keyOptions ?? {};
37
40
 
38
41
  // We do support http here for self-hosting use cases.
39
42
  // Management service restricts this to https for hosted versions.
@@ -123,7 +126,7 @@ export class RemoteJWKSCollector implements KeyCollector {
123
126
  }
124
127
  }
125
128
 
126
- const key = await KeySpec.importKey(keyData);
129
+ const key = await KeySpec.importKey(keyData, this.keyOptions);
127
130
  keys.push(key);
128
131
  }
129
132
 
@@ -2,7 +2,7 @@ import * as jose from 'jose';
2
2
  import { KeySpec, KeyOptions } from './KeySpec.js';
3
3
  import { KeyCollector, KeyResult } from './KeyCollector.js';
4
4
 
5
- const SUPABASE_KEY_OPTIONS: KeyOptions = {
5
+ export const SUPABASE_KEY_OPTIONS: KeyOptions = {
6
6
  requiresAudience: ['authenticated'],
7
7
  maxLifetimeSeconds: 86400 * 7 + 1200 // 1 week + 20 minutes margin
8
8
  };
package/src/auth/utils.ts CHANGED
@@ -1,5 +1,9 @@
1
1
  import { AuthorizationError, ErrorCode } from '@powersync/lib-services-framework';
2
2
  import * as jose from 'jose';
3
+ import * as urijs from 'uri-js';
4
+ import * as uuid from 'uuid';
5
+ import { KeySpec } from './KeySpec.js';
6
+ import { KeyStore } from './KeyStore.js';
3
7
 
4
8
  export function mapJoseError(error: jose.errors.JOSEError, token: string): AuthorizationError {
5
9
  const tokenDetails = tokenDebugDetails(token);
@@ -61,15 +65,28 @@ export function mapAuthConfigError(error: any): AuthorizationError {
61
65
  * We use this to add details to our logs. We don't log the entire token, since it may for example
62
66
  * a password incorrectly used as a token.
63
67
  */
64
- function tokenDebugDetails(token: string): string {
68
+ export function tokenDebugDetails(token: string): string {
69
+ return parseTokenDebug(token).description;
70
+ }
71
+
72
+ function parseTokenDebug(token: string) {
65
73
  try {
66
74
  // For valid tokens, we return the header and payload
67
75
  const header = jose.decodeProtectedHeader(token);
68
76
  const payload = jose.decodeJwt(token);
69
- return `<header: ${JSON.stringify(header)} payload: ${JSON.stringify(payload)}>`;
77
+ const isSupabase = typeof payload.iss == 'string' && payload.iss.includes('supabase.co');
78
+ const isSharedSecret = isSupabase && header.alg === 'HS256';
79
+
80
+ return {
81
+ header,
82
+ payload,
83
+ isSupabase,
84
+ isSharedSecret: isSharedSecret,
85
+ description: `<header: ${JSON.stringify(header)} payload: ${JSON.stringify(payload)}>`
86
+ };
70
87
  } catch (e) {
71
88
  // Token fails to parse. Return some details.
72
- return invalidTokenDetails(token);
89
+ return { description: invalidTokenDetails(token) };
73
90
  }
74
91
  }
75
92
 
@@ -100,3 +117,106 @@ function invalidTokenDetails(token: string): string {
100
117
 
101
118
  return `<invalid JWT, length=${token.length}>`;
102
119
  }
120
+
121
+ export interface SupabaseAuthDetails {
122
+ projectId: string;
123
+ url: string;
124
+ hostname: string;
125
+ }
126
+
127
+ export function getSupabaseJwksUrl(connection: any): SupabaseAuthDetails | null {
128
+ if (connection == null) {
129
+ return null;
130
+ } else if (connection.type != 'postgresql') {
131
+ return null;
132
+ }
133
+
134
+ let hostname: string | undefined = connection.hostname;
135
+ if (hostname == null && typeof connection.uri == 'string') {
136
+ hostname = urijs.parse(connection.uri).host;
137
+ }
138
+ if (hostname == null) {
139
+ return null;
140
+ }
141
+
142
+ const match = /db.(\w+).supabase.co/.exec(hostname);
143
+ if (match == null) {
144
+ return null;
145
+ }
146
+ const projectId = match[1];
147
+
148
+ return { projectId, hostname, url: `https://${projectId}.supabase.co/auth/v1/.well-known/jwks.json` };
149
+ }
150
+
151
+ export function debugKeyNotFound(
152
+ keyStore: KeyStore,
153
+ keys: KeySpec[],
154
+ token: string
155
+ ): { configurationDetails: string; tokenDetails: string } {
156
+ const knownKeys = keys.map((key) => key.description).join(', ');
157
+ const td = parseTokenDebug(token);
158
+ const tokenDetails = td.description;
159
+ const configuredSupabase = keyStore.supabaseAuthDebug;
160
+
161
+ // Cases to check:
162
+ // 1. Is Supabase token, but supabase auth not enabled.
163
+ // 2. Is Supabase HS256 token, but no secret configured.
164
+ // 3. Is Supabase singing key token, but no Supabase signing keys configured.
165
+ // 4. Supabase project id mismatch.
166
+
167
+ if (td.isSharedSecret) {
168
+ // Supabase HS256 token
169
+ // UUID: HS256 (Shared Secret)
170
+ // Other: Legacy HS256 (Shared Secret)
171
+ // Not a big difference between the two other than terminology used on Supabase.
172
+ const isLegacy = uuid.validate(td.header.kid) ? false : true;
173
+ const addMessage =
174
+ configuredSupabase.jwksEnabled && !isLegacy
175
+ ? ' Use asymmetric keys on Supabase (RSA or ECC) to allow automatic key retrieval.'
176
+ : '';
177
+ if (!configuredSupabase.sharedSecretEnabled) {
178
+ return {
179
+ configurationDetails: `Token is a Supabase ${isLegacy ? 'Legacy ' : ''}HS256 (Shared Secret) token, but Supabase JWT secret is not configured.${addMessage}`,
180
+ tokenDetails
181
+ };
182
+ } else {
183
+ return {
184
+ // This is an educated guess
185
+ configurationDetails: `Token is a Supabase ${isLegacy ? 'Legacy ' : ''}HS256 (Shared Secret) token, but configured Supabase JWT secret does not match.${addMessage}`,
186
+ tokenDetails
187
+ };
188
+ }
189
+ } else if (td.isSupabase) {
190
+ // Supabase JWT Signing Keys
191
+ if (!configuredSupabase.jwksEnabled) {
192
+ if (configuredSupabase.jwksDetails != null) {
193
+ return {
194
+ configurationDetails: `Token uses Supabase JWT Signing Keys, but Supabase Auth is not enabled`,
195
+ tokenDetails
196
+ };
197
+ } else {
198
+ return {
199
+ configurationDetails: `Token uses Supabase JWT Signing Keys, but no Supabase connection is configured`,
200
+ tokenDetails
201
+ };
202
+ }
203
+ } else if (configuredSupabase.jwksDetails != null) {
204
+ const configuredProjectId = configuredSupabase.jwksDetails.projectId;
205
+ const issuer = td.payload.iss as string; // Is a string since since isSupabase is true
206
+ if (!issuer.includes(configuredProjectId)) {
207
+ return {
208
+ configurationDetails: `Supabase project id mismatch. Expected project: ${configuredProjectId}, got issuer: ${issuer}`,
209
+ tokenDetails
210
+ };
211
+ } else {
212
+ // Project id matches, but no matching keys found
213
+ return {
214
+ configurationDetails: `Supabase signing keys configured, but no matching keys found. Known keys: ${knownKeys}`,
215
+ tokenDetails
216
+ };
217
+ }
218
+ }
219
+ }
220
+
221
+ return { configurationDetails: `Known keys: ${knownKeys}`, tokenDetails: tokenDebugDetails(token) };
222
+ }
@@ -29,9 +29,6 @@ export class EmitterEngine implements BaseEmitterEngine {
29
29
  }
30
30
 
31
31
  emit<K extends keyof event_types.SubscribeEvents>(event: K, data: event_types.SubscribeEvents[K]): void {
32
- if (!this.events.has(event) || this.countListeners(event) === 0) {
33
- logger.warn(`${event} has no listener registered.`);
34
- }
35
32
  this.emitter.emit(event, data);
36
33
  }
37
34
 
@@ -89,6 +89,7 @@ export class CompoundConfigCollector {
89
89
  }
90
90
  ])
91
91
  );
92
+ keyStore.supabaseAuthDebug.sharedSecretEnabled = true;
92
93
  }
93
94
 
94
95
  let jwks_uris = baseConfig.client_auth?.jwks_uri ?? [];
@@ -114,6 +115,29 @@ export class CompoundConfigCollector {
114
115
  for (let uri of jwks_uris) {
115
116
  collectors.add(new auth.CachedKeyCollector(new auth.RemoteJWKSCollector(uri, { lookupOptions: jwksLookup })));
116
117
  }
118
+ const supabaseAuthDetails = auth.getSupabaseJwksUrl(baseConfig.replication?.connections?.[0]);
119
+ keyStore.supabaseAuthDebug.jwksDetails = supabaseAuthDetails;
120
+
121
+ if (baseConfig.client_auth?.supabase) {
122
+ // Automatic support for Supabase signing keys:
123
+ // https://supabase.com/docs/guides/auth/signing-keys
124
+ if (supabaseAuthDetails != null) {
125
+ const collector = new auth.RemoteJWKSCollector(supabaseAuthDetails.url, {
126
+ lookupOptions: jwksLookup,
127
+ // Special case aud and max lifetime for Supabase keys
128
+ keyOptions: auth.SUPABASE_KEY_OPTIONS
129
+ });
130
+ collectors.add(new auth.CachedKeyCollector(collector));
131
+ keyStore.supabaseAuthDebug.jwksEnabled = true;
132
+ logger.info(`Configured Supabase Auth with ${supabaseAuthDetails.url}`);
133
+ } else {
134
+ logger.warn(
135
+ 'Supabase Auth is enabled, but no Supabase connection string found. Skipping Supabase JWKS URL configuration.'
136
+ );
137
+ }
138
+ } else if (supabaseAuthDetails != null) {
139
+ logger.warn(`Supabase connection string found, but Supabase Auth is not enabled in the config.`);
140
+ }
117
141
 
118
142
  const sync_rules = await this.collectSyncRules(baseConfig, runnerConfig);
119
143
 
@@ -6,7 +6,8 @@ import { KeySpec } from '../../src/auth/KeySpec.js';
6
6
  import { RemoteJWKSCollector } from '../../src/auth/RemoteJWKSCollector.js';
7
7
  import { KeyResult } from '../../src/auth/KeyCollector.js';
8
8
  import { CachedKeyCollector } from '../../src/auth/CachedKeyCollector.js';
9
- import { JwtPayload } from '@/index.js';
9
+ import { JwtPayload, StaticSupabaseKeyCollector } from '@/index.js';
10
+ import { debugKeyNotFound } from '../../src/auth/utils.js';
10
11
 
11
12
  const publicKeyRSA: jose.JWK = {
12
13
  use: 'sig',
@@ -438,4 +439,325 @@ describe('JWT Auth', () => {
438
439
 
439
440
  expect(verified.claim).toEqual('test-claim-2');
440
441
  });
442
+
443
+ describe('debugKeyNotFound', () => {
444
+ test('Supabase token with legacy auth not configured', async () => {
445
+ const keys = await StaticSupabaseKeyCollector.importKeys([]);
446
+ const store = new KeyStore(keys);
447
+
448
+ // Mock Supabase debug info - legacy not enabled
449
+ store.supabaseAuthDebug = {
450
+ jwksDetails: null,
451
+ jwksEnabled: false,
452
+ sharedSecretEnabled: false
453
+ };
454
+
455
+ // Create a legacy Supabase token (HS256)
456
+ const token = await new jose.SignJWT({})
457
+ .setProtectedHeader({ alg: 'HS256', kid: 'test' })
458
+ .setSubject('test')
459
+ .setIssuer('https://abc123.supabase.co/auth/v1')
460
+ .setAudience('authenticated')
461
+ .setExpirationTime('1h')
462
+ .setIssuedAt()
463
+ .sign(Buffer.from('secret'));
464
+
465
+ const err = await store.verifyJwt(token, { defaultAudiences: [], maxAge: '1d' }).catch((e) => e);
466
+ expect(err.configurationDetails).toMatch(
467
+ 'Token is a Supabase Legacy HS256 (Shared Secret) token, but Supabase JWT secret is not configured'
468
+ );
469
+ });
470
+
471
+ test('Legacy Supabase token with wrong secret', async () => {
472
+ const keys = await StaticSupabaseKeyCollector.importKeys([sharedKey]);
473
+ const store = new KeyStore(keys);
474
+
475
+ // Mock Supabase debug info - legacy enabled
476
+ store.supabaseAuthDebug = {
477
+ jwksDetails: null,
478
+ jwksEnabled: false,
479
+ sharedSecretEnabled: true
480
+ };
481
+
482
+ // Create a legacy Supabase token (HS256)
483
+ const token = await new jose.SignJWT({})
484
+ .setProtectedHeader({ alg: 'HS256', kid: sharedKey2.kid })
485
+ .setSubject('test')
486
+ .setIssuer('https://abc123.supabase.co/auth/v1')
487
+ .setAudience('authenticated')
488
+ .setExpirationTime('1h')
489
+ .setIssuedAt()
490
+ .sign(await jose.importJWK(sharedKey2));
491
+
492
+ const err = await store.verifyJwt(token, { defaultAudiences: [], maxAge: '1d' }).catch((e) => e);
493
+ expect(err.configurationDetails).toMatch(
494
+ 'Token is a Supabase Legacy HS256 (Shared Secret) token, but configured Supabase JWT secret does not match'
495
+ );
496
+ });
497
+
498
+ test('New HS256 Supabase token with wrong secret', async () => {
499
+ const keys = await StaticSupabaseKeyCollector.importKeys([sharedKey]);
500
+ const store = new KeyStore(keys);
501
+
502
+ // Mock Supabase debug info - legacy enabled
503
+ store.supabaseAuthDebug = {
504
+ jwksDetails: null,
505
+ jwksEnabled: false,
506
+ sharedSecretEnabled: true
507
+ };
508
+
509
+ // Create a new HS256 Supabase token.
510
+ // The only real difference here is that the kid is a UUID
511
+ const token = await new jose.SignJWT({})
512
+ .setProtectedHeader({ alg: 'HS256', kid: '2fc01f1d-90fb-4c8b-b646-1c06ed86be46' })
513
+ .setSubject('test')
514
+ .setIssuer('https://abc123.supabase.co/auth/v1')
515
+ .setAudience('authenticated')
516
+ .setExpirationTime('1h')
517
+ .setIssuedAt()
518
+ .sign(await jose.importJWK(sharedKey2));
519
+
520
+ const err = await store.verifyJwt(token, { defaultAudiences: [], maxAge: '1d' }).catch((e) => e);
521
+ expect(err.configurationDetails).toMatch(
522
+ 'Token is a Supabase HS256 (Shared Secret) token, but configured Supabase JWT secret does not match'
523
+ );
524
+ });
525
+
526
+ test('Supabase signing key token with no Supabase connection', async () => {
527
+ const keys = await StaticSupabaseKeyCollector.importKeys([]);
528
+ const store = new KeyStore(keys);
529
+
530
+ // Mock Supabase debug info - no Supabase connection
531
+ store.supabaseAuthDebug = {
532
+ jwksDetails: null,
533
+ jwksEnabled: false,
534
+ sharedSecretEnabled: false
535
+ };
536
+
537
+ const signKey = await jose.importJWK(privateKeyECDSA);
538
+ const token = await new jose.SignJWT({})
539
+ .setProtectedHeader({ alg: 'ES256', kid: 'test-kid' })
540
+ .setSubject('test')
541
+ .setIssuer('https://abc123.supabase.co/auth/v1')
542
+ .setAudience('authenticated')
543
+ .setExpirationTime('1h')
544
+ .setIssuedAt()
545
+ .sign(signKey);
546
+
547
+ const err = await store.verifyJwt(token, { defaultAudiences: [], maxAge: '1d' }).catch((e) => e);
548
+ expect(err.configurationDetails).toMatch(
549
+ 'Token uses Supabase JWT Signing Keys, but no Supabase connection is configured'
550
+ );
551
+ });
552
+
553
+ test('Supabase signing key token with Supabase auth disabled', async () => {
554
+ const keys = await StaticSupabaseKeyCollector.importKeys([]);
555
+ const store = new KeyStore(keys);
556
+
557
+ // Mock Supabase debug info - Supabase project, but Supabase auth not enabled
558
+ store.supabaseAuthDebug = {
559
+ jwksDetails: {
560
+ projectId: 'abc123',
561
+ hostname: 'db.abc123.supabase.co',
562
+ url: 'https://abc123.supabase.co/auth/v1/.well-known/jwks.json'
563
+ },
564
+ jwksEnabled: false,
565
+ sharedSecretEnabled: false
566
+ };
567
+
568
+ const signKey = await jose.importJWK(privateKeyECDSA);
569
+ const token = await new jose.SignJWT({})
570
+ .setProtectedHeader({ alg: 'ES256', kid: 'test-kid' })
571
+ .setSubject('test')
572
+ .setIssuer('https://abc123.supabase.co/auth/v1')
573
+ .setAudience('authenticated')
574
+ .setExpirationTime('1h')
575
+ .setIssuedAt()
576
+ .sign(signKey);
577
+
578
+ const err = await store.verifyJwt(token, { defaultAudiences: [], maxAge: '1d' }).catch((e) => e);
579
+ expect(err.configurationDetails).toMatch(
580
+ 'Token uses Supabase JWT Signing Keys, but Supabase Auth is not enabled'
581
+ );
582
+ });
583
+
584
+ test('Supabase project ID mismatch', async () => {
585
+ const keys = await StaticSupabaseKeyCollector.importKeys([publicKeyRSA]);
586
+ const store = new KeyStore(keys);
587
+
588
+ // Mock Supabase debug info - JWKS enabled with different project ID
589
+ store.supabaseAuthDebug = {
590
+ jwksDetails: {
591
+ projectId: 'expected123',
592
+ hostname: 'db.expected123.supabase.co',
593
+ url: 'https://expected123.supabase.co/auth/v1/.well-known/jwks.json'
594
+ },
595
+ jwksEnabled: true,
596
+ sharedSecretEnabled: false
597
+ };
598
+
599
+ // Create a modern Supabase token with different project ID
600
+ const token = await new jose.SignJWT({})
601
+ .setProtectedHeader({ alg: 'ES256', kid: privateKeyECDSA.kid })
602
+ .setSubject('test')
603
+ .setIssuer('https://different456.supabase.co/auth/v1')
604
+ .setAudience('authenticated')
605
+ .setExpirationTime('1h')
606
+ .setIssuedAt()
607
+ .sign(await jose.importJWK(privateKeyECDSA));
608
+
609
+ const err = await store.verifyJwt(token, { defaultAudiences: [], maxAge: '1d' }).catch((e) => e);
610
+ expect(err.configurationDetails).toMatch(
611
+ 'Supabase project id mismatch. Expected project: expected123, got issuer: https://different456.supabase.co/auth/v1'
612
+ );
613
+ });
614
+
615
+ test('Supabase signing keys configured but no matching keys', async () => {
616
+ const keys = await StaticSupabaseKeyCollector.importKeys([publicKeyRSA]);
617
+ const store = new KeyStore(keys);
618
+
619
+ // Mock Supabase debug info - JWKS enabled with matching project ID
620
+ store.supabaseAuthDebug = {
621
+ jwksDetails: {
622
+ projectId: 'abc123',
623
+ hostname: 'db.abc123.supabase.co',
624
+ url: 'https://abc123.supabase.co/auth/v1/.well-known/jwks.json'
625
+ },
626
+ jwksEnabled: true,
627
+ sharedSecretEnabled: false
628
+ };
629
+
630
+ // Create a modern Supabase token with matching project ID
631
+ const token = await new jose.SignJWT({})
632
+ .setProtectedHeader({ alg: 'ES256', kid: privateKeyECDSA.kid })
633
+ .setSubject('test')
634
+ .setIssuer('https://abc123.supabase.co/auth/v1')
635
+ .setAudience('authenticated')
636
+ .setExpirationTime('1h')
637
+ .setIssuedAt()
638
+ .sign(await jose.importJWK(privateKeyECDSA));
639
+
640
+ const err = await store.verifyJwt(token, { defaultAudiences: [], maxAge: '1d' }).catch((e) => e);
641
+ expect(err.configurationDetails).toMatch(
642
+ 'Supabase signing keys configured, but no matching keys found. Known keys: '
643
+ );
644
+ });
645
+
646
+ test('non-Supabase token', async () => {
647
+ const keys = await StaticSupabaseKeyCollector.importKeys([sharedKey]);
648
+ const store = new KeyStore(keys);
649
+
650
+ // Create a regular JWT token (not Supabase)
651
+ const token = await new jose.SignJWT({})
652
+ .setProtectedHeader({ alg: 'ES256', kid: privateKeyECDSA.kid })
653
+ .setSubject('test')
654
+ .setIssuer('https://regular-issuer.com')
655
+ .setAudience('my-audience')
656
+ .setExpirationTime('1h')
657
+ .setIssuedAt()
658
+ .sign(await jose.importJWK(privateKeyECDSA));
659
+
660
+ // Treated as just a generic unknown key
661
+ const err = await store.verifyJwt(token, { defaultAudiences: ['my-audience'], maxAge: '1d' }).catch((e) => e);
662
+ expect(err.configurationDetails).toMatch('Known keys:');
663
+ });
664
+
665
+ test('Valid legacy Supabase token', async () => {
666
+ const keys = await StaticSupabaseKeyCollector.importKeys([sharedKey]);
667
+ const store = new KeyStore(keys);
668
+
669
+ // Mock Supabase debug info - legacy enabled
670
+ store.supabaseAuthDebug = {
671
+ jwksDetails: null,
672
+ jwksEnabled: false,
673
+ sharedSecretEnabled: true
674
+ };
675
+
676
+ // Create a legacy Supabase token (HS256)
677
+ const token = await new jose.SignJWT({})
678
+ .setProtectedHeader({ alg: 'HS256', kid: sharedKey.kid })
679
+ .setSubject('test')
680
+ .setIssuer('https://abc123.supabase.co/auth/v1')
681
+ .setAudience('authenticated')
682
+ .setExpirationTime('1h')
683
+ .setIssuedAt()
684
+ .sign(await jose.importJWK(sharedKey));
685
+
686
+ await store.verifyJwt(token, { defaultAudiences: [], maxAge: '1d' });
687
+ });
688
+
689
+ test('Valid Supabase signing key', async () => {
690
+ const keys = await StaticSupabaseKeyCollector.importKeys([privateKeyECDSA]);
691
+ const store = new KeyStore(keys);
692
+
693
+ // Mock Supabase debug info - JWKS enabled, legacy disabled
694
+ store.supabaseAuthDebug = {
695
+ jwksDetails: null,
696
+ jwksEnabled: true,
697
+ sharedSecretEnabled: false
698
+ };
699
+
700
+ // Create a modern Supabase signing key token (ES256)
701
+ const token = await new jose.SignJWT({})
702
+ .setProtectedHeader({ alg: 'ES256', kid: privateKeyECDSA.kid })
703
+ .setSubject('test')
704
+ .setIssuer('https://abc123.supabase.co/auth/v1')
705
+ .setAudience('authenticated')
706
+ .setExpirationTime('1h')
707
+ .setIssuedAt()
708
+ .sign(await jose.importJWK(privateKeyECDSA));
709
+
710
+ await store.verifyJwt(token, { defaultAudiences: [], maxAge: '1d' });
711
+ });
712
+
713
+ test('Legacy Supabase anon token', async () => {
714
+ const keys = await StaticSupabaseKeyCollector.importKeys([sharedKey]);
715
+ const store = new KeyStore(keys);
716
+
717
+ // Mock Supabase debug info - legacy enabled
718
+ store.supabaseAuthDebug = {
719
+ jwksDetails: null,
720
+ jwksEnabled: false,
721
+ sharedSecretEnabled: true
722
+ };
723
+
724
+ // Create a legacy Supabase token (HS256)
725
+ const token = await new jose.SignJWT({})
726
+ .setProtectedHeader({ alg: 'HS256', kid: sharedKey.kid })
727
+ .setSubject('test')
728
+ .setIssuer('https://abc123.supabase.co/auth/v1')
729
+ .setAudience('anon')
730
+ .setExpirationTime('1h')
731
+ .setIssuedAt()
732
+ .sign(await jose.importJWK(sharedKey));
733
+
734
+ const err = await store.verifyJwt(token, { defaultAudiences: [], maxAge: '1d' }).catch((e) => e);
735
+ expect(err.message).toMatch('[PSYNC_S2105] Unexpected "aud" claim value: "anon"');
736
+ });
737
+
738
+ test('Supabase signing key anon token', async () => {
739
+ const keys = await StaticSupabaseKeyCollector.importKeys([privateKeyECDSA]);
740
+ const store = new KeyStore(keys);
741
+
742
+ // Mock Supabase debug info - JWKS enabled
743
+ store.supabaseAuthDebug = {
744
+ jwksDetails: null,
745
+ jwksEnabled: true,
746
+ sharedSecretEnabled: false
747
+ };
748
+
749
+ // Create a modern Supabase signing key token (ES256)
750
+ const token = await new jose.SignJWT({})
751
+ .setProtectedHeader({ alg: 'ES256', kid: privateKeyECDSA.kid })
752
+ .setSubject('test')
753
+ .setIssuer('https://abc123.supabase.co/auth/v1')
754
+ .setAudience('anon')
755
+ .setExpirationTime('1h')
756
+ .setIssuedAt()
757
+ .sign(await jose.importJWK(privateKeyECDSA));
758
+
759
+ const err = await store.verifyJwt(token, { defaultAudiences: [], maxAge: '1d' }).catch((e) => e);
760
+ expect(err.message).toMatch('[PSYNC_S2105] Unexpected "aud" claim value: "anon"');
761
+ });
762
+ });
441
763
  });