Skip to content

Social Authentication

Kirak provides enterprise-ready social login and OAuth integration with 200+ identity providers (Google, GitHub, Apple, Microsoft, Discord, Slack, etc.) built on social-auth-core.

Enabling a provider requires zero custom authentication code: simply declare the provider in kirak.json and provide the credentials in .env.


Two Integration Architectures: Web vs. Mobile

Section titled “Two Integration Architectures: Web vs. Mobile”

Kirak supports two distinct social authentication paradigms:

sequenceDiagram
autonumber
box rgba(75, 150, 255, 0.1) Flow A: Web / SPA Browser Flow (Redirect + PKCE)
participant WebApp as Web / SPA App
participant KirakAPI as Kirak Server
participant Provider as Identity Provider (e.g. Google)
end
WebApp->>KirakAPI: GET /auth/{provider}/login?redirect_to=app://callback&code_challenge=...
KirakAPI-->>WebApp: 302 Redirect to Provider Auth URL
WebApp->>Provider: User authenticates & grants consent
Provider-->>KirakAPI: GET /auth/{provider}/callback?code=AUTH_CODE&state=...
KirakAPI->>Provider: Exchange AUTH_CODE for Provider Tokens & User Profile
Provider-->>KirakAPI: Verified User Profile (email, name, sub)
KirakAPI-->>WebApp: 303 Redirect to app://callback?code=ONE_TIME_CODE
WebApp->>KirakAPI: POST /auth/token/exchange (code + code_verifier)
KirakAPI-->>WebApp: 200 OK (accessToken, refreshToken, user)
box rgba(120, 220, 120, 0.1) Flow B: Native Mobile SDK Flow (Direct Token)
participant MobileApp as Native Mobile App (iOS / Android)
participant NativeSDK as Native Provider SDK (e.g. Google Sign-In)
participant KirakMobile as Kirak Server
end
MobileApp->>NativeSDK: Request Sign-In (Nonce generated)
NativeSDK-->>MobileApp: Returns Provider id_token
MobileApp->>KirakMobile: POST /auth/{provider}/mobile {id_token, nonce}
KirakMobile->>KirakMobile: Cryptographically verify signature, audience & nonce
KirakMobile->>KirakMobile: Find, link, or create user account
KirakMobile-->>MobileApp: 200 OK (accessToken, refreshToken, user)
  1. Web Browser Redirect Flow (Flow A): The browser initiates OAuth by visiting Kirak’s redirect URL, authenticates at the provider, and completes the handshake via PKCE. Recommended for web frontends, SPAs, and universal links.
  2. Native Mobile SDK Flow (Flow B): The mobile app uses official native SDKs (e.g., Google Sign-In or Apple Sign-In on iOS/Android) to obtain an id_token locally, then transmits it to POST /auth/{provider}/mobile. Kirak cryptographically verifies the token against the provider’s public keys.

Add the provider’s canonical backend name to auth.social_providers and configure its client ID:

{
"auth": {
"social_providers": ["google-oauth2", "github", "apple-id", "discord"],
"social": {
"google-oauth2": {
"client_id": "your-google-client-id.apps.googleusercontent.com"
},
"github": {
"client_id": "your-github-client-id"
}
}
}
}

Common canonical provider names:

Provider Canonical Name Protocol / Backend
Google google-oauth2 OAuth 2.0 / OpenID Connect
GitHub github OAuth 2.0
Apple apple-id Sign in with Apple (JWT / Form POST)
Microsoft microsoft-graph OAuth 2.0
Discord discord OAuth 2.0
LinkedIn linkedin-oauth2 OAuth 2.0
Slack slack OAuth 2.0
Twitter / X twitter-oauth2 OAuth 2.0 with PKCE

Client secrets are always stored in the environment as SOCIAL_AUTH_<NAME_UPPER>_SECRET:

# Google
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET=your-google-client-secret
# GitHub
SOCIAL_AUTH_GITHUB_SECRET=your-github-client-secret
# Discord
SOCIAL_AUTH_DISCORD_SECRET=your-discord-client-secret

Note on Client IDs: Client IDs must be declared in kirak.json under auth.social.<provider>.client_id. Kirak does not read client IDs from the environment.

Kirak auto-derives the callback URL from base_url in kirak.json:

{base_url}/auth/{provider-name}/callback

Example for Google on local development:

http://localhost:8000/auth/google-oauth2/callback

Register this exact URI in your provider’s developer console (Google Cloud Console, GitHub Developer Settings, etc.).


Direct the user’s browser to:

GET /auth/{provider}/login?platform=web&flow=login
  • redirect_to: The application URL to return to with an exchange code (required for single-page and mobile apps).
  • code_challenge: The PKCE challenge (base64url(SHA256(verifier))).
  • code_challenge_method: S256 (the default).

Kirak generates an HMAC-signed state token and redirects the browser (HTTP 302) to the provider’s authorization screen.

The provider redirects the user back to Kirak:

GET /auth/{provider}/callback?code=AUTH_CODE&state=STATE

