Files
Sebastian UnterschützandClaude Opus 5.5 d20225982a refactor: follow the google go style guide
Document every package and exported identifier, return the log level
from NewLogger instead of keeping it in a package variable, and stop
tests from mutating the argon2 parameters.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
2026-11-22 00:18:02 +01:00

120 lines
2.7 KiB
Go

package auth
import (
"errors"
"net/http"
"net/url"
"github.com/gin-gonic/gin"
)
// Names shared with the browser side.
const (
CookieName = "__Host-session"
CSRFHeader = "X-CSRF-Token"
CSRFField = "_csrf"
ctxKey = "auth.session"
)
// SetCookie sends the session cookie. The __Host- prefix makes browsers
// reject it unless it is Secure, host-only and scoped to /.
func SetCookie(c *gin.Context, sess *Session, maxAge int) {
http.SetCookie(c.Writer, &http.Cookie{
Name: CookieName,
Value: sess.Token,
Path: "/",
MaxAge: maxAge,
Secure: true,
HttpOnly: true,
SameSite: http.SameSiteLaxMode,
})
}
// ClearCookie removes the session cookie.
func ClearCookie(c *gin.Context) {
http.SetCookie(c.Writer, &http.Cookie{Name: CookieName, Value: "", Path: "/", MaxAge: -1, Secure: true, HttpOnly: true, SameSite: http.SameSiteLaxMode})
}
// Load attaches the session if one exists. It never rejects; use Require for that.
func (s *Sessions) Load() gin.HandlerFunc {
return func(c *gin.Context) {
token, err := c.Cookie(CookieName)
if err != nil {
c.Next()
return
}
sess, err := s.Get(c.Request.Context(), token)
switch {
case errors.Is(err, ErrNoSession):
ClearCookie(c)
case err != nil:
_ = c.Error(err)
c.AbortWithStatus(http.StatusServiceUnavailable)
return
default:
c.Set(ctxKey, sess)
}
c.Next()
}
}
// Require aborts with 401 unless Load attached a session.
func Require() gin.HandlerFunc {
return func(c *gin.Context) {
if Current(c) == nil {
c.AbortWithStatus(http.StatusUnauthorized)
return
}
c.Next()
}
}
// CSRF uses the synchronizer token stored in the session. SameSite=Lax alone
// does not cover same-site subdomains such as customer status pages.
func CSRF() gin.HandlerFunc {
return func(c *gin.Context) {
switch c.Request.Method {
case http.MethodGet, http.MethodHead, http.MethodOptions:
c.Next()
return
}
// Covers requests without a session (login CSRF) as well.
if !sameOrigin(c.Request) {
c.AbortWithStatus(http.StatusForbidden)
return
}
sess := Current(c)
if sess == nil {
c.Next()
return
}
token := c.GetHeader(CSRFHeader)
if token == "" {
token = c.PostForm(CSRFField)
}
if !sess.CheckCSRF(token) {
c.AbortWithStatus(http.StatusForbidden)
return
}
c.Next()
}
}
func sameOrigin(r *http.Request) bool {
origin := r.Header.Get("Origin")
if origin == "" || origin == "null" {
return origin == ""
}
u, err := url.Parse(origin)
return err == nil && u.Host == r.Host
}
// Current returns the session attached by Load, or nil.
func Current(c *gin.Context) *Session {
v, ok := c.Get(ctxKey)
if !ok {
return nil
}
return v.(*Session)
}