@powersync/service-core 1.13.3 → 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.
@@ -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: {
@@ -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;