This repository has been archived by the owner on Sep 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
middleware.go
110 lines (94 loc) · 2.87 KB
/
middleware.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package controllers
import (
"fmt"
"net"
"net/http"
"strings"
"time"
"github.com/cyruzin/feelthemovies/internal/app/config"
"github.com/cyruzin/feelthemovies/internal/pkg/errhandler"
"github.com/cyruzin/feelthemovies/internal/pkg/ratelimit"
jwt "github.com/dgrijalva/jwt-go"
)
// LoggerMiddleware logs the details of all requests.
func (s *Setup) LoggerMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
s.logger.Infow(
"request_logging",
"method", r.Method,
"url", r.URL.String(),
"agent", r.UserAgent(),
"referer", r.Referer(),
"proto", r.Proto,
"remote_address", r.RemoteAddr,
"latency", time.Since(start),
)
next.ServeHTTP(w, r)
})
}
// AuthMiddleware checks if the request contains Bearer Token
// on the headers and if it is valid.
func (s *Setup) AuthMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Capturing Authorizathion header.
tokenHeader := r.Header.Get("Authorization")
// Checking if the value is empty.
if tokenHeader == "" {
errhandler.DecodeError(w, r, s.logger, errEmptyToken, http.StatusBadRequest)
return
}
// Checking if the header contains Bearer string and if the token exists.
if !strings.Contains(tokenHeader, "Bearer") || len(strings.Split(tokenHeader, "Bearer ")) == 1 {
errhandler.DecodeError(w, r, s.logger, errMalformedToken, http.StatusUnauthorized)
return
}
// Capturing the token.
jwtString := strings.Split(tokenHeader, "Bearer ")[1]
// Parsing the token to verify its authenticity.
token, err := jwt.Parse(jwtString, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
}
cfg, err := config.Load()
if err != nil {
return nil, err
}
return []byte(cfg.JWTSecret), nil
})
// Returning parsing errors.
if err != nil {
errhandler.DecodeError(w, r, s.logger, err.Error(), http.StatusUnauthorized)
return
}
// If the token is valid.
if token.Valid {
next.ServeHTTP(w, r)
} else {
errhandler.DecodeError(w, r, s.logger, errInvalidJWTToken, http.StatusUnauthorized)
return
}
})
}
// RateLimit middleware handles the rate limiting.
func (s *Setup) RateLimit(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ip, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
errhandler.DecodeError(w, r, s.logger, errInternal, http.StatusInternalServerError)
return
}
limiter := ratelimit.GetVisitor(ip)
if !limiter.Allow() {
errhandler.DecodeError(
w,
r,
s.logger,
http.StatusText(http.StatusTooManyRequests),
http.StatusTooManyRequests,
)
return
}
next.ServeHTTP(w, r)
})
}