forked from ory/hydra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
420 lines (384 loc) · 14.6 KB
/
handler.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
* Copyright © 2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @author Aeneas Rekkas <aeneas+oss@aeneas.io>
* @Copyright 2017-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
* @license Apache-2.0
*/
package consent
import (
"encoding/json"
"net/http"
"net/url"
"time"
"github.com/julienschmidt/httprouter"
"github.com/ory/go-convenience/urlx"
"github.com/ory/herodot"
"github.com/pkg/errors"
)
type Handler struct {
H herodot.Writer
M Manager
RequestMaxAge time.Duration
}
const (
LoginPath = "/oauth2/auth/requests/login"
ConsentPath = "/oauth2/auth/requests/consent"
)
func NewHandler(
h herodot.Writer,
m Manager,
) *Handler {
return &Handler{
H: h,
M: m,
}
}
func (h *Handler) SetRoutes(r *httprouter.Router) {
r.GET(LoginPath+"/:challenge", h.GetLoginRequest)
r.PUT(LoginPath+"/:challenge/accept", h.AcceptLoginRequest)
r.PUT(LoginPath+"/:challenge/reject", h.RejectLoginRequest)
r.GET(ConsentPath+"/:challenge", h.GetConsentRequest)
r.PUT(ConsentPath+"/:challenge/accept", h.AcceptConsentRequest)
r.PUT(ConsentPath+"/:challenge/reject", h.RejectConsentRequest)
}
// swagger:route GET /oauth2/auth/requests/login/{challenge} oAuth2 getLoginRequest
//
// Get an login request
//
// When an authorization code, hybrid, or implicit OAuth 2.0 Flow is initiated, ORY Hydra asks the login provider
// (sometimes called "identity provider") to authenticate the user and then tell ORY Hydra now about it. The login
// provider is an web-app you write and host, and it must be able to authenticate ("show the user a login screen")
// a user (in OAuth2 the proper name for user is "resource owner").
//
// The authentication challenge is appended to the login provider URL to which the user's user-agent (browser) is redirected to. The login
// provider uses that challenge to fetch information on the OAuth2 request and then accept or reject the requested authentication process.
//
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Schemes: http, https
//
// Responses:
// 200: loginRequest
// 401: genericError
// 500: genericError
func (h *Handler) GetLoginRequest(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
request, err := h.M.GetAuthenticationRequest(ps.ByName("challenge"))
if err != nil {
h.H.WriteError(w, r, err)
return
}
request.Client = sanitizeClient(request.Client)
h.H.Write(w, r, request)
}
// swagger:route PUT /oauth2/auth/requests/login/{challenge}/accept oAuth2 acceptLoginRequest
//
// Accept an login request
//
// When an authorization code, hybrid, or implicit OAuth 2.0 Flow is initiated, ORY Hydra asks the login provider
// (sometimes called "identity provider") to authenticate the user and then tell ORY Hydra now about it. The login
// provider is an web-app you write and host, and it must be able to authenticate ("show the user a login screen")
// a user (in OAuth2 the proper name for user is "resource owner").
//
// The authentication challenge is appended to the login provider URL to which the user's user-agent (browser) is redirected to. The login
// provider uses that challenge to fetch information on the OAuth2 request and then accept or reject the requested authentication process.
//
// This endpoint tells ORY Hydra that the user has successfully authenticated and includes additional information such as
// the user's ID and if ORY Hydra should remember the user's user agent for future authentication attempts by setting
// a cookie.
//
// The response contains a redirect URL which the login provider should redirect the user-agent to.
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Schemes: http, https
//
// Responses:
// 200: completedRequest
// 401: genericError
// 500: genericError
func (h *Handler) AcceptLoginRequest(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var p HandledAuthenticationRequest
d := json.NewDecoder(r.Body)
d.DisallowUnknownFields()
if err := d.Decode(&p); err != nil {
h.H.WriteErrorCode(w, r, http.StatusBadRequest, errors.WithStack(err))
return
}
p.Challenge = ps.ByName("challenge")
ar, err := h.M.GetAuthenticationRequest(ps.ByName("challenge"))
if err != nil {
h.H.WriteError(w, r, err)
return
} else if ar.Subject != "" && p.Subject != ar.Subject {
h.H.WriteErrorCode(w, r, http.StatusBadRequest, errors.New("Subject from payload does not match subject from previous authentication"))
return
} else if ar.Skip && p.Remember {
h.H.WriteErrorCode(w, r, http.StatusBadRequest, errors.New("Can not remember authentication because no user interaction was required"))
return
}
if !ar.Skip {
p.AuthenticatedAt = time.Now().UTC()
} else {
p.AuthenticatedAt = ar.AuthenticatedAt
}
p.RequestedAt = ar.RequestedAt
request, err := h.M.HandleAuthenticationRequest(ps.ByName("challenge"), &p)
if err != nil {
h.H.WriteError(w, r, errors.WithStack(err))
return
}
ru, err := url.Parse(request.RequestURL)
if err != nil {
h.H.WriteError(w, r, err)
return
}
h.H.Write(w, r, &RequestHandlerResponse{
RedirectTo: urlx.SetQuery(ru, url.Values{"login_verifier": {request.Verifier}}).String(),
})
}
// swagger:route PUT /oauth2/auth/requests/login/{challenge}/reject oAuth2 rejectLoginRequest
//
// Reject an logout request
//
// When an authorization code, hybrid, or implicit OAuth 2.0 Flow is initiated, ORY Hydra asks the login provider
// (sometimes called "identity provider") to authenticate the user and then tell ORY Hydra now about it. The login
// provider is an web-app you write and host, and it must be able to authenticate ("show the user a login screen")
// a user (in OAuth2 the proper name for user is "resource owner").
//
// The authentication challenge is appended to the login provider URL to which the user's user-agent (browser) is redirected to. The login
// provider uses that challenge to fetch information on the OAuth2 request and then accept or reject the requested authentication process.
//
// This endpoint tells ORY Hydra that the user has not authenticated and includes a reason why the authentication
// was be denied.
//
// The response contains a redirect URL which the login provider should redirect the user-agent to.
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Schemes: http, https
//
// Responses:
// 200: completedRequest
// 401: genericError
// 500: genericError
func (h *Handler) RejectLoginRequest(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var p RequestDeniedError
d := json.NewDecoder(r.Body)
d.DisallowUnknownFields()
if err := d.Decode(&p); err != nil {
h.H.WriteErrorCode(w, r, http.StatusBadRequest, errors.WithStack(err))
return
}
ar, err := h.M.GetAuthenticationRequest(ps.ByName("challenge"))
if err != nil {
h.H.WriteError(w, r, err)
return
}
request, err := h.M.HandleAuthenticationRequest(ps.ByName("challenge"), &HandledAuthenticationRequest{
Error: &p,
Challenge: ps.ByName("challenge"),
RequestedAt: ar.RequestedAt,
})
if err != nil {
h.H.WriteError(w, r, errors.WithStack(err))
return
}
ru, err := url.Parse(request.RequestURL)
if err != nil {
h.H.WriteError(w, r, err)
return
}
h.H.Write(w, r, &RequestHandlerResponse{
RedirectTo: urlx.SetQuery(ru, url.Values{"login_verifier": {request.Verifier}}).String(),
})
}
// swagger:route GET /oauth2/auth/requests/consent/{challenge} oAuth2 getConsentRequest
//
// Get consent request information
//
// When an authorization code, hybrid, or implicit OAuth 2.0 Flow is initiated, ORY Hydra asks the login provider
// to authenticate the user and then tell ORY Hydra now about it. If the user authenticated, he/she must now be asked if
// the OAuth 2.0 Client which initiated the flow should be allowed to access the resources on the user's behalf.
//
// The consent provider which handles this request and is a web app implemented and hosted by you. It shows a user interface which asks the user to
// grant or deny the client access to the requested scope ("Application my-dropbox-app wants write access to all your private files").
//
// The consent challenge is appended to the consent provider's URL to which the user's user-agent (browser) is redirected to. The consent
// provider uses that challenge to fetch information on the OAuth2 request and then tells ORY Hydra if the user accepted
// or rejected the request.
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Schemes: http, https
//
// Responses:
// 200: consentRequest
// 401: genericError
// 500: genericError
func (h *Handler) GetConsentRequest(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
request, err := h.M.GetConsentRequest(ps.ByName("challenge"))
if err != nil {
h.H.WriteError(w, r, err)
return
}
request.Client = sanitizeClient(request.Client)
h.H.Write(w, r, request)
}
// swagger:route PUT /oauth2/auth/requests/consent/{challenge}/accept oAuth2 acceptConsentRequest
//
// Accept an consent request
//
// When an authorization code, hybrid, or implicit OAuth 2.0 Flow is initiated, ORY Hydra asks the login provider
// to authenticate the user and then tell ORY Hydra now about it. If the user authenticated, he/she must now be asked if
// the OAuth 2.0 Client which initiated the flow should be allowed to access the resources on the user's behalf.
//
// The consent provider which handles this request and is a web app implemented and hosted by you. It shows a user interface which asks the user to
// grant or deny the client access to the requested scope ("Application my-dropbox-app wants write access to all your private files").
//
// The consent challenge is appended to the consent provider's URL to which the user's user-agent (browser) is redirected to. The consent
// provider uses that challenge to fetch information on the OAuth2 request and then tells ORY Hydra if the user accepted
// or rejected the request.
//
// This endpoint tells ORY Hydra that the user has authorized the OAuth 2.0 client to access resources on his/her behalf.
// The consent provider includes additional information, such as session data for access and ID tokens, and if the
// consent request should be used as basis for future requests.
//
// The response contains a redirect URL which the consent provider should redirect the user-agent to.
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Schemes: http, https
//
// Responses:
// 200: completedRequest
// 401: genericError
// 500: genericError
func (h *Handler) AcceptConsentRequest(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var p HandledConsentRequest
d := json.NewDecoder(r.Body)
d.DisallowUnknownFields()
if err := d.Decode(&p); err != nil {
h.H.WriteErrorCode(w, r, http.StatusBadRequest, errors.WithStack(err))
return
}
cr, err := h.M.GetConsentRequest(ps.ByName("challenge"))
if err != nil {
h.H.WriteError(w, r, errors.WithStack(err))
return
}
p.Challenge = ps.ByName("challenge")
p.RequestedAt = cr.RequestedAt
hr, err := h.M.HandleConsentRequest(ps.ByName("challenge"), &p)
if err != nil {
h.H.WriteError(w, r, errors.WithStack(err))
return
} else if hr.Skip && p.Remember {
h.H.WriteErrorCode(w, r, http.StatusBadRequest, errors.New("Can not remember consent because no user interaction was required"))
return
}
ru, err := url.Parse(hr.RequestURL)
if err != nil {
h.H.WriteError(w, r, err)
return
}
h.H.Write(w, r, &RequestHandlerResponse{
RedirectTo: urlx.SetQuery(ru, url.Values{"consent_verifier": {hr.Verifier}}).String(),
})
}
// swagger:route PUT /oauth2/auth/requests/consent/{challenge}/reject oAuth2 rejectConsentRequest
//
// Reject an consent request
//
// When an authorization code, hybrid, or implicit OAuth 2.0 Flow is initiated, ORY Hydra asks the login provider
// to authenticate the user and then tell ORY Hydra now about it. If the user authenticated, he/she must now be asked if
// the OAuth 2.0 Client which initiated the flow should be allowed to access the resources on the user's behalf.
//
// The consent provider which handles this request and is a web app implemented and hosted by you. It shows a user interface which asks the user to
// grant or deny the client access to the requested scope ("Application my-dropbox-app wants write access to all your private files").
//
// The consent challenge is appended to the consent provider's URL to which the user's user-agent (browser) is redirected to. The consent
// provider uses that challenge to fetch information on the OAuth2 request and then tells ORY Hydra if the user accepted
// or rejected the request.
//
// This endpoint tells ORY Hydra that the user has not authorized the OAuth 2.0 client to access resources on his/her behalf.
// The consent provider must include a reason why the consent was not granted.
//
// The response contains a redirect URL which the consent provider should redirect the user-agent to.
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Schemes: http, https
//
// Responses:
// 200: completedRequest
// 401: genericError
// 500: genericError
func (h *Handler) RejectConsentRequest(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var p RequestDeniedError
d := json.NewDecoder(r.Body)
d.DisallowUnknownFields()
if err := d.Decode(&p); err != nil {
h.H.WriteErrorCode(w, r, http.StatusBadRequest, errors.WithStack(err))
return
}
hr, err := h.M.GetConsentRequest(ps.ByName("challenge"))
if err != nil {
h.H.WriteError(w, r, errors.WithStack(err))
return
}
request, err := h.M.HandleConsentRequest(ps.ByName("challenge"), &HandledConsentRequest{
Error: &p,
Challenge: ps.ByName("challenge"),
RequestedAt: hr.RequestedAt,
})
if err != nil {
h.H.WriteError(w, r, errors.WithStack(err))
return
}
ru, err := url.Parse(request.RequestURL)
if err != nil {
h.H.WriteError(w, r, err)
return
}
h.H.Write(w, r, &RequestHandlerResponse{
RedirectTo: urlx.SetQuery(ru, url.Values{"consent_verifier": {request.Verifier}}).String(),
})
}