Documentation
¶
Overview ¶
Package auth provides Google OAuth login and session-cookie middleware for the web API.
Index ¶
- func RequireAuth(sessionStore db.SessionStore) func(http.Handler) http.Handler
- func WithUser(ctx context.Context, user *User) context.Context
- type Config
- type GoogleUserInfo
- type Handlers
- func (h *Handlers) Enabled() bool
- func (h *Handlers) HandleCallback(w http.ResponseWriter, r *http.Request)
- func (h *Handlers) HandleLogin(w http.ResponseWriter, r *http.Request)
- func (h *Handlers) HandleLogout(w http.ResponseWriter, r *http.Request)
- func (h *Handlers) HandleMe(w http.ResponseWriter, r *http.Request)
- type User
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RequireAuth ¶
RequireAuth returns middleware that validates the session cookie and injects the authenticated User into the request context. Unauthenticated requests receive a 401 JSON response.
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) 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.
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 ¶
CheckSession validates the session cookie without blocking. Used for endpoints like WebSocket that need auth but not the middleware chain.
func UserFromContext ¶
UserFromContext extracts the User from the request context. Returns nil if no user is set (i.e. unauthenticated).