Merge media-webui/media-backend into the general shared component/backend system
Extracts a new backend/authproxy package for the AuthGuard-proxy helper
functions (session cookie handling, input normalization, error
translation) that Bitmagnet/GameStream/media-backend/AutoCode/
CollabTable/PubliSafe/SimpleCollection/SyncServer/UnraidMonitor/
OutdoorGames were each independently reimplementing, and repoints all
of them onto it.
Extends packages/web-ui's Button/Badge with the variants media-webui
needed (outline, icon size, primary badge) and promotes EmptyState/
SectionTitle/Metric/StatusWidget/StatusDialog into the shared library,
then migrates media-webui off its own diverged ui/{button,card,badge,
metric} components and local duplicates of the promoted components
onto @gabriel20xx/web-ui, so media-webui is a proper consumer of the
same design system every other project already uses instead of a
parallel fork.
Also removes the dead `desktop`/MediaDesktop service variant from
media-backend and media-webui (MediaDesktop is an Electron client that
never actually runs this stack).
This commit is contained in:
1 parent
9589d07baf
commit
7b5a4e6c2c
49 files changed
+872
-726
No files matched your search
@@ -0,0 +1,135 @@
|
||||
// Package authproxy holds small helpers shared by every Go service in this
|
||||
// workspace that proxies its own /auth/... (or /api/auth/...) HTTP surface
|
||||
// to a centralized AuthGuard instance via shared/backend/authclient, instead
|
||||
// of embedding its own local user table.
|
||||
//
|
||||
// Three services independently reimplemented the same handful of pieces
|
||||
// below before this package existed (Forks/Bitmagnet/backend/internal/auth/service.go,
|
||||
// Original/GameStream/backend/cmd/gamestreamd/auth.go,
|
||||
// Central/shared/media-backend/internal/auth/auth.go — read any one of
|
||||
// their top-of-file doc comments for the fuller AuthGuard-migration
|
||||
// rationale: session cookie forwarding, the local one-time setup-claim
|
||||
// flag, "auth disabled" mode, etc.). Those pieces stay project-specific
|
||||
// (they depend on each project's own storage/framework/response shape) —
|
||||
// only the pieces below were duplicated near-verbatim everywhere:
|
||||
//
|
||||
// - SessionToken / ClearSessionCookies: reading and clearing AuthGuard's
|
||||
// own session cookie is identical logic in every caller, since none of
|
||||
// them mint a session token of their own anymore.
|
||||
// - NormalizeUsername / NormalizeEmail / ValidPassword: the same
|
||||
// trim+shape checks, just with different max/min lengths and (kept
|
||||
// deliberately un-touched by this package) different caller-owned
|
||||
// error messages — see each function's doc comment for why this
|
||||
// package returns a plain (value, bool) rather than an error.
|
||||
// - APIErrorResponse: unwrapping an authclient.APIError into the
|
||||
// (status code, message) pair every caller's own writeError-equivalent
|
||||
// forwards close to verbatim.
|
||||
package authproxy
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
authclient "repos.gfranz.ch/Gabriel20xx/shared/backend/authclient"
|
||||
)
|
||||
|
||||
// CookieReader is the minimal interface SessionToken needs from an incoming
|
||||
// request — just *http.Request.Cookie's signature, not the whole request.
|
||||
// *http.Request satisfies this trivially; a caller (like Bitmagnet's own
|
||||
// internal/auth.HTTPContext) that deliberately abstracts its request behind
|
||||
// a narrower interface of its own, e.g. for testability, can pass that
|
||||
// directly instead of needing a concrete *http.Request.
|
||||
type CookieReader interface {
|
||||
Cookie(name string) (*http.Cookie, error)
|
||||
}
|
||||
|
||||
// SessionToken reads AuthGuard's own session cookie
|
||||
// (authclient.DefaultCookieName) off an incoming request, returning "" if
|
||||
// it is absent or empty. Every AuthGuard-proxying service in this workspace
|
||||
// mints no session token of its own — AuthGuard's cookie *is* the session —
|
||||
// so this same read is duplicated in every one of them.
|
||||
func SessionToken(r CookieReader) string {
|
||||
cookie, err := r.Cookie(authclient.DefaultCookieName)
|
||||
if err != nil || cookie.Value == "" {
|
||||
return ""
|
||||
}
|
||||
return cookie.Value
|
||||
}
|
||||
|
||||
// ClearSessionCookies clears AuthGuard's own session cookie plus one
|
||||
// legacy, pre-migration cookie name a caller's browser might still be
|
||||
// holding (each project's own cookie name from before it adopted
|
||||
// AuthGuard — e.g. "gamestream_session", "bitmagnet_session",
|
||||
// "mediacollection_session"; pass "" if a given caller has none). Normally
|
||||
// AuthGuard's own Set-Cookie header, forwarded via authclient.ForwardCookies,
|
||||
// already clears its cookie on logout — every project's own Logout handler
|
||||
// also calls this explicitly as a fallback and to clear the legacy cookie,
|
||||
// which AuthGuard has no way to know about.
|
||||
func ClearSessionCookies(w http.ResponseWriter, legacyCookieName string, secure bool) {
|
||||
http.SetCookie(w, &http.Cookie{
|
||||
Name: authclient.DefaultCookieName, Value: "", Path: "/", HttpOnly: true,
|
||||
Secure: secure, SameSite: http.SameSiteLaxMode, MaxAge: -1,
|
||||
})
|
||||
if legacyCookieName != "" && legacyCookieName != authclient.DefaultCookieName {
|
||||
http.SetCookie(w, &http.Cookie{
|
||||
Name: legacyCookieName, Value: "", Path: "/", HttpOnly: true,
|
||||
Secure: secure, SameSite: http.SameSiteLaxMode, MaxAge: -1,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// NormalizeUsername reports whether value, once trimmed, is non-empty and
|
||||
// no longer than maxLen runes, and returns the trimmed value either way.
|
||||
//
|
||||
// This returns (value, bool) rather than (value, error) deliberately: every
|
||||
// existing caller already owns its own error value/message for this
|
||||
// failure (e.g. "username is required", "invalid authentication input:
|
||||
// username must be between 1 and 64 characters") as part of its public API
|
||||
// response shape, and unifying those messages was not part of what made
|
||||
// this duplicated — only the trim+shape check was. A caller with "empty
|
||||
// means leave this field unchanged" update semantics (a partial-update
|
||||
// endpoint) should special-case an empty value itself before calling this,
|
||||
// since this function always reports false for one.
|
||||
func NormalizeUsername(value string, maxLen int) (normalized string, ok bool) {
|
||||
normalized = strings.TrimSpace(value)
|
||||
return normalized, normalized != "" && utf8.RuneCountInString(normalized) <= maxLen
|
||||
}
|
||||
|
||||
// NormalizeEmail reports whether value, once lowercased and trimmed, looks
|
||||
// like a valid email address (exactly one "@", a non-empty local part, and
|
||||
// a domain part containing at least one ".") and is no longer than maxLen
|
||||
// runes, and returns the normalized value either way. Same (value, bool)
|
||||
// shape and rationale as NormalizeUsername.
|
||||
func NormalizeEmail(value string, maxLen int) (normalized string, ok bool) {
|
||||
normalized = strings.ToLower(strings.TrimSpace(value))
|
||||
if normalized == "" || utf8.RuneCountInString(normalized) > maxLen || strings.Count(normalized, "@") != 1 {
|
||||
return normalized, false
|
||||
}
|
||||
local, domain, _ := strings.Cut(normalized, "@")
|
||||
if local == "" || domain == "" || !strings.Contains(domain, ".") {
|
||||
return normalized, false
|
||||
}
|
||||
return normalized, true
|
||||
}
|
||||
|
||||
// ValidPassword reports whether password is at least minLen runes long.
|
||||
func ValidPassword(password string, minLen int) bool {
|
||||
return utf8.RuneCountInString(password) >= minLen
|
||||
}
|
||||
|
||||
// APIErrorResponse extracts the HTTP status code and message an
|
||||
// authclient.APIError carries, for a caller's own writeError-equivalent to
|
||||
// forward. ok is false when err is not an *authclient.APIError (a
|
||||
// transport failure actually reaching AuthGuard, as opposed to AuthGuard
|
||||
// giving a real answer the caller didn't like) — callers should fall back
|
||||
// to a generic 500 with their own message in that case, matching every
|
||||
// existing writeAuthGuardError implementation's behavior.
|
||||
func APIErrorResponse(err error) (statusCode int, message string, ok bool) {
|
||||
var apiErr *authclient.APIError
|
||||
if errors.As(err, &apiErr) {
|
||||
return apiErr.StatusCode, apiErr.Message, true
|
||||
}
|
||||
return 0, "", false
|
||||
}
|
||||
Reference in new issue
Block a user