Kirak validates the state token, exchanges the authorization code for provider tokens, retrieves the user profile, finds or links the user account, and either:

  • Returns JSON containing JWT tokens (default direct response).
  • Redirects to your redirect_to target with a one-time exchange code (when PKCE was used).

Step 3: Returning to Single-Page & Mobile Apps (PKCE Exchange)

Section titled “Step 3: Returning to Single-Page & Mobile Apps (PKCE Exchange)”

To receive tokens in a client-side app:

  1. Whitelist the target URI in kirak.json:
    "auth": {
    "redirect_urls": ["https://app.example.com/callback", "myapp://auth/callback"]
    }
  2. Initiate with PKCE:
    GET /auth/github/login?redirect_to=https://app.example.com/callback&code_challenge=XYZ...&code_challenge_method=S256
  3. Receive One-Time Code: The browser redirects to https://app.example.com/callback?code=123....
  4. Exchange for JWT Tokens:
    Terminal window
    POST /auth/token/exchange
    Content-Type: application/json
    {
    "code": "123...",
    "code_verifier": "original-secret-verifier",
    "redirect_to": "https://app.example.com/callback"
    }

For iOS and Android applications using native SDKs (e.g., Google Sign-In SDK, Apple Authentication Services):

POST /auth/{provider}/mobile

The native app obtains credentials locally and sends them directly to Kirak:

1. Sending an OpenID Connect id_token (Google, Apple, Auth0, Okta):

Section titled “1. Sending an OpenID Connect id_token (Google, Apple, Auth0, Okta):”
{
"id_token": "eyJhbGciOi...",
"nonce": "optional-raw-nonce",
"first_name": "Alice",
"last_name": "Smith"
}

2. Sending an OAuth 2.0 Authorization Code:

Section titled “2. Sending an OAuth 2.0 Authorization Code:”
{
"code": "auth-code-from-sdk",
"code_verifier": "pkce-verifier-if-used",
"redirect_uri": "redirect-uri-used-by-sdk"
}
  • Audience Verification: Kirak ensures the token was minted specifically for your app (checks client_id and any values listed in auth.social.<provider>.audience).
  • Signature & Expiry Check: Kirak fetches the provider’s official JSON Web Key Set (JWKS) and verifies the cryptographic signature and expiration.
  • Nonce Replay Prevention: If a nonce is supplied, Kirak verifies that SHA256(nonce) matches the token’s claim. The nonce is recorded only after the signature check passes, and a token valid for more than 24 hours is refused, since the nonce would be forgotten while the token still works.

  1. Automatic Linking to a Verified Account: If a user registered earlier via email/password as alice@example.com, and later logs in using Google with alice@example.com, Kirak automatically links the Google identity to Alice’s account, provided Alice’s account has a verified email address.
  2. Unverified Accounts Refused: If the account with the sign-in’s email has an unverified email address, Kirak refuses the link with 409 SOCIAL_EMAIL_NOT_VERIFIED, whatever the provider says about its email. This stops an attacker from claiming an account with someone else’s address through a provider that does not verify emails. The provider’s own verified flag is not enough. A sign-in whose provider does not confirm its email still links to a verified account. Set require_verified_email in the provider block to also refuse a first sign-in whose email the provider does not mark as verified (403 EMAIL_NOT_VERIFIED).
  3. Providers Without Emails (TikTok, Reddit, Instagram): Some networks do not share an email address. By default, these fail with 401 SOCIAL_EMAIL_MISSING. To allow passwordless accounts without an email, enable "allow_no_email": true in kirak.json:
    "social": {
    "reddit": { "client_id": "...", "allow_no_email": true }
    }
  4. New Accounts Are Sign-Ups: A social sign-in that matches no user creates one. With auth.registration "closed" it answers 403 REGISTRATION_CLOSED instead, and the before_register / after_register hooks run with method "social" (see Who Can Sign Up). Linking to an existing account is not a sign-up.

Apple uses a unique flow with strict requirements:

  1. Response Mode: Apple uses response_mode=form_post. Kirak automatically handles both GET and form POST callbacks on the same route (/auth/apple-id/callback).
  2. Credentials: Apple requires your team_id and key_id in kirak.json, and the private key (.p8) in .env:
    "auth": {
    "social": {
    "apple-id": {
    "client_id": "com.example.app.service",
    "team_id": "ABC123DEFG",
    "key_id": "KEY1234567"
    }
    }
    }
    SOCIAL_AUTH_APPLE_ID_SECRET="-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----"
  3. User Profile on First Sign-In: Apple provides the user’s name only once, during the very first authorization. Kirak captures and saves this profile data immediately to the users table.

You can verify that your OAuth credentials and redirect URIs are correct without having to perform a manual login:

Terminal window
kirak providers auth social

To run an automated test against the provider’s API:

from kirak.auth.social import check_social_backend
result = await check_social_backend(
"google-oauth2",
{"client_id": "...", "redirect_uri": "https://api.example.com/auth/google-oauth2/callback"},
{"SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET": "..."},
)
print(result.status) # "ok", "warning", or "failed"