This repository has been archived by the owner on Mar 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtokens.go
335 lines (299 loc) · 8.49 KB
/
tokens.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// This Source Code Form is subject to the terms of the Mozilla Public
// License, version 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package oauth2
import (
"log"
"net/http"
"path"
"strings"
"github.com/hooklift/oauth2/internal/render"
"github.com/hooklift/oauth2/types"
)
// TokenHandlers is a map to functions where each function handles a particular HTTP
// verb or method.
var TokenHandlers map[string]func(http.ResponseWriter, *http.Request, config) = map[string]func(http.ResponseWriter, *http.Request, config){
"POST": IssueToken,
"DELETE": RevokeToken,
}
// IssueToken handles all requests going to tokens endpoint.
func IssueToken(w http.ResponseWriter, req *http.Request, cfg config) {
provider := cfg.provider
username, password, ok := req.BasicAuth()
cinfo, err := provider.AuthenticateClient(username, password)
if !ok || err != nil {
render.JSON(w, render.Options{
Status: http.StatusBadRequest,
Data: ErrUnauthorizedClient,
})
return
}
grantType := req.FormValue("grant_type")
switch grantType {
case "authorization_code":
authCodeGrant2(w, req, cfg, cinfo)
case "client_credentials":
clientCredentialsGrant(w, req, cfg, cinfo)
case "password":
resourceOwnerCredentialsGrant(w, req, cfg, cinfo)
case "refresh_token":
refreshToken(w, req, cfg, cinfo)
default:
render.JSON(w, render.Options{
Status: http.StatusBadRequest,
Data: ErrUnsupportedGrantType,
})
return
}
}
// Implements http://tools.ietf.org/html/rfc6749#section-4.1.3,
// http://tools.ietf.org/html/rfc6749#section-4.1.4 and
// http://tools.ietf.org/html/rfc6749#section-5.2
//
// Implementation notes:
// * Ignores client_id as we are always requiring the client to authenticate
// * Ignores redirect_uri as we force a static and pre-registered redirect URI for the client
func authCodeGrant2(w http.ResponseWriter, req *http.Request, cfg config, cinfo types.Client) {
provider := cfg.provider
code := req.FormValue("code")
if code == "" {
err := ErrUnauthorizedClient
err.Description = "Authorization code can't be empty."
render.JSON(w, render.Options{
Status: http.StatusBadRequest,
Data: ErrUnauthorizedClient,
})
return
}
grant, err := provider.GrantInfo(code)
if err != nil {
e := ErrInvalidGrant
e.Description = err.Error()
render.JSON(w, render.Options{
Status: http.StatusBadRequest,
Data: e,
})
return
}
if grant.Status == types.GrantRevoked ||
grant.Status == types.GrantExpired ||
grant.Status == types.GrantUsed {
e := ErrInvalidGrant
e.Description = "Grant code was revoked, expired or already used."
render.JSON(w, render.Options{
Status: http.StatusBadRequest,
Data: e,
})
return
}
if cinfo.RedirectURL.String() != grant.RedirectURL.String() {
e := ErrInvalidGrant
e.Description = "Grant code was generated for a different redirect URI."
render.JSON(w, render.Options{
Status: http.StatusBadRequest,
Data: e,
})
return
}
// This should not happen if the provider is doing its work properly but we are
// checking anyways.
if grant.ClientID != cinfo.ID {
e := ErrInvalidGrant
e.Description = "Grant code was generated for a different client ID."
render.JSON(w, render.Options{
Status: http.StatusBadRequest,
Data: e,
})
return
}
token, err := provider.GenToken(grant, cinfo, true, cfg.tokenExpiration)
if err != nil {
render.JSON(w, render.Options{
Status: http.StatusInternalServerError,
Data: ErrServerError("", err),
})
return
}
render.JSON(w, render.Options{
Status: http.StatusOK,
Data: token,
})
}
// Implements http://tools.ietf.org/html/rfc6749#section-4.3
func resourceOwnerCredentialsGrant(w http.ResponseWriter, req *http.Request, cfg config, cinfo types.Client) {
provider := cfg.provider
if ok := provider.AuthenticateUser(req.FormValue("username"), req.FormValue("password")); !ok {
render.JSON(w, render.Options{
Status: http.StatusBadRequest,
Data: ErrUnathorizedUser,
})
return
}
scope := req.FormValue("scope")
var scopes types.Scopes
if scope != "" {
var err error
scopes, err = provider.ScopesInfo(scope)
if err != nil {
render.JSON(w, render.Options{
Status: http.StatusBadRequest,
Data: ErrServerError("", err),
})
return
}
}
noAuthzGrant := types.Grant{
Scopes: scopes,
}
token, err := provider.GenToken(noAuthzGrant, cinfo, true, cfg.tokenExpiration)
if err != nil {
render.JSON(w, render.Options{
Status: http.StatusInternalServerError,
Data: ErrServerError("", err),
})
return
}
render.JSON(w, render.Options{
Status: http.StatusOK,
Data: token,
})
}
// Implements http://tools.ietf.org/html/rfc6749#section-4.4
func clientCredentialsGrant(w http.ResponseWriter, req *http.Request, cfg config, cinfo types.Client) {
provider := cfg.provider
scope := req.FormValue("scope")
var scopes types.Scopes
if scope != "" {
var err error
scopes, err = provider.ScopesInfo(scope)
if err != nil {
render.JSON(w, render.Options{
Status: http.StatusBadRequest,
Data: ErrServerError("", err),
})
return
}
}
noAuthzGrant := types.Grant{
Scopes: scopes,
}
token, err := provider.GenToken(noAuthzGrant, cinfo, false, cfg.tokenExpiration)
if err != nil {
render.JSON(w, render.Options{
Status: http.StatusInternalServerError,
Data: ErrServerError("", err),
})
return
}
render.JSON(w, render.Options{
Status: http.StatusOK,
Data: token,
})
}
// Implements http://tools.ietf.org/html/rfc6749#section-6
func refreshToken(w http.ResponseWriter, req *http.Request, cfg config, cinfo types.Client) {
provider := cfg.provider
code := req.FormValue("refresh_token")
token, err := provider.TokenInfo(code)
if err != nil {
render.JSON(w, render.Options{
Status: http.StatusInternalServerError,
Data: ErrServerError("", err),
})
return
}
scope := req.FormValue("scope")
var scopes types.Scopes
if scope != "" {
var err error
scopes, err = provider.ScopesInfo(scope)
if err != nil {
render.JSON(w, render.Options{
Status: http.StatusInternalServerError,
Data: ErrServerError("", err),
})
return
}
// The requested scope MUST NOT include any scope not originally granted
// by the resource owner, and if omitted is treated as equal to the scope
// originally granted by the resource owner.
tscopes := token.Scopes.Encode()
for _, s := range scopes {
// TODO(c4milo): make more robust
if !strings.Contains(tscopes, s.ID) {
render.JSON(w, render.Options{
Status: http.StatusBadRequest,
Data: ErrInvalidScope,
})
return
}
}
}
if len(scopes) == 0 {
scopes = token.Scopes
}
if token.ClientID != cinfo.ID {
render.JSON(w, render.Options{
Status: http.StatusBadRequest,
Data: ErrClientIDMismatch,
})
return
}
newToken, err := provider.RefreshToken(token, scopes)
if err != nil {
render.JSON(w, render.Options{
Status: http.StatusInternalServerError,
Data: ErrServerError("", err),
})
return
}
render.JSON(w, render.Options{
Status: http.StatusOK,
Data: newToken,
})
}
// Implements https://tools.ietf.org/html/rfc7009
// It does not take into account token_type_hint as the common use case is to
// have access and refresh tokens uniquely identified throughout the system. That said,
// unsupported_token_type error responses are not produced by this implementation either.
func RevokeToken(w http.ResponseWriter, req *http.Request, cfg config) {
provider := cfg.provider
username, password, ok := req.BasicAuth()
cinfo, err := provider.AuthenticateClient(username, password)
if !ok || err != nil {
// TODO(c4milo): verify other implementations to see if they reply
// with 401 instead of 400. Spec is sort of contradictory in this regard.
render.JSON(w, render.Options{
Status: http.StatusBadRequest,
Data: ErrUnauthorizedClient,
})
return
}
token := path.Base(req.URL.Path)
tokenInfo, err := provider.TokenInfo(token)
if err != nil {
log.Printf("[ERROR] Error getting token info: %+v", err)
render.JSON(w, render.Options{
Status: http.StatusServiceUnavailable,
})
return
}
if tokenInfo.ClientID != cinfo.ID {
render.JSON(w, render.Options{
Status: http.StatusBadRequest,
Data: ErrClientIDMismatch,
})
return
}
err = provider.RevokeToken(token)
if err != nil {
log.Printf("[ERROR] Error revoking token: %+v", err)
render.JSON(w, render.Options{
Status: http.StatusServiceUnavailable,
})
return
}
render.JSON(w, render.Options{
Status: http.StatusOK,
})
}