29 lines
534 B
Go
29 lines
534 B
Go
package main
|
|
|
|
import (
|
|
"net/http"
|
|
"os"
|
|
)
|
|
|
|
func authMiddleware(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
expectedKey := os.Getenv("FRONTEND_API_KEY")
|
|
if expectedKey == "" {
|
|
http.Error(w, "server misconfigured", 500)
|
|
return
|
|
}
|
|
|
|
authHeader := r.Header.Get("Authorization")
|
|
|
|
expectedHeader := "Bearer " + expectedKey
|
|
|
|
if authHeader != expectedHeader {
|
|
http.Error(w, "unauthorized", http.StatusUnauthorized)
|
|
return
|
|
}
|
|
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|