@powersync/service-core 1.13.2 → 1.13.4

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 (39) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/dist/auth/CachedKeyCollector.js +26 -2
  3. package/dist/auth/CachedKeyCollector.js.map +1 -1
  4. package/dist/auth/KeySpec.d.ts +1 -0
  5. package/dist/auth/KeySpec.js +12 -0
  6. package/dist/auth/KeySpec.js.map +1 -1
  7. package/dist/auth/KeyStore.js +2 -2
  8. package/dist/auth/KeyStore.js.map +1 -1
  9. package/dist/auth/RemoteJWKSCollector.js +6 -2
  10. package/dist/auth/RemoteJWKSCollector.js.map +1 -1
  11. package/dist/routes/auth.d.ts +1 -21
  12. package/dist/routes/auth.js +1 -97
  13. package/dist/routes/auth.js.map +1 -1
  14. package/dist/routes/configure-rsocket.js +4 -2
  15. package/dist/routes/configure-rsocket.js.map +1 -1
  16. package/dist/routes/router.d.ts +1 -2
  17. package/dist/routes/router.js.map +1 -1
  18. package/dist/util/config/compound-config-collector.js +0 -13
  19. package/dist/util/config/compound-config-collector.js.map +1 -1
  20. package/dist/util/config/types.d.ts +0 -12
  21. package/dist/util/util-index.d.ts +1 -0
  22. package/dist/util/util-index.js +1 -0
  23. package/dist/util/util-index.js.map +1 -1
  24. package/dist/util/version.d.ts +1 -0
  25. package/dist/util/version.js +3 -0
  26. package/dist/util/version.js.map +1 -0
  27. package/package.json +4 -4
  28. package/src/auth/CachedKeyCollector.ts +25 -3
  29. package/src/auth/KeySpec.ts +14 -0
  30. package/src/auth/KeyStore.ts +2 -2
  31. package/src/auth/RemoteJWKSCollector.ts +6 -2
  32. package/src/routes/auth.ts +1 -124
  33. package/src/routes/configure-rsocket.ts +3 -2
  34. package/src/routes/router.ts +0 -1
  35. package/src/util/config/compound-config-collector.ts +0 -16
  36. package/src/util/config/types.ts +0 -11
  37. package/src/util/util-index.ts +1 -0
  38. package/src/util/version.ts +3 -0
  39. package/tsconfig.tsbuildinfo +1 -1
@@ -3,7 +3,7 @@ import timers from 'timers/promises';
3
3
  import { KeySpec } from './KeySpec.js';
4
4
  import { LeakyBucket } from './LeakyBucket.js';
5
5
  import { KeyCollector, KeyResult } from './KeyCollector.js';
6
- import { AuthorizationError } from '@powersync/lib-services-framework';
6
+ import { AuthorizationError, ErrorCode, logger } from '@powersync/lib-services-framework';
7
7
  import { mapAuthConfigError } from './utils.js';
8
8
 
