package middlewares 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, } }