/
authorized.go
224 lines (185 loc) · 5.89 KB
/
authorized.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Package middleware defines HTTP middleware such as those used for user
// validation.
package middleware
import (
"github.com/cyclopsci/apollo"
"github.com/serdmanczyk/freyr/models"
"github.com/serdmanczyk/freyr/oauth"
"github.com/serdmanczyk/freyr/token"
"golang.org/x/net/context"
"net/http"
"strconv"
"time"
)
// Constant definitions for authorization type headers and
// header values.
const (
APIAuthTypeValue = "API"
DeviceAuthTypeValue = "DEVICE"
AuthTypeHeader = "X-FREYR-AUTHTYPE"
TokenHeader = "X-FREYR-TOKEN"
AuthUserHeader = "X-FREYR-USER"
APIAuthDateHeader = "X-FREYR-DATETIME"
APISignatureHeader = "X-FREYR-SIGNATURE"
)
// Authorizer is an interface that represents types capable of validating
// if an HTTP request is authorized, and returning a context indicating the
// authorized user
type Authorizer interface {
Authorize(ctx context.Context, r *http.Request) context.Context
}
// Authorize returns a piece of middleware that will verify if the request
// is authorized by and of the Authorizers passed in before calling subsequent
// handlers.
func Authorize(auths ...Authorizer) apollo.Constructor {
return apollo.Constructor(func(next apollo.Handler) apollo.Handler {
return apollo.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
for _, auth := range auths {
authCtx := auth.Authorize(ctx, r)
if authCtx != nil {
next.ServeHTTP(authCtx, w, r)
return
}
}
http.Error(w, "Request not authorized", http.StatusUnauthorized)
})
})
}
// WebAuthorizer is used to verify if requests are signed in a manner
// expected users accessing the api via a web browser.
type WebAuthorizer struct {
tokenStore token.JWTTokenGen
}
// NewWebAuthorizer generates a new *WebAuthorizer
func NewWebAuthorizer(tS token.JWTTokenGen) *WebAuthorizer {
return &WebAuthorizer{tokenStore: tS}
}
// Authorize validates the request contains a cookie placed by the system's
// oauth handler and that the cookie has a valid signature signed by the
// master token.JtwTokenGen
func (u *WebAuthorizer) Authorize(ctx context.Context, r *http.Request) context.Context {
cookie, err := r.Cookie(oauth.CookieName)
if err != nil {
return nil
}
claims, err := u.tokenStore.ValidateToken(cookie.Value)
if err != nil {
return nil
}
userEmail, ok := claims["email"].(string)
if !ok {
return nil
}
return context.WithValue(ctx, "email", userEmail)
}
// APIAuthorizer is a type used to validate requests were signed in
// the manner expected of API style requests.
type APIAuthorizer struct {
secretStore models.SecretStore
}
// NewAPIAuthorizer returns a new APIAuthorizer
func NewAPIAuthorizer(ss models.SecretStore) *APIAuthorizer {
return &APIAuthorizer{secretStore: ss}
}
// Authorize validates that a request has been signed with a user's secret
func (a *APIAuthorizer) Authorize(ctx context.Context, r *http.Request) context.Context {
authType := r.Header.Get(AuthTypeHeader)
if authType != APIAuthTypeValue {
return nil
}
signature := r.Header.Get(APISignatureHeader)
if signature == "" {
return nil
}
userEmail, signingString := apiSigningString(r)
if userEmail == "" {
return nil
}
userSecret, err := a.secretStore.GetSecret(userEmail)
if err != nil {
return nil
}
if !userSecret.Verify(signingString, signature) {
return nil
}
return context.WithValue(ctx, "email", userEmail)
}
func apiSigningString(r *http.Request) (userEmail string, signinString string) {
datetime := r.Header.Get(APIAuthDateHeader)
user := r.Header.Get(AuthUserHeader)
if datetime == "" || user == "" {
return
}
timeInt, err := strconv.ParseInt(datetime, 10, 64)
if err != nil {
return
}
if timeInt < time.Now().Add(time.Second*-5).Unix() || timeInt > time.Now().Add(time.Second*5).Unix() {
return
}
userEmail = user
if r.Method == "POST" {
signinString = r.Method + r.URL.RawPath + datetime + user + strconv.FormatInt(r.ContentLength, 10)
} else {
signinString = r.Method + r.URL.RawPath + datetime + user
}
return
}
// SignRequest builds a signing-string from the request's content and signs
// it with the given secret.
func SignRequest(s models.Secret, userEmail string, r *http.Request) {
r.Header.Add(AuthTypeHeader, APIAuthTypeValue)
r.Header.Add(AuthUserHeader, userEmail)
n := time.Now().Unix()
unixStamp := strconv.FormatInt(n, 10)
r.Header.Add(APIAuthDateHeader, unixStamp)
_, signingString := apiSigningString(r)
signature := s.Sign(signingString)
r.Header.Add(APISignatureHeader, signature)
}
// DeviceAuthorizer is a type used to verify that a request was signed in the
// manner specified for requests from a device.
type DeviceAuthorizer struct {
secretStore models.SecretStore
}
// NewDeviceAuthorizer returns a new *DeviceAuthorizer
func NewDeviceAuthorizer(ss models.SecretStore) *DeviceAuthorizer {
return &DeviceAuthorizer{secretStore: ss}
}
// Authorize validates a a valid JWT signature header is present signed by
// the user's secret and that the content in the signature matches the headers
// describing the user and core on who's behalf the request was made.
func (d *DeviceAuthorizer) Authorize(ctx context.Context, r *http.Request) context.Context {
authType := r.Header.Get(AuthTypeHeader)
if authType != DeviceAuthTypeValue {
return nil
}
jwtTokenString := r.Header.Get(TokenHeader)
if jwtTokenString == "" {
return nil
}
requestUserEmail := r.Header.Get(AuthUserHeader)
if requestUserEmail == "" {
return nil
}
requestCoreID := r.PostFormValue("coreid")
if requestCoreID == "" {
return nil
}
claims, err := token.ValidateUserToken(d.secretStore, jwtTokenString)
if err != nil {
return nil
}
tokenCoreID, ok := claims["coreid"].(string)
if !ok {
return nil
}
tokenUserEmail, ok := claims["email"].(string)
if !ok {
return nil
}
if tokenCoreID != requestCoreID || tokenUserEmail != requestUserEmail {
return nil
}
return context.WithValue(ctx, "email", requestUserEmail)
}