Browser Authentication

Form-based login/signup, OAuth integration, channel linking, email verification, password reset, session management, validation, and error...

Browser Authentication

Form-based login/signup, OAuth integration, channel linking, email verification, password reset, session management, validation, and error handling for browser-based authentication flows.

How It Works

flowchart LR
    B["Browser"]
    L["LocalAuth"]
    H["HandleUser<br/>(app-owned)<br/>set session"]

    B -->|"POST /auth/login"| L
    L -->|"cookie"| B
    L -->|"callback"| H

OneAuth handles credential validation; your application controls session creation via the HandleUser callback.

Setup

import (
    "github.com/panyam/oneauth/core"
    "github.com/panyam/oneauth/localauth"
    "github.com/panyam/oneauth/httpauth"
    oa2 "github.com/panyam/oneauth/oauth2"
    "github.com/panyam/oneauth/stores/fs"
)

storagePath := "/path/to/storage"
userStore := fs.NewFSUserStore(storagePath)
identityStore := fs.NewFSIdentityStore(storagePath)
channelStore := fs.NewFSChannelStore(storagePath)
tokenStore := fs.NewFSTokenStore(storagePath)
usernameStore := fs.NewFSUsernameStore(storagePath)

// Create OneAuth router
oneAuth := httpauth.New("myapp")
oneAuth.HandleUser = localauth.NewEnsureAuthUserFunc(localauth.EnsureAuthUserConfig{
    UserStore:     userStore,
    IdentityStore: identityStore,
    ChannelStore:  channelStore,
    UsernameStore: usernameStore,
})

// Add local auth (email/password)
la := localauth.NewLocalAuth(localauth.LocalAuthConfig{
    Session:       session,
    UserStore:     userStore,
    IdentityStore: identityStore,
    ChannelStore:  channelStore,
    TokenStore:    tokenStore,
    UsernameStore: usernameStore,
    SignupPolicy: &core.SignupPolicy{
        RequireEmail:    true,
        RequirePassword: true,
        MinPasswordLength: 8,
    },
})
oneAuth.AddAuth("/local", la)

// Add OAuth providers
oneAuth.AddAuth("/google", oa2.NewGoogleOAuth2(clientID, clientSecret, callbackURL, oneAuth.SaveUserAndRedirect).Handler())
oneAuth.AddAuth("/github", oa2.NewGithubOAuth2(clientID, clientSecret, callbackURL, oneAuth.SaveUserAndRedirect).Handler())

Request Flows

Login Flow

1. POST /auth/login (email or username, password)
2. LocalAuth.ServeHTTP
3. Auto-detect input: contains "@" → email, otherwise → username
4. If username: UsernameStore.GetUserByUsername → userID → User → email
5. Identity lookup by email
6. Channel lookup (provider=local) → password hash
7. bcrypt.CompareHashAndPassword
8. HandleUser callback → session creation
9. Redirect to callbackURL

Signup Flow

1. POST /auth/signup (email, password, optional username)
2. Validate per SignupPolicy
3. Check Identity doesn't exist for email
4. Create User with profile
5. Create Identity (type=email, verified=false)
6. Create Channel (provider=local, password_hash)
7. If UsernameStore configured + username provided: reserve username
8. Optional: send verification email
9. Auto-login → HandleUser callback → session
10. Redirect to callbackURL

Email Verification Flow

1. After signup, verification email sent (if EmailSender configured)
2. Email contains link: /auth/verify?token=abc123
3. User clicks link
4. Token validated (exists + not expired)
5. Identity.Verified = true
6. Token deleted (single-use)

Password Reset Flow

1. POST /auth/forgot-password (email)
2. Create password reset token (time-limited)
3. Email link: /auth/reset-password?token=abc123
4. User clicks link, enters new password
5. POST /auth/reset-password (token, new_password)
6. Update password hash in local Channel
   (or create local Channel if user is OAuth-only)
7. Token deleted (single-use)

Login Decision Tree

