feat(auth): implement user and device authentication

This commit is contained in:
QiuSW
2026-07-26 15:18:48 +08:00
parent c5d3b215ff
commit 49db5b8305
66 changed files with 6216 additions and 271 deletions
@@ -0,0 +1,49 @@
package authcommon
import (
"context"
"crypto/subtle"
"encoding/base64"
"strings"
"cmroubao/backend-api/internal/domain"
)
const (
AdminSessionCookieName = "cmroubao_admin_session"
CSRFCookieName = "cmroubao_admin_csrf"
CSRFFormField = "csrf_token"
CSRFHeader = "X-CSRF-Token"
)
type principalContextKey struct{}
func WithPrincipal(
ctx context.Context,
principal domain.AuthPrincipal,
) context.Context {
return context.WithValue(ctx, principalContextKey{}, principal)
}
func Principal(ctx context.Context) (domain.AuthPrincipal, bool) {
principal, ok := ctx.Value(principalContextKey{}).(domain.AuthPrincipal)
return principal, ok
}
func ValidCSRFPair(cookieValue, presentedValue string) bool {
cookieValue = strings.TrimSpace(cookieValue)
presentedValue = strings.TrimSpace(presentedValue)
if !ValidOpaqueValue(cookieValue) ||
len(cookieValue) != len(presentedValue) {
return false
}
return subtle.ConstantTimeCompare(
[]byte(cookieValue),
[]byte(presentedValue),
) == 1
}
func ValidOpaqueValue(value string) bool {
decoded, err := base64.RawURLEncoding.DecodeString(value)
return err == nil && len(decoded) == 32
}