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:
A key-pair (private + public key) is required to sign and verify JWTs. The private key is used to sign the JWT, and the public key is advertised on a public JWKS URL.
Requirements for the key in the JWKS URL:
- The URL must be a public URL in the JWKS format.
- We have an example endpoint available here; ensure that your response looks similar.
- Supported signature schemes: RSA and EdDSA. ECDSA is not supported yet.
- Key type (
kty
):RSA
orOKP
(EdDSA). - Algorithm (
alg
):RS256
,RS384
orRS512
for RSA, orEdDSA
for EdDSA. - Curve (
crv
):Ed25519
orEd448
, only relevant for EdDSA. - A
kid
must be specified, and must match thekid
in the JWT.
Requirements for the signed JWT:
- The JWT must be signed using a key in the JWKS URL.
- JWT must have a
kid
matching the key in the JWKS URL. - 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 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.