This repository has been archived on 2026-05-07. You can view files and clone it, but you cannot make any changes to its state, such as pushing and creating new issues, pull requests or comments.
ccrm/server/internal/api/auth.go

27 lines
589 B
Go

package api
import (
"net/http"
"strings"
)
// DashboardAuth middleware validates the dashboard token.
func DashboardAuth(token string) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
auth := r.Header.Get("Authorization")
if auth == "" {
auth = r.URL.Query().Get("token")
}
auth = strings.TrimPrefix(auth, "Bearer ")
if auth != token {
http.Error(w, `{"error":"unauthorized"}`, http.StatusUnauthorized)
return
}
next.ServeHTTP(w, r)
})
}
}