flowchart TD
    START["LOGIN ATTEMPT"]
    SUCCESS["LOGIN SUCCESS<br/>Create Session"]

    START --> OAUTH["OAuth (Google/GitHub)"]
    START --> LOCAL["Email/Username + Password"]

    OAUTH --> O1["1. Provider authenticates user"]
    O1 --> O2["2. Callback receives: email, name, avatar"]
    O2 --> O3["3. Look up Identity by email"]
    O3 -->|"NOT found"| O4["Create User + Identity + Channel"]
    O3 -->|"FOUND"| O5["Get User, add/update OAuth Channel"]
    O4 --> SUCCESS
    O5 --> SUCCESS

    LOCAL --> AT{"Contains '@'?"}

    AT -->|"YES"| E1["Look up Identity by email"]
    E1 -->|"NOT found"| E1X["'Invalid credentials'"]
    E1 -->|"FOUND"| E2["Get local Channel"]
    E2 -->|"No local Channel"| E2X["error"]
    E2 --> E3["Verify password"]
    E3 --> SUCCESS

    AT -->|"NO"| U1["UsernameStore.GetUserByUsername()"]
    U1 -->|"NOT found"| U1X["'Invalid credentials'"]
    U1 -->|"FOUND"| U2["resolve to email, continue"]
    U2 --> SUCCESS

Signup Decision Tree

flowchart TD
    S["SIGNUP ATTEMPT (email + password)"]
    S --> V["Validate SignupPolicy"]
    V --> C{"Check Identity exists for email?"}

    C -->|"EXISTS"| X1["Error: 'Email already registered'"]
    X1 --> X2["OnSignupError callback"]

    C -->|"NOT found"| N1["Create User"]
    N1 --> N2["Create Identity (verified=false)"]
    N2 --> N3["Create Channel (local, password_hash)"]
    N3 --> N4["Reserve username (if UsernameStore + username provided)"]
    N4 --> N5["Send verification email (if EmailSender configured)"]
    N5 --> N6["Auto-login → redirect"]

OAuth Integration

Adding OAuth Providers

oneAuth.AddAuth("/google", oauth2.NewGoogleOAuth2(
    clientID,
    clientSecret,
    callbackURL,
    oneAuth.SaveUserAndRedirect,
).Handler())

oneAuth.AddAuth("/github", oauth2.NewGithubOAuth2(
    clientID,
    clientSecret,
    callbackURL,
    oneAuth.SaveUserAndRedirect,
).Handler())

For GitHub Enterprise, call SetEnterpriseURL on the GitHub provider with the web host, such as https://ghe.example.com or https://acme.ghe.com. Code that starts from a REST API root instead, like a --github-api flag, calls oauth2.GitHubHostsFor(api). It returns the web host, the API root, the OAuth endpoint and the /user URL for github.com, GitHub Enterprise Server (<host>/api/v3) and GHE.com (api.<tenant>.ghe.com). Any other root, such as a test’s fake, serves everything on itself (issue 554).

Any OpenID Connect provider

oauth2/oidc signs people in through their organisation’s identity provider (Entra ID, Okta, Google Workspace, Keycloak) by issuer URL. Discovery runs once, in oidc.New. HandleUser runs only after the ID token’s signature, iss, aud, exp and nonce check out, and it receives the token’s claims.

p, err := oidc.New(ctx, oidc.Config{
    Issuer:       "https://login.microsoftonline.com/<tenant>/v2.0",
    ClientID:     clientID,
    ClientSecret: clientSecret,
    CallbackURL:  "https://app.example.com/auth/oidc/callback/",
    HandleUser:   oneAuth.SaveUserAndRedirect,
})
oneAuth.AddAuth("/oidc", p.Handler())

oidc.AllowList decides who may sign in from those claims. It matches a verified email domain, a groups value, sub, or Google’s hd claim (HostedDomains). hd admits only Workspace accounts in the domain, which Google recommends over the email domain. Allows returns a bool, and Decide also names the rule that matched.

Entra ID leaves groups out of the ID token once a person is in more than 200 groups, and names it in _claim_names instead. Decide reports that as GroupsOverage, so the app can log it rather than a plain refusal. Resolve goes one step further and fetches the groups from Microsoft Graph. It needs a Graph permission on the app registration and its scope in Config.Scopes (issue 555):

allow := oidc.AllowList{Groups: []string{"<group object ID>"}}

