-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.go
More file actions
71 lines (64 loc) · 1.76 KB
/
middleware.go
File metadata and controls
71 lines (64 loc) · 1.76 KB
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
package main
import (
"context"
"net/http"
"strings"
"github.com/golang-jwt/jwt/v4"
)
type key int
const (
ctxUserKey key = iota
)
func WithCORS(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
if r.Method == http.MethodOptions {
w.WriteHeader(http.StatusOK)
return
}
next.ServeHTTP(w, r)
})
}
func WithAuth(secret string, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
authHeader := r.Header.Get("Authorization")
if authHeader == "" {
http.Error(w, "missing authorization header", http.StatusUnauthorized)
return
}
parts := strings.SplitN(authHeader, " ", 2)
if len(parts) != 2 || parts[0] != "Bearer" {
http.Error(w, "invalid authorization header", http.StatusUnauthorized)
return
}
tokStr := parts[1]
tkn, err := jwt.Parse(tokStr, func(token *jwt.Token) (interface{}, error) {
return []byte(secret), nil
})
if err != nil || !tkn.Valid {
http.Error(w, "invalid token", http.StatusUnauthorized)
return
}
claims, ok := tkn.Claims.(jwt.MapClaims)
if !ok {
http.Error(w, "invalid token claims", http.StatusUnauthorized)
return
}
userID, _ := claims["userId"].(string)
// Inject user id into context for handlers
r = r.WithContext(context.WithValue(r.Context(), ctxUserKey, userID))
next.ServeHTTP(w, r)
})
}
func GetContextUserID(r *http.Request) string {
v := r.Context().Value(ctxUserKey)
if v == nil {
return ""
}
if s, ok := v.(string); ok {
return s
}
return ""
}