auth

package
v0.6.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 14, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package auth provides Google OAuth login and session-cookie middleware for the web API.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RequireAuth

func RequireAuth(sessionStore db.SessionStore) func(http.Handler) http.Handler

RequireAuth returns middleware that validates the session cookie and injects the authenticated User into the request context. Unauthenticated requests receive a 401 JSON response.

func WithUser

func WithUser(ctx context.Context, user *User) context.Context

WithUser adds a User to the request context.

Types

type Config

type Config struct {
	GoogleClientID     string
	GoogleClientSecret string
	AllowedDomain      string
	SessionTTL         time.Duration
}

Config holds authentication configuration.

type GoogleUserInfo

type GoogleUserInfo struct {
	Email   string `json:"email"`
	Name    string `json:"name"`
	Picture string `json:"picture"`
}

GoogleUserInfo is the response from Google's userinfo endpoint.

type Handlers

type Handlers struct {
	// contains filtered or unexported fields
}

Handlers provides OAuth2 authentication HTTP handlers.

func NewHandlers

func NewHandlers(sessionStore db.SessionStore, cfg Config) *Handlers

NewHandlers creates auth handlers. If client ID or secret are empty, authentication is disabled and handlers return 503.

func (*Handlers) Enabled

func (h *Handlers) Enabled() bool

Enabled returns whether OAuth authentication is configured.

func (*Handlers) HandleCallback

func (h *Handlers) HandleCallback(w http.ResponseWriter, r *http.Request)

HandleCallback processes the OAuth2 callback from Google.

func (*Handlers) HandleLogin

func (h *Handlers) HandleLogin(w http.ResponseWriter, r *http.Request)

HandleLogin redirects the user to Google's OAuth2 consent screen.

func (*Handlers) HandleLogout

func (h *Handlers) HandleLogout(w http.ResponseWriter, r *http.Request)

HandleLogout clears the session and cookie.

func (*Handlers) HandleMe

func (h *Handlers) HandleMe(w http.ResponseWriter, r *http.Request)

HandleMe returns the currently authenticated user. When auth is disabled, returns an anonymous user so the frontend can proceed.

type User

type User struct {
	Email   string `json:"email"`
	Name    string `json:"name"`
	Picture string `json:"picture"`
}

User represents an authenticated user extracted from a session.

func CheckSession

func CheckSession(sessionStore db.SessionStore, r *http.Request) (*User, error)

CheckSession validates the session cookie without blocking. Used for endpoints like WebSocket that need auth but not the middleware chain.

func UserFromContext

func UserFromContext(ctx context.Context) *User

UserFromContext extracts the User from the request context. Returns nil if no user is set (i.e. unauthenticated).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL