File tree Expand file tree Collapse file tree 1 file changed +19
-0
lines changed
tools/diagnostics-app/src/library/powersync Expand file tree Collapse file tree 1 file changed +19
-0
lines changed Original file line number Diff line number Diff line change @@ -23,6 +23,7 @@ export class TokenConnector implements PowerSyncBackendConnector {
2323
2424 async signIn ( credentials : Credentials ) {
2525 validateSecureContext ( credentials . endpoint ) ;
26+ checkJWT ( credentials . token ) ;
2627 try {
2728 localStorage . setItem ( 'powersync_credentials' , JSON . stringify ( credentials ) ) ;
2829 await connect ( ) ;
@@ -56,3 +57,21 @@ function validateSecureContext(url: string) {
5657Run either the PowerSync endpoint on http://localhost, or the diagnostics app on http://localhost.` ) ;
5758 }
5859}
60+
61+ function checkJWT ( token : string ) {
62+ // Split the token into parts by "."
63+ const parts = token . split ( '.' ) ;
64+
65+ // Check that it has exactly three parts (header, payload, signature)
66+ if ( parts . length !== 3 ) {
67+ throw new Error ( `Token must be a JWT: Expected 3 parts, got ${ parts . length } ` ) ;
68+ }
69+
70+ // Check that each part is base64 or base64url encoded
71+ const base64UrlRegex = / ^ [ A - Z a - z 0 - 9 - _ ] + $ / ;
72+
73+ const isBase64 = parts . every ( ( part ) => base64UrlRegex . test ( part ) ) ;
74+ if ( ! isBase64 ) {
75+ throw new Error ( `Token must be a JWT: Not all parts are base64 encoded` ) ;
76+ }
77+ }
You can’t perform that action at this time.
0 commit comments