Adopt shared authproxy helpers for the AuthGuard-proxy auth service
Replaces locally-duplicated session-cookie handling, input normalization, and AuthGuard-error translation with shared/backend/authproxy, shared with Bitmagnet/GameStream/media-backend and others doing the same AuthGuard-proxy pattern.
This commit is contained in:
1 parent
68f6dd5a87
commit
b412ca0f69
1 file changed
+12
-27
+12
-27
@@ -13,6 +13,7 @@ import (
|
||||
|
||||
sharedauth "repos.gfranz.ch/Gabriel20xx/shared/backend/auth"
|
||||
authclient "repos.gfranz.ch/Gabriel20xx/shared/backend/authclient"
|
||||
"repos.gfranz.ch/Gabriel20xx/shared/backend/authproxy"
|
||||
)
|
||||
|
||||
// AuthGuard migration
|
||||
@@ -221,27 +222,23 @@ func newAuthService(authGuard *authclient.Client, db *DB, secureCookie bool) *Au
|
||||
}
|
||||
|
||||
func normalizeUsername(value string) (string, error) {
|
||||
value = strings.TrimSpace(value)
|
||||
if value == "" || len([]rune(value)) > maxUsernameLength {
|
||||
normalized, ok := authproxy.NormalizeUsername(value, maxUsernameLength)
|
||||
if !ok {
|
||||
return "", errUsernameRequired
|
||||
}
|
||||
return value, nil
|
||||
return normalized, nil
|
||||
}
|
||||
|
||||
func normalizeEmail(value string) (string, error) {
|
||||
value = strings.ToLower(strings.TrimSpace(value))
|
||||
if value == "" || len([]rune(value)) > maxEmailLength || strings.Count(value, "@") != 1 {
|
||||
normalized, ok := authproxy.NormalizeEmail(value, maxEmailLength)
|
||||
if !ok {
|
||||
return "", errEmailRequired
|
||||
}
|
||||
parts := strings.SplitN(value, "@", 2)
|
||||
if parts[0] == "" || parts[1] == "" || !strings.Contains(parts[1], ".") {
|
||||
return "", errEmailRequired
|
||||
}
|
||||
return value, nil
|
||||
return normalized, nil
|
||||
}
|
||||
|
||||
func validatePassword(value string) error {
|
||||
if len([]rune(value)) < minPasswordLength {
|
||||
if !authproxy.ValidPassword(value, minPasswordLength) {
|
||||
return errPasswordTooShort
|
||||
}
|
||||
return nil
|
||||
@@ -269,25 +266,14 @@ func (a *AuthService) enabled() bool {
|
||||
// sessionToken reads the AuthGuard session cookie off the incoming request.
|
||||
// See the top-of-file doc comment's "Session cookie / token model" section.
|
||||
func (a *AuthService) sessionToken(r *http.Request) string {
|
||||
cookie, err := r.Cookie(authclient.DefaultCookieName)
|
||||
if err != nil || cookie.Value == "" {
|
||||
return ""
|
||||
}
|
||||
return cookie.Value
|
||||
return authproxy.SessionToken(r)
|
||||
}
|
||||
|
||||
// clearSessionCookies clears both the AuthGuard cookie (as a fallback --
|
||||
// normally AuthGuard's own Set-Cookie, forwarded via authclient.ForwardCookies,
|
||||
// already does this) and the pre-migration CollabTable cookie name.
|
||||
func (a *AuthService) clearSessionCookies(w http.ResponseWriter) {
|
||||
http.SetCookie(w, &http.Cookie{
|
||||
Name: authclient.DefaultCookieName, Value: "", Path: "/", HttpOnly: true,
|
||||
Secure: a.secureCookie, SameSite: http.SameSiteLaxMode, MaxAge: -1,
|
||||
})
|
||||
http.SetCookie(w, &http.Cookie{
|
||||
Name: legacySessionCookieName, Value: "", Path: "/", HttpOnly: true,
|
||||
Secure: a.secureCookie, SameSite: http.SameSiteLaxMode, MaxAge: -1,
|
||||
})
|
||||
authproxy.ClearSessionCookies(w, legacySessionCookieName, a.secureCookie)
|
||||
}
|
||||
|
||||
// currentUser resolves the request's AuthGuard session cookie via the
|
||||
@@ -943,9 +929,8 @@ func (a *AuthService) managedUserView(ctx context.Context, user authclient.Manag
|
||||
// failure actually reaching AuthGuard) falls back to a generic 500 with
|
||||
// fallbackMessage.
|
||||
func writeAuthGuardError(w http.ResponseWriter, err error, fallbackMessage string) {
|
||||
var apiErr *authclient.APIError
|
||||
if errors.As(err, &apiErr) {
|
||||
writeJSONError(w, apiErr.StatusCode, apiErr.Message)
|
||||
if statusCode, message, ok := authproxy.APIErrorResponse(err); ok {
|
||||
writeJSONError(w, statusCode, message)
|
||||
return
|
||||
}
|
||||
writeJSONError(w, http.StatusInternalServerError, fallbackMessage)
|
||||
|
||||
Reference in new issue
Block a user