Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:ALwrity ALwrity OAuth Authorization Flow

From Leeroopedia
Knowledge Sources
Domains OAuth, Security, Integration
Last Updated 2026-10-24 00:00 GMT

Overview

Mechanism that obtains user authorisation to access a third-party platform by redirecting the user to the provider's consent screen with a state-validated URL, then exchanging the returned authorization code for encrypted access and refresh tokens.

Description

The OAuth 2.0 authorization code flow is the standard pattern for obtaining delegated access to a user's account on a third-party platform (Bing, WordPress, LinkedIn, Wix). It has two stages: (1) authorization URL generation — the server creates a URL pointing to the provider's consent screen, embedding a state parameter that binds the request to the user (typically {user_id}:{random_token}) and storing this state in the per-user database with an expiry for later validation; (2) callback processing — after the user consents, the provider redirects back with an authorization code and the state; the server validates the state against the stored value, extracts the user_id, exchanges the code for access/refresh tokens at the provider's token endpoint, and stores the tokens encrypted in the per-user database.

The state parameter is critical for security: it prevents CSRF attacks by ensuring the callback corresponds to a request the server actually initiated.

Usage

Use this principle when:

  • A platform needs delegated access to a user's third-party account.
  • The authorization code flow (server-side) is appropriate (not client-side implicit).
  • A state token must bind the callback to the initiating user.
  • Tokens must be encrypted at rest after exchange.

Theoretical Basis

The flow is a two-message exchange:

# Abstract OAuth authorization code flow (NOT a real implementation)
def generate_auth_url(user_id, scope):
    state = f"{user_id}:{secrets.token_urlsafe(32)}"
    db.store(state, user_id, expires_in=1200)        # 20-min expiry
    url = f"{provider}/authorize?response_type=code&client_id={cid}&redirect_uri={uri}&scope={scope}&state={state}"
    return {"auth_url": url, "state": state}

def handle_callback(code, state):
    user_id = state.split(":")[0]
    if not db.validate(state, user_id):              # state must match stored
        return None
    tokens = http_post(provider_token_endpoint, code=code, client_id=cid, client_secret=secret)
    db.store_encrypted(user_id, encrypt(tokens.access_token), encrypt(tokens.refresh_token))
    return tokens

Practical Guide

  • State validation.' Always validate the state against a stored value with an expiry; never accept a callback without a matching state.
  • User ID in state.' Embed the user_id in the state so the callback can resolve the user without a session.
  • Encrypt tokens.' Store access and refresh tokens encrypted (Fernet) in the per-user database, never in plaintext.
  • Token expiry tracking.' Store the token expiry so the monitoring service can detect refresh needs.

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment