31 lines
617 B
Go
31 lines
617 B
Go
package http
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
func BearerAuth(staticToken string, next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if bearerToken(r.Header.Get("Authorization")) != staticToken {
|
|
WriteJSON(w, http.StatusUnauthorized, Response{
|
|
Success: false,
|
|
Message: "Unauthenticated.",
|
|
Data: nil,
|
|
})
|
|
return
|
|
}
|
|
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|
|
|
|
func bearerToken(header string) string {
|
|
const prefix = "Bearer "
|
|
if !strings.HasPrefix(header, prefix) {
|
|
return ""
|
|
}
|
|
|
|
return strings.TrimSpace(strings.TrimPrefix(header, prefix))
|
|
}
|