forked from FabianVowie/Lithium
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
650 B
28 lines
650 B
package auth
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
type AuthenticationMiddleware struct {
|
|
secret string
|
|
}
|
|
|
|
func (middleware AuthenticationMiddleware) Middleware(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
authToken := r.Header.Get("Authorization")
|
|
|
|
if authToken == "" || strings.HasPrefix(authToken, "Bearer ") == false || authToken[7:] != middleware.secret {
|
|
http.Error(w, "Forbidden", http.StatusForbidden)
|
|
} else {
|
|
next.ServeHTTP(w, r)
|
|
}
|
|
})
|
|
}
|
|
|
|
func CreateAuthenticationMiddleware(secret string) AuthenticationMiddleware {
|
|
return AuthenticationMiddleware{
|
|
secret,
|
|
}
|
|
}
|