// in oidc.Config:
HandleUser: func(authtype, provider string, token *oauth2.Token, claims map[string]any, w http.ResponseWriter, r *http.Request) {
    d, err := allow.Resolve(r.Context(), claims, oidc.EntraGroups{AccessToken: token.AccessToken}.Groups)
    if !d.Allowed {
        slog.Info("sign-in refused", "groups_overage", d.GroupsOverage, "err", err)
        http.Redirect(w, r, "/auth/failed", http.StatusSeeOther)
        return
    }
    oneAuth.SaveUserAndRedirect(authtype, provider, token, claims, w, r)
},

To sign the user out at the provider too (RP-Initiated Logout), clear your own session, then redirect to p.LogoutURL(idTokenHint, postLogoutRedirectURI, state). idTokenHint is the raw ID token from sign-in, which HandleUser gets as token.Extra("id_token"), so keep it with the session. Most providers want it, and Keycloak shows its own confirmation page without it. LogoutURL returns oidc.ErrNoEndSessionEndpoint when the provider’s discovery lists no end_session_endpoint (issue 546). A CLI signs in with p.DeviceAuth and p.DeviceAccessToken when the provider lists a device endpoint. oauth2/oidc/example runs both flows against the Keycloak in docker-compose.yml.

To test your wiring without a real provider, point the provider at oauth2/oidc/oidctest. It is a fake issuer on an httptest.Server. A test picks who signs in, so /authorize redirects straight back with a code. Register the app’s callback URL with WithClient, since the fake checks it exactly, as a real provider does:

iss := oidctest.New(t, oidctest.WithClient("app", "secret", "http://127.0.0.1:8080/auth/oidc/callback/"))
iss.AddUser("u-123", map[string]any{"email": "ada@example.com", "email_verified": true, "groups": []string{"g-eng"}})
iss.SignInAs("u-123")
p, _ := oidc.New(ctx, oidc.Config{Issuer: iss.URL, ClientID: "app", ClientSecret: "secret", ...})
// A CLI test: da, _ := p.DeviceAuth(ctx); iss.Approve(da.UserCode, "u-123")

WithGroupsOverage makes the ID token name groups in _claim_names instead of carrying it, and EntraGroups{GraphURL: iss.GraphURL()} resolves it through the fake (issue 566).

PKCE (Proof Key for Code Exchange)

All OAuth2 flows use PKCE (RFC 7636) by default to prevent authorization code interception attacks. This is especially important for public clients (SPAs, mobile apps) where the client secret cannot be securely stored.

Authorization redirect:
  → code_challenge=SHA256(verifier) & code_challenge_method=S256
  → verifier stored in HttpOnly cookie (pkce_verifier)

Callback:
  ← reads verifier from cookie
  ← sends code_verifier in token exchange
  ← provider verifies SHA256(verifier) == challenge

PKCE is enabled by default. To disable for a provider that doesn’t support it:

google := oauth2.NewGoogleOAuth2(clientID, clientSecret, callbackURL, handleUser)
google.DisablePKCE = true  // logs a warning — not recommended

See: RFC 7636

Channel Linking (Multiple Auth Methods)

A user can sign up with email/password and later link their Google account, or vice versa. Multiple channels share the same user via email Identity.

flowchart TD
    U["User (id: abc123)"]
    U --> I["Identity: email → user@example.com (verified)"]
    U --> CL["Channel: local → email:user@example.com (password_hash)"]
    U --> CG["Channel: google → email:user@example.com (oauth profile)"]
    U --> CH["Channel: github → email:user@example.com (oauth profile)"]

The user profile tracks linked providers: profile["channels"] = ["local", "google", "github"]

Provider Linking Matrix

Second auth attempt → Local Email + Password Google OAuth Different Email
No existing account Create new Create new Create new
Has Local (same email) Login Link OAuth New account
Has Google (same email) Fails* Login New account

* “Fails” = Signup fails because Identity exists, but no local Channel.
User should login via OAuth, then set password via profile or password reset.

Key rule: Same email = same account (via Identity), regardless of auth method.

Setting Up Channel-Aware User Creation

Use NewEnsureAuthUserFunc for OAuth callbacks that automatically link channels:

