-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.go
34 lines (32 loc) · 938 Bytes
/
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
package main
import (
"fmt"
"net/http"
"time"
)
func (s *Server) ValidateToken(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
token, err := s.GetTokenFromSession(r)
if err != nil {
s.Logger.Println("error getting token from session", err)
w.Header().Set("HX-Redirect", "/access")
http.Error(w, "error getting token from session", http.StatusUnauthorized)
return
}
tk, err := s.GetToken(token)
if err != nil {
s.Logger.Println("error getting token", token)
fmt.Println("erroneous token", token)
http.Error(w, "error getting token", http.StatusUnauthorized)
return
}
if tk.ExpiresAt.Before(time.Now()) {
s.Logger.Println("token expired", tk.ExpiresAt)
w.Header().Set("HX-Redirect", "/access")
http.Error(w, "token expired", http.StatusUnauthorized)
return
}
// s.Logger.Println("token valid")
next.ServeHTTP(w, r)
})
}