Custom
Any authentication provider can be supported by generating custom JWTs for PowerSync.
For a quick way to get started before implementing custom auth, Development Tokens can be used instead.
The process is as follows:
- The client authenticates the user using the app’s authentication provider and typically gets a session token — either a third-party authentication provider or a custom one.
- The client makes a backend call (authenticated using the above session token), which generates and signs a JWT for PowerSync.
- For example implementations of this backend endpoint, see Custom Backend Examples
- The client connects to the PowerSync Service using the above JWT.
- PowerSync verifies the JWT.
The requirements are:
- An RSA key-pair (private + public key) is required to sign and verify JWTs.
- The JWT must be signed using the RS256, RS384 or RS512 algorithm with the private key.
- The public key must be available on a public URL in JWKS format.
- We have an example endpoint available here; ensure that your response looks similar.
- The
aud
of the JWT must match the PowerSync instance URL.- To get the instance URL of a PowerSync instance when using PowerSync Cloud: In the project tree on the PowerSync dashboard, click on the “Copy instance URL” icon.
- Alternatively, specify a custom audience in the instance settings.
- The JWT must have a
kid
matching one of the keys in the JWKS URL. - The JWT must expire in 60 minutes or less. Specifically, both
iat
andexp
fields must be present, with a difference of 3600 or less between the two. - The user ID must be used as the
sub
of the JWT. - Additional fields can be added which can be referenced in Sync Rules parameter queries.
Refer to this example for creating and verifying JWTs for PowerSync authentication.
Since there is no way to revoke a JWT once issued without rotating the key, we recommend using short expiration periods (e.g. 5 minutes). JWTs older than 60 minutes are not accepted by PowerSync.
Rotating Keys
If a private key is compromised, rotate they key on the JWKS endpoint.
PowerSync refreshes the keys from the endpoint every couple of minutes, after which old tokens will not be accepted anymore.
There is a possibility of false authentication errors until PowerSync refreshes the keys. These errors are typically retried by the client and will have little impact. However, to periodically rotate keys without any authentication failures, follow this process:
- Add a new key to the JWKS endpoint.
- Wait an hour (or more) to make sure PowerSync has the new key.
- Start signing new JWT tokens using the new key.
- Wait until all existing tokens have expired.
- Remove the old key from the JWKS endpoint.