Docs

Keypair Authentication

Engine supports keypair authentication allowing your app to generate short-lived access tokens.

Use cases

  • You don't want an access token to be reused for a long duration if shared or compromised.
  • You want to restrict what each access token can do (e.g. specific calls).

Usage

1. Generate a keypair

Generate a cryptographic keypair in your terminal with openssl.

These commands generate a private and public key with the ES256 algorithm.

openssl ecparam -name prime256v1 -genkey -noout -out private.key
openssl ec -in private.key -pubout -out public.key

2. Add your public key to Engine

  • Navigate to the Engine dashboard.
  • Select Access Tokens.
  • Select Keypair Authentication.
    • Note: If this option is unavailable, your Engine instance may not be configured to support keypairs.
  • Select Add Public Key.
  • Add your public key including the ----- boundary lines.

3. Sign a JWT with your private key

This step must be done for each request.

import jsonwebtoken from "jsonwebtoken";
const payload = {
iss: publicKey,
};
const accessToken = jsonwebtoken.sign(payload, privateKey, {
algorithm: "ES256", // The keypair algorithm
expiresIn: "15s", // Invalidate after 15 seconds
});

4. Authenticate Engine requests

Provide the access token in the Authorization header.

await fetch(`${engineBaseUrl}/backend-wallet/get-all`, {
headers: {
authorization: `Bearer ${accessToken}`,
},
});

FAQ

How do I enable this feature on my self-hosted Engine?

Set the environment variable ENABLE_KEYPAIR_AUTH="true" and restart your Engine.

Which cryptographic algorithms are supported?

The following algorithms can be used.

algorithmDescription
RS256RSASSA-PKCS1-v1_5 using SHA-256 hash algorithm
PS256RSASSA-PSS using SHA-256 hash algorithm
ES256ECDSA using P-256 curve and SHA-256 hash algorithm

Refer to the OpenSSL documentation on generating keypairs for different algorithms.

Remember to change algorithm in the jsonwebtoken.sign() call on Step 3. Sign a JWT with your private key.