/
handler.go
164 lines (134 loc) · 4.1 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
package connection
import (
"encoding/json"
"fmt"
"net/http"
"github.com/asaskevich/govalidator"
"github.com/go-errors/errors"
"github.com/julienschmidt/httprouter"
"github.com/ory-am/hydra/firewall"
"github.com/ory-am/hydra/herodot"
"github.com/ory-am/ladon"
"github.com/pborman/uuid"
"golang.org/x/net/context"
)
const (
connectionsResource = "rn:hydra:connections"
connectionResource = "rn:hydra:connections:%s"
scope = "hydra.connections"
)
type Handler struct {
Manager Manager
H herodot.Herodot
W firewall.Firewall
}
func (h *Handler) SetRoutes(r *httprouter.Router) {
r.GET("/connections", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if r.URL.Query().Get("local_subject") != "" {
h.FindLocal(w, r, ps)
return
}
if r.URL.Query().Get("remote_subject") != "" && r.URL.Query().Get("provider") != "" {
h.FindRemote(w, r, ps)
return
}
var ctx = context.Background()
h.H.WriteErrorCode(ctx, w, r, http.StatusBadRequest, errors.New("Pass either [local_subject] or [remote_subject, provider] as query to this request"))
})
r.POST("/connections", h.Create)
r.GET("/connections/:id", h.Get)
r.DELETE("/connections/:id", h.Delete)
}
func (h *Handler) Create(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var conn Connection
var ctx = context.Background()
if _, err := h.W.TokenAllowed(ctx, h.W.TokenFromRequest(r), &ladon.Request{
Resource: connectionsResource,
Action: "create",
}, scope); err != nil {
h.H.WriteError(ctx, w, r, err)
return
}
if err := json.NewDecoder(r.Body).Decode(&conn); err != nil {
h.H.WriteErrorCode(ctx, w, r, http.StatusBadRequest, err)
return
}
if v, err := govalidator.ValidateStruct(conn); err != nil {
h.H.WriteErrorCode(ctx, w, r, http.StatusBadRequest, err)
return
} else if !v {
h.H.WriteErrorCode(ctx, w, r, http.StatusBadRequest, errors.New("Payload did not validate."))
return
}
conn.ID = uuid.New()
if err := h.Manager.Create(&conn); err != nil {
h.H.WriteError(ctx, w, r, err)
return
}
h.H.WriteCreated(ctx, w, r, "/oauth2/connections/"+conn.ID, &conn)
}
func (h *Handler) FindLocal(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var ctx = context.Background()
if _, err := h.W.TokenAllowed(ctx, h.W.TokenFromRequest(r), &ladon.Request{
Resource: connectionsResource,
Action: "find",
}, scope); err != nil {
h.H.WriteError(ctx, w, r, err)
return
}
conns, err := h.Manager.FindAllByLocalSubject(r.URL.Query().Get("local_subject"))
if err != nil {
h.H.WriteError(ctx, w, r, err)
return
}
h.H.Write(ctx, w, r, conns)
}
func (h *Handler) FindRemote(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var ctx = context.Background()
if _, err := h.W.TokenAllowed(ctx, h.W.TokenFromRequest(r), &ladon.Request{
Resource: connectionsResource,
Action: "find",
}, scope); err != nil {
h.H.WriteError(ctx, w, r, err)
return
}
conns, err := h.Manager.FindByRemoteSubject(r.URL.Query().Get("provider"), r.URL.Query().Get("remote_subject"))
if err != nil {
h.H.WriteError(ctx, w, r, err)
return
}
h.H.Write(ctx, w, r, conns)
}
func (h *Handler) Get(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var ctx = context.Background()
var id = ps.ByName("id")
if _, err := h.W.TokenAllowed(ctx, h.W.TokenFromRequest(r), &ladon.Request{
Resource: fmt.Sprintf(connectionResource, id),
Action: "get",
}, scope); err != nil {
h.H.WriteError(ctx, w, r, err)
return
}
conn, err := h.Manager.Get(id)
if err != nil {
h.H.WriteError(ctx, w, r, err)
return
}
h.H.Write(ctx, w, r, conn)
}
func (h *Handler) Delete(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var ctx = context.Background()
var id = ps.ByName("id")
if _, err := h.W.TokenAllowed(ctx, h.W.TokenFromRequest(r), &ladon.Request{
Resource: fmt.Sprintf(connectionResource, id),
Action: "delete",
}, scope); err != nil {
h.H.WriteError(ctx, w, r, err)
return
}
if err := h.Manager.Delete(ps.ByName("id")); err != nil {
h.H.WriteError(ctx, w, r, err)
return
}
w.WriteHeader(http.StatusNoContent)
}