Micro-service for file storage and processing written in Go
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
657 B

  1. package middlewares
  2. import (
  3. "net/http"
  4. "strings"
  5. )
  6. type AuthenticationMiddleware struct {
  7. secret string
  8. }
  9. func (middleware AuthenticationMiddleware) Middleware(next http.Handler) http.Handler {
  10. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  11. authToken := r.Header.Get("Authorization")
  12. if authToken == "" || strings.HasPrefix(authToken, "Bearer ") == false || authToken[7:] != middleware.secret {
  13. http.Error(w, "Forbidden", http.StatusForbidden)
  14. } else {
  15. next.ServeHTTP(w, r)
  16. }
  17. })
  18. }
  19. func CreateAuthenticationMiddleware(secret string) AuthenticationMiddleware {
  20. return AuthenticationMiddleware{
  21. secret,
  22. }
  23. }