-
Notifications
You must be signed in to change notification settings - Fork 4
/
middleware.go
123 lines (103 loc) · 2.69 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
111
112
113
114
115
116
117
118
119
120
121
122
123
package firebase
import (
"fmt"
"net/http"
)
const bearer = "Bearer"
type AuthFunc func(*Token) (bool, error)
func AuthorizationFromParam(req *http.Request) (string, error) {
return req.URL.Query().Get("authorization"), nil
}
func AuthorizationFromHeader(req *http.Request) (string, error) {
header := req.Header.Get("Authorization")
if header == "" {
return "", fmt.Errorf("Authorization header not found")
}
l := len(bearer)
if len(header) > l+1 && header[:l] == bearer {
return header[l+1:], nil
}
return "", fmt.Errorf("Authorization header format must be 'Bearer {token}'")
}
func AuthorizationFromRequest(req *http.Request) (string, error) {
authorization, err := AuthorizationFromParam(req)
if authorization == "" {
authorization, err = AuthorizationFromHeader(req)
if err != nil {
return "", err
}
}
return authorization, nil
}
func (a *Auth) Authorize(h http.Handler, authFn AuthFunc) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
authorization, err := AuthorizationFromRequest(r)
if err != nil {
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
}
// check that it's valid
ctx, err := RequestContext(r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
token, err := a.VerifyIDToken(ctx, authorization)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
ok, err := authFn(token)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if !ok {
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
}
h.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
func (a *Auth) Authenticated(h http.Handler, roles ...string) http.Handler {
return a.Authorize(h, func(token *Token) (bool, error) {
return true, nil
})
}
func (a *Auth) AnyRole(h http.Handler, roles ...string) http.Handler {
return a.Authorize(h, func(token *Token) (bool, error) {
authorized := false
claimedRoles := token.Claims().Get("roles").([]interface{})
for _, role := range roles {
for _, claimedRole := range claimedRoles {
if claimedRole.(string) == role {
authorized = true
break
}
}
if authorized {
break
}
}
return authorized, nil
})
}
func (a *Auth) AllRoles(h http.Handler, roles ...string) http.Handler {
return a.Authorize(h, func(token *Token) (bool, error) {
claimedRoles := token.Claims().Get("roles").([]interface{})
for _, role := range roles {
authorized := false
for _, claimedRole := range claimedRoles {
if claimedRole.(string) == role {
authorized = true
break
}
}
if !authorized {
return false, nil
}
}
return true, nil
})
}