9
9
  /**
@@ -70,8 +70,21 @@ export class CachedKeyCollector implements KeyCollector {
70
70
  // e.g. in the case of waiting for error retries.
71
71
  // In the case of very slow requests, we don't wait for it to complete, but the
72
72
  // request can still complete in the background.
73
- const timeout = timers.setTimeout(3000);
74
- await Promise.race([this.refreshPromise, timeout]);
73
+ const WAIT_TIMEOUT_SECONDS = 3;
74
+ const timeout = timers.setTimeout(WAIT_TIMEOUT_SECONDS * 1000).then(() => {
75
+ throw new AuthorizationError(ErrorCode.PSYNC_S2204, `JWKS request failed`, {
76
+ cause: { message: `Key request timed out in ${WAIT_TIMEOUT_SECONDS}s`, name: 'AbortError' }
77
+ });
78
+ });
79
+ try {
80
+ await Promise.race([this.refreshPromise, timeout]);
81
+ } catch (e) {
82
+ if (e instanceof AuthorizationError) {
83
+ return { keys: this.currentKeys, errors: [...this.currentErrors, e] };
84
+ } else {
85
+ throw e;
86
+ }
87
+ }
75
88
  }
76
89
 
77
90
  return { keys: this.currentKeys, errors: this.currentErrors };
@@ -102,7 +115,16 @@ export class CachedKeyCollector implements KeyCollector {
102
115
  this.currentErrors = errors;
103
116
  this.keyTimestamp = Date.now();
104
117
  this.error = false;
118
+
119
+ // Due to caching and background refresh behavior, errors are not always propagated to the request handler,
120
+ // so we log them here.
121
+ for (let error of errors) {
122
+ logger.error(`Soft key refresh error`, error);
123
+ }
105
124
  } catch (e) {
125
+ // Due to caching and background refresh behavior, errors are not always propagated to the request handler,
126
+ // so we log them here.
127
+ logger.error(`Hard key refresh error`, e);
106
128
  this.error = true;
107
129
  // No result - keep previous keys
108
130
  this.currentErrors = [mapAuthConfigError(e)];
@@ -40,6 +40,20 @@ export class KeySpec {
40
40
  return this.source.kid;
41
41
  }
42
42
 
43
+ get description(): string {
44
+ let details: string[] = [];
45
+ details.push(`kid: ${this.kid ?? '*'}`);
46
+ details.push(`kty: ${this.source.kty}`);
47
+ if (this.source.alg != null) {
48
+ details.push(`alg: ${this.source.alg}`);
49
+ }
50
+ if (this.options.requiresAudience != null) {
51
+ details.push(`aud: ${this.options.requiresAudience.join(', ')}`);
52
+ }
53
+
54
+ return `<${details.filter((x) => x != null).join(', ')}>`;
55
+ }
56
+
43
57
  matchesAlgorithm(jwtAlg: string): boolean {
44
58
  if (this.source.alg) {
45
59
  return jwtAlg === this.source.alg;
@@ -1,4 +1,4 @@
1
- import { logger, errors, AuthorizationError, ErrorCode } from '@powersync/lib-services-framework';
1
+ import { AuthorizationError, ErrorCode, logger } from '@powersync/lib-services-framework';
2
2
  import * as jose from 'jose';
3
3
  import secs from '../util/secs.js';
4
4
  import { JwtPayload } from './JwtPayload.js';
@@ -169,7 +169,7 @@ export class KeyStore<Collector extends KeyCollector = KeyCollector> {
169
169
  ErrorCode.PSYNC_S2101,
170
170
  'Could not find an appropriate key in the keystore. The key is missing or no key matched the token KID',
171
171
  {
172
- configurationDetails: `Known kid values: ${keys.map((key) => key.kid ?? '*').join(', ')}`
172
+ configurationDetails: `Known keys: ${keys.map((key) => key.description).join(', ')}`
173
173
  // tokenDetails automatically populated later
174
174
  }
175
175
  );
@@ -49,9 +49,10 @@ export class RemoteJWKSCollector implements KeyCollector {
49
49
 
50
50
  private async getJwksData(): Promise<any> {
51
51
  const abortController = new AbortController();
52
+ const REQUEST_TIMEOUT_SECONDS = 30;
52
53
  const timeout = setTimeout(() => {
53
54
  abortController.abort();
54
- }, 30_000);
55
+ }, REQUEST_TIMEOUT_SECONDS * 1000);
55
56
 
56
57
  try {
57
58
  const res = await fetch(this.url, {
@@ -71,11 +72,14 @@ export class RemoteJWKSCollector implements KeyCollector {
71
72
 
72
73
  return (await res.json()) as any;
73
74
  } catch (e) {
75
+ if (e instanceof Error && e.name === 'AbortError') {
76
+ e = { message: `Request timed out in ${REQUEST_TIMEOUT_SECONDS}s`, name: 'AbortError' };
77
+ }
74
78
  throw new AuthorizationError(ErrorCode.PSYNC_S2204, `JWKS request failed`, {
75
79
  configurationDetails: `JWKS URL: ${this.url}`,
76
80
  // This covers most cases of FetchError
77
81
  // `cause: e` could lose the error message
78
- cause: { message: e.message, code: e.code }
82
+ cause: { message: e.message, code: e.code, name: e.name }
79
83
  });
80
84
  } finally {
81
85
  clearTimeout(timeout);
@@ -1,10 +1,7 @@
1
- import * as jose from 'jose';
2
-
1
+ import { AuthorizationError, AuthorizationResponse, ErrorCode } from '@powersync/lib-services-framework';
3
2
  import * as auth from '../auth/auth-index.js';
4
3
  import { ServiceContext } from '../system/ServiceContext.js';
5
- import * as util from '../util/util-index.js';
6
4
  import { BasicRouterRequest, Context, RequestEndpointHandlerPayload } from './router.js';
7
- import { AuthorizationError, AuthorizationResponse, ErrorCode, ServiceError } from '@powersync/lib-services-framework';
8
5
 
9
6
  export function endpoint(req: BasicRouterRequest) {
10
7
  const protocol = req.headers['x-forwarded-proto'] ?? req.protocol;
@@ -12,81 +9,6 @@ export function endpoint(req: BasicRouterRequest) {
12
9
  return `${protocol}://${host}`;
13
10
  }
14
11
 
15
- function devAudience(req: BasicRouterRequest): string {
16
- return `${endpoint(req)}/dev`;
17
- }
18
-
19
- /**
20
- * @deprecated
21
- *
22
- * Will be replaced by temporary tokens issued by PowerSync Management service.
23
- */
24
- export async function issueDevToken(req: BasicRouterRequest, user_id: string, config: util.ResolvedPowerSyncConfig) {
25
- const iss = devAudience(req);
26
- const aud = devAudience(req);
27
-
28
- const key = config.dev.dev_key;
29
- if (key == null) {
30
- throw new Error('Auth disabled');
31
- }
32
-
33
- return await new jose.SignJWT({})
34
- .setProtectedHeader({ alg: key.source.alg!, kid: key.kid })
35
- .setSubject(user_id)
36
- .setIssuedAt()
37
- .setIssuer(iss)
38
- .setAudience(aud)
39
- .setExpirationTime('30d')
40
- .sign(key.key);
41
- }
42
-
43
- /** @deprecated */
44
- export async function issueLegacyDevToken(
45
- req: BasicRouterRequest,
46
- user_id: string,
47
- config: util.ResolvedPowerSyncConfig
48
- ) {
49
- const iss = devAudience(req);
50
- const aud = config.jwt_audiences[0];
51
-
52
- const key = config.dev.dev_key;
53
- if (key == null || aud == null) {
54
- throw new Error('Auth disabled');
55
- }
56
-
57
- return await new jose.SignJWT({})
58
- .setProtectedHeader({ alg: key.source.alg!, kid: key.kid })
59
- .setSubject(user_id)
60
- .setIssuedAt()
61
- .setIssuer(iss)
62
- .setAudience(aud)
63
- .setExpirationTime('60m')
64
- .sign(key.key);
65
- }
66
-
67
- export async function issuePowerSyncToken(
68
- req: BasicRouterRequest,
69
- user_id: string,
70
- config: util.ResolvedPowerSyncConfig
71
- ) {
72
- const iss = devAudience(req);
73
- const aud = config.jwt_audiences[0];
74
- const key = config.dev.dev_key;
75
- if (key == null || aud == null) {
76
- throw new Error('Auth disabled');
77
- }
78
-
79
- const jwt = await new jose.SignJWT({})
80
- .setProtectedHeader({ alg: key.source.alg!, kid: key.kid })
81
- .setSubject(user_id)
82
- .setIssuedAt()
83
- .setIssuer(iss)
84
- .setAudience(aud)
85
- .setExpirationTime('5m')
86
- .sign(key.key);
87
- return jwt;
88
- }
89
-
90
12
  export function getTokenFromHeader(authHeader: string = ''): string | null {
91
13
  const tokenMatch = /^(Token|Bearer) (\S+)$/.exec(authHeader);
92
14
  if (!tokenMatch) {
@@ -146,51 +68,6 @@ export async function generateContext(serviceContext: ServiceContext, token: str
146
68
  }
147
69
  }
148
70
 
149
- /**
150
- * @deprecated
151
- */
152
- export const authDevUser = async (payload: RequestEndpointHandlerPayload) => {
153
- const {
154
- context: {
155
- service_context: { configuration }
156
- }
157
- } = payload;
158
-
159
- const token = getTokenFromHeader(payload.request.headers.authorization as string);
160
- if (!configuration.dev.demo_auth) {
161
- return {
162
- authorized: false,
163
- errors: ['Authentication disabled']
164
- };
165
- }
166
- if (token == null) {
167
- return {
168
- authorized: false,
169
- errors: ['Authentication required']
170
- };
171
- }
172
-
173
- // Different from the configured audience.
174
- // Should also not be changed by keys
175
- const audience = [devAudience(payload.request)];
176
-
177
- let tokenPayload: auth.JwtPayload;
178
- try {
179
- tokenPayload = await configuration.dev_client_keystore.verifyJwt(token, {
180
- defaultAudiences: audience,
181
- maxAge: '31d'
182
- });
183
- } catch (err) {
184
- return {
185
- authorized: false,
186
- errors: [err.message]
187
- };
188
- }
189
-
190
- payload.context.user_id = tokenPayload.sub;
191
- return { authorized: true };
192
- };
193
-
194
71
  export const authApi = (payload: RequestEndpointHandlerPayload) => {
195
72
  const {
196
73
  context: {
@@ -38,7 +38,9 @@ export function configureRSocket(router: ReactiveSocketRouter<Context>, options:
38
38
  const extracted_token = getTokenFromHeader(token);
39
39
  if (extracted_token != null) {
40
40
  const { context, tokenError } = await generateContext(options.service_context, extracted_token);
41
- if (context?.token_payload == null) {
41
+ if (tokenError != null) {
42
+ throw tokenError;
43
+ } else if (context?.token_payload == null) {
42
44
  throw new errors.AuthorizationError(ErrorCode.PSYNC_S2106, 'Authentication required');
43
45
  }
44
46
 
@@ -46,7 +48,6 @@ export function configureRSocket(router: ReactiveSocketRouter<Context>, options:
46
48
  token,
47
49
  user_agent,
48
50
  ...context,
49
- token_error: tokenError,
50
51
  service_context: service_context as RouterServiceContext,
51
52
  logger: connectionLogger
52
53
  };
@@ -17,7 +17,6 @@ export type Context = {
17
17
  service_context: RouterServiceContext;
18
18
 
19
19
  token_payload?: JwtPayload;
20
- token_error?: ServiceError;
21
20
  /**
22
21
  * Only on websocket endpoints.
23
22
  */
@@ -42,8 +42,6 @@ export type ConfigCollectorListener = {
42
42
  configCollected?: (event: ConfigCollectedEvent) => Promise<void>;
43
43
  };
44
44
 
45
- const POWERSYNC_DEV_KID = 'powersync-dev';
46
-
47
45
  const DEFAULT_COLLECTOR_OPTIONS: CompoundConfigCollectorOptions = {
48
46
  configCollectors: [new Base64ConfigCollector(), new FileSystemConfigCollector(), new FallbackConfigCollector()],
49
47
  syncRulesCollectors: [
@@ -117,13 +115,6 @@ export class CompoundConfigCollector {
117
115
  collectors.add(new auth.CachedKeyCollector(new auth.RemoteJWKSCollector(uri, { lookupOptions: jwksLookup })));
118
116
  }
119
117
 
120
- const baseDevKey = (baseConfig.client_auth?.jwks?.keys ?? []).find((key) => key.kid == POWERSYNC_DEV_KID);
121
-
122
- let devKey: auth.KeySpec | undefined;
123
- if (baseConfig.dev?.demo_auth && baseDevKey != null && baseDevKey.kty == 'oct') {
124
- devKey = await auth.KeySpec.importKey(baseDevKey);
125
- }
126
-
127
118
  const sync_rules = await this.collectSyncRules(baseConfig, runnerConfig);
128
119
 
129
120
  let jwt_audiences: string[] = baseConfig.client_auth?.audience ?? [];
@@ -138,14 +129,7 @@ export class CompoundConfigCollector {
138
129
  }
139
130
  },
140
131
  client_keystore: keyStore,
141
- // Dev tokens only use the static keys, no external key sources
142
- // We may restrict this even further to only the powersync-dev key.
143
- dev_client_keystore: new auth.KeyStore(staticCollector),
144
132
  api_tokens: baseConfig.api?.tokens ?? [],
145
- dev: {
146
- demo_auth: baseConfig.dev?.demo_auth ?? false,
147
- dev_key: devKey
148
- },
149
133
  port: baseConfig.port ?? 8080,
150
134
  sync_rules,
151
135
  jwt_audiences,
@@ -32,18 +32,7 @@ export type ResolvedPowerSyncConfig = {
32
32
  base_config: configFile.PowerSyncConfig;
33
33
  connections?: configFile.GenericDataSourceConfig[];
34
34
  storage: configFile.GenericStorageConfig;
35
- dev: {
36
- demo_auth: boolean;
37
- /**
38
- * Only present when demo_auth == true
39
- */
40
- dev_key?: KeySpec;
41
- };
42
35
  client_keystore: KeyStore<CompoundKeyCollector>;
43
- /**
44
- * Keystore for development tokens.
45
- */
46
- dev_client_keystore: KeyStore;
47
36
  port: number;
48
37
  sync_rules: SyncRulesConfig;
49
38
  api_tokens: string[];
@@ -6,6 +6,7 @@ export * from './protocol-types.js';
6
6
  export * from './secs.js';
7
7
  export * from './utils.js';
8
8
  export * from './checkpointing.js';
9
+ export * from './version.js';
9
10
 
10
11
  export * from './config.js';
11
12
  export * from './config/compound-config-collector.js';
@@ -0,0 +1,3 @@
1
+ import pkg from '../../package.json' with { type: 'json' };
2
+
3
+ export const POWERSYNC_VERSION = pkg.version;