-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
107 lines (91 loc) · 2.78 KB
/
auth.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
package rest
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"github.com/dgrijalva/jwt-go"
"github.com/dgrijalva/jwt-go/request"
)
var (
errInvalidTokenClaims = errors.New("invalid token claims")
guestRole = "gst"
)
type authClaims struct {
UniqueID string `json:"nameid,omitempty"`
Username string `json:"unique_name,omitempty"`
Email string `json:"sub,omitempty"`
Role string `json:"role,omitempty"`
}
func (h *Handler) Authenticator(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
token, err := h.getRequestToken(r); if err != nil {
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
fmt.Println()
return
}
if token == nil || !token.Valid {
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
// Token is authenticated, pass it through
next.ServeHTTP(w, r)
})
}
func (h *Handler) Authorizer(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
token, err := h.getRequestToken(r); if err != nil {
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
fmt.Println()
return
}
claims, err := getClaims(token); if err != nil {
http.Error(w, errInvalidTokenClaims.Error(), http.StatusInternalServerError)
fmt.Println()
return
}
if claims != nil && claims.Role != guestRole {
r.URL.User = url.User(claims.UniqueID)
} else {
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
fmt.Println()
return
}
next.ServeHTTP(w, r)
})
}
func (h *Handler) UserSetter(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
token, err := h.getRequestToken(r); if err != nil {
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
fmt.Println()
return
}
if claims, err := getClaims(token); err == nil && claims != nil && claims.Role != guestRole {
r.URL.User = url.User(claims.UniqueID)
} else {
r.URL.User = nil
}
next.ServeHTTP(w, r)
})
}
func (h *Handler) getRequestToken(r *http.Request) (token *jwt.Token, err error) {
token, err = request.ParseFromRequest(r, request.OAuth2Extractor, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodRSA); ok {
return h.auth.PublicKey(), nil
}
return nil, fmt.Errorf("authenticator: unexpected signing method: %q", token.Header["alg"])
})
return
}
func getClaims(token *jwt.Token) (*authClaims, error) {
payload, err := json.Marshal(token.Claims); if err != nil {
return nil, err
}
claims := &authClaims{}
if err = json.Unmarshal(payload, claims); err != nil {
return nil, err
}
return claims, nil
}