Initial CPONE middleware implementation
This commit is contained in:
30
internal/http/middleware.go
Normal file
30
internal/http/middleware.go
Normal file
@@ -0,0 +1,30 @@
|
||||
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))
|
||||
}
|
||||
Reference in New Issue
Block a user