veko doc
Self-Contained API Reference • Gradual Types • Capabilities • Wasm Sandbox

auth/oauth2

lib/auth/oauth2.vk

lib/auth/oauth2.vk Pure-Veko OAuth 2.0 + RFC 7636 PKCE (S256) helpers. Generates cryptographic PKCE verifier/challenge pairs, CSRF state tokens, RFC 3986 authorization URLs, and form-encoded token exchange payloads.

PkcePair struct
signature
PkcePair = struct { verifier str, challenge str, method str }

PKCE verifier and S256 challenge pair (RFC 7636).

signature
OAuthConfig = struct { client_id str, auth_url str, token_url str, redirect_uri str, scope str = ... }

OAuth 2.0 client configuration.

signature
pkce-challenge = fn [verifier str] str

Computes the RFC 7636 S256 code challenge (BASE64URL(SHA256(verifier))).

Executable Example
veko
use [oa = auth/oauth2]
ch = oa/pkce-challenge 'dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk'
if ch != 'E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM' [ err 'rfc7636 vector' ] else [ .ok ]
signature
generate-pkce = fn [] PkcePair

Generates a fresh cryptographic PKCE S256 verifier/challenge pair using CSPRNG entropy.

Executable Example
veko
use [oa = auth/oauth2]
p = oa/generate-pkce
if not (oa/verify-pkce p.verifier p.challenge) [ err 'pkce verify' ] else [ .ok ]
signature
verify-pkce = fn [verifier str, challenge str] bool

Verifies a PKCE verifier against an S256 challenge in constant time.

signature
generate-state = fn [] str

Generates a 24-byte random base64url CSRF state token.

signature
authorize-url = fn [cfg OAuthConfig, state str, challenge str] str

Builds an OAuth 2.0 authorization URL with PKCE S256 parameters.

Executable Example
veko
use [oa = auth/oauth2]
cfg = oa/OAuthConfig { client_id = 'veko-cli', auth_url = 'https://idp.example/authorize', token_url = 'https://idp.example/token', redirect_uri = 'http://127.0.0.1:8787/cb', scope = 'openid profile' }
u = oa/authorize-url cfg 'st-1' 'ch-1'
if not (str/contains? u 'code_challenge=ch-1') or not (str/contains? u 'response_type=code') [ err 'auth url' ] else [ .ok ]
signature
token-exchange-body = fn [cfg OAuthConfig, code str, verifier str] str

Encodes an application/x-www-form-urlencoded body for the authorization-code + PKCE token exchange.

signature
refresh-token-body = fn [cfg OAuthConfig, refresh_token str] str

Encodes an application/x-www-form-urlencoded body for a refresh-token grant.

signature
bearer-header = fn [access_token str] str

Formats an Authorization header value ('Bearer <token>').