Handle missing SECRET in auth flow
Lint / Lint (push) Successful in 44s
Tests / Go backend (push) Successful in 39s
Docker / Docker Build & Push (push) Successful in 3m30s
Tests / Dashboard checks (push) Successful in 46s

This commit is contained in:
Gabriel20xx committed 2026-09-15 23:27:42 +02:00
1 parent 07e25a3cf3
commit eb67f251d8
4 files changed
+43 -7

No files matched your search

+34 -3
View File
@@ -169,6 +169,7 @@ import (
"errors"
"io"
"net/http"
"os"
"strconv"
"strings"
"time"
@@ -219,6 +220,7 @@ var (
// message rather than a generic "internal error" that would be
// indistinguishable from any other server bug.
errAuthGuardUnavailable = errors.New("Unable to reach the authentication service. Please try again shortly.")
errSecretNotSet = errors.New("SECRET environment variable is not set.")
errSelfServiceProfile = errors.New("Self-service profile editing is only available to administrator accounts; ask an administrator to update this account.")
)
@@ -272,7 +274,8 @@ type Service struct {
// Service (auth turned on, but no URL known yet) still has a non-nil
// client, just one whose calls fail until SetAuthGuardBaseURL supplies a
// real base URL.
authGuard *authclient.Client
authGuard *authclient.Client
configurationError error
}
// ServiceOption configures optional New/NewWithStore construction behavior.
@@ -281,7 +284,8 @@ type Service struct {
type ServiceOption func(*serviceOptions)
type serviceOptions struct {
disabled bool
disabled bool
requireSecret bool
}
// Disabled explicitly builds a Service in auth-disabled mode (see the
@@ -295,6 +299,14 @@ func Disabled() ServiceOption {
return func(o *serviceOptions) { o.disabled = true }
}
// RequireSecret keeps the service available when SECRET is omitted, but makes
// the public auth status/me endpoints report the configuration error so the
// frontend can show it in the auth screen. Internal-service routes remain
// fail-closed until a secret is configured.
func RequireSecret() ServiceOption {
return func(o *serviceOptions) { o.requireSecret = true }
}
// New constructs a Service backed by the media family's shared `settings`
// table (pgSettingsStore) for the login-required toggle and setup-claim
// flag, and by db directly for the unrelated "moderatable platform users"
@@ -336,7 +348,11 @@ func NewWithStore(store ProxyStore, cookieName string, secureCookies bool, authG
// moment one arrives.
client = authclient.New(strings.TrimSpace(authGuardURL), authclient.WithProjectKey(projectKey))
}
return &Service{store: store, cookieName: cookieName, secureCookies: secureCookies, authGuard: client}
var configurationError error
if options.requireSecret && !options.disabled && strings.TrimSpace(os.Getenv("SECRET")) == "" {
configurationError = errSecretNotSet
}
return &Service{store: store, cookieName: cookieName, secureCookies: secureCookies, authGuard: client, configurationError: configurationError}
}
// pgSettingsStore is the default ProxyStore New builds over the media
@@ -479,6 +495,9 @@ func (s *Service) currentProfile(ctx context.Context, token string) (*authclient
// RequireAdminIfLoginRequired) resolves a session through - none of them
// needed to change for this migration, only what CurrentUser itself does.
func (s *Service) CurrentUser(r *http.Request) (*ProxyUser, error) {
if s.configurationError != nil {
return nil, s.configurationError
}
if !s.enabled() {
user := localProfile
return &user, nil
@@ -603,6 +622,10 @@ func IsAdmin(user *ProxyUser) bool {
}
func (s *Service) Me(w http.ResponseWriter, r *http.Request) {
if s.configurationError != nil {
writeProxyError(w, http.StatusInternalServerError, s.configurationError.Error())
return
}
user, err := s.CurrentUser(r)
if err != nil {
writeProxyError(w, http.StatusInternalServerError, errAuthGuardUnavailable.Error())
@@ -682,6 +705,10 @@ func (s *Service) UpdateMe(w http.ResponseWriter, r *http.Request) {
// AdminNeeded answers GET /auth/bootstrap/admin-needed. See the package doc
// comment's "Setup flow" section.
func (s *Service) AdminNeeded(w http.ResponseWriter, r *http.Request) {
if s.configurationError != nil {
writeProxyError(w, http.StatusInternalServerError, s.configurationError.Error())
return
}
if !s.enabled() {
writeProxyJSON(w, http.StatusOK, map[string]bool{"adminNeeded": false})
return
@@ -741,6 +768,10 @@ func (s *Service) SetLoginRequired(ctx context.Context, required bool) error {
}
func (s *Service) Status(w http.ResponseWriter, r *http.Request) {
if s.configurationError != nil {
writeProxyError(w, http.StatusInternalServerError, s.configurationError.Error())
return
}
if !s.enabled() {
user := localProfile
writeProxyJSON(w, http.StatusOK, map[string]any{"setupRequired": false, "authenticated": true, "authRequired": true, "user": &user})