1- import { logger } from '@powersync/lib-services-framework' ;
1+ import { logger , errors , AuthorizationError2 , ErrorCode } from '@powersync/lib-services-framework' ;
22import * as jose from 'jose' ;
33import secs from '../util/secs.js' ;
44import { JwtPayload } from './JwtPayload.js' ;
@@ -69,7 +69,11 @@ export class KeyStore<Collector extends KeyCollector = KeyCollector> {
6969 return audiences . includes ( a ) ;
7070 } )
7171 ) {
72- throw new jose . errors . JWTClaimValidationFailed ( 'unexpected "aud" claim value' , 'aud' , 'check_failed' ) ;
72+ throw new AuthorizationError2 (
73+ ErrorCode . PSYNC_S2105 ,
74+ `Unexpected "aud" claim value: ${ JSON . stringify ( tokenPayload . aud ) } ` ,
75+ { sensitiveDetails : `Current configuration allows these audience values: ${ JSON . stringify ( audiences ) } ` }
76+ ) ;
7377 }
7478
7579 const tokenDuration = tokenPayload . exp ! - tokenPayload . iat ! ;
@@ -78,12 +82,15 @@ export class KeyStore<Collector extends KeyCollector = KeyCollector> {
7882 // is too far into the future.
7983 const maxAge = keyOptions . maxLifetimeSeconds ?? secs ( options . maxAge ) ;
8084 if ( tokenDuration > maxAge ) {
81- throw new jose . errors . JWTInvalid ( `Token must expire in a maximum of ${ maxAge } seconds, got ${ tokenDuration } ` ) ;
85+ throw new AuthorizationError2 (
86+ ErrorCode . PSYNC_S2104 ,
87+ `Token must expire in a maximum of ${ maxAge } seconds, got ${ tokenDuration } s`
88+ ) ;
8289 }
8390
8491 const parameters = tokenPayload . parameters ;
8592 if ( parameters != null && ( Array . isArray ( parameters ) || typeof parameters != 'object' ) ) {
86- throw new jose . errors . JWTInvalid ( ' parameters must be an object' ) ;
93+ throw new AuthorizationError2 ( ErrorCode . PSYNC_S2101 , `Payload parameters must be an object` ) ;
8794 }
8895
8996 return tokenPayload as JwtPayload ;
@@ -112,7 +119,9 @@ export class KeyStore<Collector extends KeyCollector = KeyCollector> {
112119 for ( let key of keys ) {
113120 if ( key . kid == kid ) {
114121 if ( ! key . matchesAlgorithm ( header . alg ) ) {
115- throw new jose . errors . JOSEAlgNotAllowed ( `Unexpected token algorithm ${ header . alg } ` ) ;
122+ throw new AuthorizationError2 ( ErrorCode . PSYNC_S2101 , `Unexpected token algorithm ${ header . alg } ` , {
123+ sensitiveDetails : `Key kid: ${ key . source . kid } , alg: ${ key . source . alg } , kty: ${ key . source . kty } `
124+ } ) ;
116125 }
117126 return key ;
118127 }
@@ -145,8 +154,12 @@ export class KeyStore<Collector extends KeyCollector = KeyCollector> {
145154 logger . error ( `Failed to refresh keys` , e ) ;
146155 } ) ;
147156
148- throw new jose . errors . JOSEError (
149- 'Could not find an appropriate key in the keystore. The key is missing or no key matched the token KID'
157+ throw new AuthorizationError2 (
158+ ErrorCode . PSYNC_S2101 ,
159+ 'Could not find an appropriate key in the keystore. The key is missing or no key matched the token KID' ,
160+ {
161+ sensitiveDetails : `Token kid: ${ kid } , token algorithm: ${ header . alg } , known kid values: ${ keys . map ( ( key ) => key . kid ?? '*' ) . join ( ', ' ) } `
162+ }
150163 ) ;
151164 }
152165 }
0 commit comments