Authentication with OAuth 2.0 and PKCE
Login Kit is designed around the OAuth 2.0 authorization code flow with PKCE. Your app never sees a Fentra password, and people see exactly what they're agreeing to.
The flow at a glance
Fentra already signs people in to its own sites with a “Continue with Fentra” handoff that uses PKCE. Login Kit opens the same pattern to other apps, using standard OAuth 2.0 (RFC 6749) with Proof Key for Code Exchange (RFC 7636).
- Your app makes a random code verifier and keeps it.
- It sends the person to Fentra's authorization page with the verifier's hash, the code challenge.
- The person sees your app and each permission you ask for, and allows or cancels.
- Fentra redirects to your redirect URI with a one-time authorization code.
- Your server exchanges the code and the verifier for an access token.
PKCE means an intercepted code is useless without the verifier, so the same flow is safe for mobile apps and single-page apps that can't keep a secret.
1. Make a code verifier and challenge
This is standard PKCE and works in any modern browser or in Node with the Web Crypto API.
function base64url(bytes: Uint8Array): string {
return btoa(String.fromCharCode(...bytes)).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
}
export async function pkcePair() {
const verifier = base64url(crypto.getRandomValues(new Uint8Array(32)));
const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(verifier));
return { verifier, challenge: base64url(new Uint8Array(digest)) };
}Keep the verifier where only this sign-in can reach it: the server-side session, or memory in a native app.
2. Send the person to Fentra
GET https://open.fentra.com/v1/oauth/authorize
?client_id=YOUR_CLIENT_ID
&response_type=code
&scope=user.info.basic,video.list
&redirect_uri=https://app.example.com/auth/fentra
&state=af0ifjsldkj
&code_challenge=E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM
&code_challenge_method=S256| Parameter | What it is |
|---|---|
client_id | Your app's client ID, from registration. |
response_type | Always code. |
scope | The scopes you need, comma-separated. See Scopes. |
redirect_uri | One of the redirect URIs registered for your app, matched exactly. |
state | A random value you check when the person comes back, to stop cross-site request forgery. |
code_challenge | The base64url SHA-256 of your code verifier. |
code_challenge_method | Always S256. Plain challenges won't be accepted. |
3. Handle the redirect
If the person allows access, Fentra redirects to your URI with code and your state. If they cancel, it carries error=access_denied instead. Check that state matches before you do anything else.
GET https://app.example.com/auth/fentra?code=fc_8xLOxBtZp8&state=af0ifjsldkj4. Exchange the code for a token
Codes are single-use and short-lived. Exchange it from your server.
POST https://open.fentra.com/v1/oauth/token
Content-Type: application/x-www-form-urlencoded
grant_type=authorization_code&code=fc_8xLOxBtZp8&client_id=YOUR_CLIENT_ID&redirect_uri=https://app.example.com/auth/fentra&code_verifier=dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk{
"access_token": "fat_…",
"token_type": "Bearer",
"expires_in": 86400,
"refresh_token": "frt_…",
"scope": "user.info.basic,video.list",
"open_id": "fo_4c1d9a0e27b8"
}open_id identifies the person to your app. It's different for every app, so two apps can't match up the same person by it.
Refreshing and withdrawn access
- Refresh: when the access token expires, exchange the refresh token for a new pair with
grant_type=refresh_token. - Withdrawn access: people will be able to remove your app from their Fentra settings at any time. Its tokens stop working at once; treat a
401as signed out, and delete what you stored. Webhooks are designed to tell you when it happens. - Sign out: when someone signs out of your app, revoke their token so it can't be used again.
Security checklist
- Never put a client secret in a mobile app, a desktop app or a web page.
- Always use PKCE with
S256, even on a server. - Check
stateon every redirect. - Register exact redirect URIs over HTTPS. Wildcards won't be accepted.
- Store tokens encrypted, and never log them.