/
consent_handler.go
112 lines (96 loc) · 2.98 KB
/
consent_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
package oauth2
import (
"encoding/json"
"fmt"
"net/http"
"github.com/julienschmidt/httprouter"
"github.com/ory/herodot"
"github.com/ory/hydra/firewall"
"github.com/pkg/errors"
)
const (
ConsentRequestAccepted = "accepted"
ConsentRequestRejected = "rejected"
ConsentSessionPath = "/oauth2/consent/requests"
ConsentResource = "rn:hydra:oauth2:consent:requests:%s"
ConsentScope = "hydra.consent"
)
type ConsentSessionHandler struct {
H herodot.Writer
M ConsentRequestManager
W firewall.Firewall
}
func (h *ConsentSessionHandler) SetRoutes(r *httprouter.Router) {
r.GET(ConsentSessionPath+"/:id", h.FetchConsentRequest)
r.PATCH(ConsentSessionPath+"/:id/reject", h.RejectConsentRequestHandler)
r.PATCH(ConsentSessionPath+"/:id/accept", h.AcceptConsentRequestHandler)
}
// swagger:route GET /.well-known/openid-configuration oauth2 openid-connect WellKnownHandler
//
// Server well known configuration
//
// For more information, please refer to https://openid.net/specs/openid-connect-discovery-1_0.html
//
// Consumes:
// - application/x-www-form-urlencoded
//
// Produces:
// - application/json
//
// Schemes: http, https
//
// Security:
// oauth2:
//
// Responses:
// 200: WellKnown
// 401: genericError
// 500: genericError
func (h *ConsentSessionHandler) FetchConsentRequest(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if _, err := h.W.TokenAllowed(r.Context(), h.W.TokenFromRequest(r), &firewall.TokenAccessRequest{
Resource: fmt.Sprintf(ConsentResource, ps.ByName("id")),
Action: "get",
}, ConsentScope); err != nil {
h.H.WriteError(w, r, err)
return
}
if session, err := h.M.GetConsentRequest(ps.ByName("id")); err != nil {
h.H.WriteError(w, r, err)
return
} else {
h.H.Write(w, r, session)
}
}
func (h *ConsentSessionHandler) RejectConsentRequestHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if _, err := h.W.TokenAllowed(r.Context(), h.W.TokenFromRequest(r), &firewall.TokenAccessRequest{
Resource: fmt.Sprintf(ConsentResource, ps.ByName("id")),
Action: "reject",
}, ConsentScope); err != nil {
h.H.WriteError(w, r, err)
return
}
if err := h.M.RejectConsentRequest(ps.ByName("id")); err != nil {
h.H.WriteError(w, r, err)
return
}
w.WriteHeader(http.StatusNoContent)
}
func (h *ConsentSessionHandler) AcceptConsentRequestHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if _, err := h.W.TokenAllowed(r.Context(), h.W.TokenFromRequest(r), &firewall.TokenAccessRequest{
Resource: fmt.Sprintf(ConsentResource, ps.ByName("id")),
Action: "accept",
}, ConsentScope); err != nil {
h.H.WriteError(w, r, err)
return
}
var payload AcceptConsentRequestPayload
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
h.H.WriteError(w, r, errors.WithStack(err))
return
}
if err := h.M.AcceptConsentRequest(ps.ByName("id"), &payload); err != nil {
h.H.WriteError(w, r, err)
return
}
w.WriteHeader(http.StatusNoContent)
}