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"
|
sharedauth "repos.gfranz.ch/Gabriel20xx/shared/backend/auth"
|
||||||
authclient "repos.gfranz.ch/Gabriel20xx/shared/backend/authclient"
|
authclient "repos.gfranz.ch/Gabriel20xx/shared/backend/authclient"
|
||||||
|
"repos.gfranz.ch/Gabriel20xx/shared/backend/authproxy"
|
||||||
)
|
)
|
||||||
|
|
||||||
// AuthGuard migration
|
// AuthGuard migration
|
||||||
@@ -221,27 +222,23 @@ func newAuthService(authGuard *authclient.Client, db *DB, secureCookie bool) *Au
|
|||||||
}
|
}
|
||||||
|
|
||||||
func normalizeUsername(value string) (string, error) {
|
func normalizeUsername(value string) (string, error) {
|
||||||
value = strings.TrimSpace(value)
|
normalized, ok := authproxy.NormalizeUsername(value, maxUsernameLength)
|
||||||
if value == "" || len([]rune(value)) > maxUsernameLength {
|
if !ok {
|
||||||
return "", errUsernameRequired
|
return "", errUsernameRequired
|
||||||
}
|
}
|
||||||
return value, nil
|
return normalized, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func normalizeEmail(value string) (string, error) {
|
func normalizeEmail(value string) (string, error) {
|
||||||
value = strings.ToLower(strings.TrimSpace(value))
|
normalized, ok := authproxy.NormalizeEmail(value, maxEmailLength)
|
||||||
if value == "" || len([]rune(value)) > maxEmailLength || strings.Count(value, "@") != 1 {
|
if !ok {
|
||||||
return "", errEmailRequired
|
return "", errEmailRequired
|
||||||
}
|
}
|
||||||
parts := strings.SplitN(value, "@", 2)
|
return normalized, nil
|
||||||
if parts[0] == "" || parts[1] == "" || !strings.Contains(parts[1], ".") {
|
|
||||||
return "", errEmailRequired
|
|
||||||
}
|
|
||||||
return value, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func validatePassword(value string) error {
|
func validatePassword(value string) error {
|
||||||
if len([]rune(value)) < minPasswordLength {
|
if !authproxy.ValidPassword(value, minPasswordLength) {
|
||||||
return errPasswordTooShort
|
return errPasswordTooShort
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@@ -269,25 +266,14 @@ func (a *AuthService) enabled() bool {
|
|||||||
// sessionToken reads the AuthGuard session cookie off the incoming request.
|
// sessionToken reads the AuthGuard session cookie off the incoming request.
|
||||||
// See the top-of-file doc comment's "Session cookie / token model" section.
|
// See the top-of-file doc comment's "Session cookie / token model" section.
|
||||||
func (a *AuthService) sessionToken(r *http.Request) string {
|
func (a *AuthService) sessionToken(r *http.Request) string {
|
||||||
cookie, err := r.Cookie(authclient.DefaultCookieName)
|
return authproxy.SessionToken(r)
|
||||||
if err != nil || cookie.Value == "" {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
return cookie.Value
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// clearSessionCookies clears both the AuthGuard cookie (as a fallback --
|
// clearSessionCookies clears both the AuthGuard cookie (as a fallback --
|
||||||
// normally AuthGuard's own Set-Cookie, forwarded via authclient.ForwardCookies,
|
// normally AuthGuard's own Set-Cookie, forwarded via authclient.ForwardCookies,
|
||||||
// already does this) and the pre-migration CollabTable cookie name.
|
// already does this) and the pre-migration CollabTable cookie name.
|
||||||
func (a *AuthService) clearSessionCookies(w http.ResponseWriter) {
|
func (a *AuthService) clearSessionCookies(w http.ResponseWriter) {
|
||||||
http.SetCookie(w, &http.Cookie{
|
authproxy.ClearSessionCookies(w, legacySessionCookieName, a.secureCookie)
|
||||||
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,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// currentUser resolves the request's AuthGuard session cookie via the
|
// 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
|
// failure actually reaching AuthGuard) falls back to a generic 500 with
|
||||||
// fallbackMessage.
|
// fallbackMessage.
|
||||||
func writeAuthGuardError(w http.ResponseWriter, err error, fallbackMessage string) {
|
func writeAuthGuardError(w http.ResponseWriter, err error, fallbackMessage string) {
|
||||||
var apiErr *authclient.APIError
|
if statusCode, message, ok := authproxy.APIErrorResponse(err); ok {
|
||||||
if errors.As(err, &apiErr) {
|
writeJSONError(w, statusCode, message)
|
||||||
writeJSONError(w, apiErr.StatusCode, apiErr.Message)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
writeJSONError(w, http.StatusInternalServerError, fallbackMessage)
|
writeJSONError(w, http.StatusInternalServerError, fallbackMessage)
|
||||||
|
|||||||
Reference in new issue
Block a user