config := localauth.EnsureAuthUserConfig{
    UserStore:     userStore,
    IdentityStore: identityStore,
    ChannelStore:  channelStore,
    UsernameStore: usernameStore, // Optional
}

ensureUser := localauth.NewEnsureAuthUserFunc(config)
oneAuth.UserStore = &myAuthUserStore{ensureUser: ensureUser}

Adding Password to OAuth User

linkConfig := localauth.LinkCredentialsConfig{
    UserStore:     userStore,
    IdentityStore: identityStore,
    ChannelStore:  channelStore,
    UsernameStore: usernameStore,
}

getUser := func(r *http.Request) (string, error) {
    return getLoggedInUserIDFromSession(r), nil
}

mux.Handle("POST /auth/link-credentials",
    localAuth.HandleLinkCredentials(linkConfig, getUser))

Frontend form:

<form action="/auth/link-credentials" method="POST">
    <input name="username" placeholder="Username (optional)">
    <input name="password" type="password" placeholder="Password" required>
    <button type="submit">Add Password Login</button>
</form>

Adding OAuth to Password User

// Step 1: Start linking flow
func handleLinkGoogle(w http.ResponseWriter, r *http.Request) {
    userID := getLoggedInUserID(r)
    oneAuth.StartLinkOAuth(r, userID)
    http.Redirect(w, r, "/auth/google/", http.StatusFound)
}

// Step 2: In your OAuth callback, detect linking mode
func googleCallback(w http.ResponseWriter, r *http.Request) {
    // ... exchange code for token, get userInfo ...

    linkingUserID := oneAuth.GetLinkingUserID(r)
    if linkingUserID != "" {
        linkConfig := localauth.LinkOAuthConfig{
            UserStore:     userStore,
            IdentityStore: identityStore,
            ChannelStore:  channelStore,
        }
        oneAuth.HandleLinkOAuthCallback(linkConfig, linkingUserID, "google", userInfo, w, r)
        return
    }

    // Normal login/signup flow
    oneAuth.SaveUserAndRedirect("oauth", "google", token, userInfo, w, r)
}

Programmatic Channel Linking

err := localauth.LinkLocalCredentials(config, userID, "newusername", "password123", userEmail)

Channel Linking Security

  1. Email Matching: OAuth linking verifies the OAuth email matches the user’s existing email
  2. Cannot Link Different Emails: Users cannot link an OAuth account with a different email address
  3. Duplicate Prevention: The same provider can only be linked once per identity

Username-Based Login

validateCreds := localauth.NewCredentialsValidatorWithUsername(
    identityStore, channelStore, userStore, usernameStore,
)

la := &localauth.LocalAuth{
    ValidateCredentials: validateCreds,
}

Users can then login with either user@example.com + password or johndoe + password.

User Journeys

Journey 1: Multiple OAuth Providers (Same Email)

Day 1: "Continue with Google" (alice@gmail.com)
  → Create User, Identity, Channel(google)

Day 7: "Continue with GitHub" (alice@gmail.com — same email)
  → Identity exists → same user
  → Create Channel(github) for existing user

Result: Login with Google OR GitHub, same account

Journey 2: OAuth User Adds Password

Day 1: "Continue with Google"
  → Create User + Channel(google)

Day 3: Set username "bobsmith" + password
  → Reserve username, create Channel(local)

Result: Login via Google, email+password, or username+password
Day 1: Sign up with email + password
  → Create User + Channel(local)

Day 5: "Continue with Google" (same email)
  → Identity exists → add Channel(google)

Result: Login with email+password OR Google

Journey 4: OAuth User Resets Password (No Local Channel)

Day 1: "Continue with Google"
  → User has no local Channel

Day 2: "Forgot Password" → enter email → reset link
  → NewUpdatePasswordFunc detects no local Channel
  → Creates Channel(local) with new password

Result: Login via Google OR email+password

Journey 5: Different Emails on Different Providers

Day 1: Google login (alice@gmail.com) → User A
Day 3: GitHub login (alice@company.com) → User B (different account!)

By design: different emails = different accounts.

Session Management

The HandleUser callback is called after successful authentication:

