For a React, Vue or serverless JavaScript application, register a public client: no secret, the browser’s crypto API (crypto.subtle) computes the PKCE challenge. The /oauth/token, /oauth/userinfo and /oauth/revoke endpoints accept calls from any origin (CORS).
Keep tokens in memory rather than in localStorage.
auth-spa.js
const b64url = buf => btoa(String.fromCharCode(...new Uint8Array(buf)))
.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
async function loginWithOrneos(clientId, redirectUri) {
const verifier = b64url(crypto.getRandomValues(new Uint8Array(32)));
const challenge = b64url(await crypto.subtle.digest('SHA-256', new TextEncoder().encode(verifier)));
const state = b64url(crypto.getRandomValues(new Uint8Array(16)));
sessionStorage.setItem('orneos_pkce_verifier', verifier);
sessionStorage.setItem('orneos_oauth_state', state);
window.location.assign('https://login.orneos.eu/oauth/authorize?' + new URLSearchParams({
client_id: clientId,
redirect_uri: redirectUri,
response_type: 'code',
scope: 'openid profile email',
state,
code_challenge: challenge,
code_challenge_method: 'S256',
}));
}
async function handleCallback(clientId, redirectUri) {
const params = new URLSearchParams(window.location.search);
const code = params.get('code');
const verifier = sessionStorage.getItem('orneos_pkce_verifier');
if (!code || !verifier || params.get('state') !== sessionStorage.getItem('orneos_oauth_state')) {
throw new Error('Invalid state');
}
sessionStorage.removeItem('orneos_pkce_verifier');
sessionStorage.removeItem('orneos_oauth_state');
// Public client: client_id in the body, no secret
const tokens = await (await fetch('https://orneos.eu/oauth/token', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: new URLSearchParams({
grant_type: 'authorization_code',
client_id: clientId,
code,
redirect_uri: redirectUri,
code_verifier: verifier,
}),
})).json();
const user = await (await fetch('https://orneos.eu/oauth/userinfo', {
headers: { Authorization: `Bearer ${tokens.access_token}` },
})).json();
return { tokens, user };
}
The whole flow from a terminal. Register http://localhost:8080/callback in the console: after consent, the browser shows a connection error, which is expected; copy the code from the address bar and exchange it within 60 seconds.
Terminal (bash)
# 1. Random code_verifier
VERIFIER=$(openssl rand -base64 32 | tr -d '=\n' | tr '+/' '-_')
# 2. S256 code_challenge
CHALLENGE=$(printf '%s' "$VERIFIER" | openssl dgst -sha256 -binary | openssl base64 -A | tr -d '=' | tr '+/' '-_')
# 3. Open this address in your browser
echo "https://login.orneos.eu/oauth/authorize?client_id=YOUR_CLIENT_ID&redirect_uri=http://localhost:8080/callback&response_type=code&scope=openid%20profile%20email&state=123&code_challenge=$CHALLENGE&code_challenge_method=S256"
# 4. Exchange the received code (public client: replace -u with -d "client_id=YOUR_CLIENT_ID")
curl -X POST https://orneos.eu/oauth/token \
-u "YOUR_CLIENT_ID:YOUR_SECRET" \
-d "grant_type=authorization_code" \
-d "code=RECEIVED_CODE" \
-d "redirect_uri=http://localhost:8080/callback" \
-d "code_verifier=$VERIFIER"