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)- 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. - 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 anid_tokenlocally, then transmits it toPOST /auth/{provider}/mobile. Kirak cryptographically verifies the token against the provider’s public keys.
Enabling a Provider
Section titled “Enabling a Provider”1. Declare Provider in kirak.json
Section titled “1. Declare Provider in kirak.json”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-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-oauth2 |
OAuth 2.0 | |
| Slack | slack |
OAuth 2.0 |
| Twitter / X | twitter-oauth2 |
OAuth 2.0 with PKCE |
2. Configure Secrets in .env
Section titled “2. Configure Secrets in .env”Client secrets are always stored in the environment as SOCIAL_AUTH_<NAME_UPPER>_SECRET:
# GoogleSOCIAL_AUTH_GOOGLE_OAUTH2_SECRET=your-google-client-secret
# GitHubSOCIAL_AUTH_GITHUB_SECRET=your-github-client-secret
# DiscordSOCIAL_AUTH_DISCORD_SECRET=your-discord-client-secretNote on Client IDs: Client IDs must be declared in
kirak.jsonunderauth.social.<provider>.client_id. Kirak does not read client IDs from the environment.
3. Register Redirect URIs
Section titled “3. Register Redirect URIs”Kirak auto-derives the callback URL from base_url in kirak.json:
{base_url}/auth/{provider-name}/callbackExample for Google on local development:
http://localhost:8000/auth/google-oauth2/callbackRegister this exact URI in your provider’s developer console (Google Cloud Console, GitHub Developer Settings, etc.).
Flow A: Web Browser Redirect Flow
Section titled “Flow A: Web Browser Redirect Flow”Step 1: Initiate Sign-In
Section titled “Step 1: Initiate Sign-In”Direct the user’s browser to:
GET /auth/{provider}/login?platform=web&flow=loginOptional Query Parameters:
Section titled “Optional Query Parameters:”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.
Step 2: Handle Provider Callback
Section titled “Step 2: Handle Provider Callback”The provider redirects the user back to Kirak:
GET /auth/{provider}/callback?code=AUTH_CODE&state=STATEKirak 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_totarget 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:
- Whitelist the target URI in
kirak.json:"auth": {"redirect_urls": ["https://app.example.com/callback", "myapp://auth/callback"]} - Initiate with PKCE:
GET /auth/github/login?redirect_to=https://app.example.com/callback&code_challenge=XYZ...&code_challenge_method=S256
- Receive One-Time Code: The browser redirects to
https://app.example.com/callback?code=123.... - Exchange for JWT Tokens:
Terminal window POST /auth/token/exchangeContent-Type: application/json{"code": "123...","code_verifier": "original-secret-verifier","redirect_to": "https://app.example.com/callback"}
Flow B: Native Mobile Social Auth
Section titled “Flow B: Native Mobile Social Auth”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"}Mobile Security Verification:
Section titled “Mobile Security Verification:”- Audience Verification: Kirak ensures the token was minted specifically for your app (checks
client_idand any values listed inauth.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
nonceis supplied, Kirak verifies thatSHA256(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.
Social Account Linking & Email Safety
Section titled “Social Account Linking & Email Safety”- Automatic Linking to a Verified Account:
If a user registered earlier via email/password as
alice@example.com, and later logs in using Google withalice@example.com, Kirak automatically links the Google identity to Alice’s account, provided Alice’s account has a verified email address. - 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. Setrequire_verified_emailin the provider block to also refuse a first sign-in whose email the provider does not mark as verified (403 EMAIL_NOT_VERIFIED). - 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": trueinkirak.json:"social": {"reddit": { "client_id": "...", "allow_no_email": true }} - New Accounts Are Sign-Ups:
A social sign-in that matches no user creates one. With
auth.registration"closed"it answers403 REGISTRATION_CLOSEDinstead, and thebefore_register/after_registerhooks run withmethod"social"(see Who Can Sign Up). Linking to an existing account is not a sign-up.
Sign in with Apple
Section titled “Sign in with Apple”Apple uses a unique flow with strict requirements:
- 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). - Credentials: Apple requires your
team_idandkey_idinkirak.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-----" - 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
userstable.
Verifying Social Credentials via CLI
Section titled “Verifying Social Credentials via CLI”You can verify that your OAuth credentials and redirect URIs are correct without having to perform a manual login:
kirak providers auth socialTo 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"