HandleUser: func(authtype string, provider string, token *oauth2.Token,
                  userInfo map[string]any, w http.ResponseWriter, r *http.Request) {
    // token is nil for local auth, populated for OAuth
    sessionID := generateSessionID()
    sessionStore.Save(sessionID, userInfo)

    http.SetCookie(w, &http.Cookie{
        Name:     "session_id",
        Value:    sessionID,
        Path:     "/",
        HttpOnly: true,
        Secure:   true,
        SameSite: http.SameSiteLaxMode,
    })

    json.NewEncoder(w).Encode(map[string]any{
        "success": true,
        "user":    userInfo,
    })
}

Email Integration

Custom Email Sender

type SMTPEmailSender struct {
    host, username, password string
    port                     int
}

func (s *SMTPEmailSender) SendVerificationEmail(to, link string) error {
    auth := smtp.PlainAuth("", s.username, s.password, s.host)
    msg := fmt.Sprintf("To: %s\r\nSubject: Verify your email\r\n\r\nClick: %s\r\n", to, link)
    return smtp.SendMail(fmt.Sprintf("%s:%d", s.host, s.port), auth, s.username, []string{to}, []byte(msg))
}

func (s *SMTPEmailSender) SendPasswordResetEmail(to, link string) error { /* similar */ }

Console Email Sender (Development)

la.EmailSender = &localauth.ConsoleEmailSender{}

Logs emails to stdout instead of sending them.

Validation

la.SignupPolicy = &core.SignupPolicy{
    RequireUsername:       true,
    RequireEmail:          true,
    RequirePassword:       true,
    EnforceUsernameUnique: true,
    EnforceEmailUnique:    true,
    MinPasswordLength:     12,
    UsernamePattern:       `^[a-z][a-z0-9_]{2,19}$`,
}

Preset Policies

Policy Username Email Password Use Case
PolicyEmailOnly Optional Required Required Most web apps
PolicyUsernameRequired Required Required Required Apps needing usernames
PolicyFlexible Optional Optional Optional OAuth-first apps

Custom Validator (Legacy)

la.ValidateSignup = func(creds *core.Credentials) error {
    if len(creds.Password) < 12 {
        return fmt.Errorf("password must be at least 12 characters")
    }
    return nil
}

If SignupPolicy is set, it takes precedence over ValidateSignup.

Error Handling

Structured Errors (AuthError)

type AuthError struct {
    Code    string // "email_exists", "username_taken", etc.
    Message string // Human-readable message
    Field   string // Form field with the error
}
Code Description
email_exists Email already registered
username_taken Username already taken
weak_password Password doesn’t meet requirements
invalid_username Username format invalid
invalid_email Email format invalid
missing_field Required field not provided
invalid_credentials Wrong email/password

Custom Error Handlers

la.OnSignupError = func(err *core.AuthError, w http.ResponseWriter, r *http.Request) bool {
    session.SetFlash(r, "error", err.Message)
    session.SetFlash(r, "error_field", err.Field)
    http.Redirect(w, r, "/signup", http.StatusSeeOther)
    return true // handled
}

la.OnLoginError = func(err *core.AuthError, w http.ResponseWriter, r *http.Request) bool {
    session.SetFlash(r, "login_error", err.Message)
    http.Redirect(w, r, "/login", http.StatusSeeOther)
    return true
}

If the handler returns false (or is nil), OneAuth returns a default JSON response:

{"error": "Email already registered", "code": "email_exists", "field": "email"}

HTTP Status Codes

Code Meaning
200 Successful authentication
400 Invalid input or validation failure
401 Invalid credentials
409 Resource already exists
500 Server-side error

Password Reset Redirect Mode

Two response modes for password reset:

Mode When Behavior
JSON (default) ForgotPasswordURL / ResetPasswordURL empty GET renders basic HTML form, POST returns JSON
Redirect URLs set GET redirects to app page, POST redirects with query params
mux.Handle("/auth/forgot-password", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    if r.Method == http.MethodGet {
        localAuth.HandleForgotPasswordForm(w, r)
    } else {
        localAuth.HandleForgotPassword(w, r)
    }
}))

