Skip to content

Commit 3e1fbb2

Browse files
Extract HTTP error message constants in authz server
- Add constants for common HTTP error messages (unauthorized, internal error, forbidden) - Replace hardcoded HTTP error strings with named constants for consistency - Improve maintainability and reduce string duplication across handlers - Address goconst linter warnings for repeated error responses Error constants added: - errUnauthorized: for 401 responses - errInternalError: for 500 responses - errForbidden: for 403 responses This ensures consistent error messaging across all HTTP handlers and makes error response changes easier to maintain. Co-authored-by: Amp <[email protected]> Amp-Thread-ID: https://ampcode.com/threads/T-5be4213f-26eb-400c-bb7b-d4c79b7ee6fe
1 parent 5a135af commit 3e1fbb2

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

services/authz/server/server.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ const (
6464
errInvalidPEM = "invalid pem"
6565
errMissingToken = "missing token"
6666
errInvalidTokenFormat = "invalid token format"
67+
errUnauthorized = "unauthorized"
68+
errInternalError = "internal error"
69+
errForbidden = "forbidden"
6770

6871
// Tailscale network constants
6972
tailscaleIP1 = 100
@@ -312,14 +315,14 @@ func (s *Server) verifyHandler(w http.ResponseWriter, r *http.Request) {
312315

313316
claims, err := token.VerifyGoogleJWT(r.Context(), req.Token, s.cfg.GoogleClientID)
314317
if err != nil {
315-
http.Error(w, "unauthorized", http.StatusUnauthorized)
318+
http.Error(w, errUnauthorized, http.StatusUnauthorized)
316319
return
317320
}
318321

319322
decision, err := s.evaluateOPA(r.Context(), claims, req.DeviceID, req.ClientIP)
320323
if err != nil {
321324
log.Printf("OPA eval error: %v", err)
322-
http.Error(w, "internal error", http.StatusInternalServerError)
325+
http.Error(w, errInternalError, http.StatusInternalServerError)
323326
return
324327
}
325328

@@ -356,14 +359,14 @@ func (s *Server) envoyAuthHandler(w http.ResponseWriter, r *http.Request) {
356359

357360
claims, err := s.validateBearerToken(r.Context(), authHeader)
358361
if err != nil {
359-
http.Error(w, "unauthorized", http.StatusUnauthorized)
362+
http.Error(w, errUnauthorized, http.StatusUnauthorized)
360363
return
361364
}
362365

363366
decision, err := s.evaluateOPA(r.Context(), claims, deviceID, clientIP)
364367
if err != nil {
365368
log.Printf("OPA eval error: %v", err)
366-
http.Error(w, "internal error", http.StatusInternalServerError)
369+
http.Error(w, errInternalError, http.StatusInternalServerError)
367370
return
368371
}
369372

@@ -390,7 +393,7 @@ func (s *Server) envoyAuthHandler(w http.ResponseWriter, r *http.Request) {
390393
log.Printf("failed to encode envoy auth response: %v", err)
391394
}
392395
default:
393-
http.Error(w, "forbidden", http.StatusForbidden)
396+
http.Error(w, errForbidden, http.StatusForbidden)
394397
}
395398
}
396399

0 commit comments

Comments
 (0)