more platform

This commit is contained in:
mats committed 2026-06-29 12:35:32 +02:00
1 parent 8a71e32149
commit 2a66754f9e
27 files changed
+2435 -540

No files matched your search

+35
View File
@@ -0,0 +1,35 @@
package auth
import "net/http"
// BearerMiddleware wraps next, requiring a valid bearer credential from a.
// On success the request is forwarded unchanged; on failure a 401 is
// returned.
func BearerMiddleware(a Authenticator, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if _, err := a.Authenticate(r); err != nil {
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
}
next.ServeHTTP(w, r)
})
}
// WriteMiddleware wraps next, requiring a valid bearer credential from a
// only for mutating HTTP methods (POST, PUT, PATCH, DELETE). Safe methods
// (GET, HEAD, OPTIONS) pass through unauthenticated — suitable for
// services that serve public reads but gate writes behind auth.
func WriteMiddleware(a Authenticator, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet, http.MethodHead, http.MethodOptions:
next.ServeHTTP(w, r)
default:
if _, err := a.Authenticate(r); err != nil {
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
}
next.ServeHTTP(w, r)
}
})
}