domain/content/publisher/mux.go (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | package publisher
import (
"crypto/rand"
"encoding/base64"
"encoding/json"
"net/http"
"golang.org/x/oauth2"
"alin.ovh/homestead/domain/content/publisher/templates"
basetpl "alin.ovh/homestead/domain/web/templates"
ihttp "alin.ovh/homestead/shared/http"
)
type user struct {
ID string `json:"sub"`
Email string `json:"email"`
EmailVerified bool `json:"email_verified"`
Name string `json:"name"`
}
const (
sessionCookieName = "oidc_session"
stateCookieName = "oidc_state"
verifierCookieName = "oidc_verifier"
)
var (
ErrCannotDetermineUser = ihttp.NewError("cannot determine user", http.StatusInternalServerError)
ErrRenderFailure = ihttp.NewError("failed to render page", http.StatusInternalServerError)
)
func (s *Service) Index(w http.ResponseWriter, r *http.Request) error {
userName := "Guest"
isLoggedIn := false
if user, err := getUserFromRequest(r); err == nil {
isLoggedIn = true
userName = user.Name
if userName == "" {
userName = user.Email
}
}
w.Header().Set("Vary", "Cookie")
err := templates.IndexPage(s.siteSettings, templates.PageSettings{
User: userName,
IsLoggedIn: isLoggedIn,
PageSettings: basetpl.PageSettings{
Title: "Home",
},
}).Render(w)
if err != nil {
return ErrRenderFailure
}
return nil
}
// Login initiates the OIDC authentication flow with PKCE S256.
// PKCE (Proof Key for Code Exchange) protects against authorization code interception attacks
// by using a cryptographically random verifier and its SHA256 hash challenge.
// See: https://www.rfc-editor.org/rfc/rfc7636
func (s *Service) Login(w http.ResponseWriter, r *http.Request) error {
// Generate random state for CSRF protection
state, err := generateRandomState()
if err != nil {
s.log.Error("failed to generate state", "error", err)
return ihttp.NewError("failed to generate state", http.StatusInternalServerError)
}
// Generate PKCE verifier for S256 challenge
verifier := oauth2.GenerateVerifier()
// Store state in cookie for verification in callback
http.SetCookie(w, &http.Cookie{
Name: stateCookieName,
Value: state,
Path: s.baseURL.Path,
MaxAge: 600, // 10 minutes
HttpOnly: true,
Secure: true,
SameSite: http.SameSiteLaxMode,
})
// Store PKCE verifier in cookie for token exchange
http.SetCookie(w, &http.Cookie{
Name: verifierCookieName,
Value: verifier,
Path: s.baseURL.Path,
MaxAge: 600, // 10 minutes
HttpOnly: true,
Secure: true,
SameSite: http.SameSiteLaxMode,
})
// Redirect to OIDC provider with PKCE S256 challenge
url := s.oauth2Config.AuthCodeURL(state, oauth2.S256ChallengeOption(verifier))
http.Redirect(w, r, url, http.StatusFound)
return nil
}
// Callback handles the OIDC provider's redirect after authentication.
// It verifies the state parameter (CSRF protection), exchanges the authorization code
// for tokens using the PKCE verifier, validates the ID token, and creates a session.
func (s *Service) Callback(w http.ResponseWriter, r *http.Request) error {
ctx := r.Context()
// Verify state parameter (CSRF protection)
stateCookie, err := r.Cookie(stateCookieName)
if err != nil {
s.log.Error("state cookie not found", "error", err)
return ihttp.NewError("invalid state", http.StatusBadRequest)
}
state := r.URL.Query().Get("state")
if state != stateCookie.Value {
s.log.Error("state mismatch")
return ihttp.NewError("invalid state", http.StatusBadRequest)
}
// Retrieve PKCE verifier for token exchange
verifierCookie, err := r.Cookie(verifierCookieName)
if err != nil {
s.log.Error("verifier cookie not found", "error", err)
return ihttp.NewError("invalid verifier", http.StatusBadRequest)
}
// Clear state cookie
http.SetCookie(w, &http.Cookie{
Name: stateCookieName,
Value: "",
Path: s.baseURL.Path,
MaxAge: -1,
HttpOnly: true,
})
// Clear verifier cookie
http.SetCookie(w, &http.Cookie{
Name: verifierCookieName,
Value: "",
Path: s.baseURL.Path,
MaxAge: -1,
HttpOnly: true,
})
// Exchange code for token with PKCE verifier
code := r.URL.Query().Get("code")
oauth2Token, err := s.oauth2Config.Exchange(
ctx,
code,
oauth2.VerifierOption(verifierCookie.Value),
)
if err != nil {
s.log.Error("failed to exchange token", "error", err)
return ihttp.NewError("failed to exchange token", http.StatusInternalServerError)
}
// Extract ID Token from OAuth2 token
rawIDToken, ok := oauth2Token.Extra("id_token").(string)
if !ok {
s.log.Error("no id_token in token response")
return ihttp.NewError("no id_token in token response", http.StatusInternalServerError)
}
// Verify ID Token
idToken, err := s.oidcVerifier.Verify(ctx, rawIDToken)
if err != nil {
s.log.Error("failed to verify ID token", "error", err)
return ihttp.NewError("failed to verify ID token", http.StatusInternalServerError)
}
// Extract user claims
var userInfo user
if err := idToken.Claims(&userInfo); err != nil {
s.log.Error("failed to parse claims", "error", err)
return ihttp.NewError("failed to parse claims", http.StatusInternalServerError)
}
// Store user info in session cookie
sessionData, err := json.Marshal(userInfo)
if err != nil {
s.log.Error("failed to marshal session data", "error", err)
return ihttp.NewError("failed to create session", http.StatusInternalServerError)
}
http.SetCookie(w, &http.Cookie{
Name: sessionCookieName,
Value: base64.StdEncoding.EncodeToString(sessionData),
Path: s.baseURL.Path,
MaxAge: 86400 * 7, // 7 days
HttpOnly: true,
Secure: true,
SameSite: http.SameSiteLaxMode,
})
s.log.Info("user logged in", "email", userInfo.Email, "name", userInfo.Name)
http.Redirect(w, r, s.baseURL.Path, http.StatusFound)
return nil
}
func (s *Service) Logout(w http.ResponseWriter, r *http.Request) error {
// Clear session cookie
http.SetCookie(w, &http.Cookie{
Name: sessionCookieName,
Value: "",
Path: s.baseURL.Path,
MaxAge: -1,
HttpOnly: true,
})
s.log.Info("user logged out")
// Redirect to home page
http.Redirect(w, r, s.baseURL.Path, http.StatusFound)
return nil
}
// getUserFromRequest extracts and decodes user information from the session cookie.
// Returns an error if the cookie is missing or invalid.
func getUserFromRequest(r *http.Request) (*user, error) {
cookie, err := r.Cookie(sessionCookieName)
if err != nil {
return nil, err
}
sessionData, err := base64.StdEncoding.DecodeString(cookie.Value)
if err != nil {
return nil, err
}
var userInfo user
if err := json.Unmarshal(sessionData, &userInfo); err != nil {
return nil, err
}
return &userInfo, nil
}
// generateRandomState generates a cryptographically secure random state string
// for CSRF protection in the OAuth2/OIDC flow.
func generateRandomState() (string, error) {
b := make([]byte, 32)
_, err := rand.Read(b)
if err != nil {
return "", err
}
return base64.URLEncoding.EncodeToString(b), nil
}
func (s *Service) Style(w http.ResponseWriter, r *http.Request) error {
w.Header().Set("Content-Type", "text/css")
http.ServeFileFS(w, r, basetpl.Files, "style.css")
return nil
}
|