mux.Handle("/auth/reset-password", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    if r.Method == http.MethodGet {
        localAuth.HandleResetPasswordForm(w, r)
    } else {
        localAuth.HandleResetPassword(w, r)
    }
}))

Form Field Configuration

la.UsernameField = "email"
la.PasswordField = "password"
la.EmailField = "email"
la.PhoneField = "phone"

Rate Limiting and Account Lockout

Rate Limiting

LocalAuth supports an optional RateLimiter field that throttles login attempts per key (typically per IP or per email). The interface is defined in core:

la.RateLimiter = core.NewInMemoryRateLimiter(10, time.Minute) // 10 attempts per minute

core.RateLimiter is a shared interface used by both localauth and apiauth.

Account Lockout

LocalAuth supports an optional Lockout field (core.AccountLockout) that temporarily locks accounts after repeated failed login attempts. Lockouts are auto-expiring — no manual unlock is needed.

la.Lockout = &core.AccountLockout{
    MaxAttempts:    5,
    LockoutWindow:  10 * time.Minute,
    LockoutDuration: 30 * time.Minute,
}

Timing Oracle Fix (CWE-208)

The credential validator now uses constant-time comparison for password validation failures, preventing timing-based user enumeration attacks. When a user is not found, a dummy bcrypt comparison is performed so the response time is indistinguishable from a real password check.

Security Considerations

Protection Implementation
Password Hashing bcrypt with cost 10
Single-Use Tokens Deleted from TokenStore after use
Token Expiration Time-limited verification/reset tokens
Generic Login Errors “Invalid credentials” (prevents enumeration)
Constant-Time Comparison Via bcrypt internals + dummy hash on missing user (CWE-208 fix)

CSRF Protection (CSRFMiddleware)

OneAuth provides a CSRFMiddleware using the double-submit cookie pattern. It is opt-in — applications wrap their form endpoints to enable protection.

csrf := &httpauth.CSRFMiddleware{Secure: true} // set Secure: true for HTTPS

// Wrap form GET handlers (generates CSRF cookie + injects token into context)
mux.Handle("GET /auth/login", csrf.Protect(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    renderTemplate(w, "login.html", map[string]any{
        "CSRFField": httpauth.CSRFTemplateField(r),
    })
})))

// Wrap form POST handlers (validates cookie vs. form field/header)
mux.Handle("POST /auth/login", csrf.Protect(http.HandlerFunc(la.ServeHTTP)))

In templates, include the hidden field inside <form> tags:

<form method="POST" action="/auth/login">
    {{.CSRFField}}
    <!-- other fields -->
</form>

How it works:

  • Safe methods (GET, HEAD, OPTIONS): Generates a random token, stores it in a csrf_token cookie (not HttpOnly, so JS can read it for AJAX), and injects it into the request context.
  • Unsafe methods (POST, PUT, DELETE, PATCH): Validates that the token from the csrf_token form field or X-CSRF-Token header matches the cookie value using constant-time comparison.
  • Bearer-token requests: Automatically exempt (not vulnerable to CSRF).
  • Per-session tokens: Token persists for the cookie lifetime (default: 1 hour). No per-request rotation, so back button and multi-tab usage work.

Configuration options:

Field Default Description
CookieName csrf_token Cookie name
FieldName csrf_token Form field name
HeaderName X-CSRF-Token Header name for AJAX
MaxAge 3600 (1 hour) Cookie lifetime in seconds
Secure false Set true for HTTPS
SameSite Strict SameSite cookie attribute
ErrorHandler 403 JSON Custom error handler
ExemptFunc Bearer exempt Custom exemption logic

Template helpers:

  • httpauth.CSRFToken(r) — extract token string from request context
  • httpauth.CSRFTemplateField(r) — returns <input type="hidden" name="csrf_token" value="..."> as template.HTML
  1. Rate Limiting: Per-IP and per-account limits on login attempts Provided — use LocalAuth.RateLimiter and LocalAuth.Lockout (see above)
  2. CSRF Tokens: On all auth forms Provided — use CSRFMiddleware (see above)
  3. Session Security: HttpOnly, Secure, SameSite cookies
  4. HTTPS: Required for OAuth callbacks
  5. Audit Logging: Log all auth events