/
handler.go
347 lines (301 loc) · 10.3 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
package client
import (
"context"
"encoding/json"
"io"
"net/http"
"time"
"github.com/ory/x/errorsx"
"github.com/ory/herodot"
"github.com/ory/x/sqlcon"
"github.com/julienschmidt/httprouter"
"github.com/pkg/errors"
"github.com/ory/x/pagination"
"github.com/driver005/oauth/models"
"github.com/driver005/oauth/helpers"
)
type Handler struct {
r InternalRegistry
}
const (
ClientsHandlerPath = "/clients"
)
func NewHandler(r InternalRegistry) *Handler {
return &Handler{
r: r,
}
}
func (h *Handler) SetRoutes(admin *helpers.RouterAdmin) {
admin.GET(ClientsHandlerPath, h.List)
admin.POST(ClientsHandlerPath, h.Create)
admin.GET(ClientsHandlerPath+"/:id", h.Get)
admin.PUT(ClientsHandlerPath+"/:id", h.Update)
admin.PATCH(ClientsHandlerPath+"/:id", h.Patch)
admin.DELETE(ClientsHandlerPath+"/:id", h.Delete)
}
// swagger:route POST /clients admin createOAuth2Client
//
// Create an OAuth 2.0 Client
//
// Create a new OAuth 2.0 client If you pass `client_secret` the secret will be used, otherwise a random secret will be generated. The secret will be returned in the response and you will not be able to retrieve it later on. Write the secret down and keep it somwhere safe.
//
// OAuth 2.0 clients are used to perform OAuth 2.0 and OpenID Connect flows. Usually, OAuth 2.0 clients are generated for applications which want to consume your OAuth 2.0 or OpenID Connect capabilities. To manage ORY Hydra, you will need an OAuth 2.0 Client as well. Make sure that this endpoint is well protected and only callable by first-party components.
//
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Schemes: http, https
//
// Responses:
// 201: oAuth2Client
// 400: jsonError
// 409: jsonError
// 500: jsonError
func (h *Handler) Create(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
var c models.Client
if err := json.NewDecoder(r.Body).Decode(&c); err != nil {
h.r.Writer().WriteError(w, r, errorsx.WithStack(err))
return
}
if len(c.Secret) == 0 {
secretb, err := helpers.GenerateSecret(26)
if err != nil {
h.r.Writer().WriteError(w, r, errorsx.WithStack(err))
return
}
c.Secret = string(secretb)
}
if err := h.r.ClientValidator().Validate(&c); err != nil {
h.r.Writer().WriteError(w, r, err)
return
}
secret := c.Secret
c.CreatedAt = time.Now().UTC().Round(time.Second)
c.UpdatedAt = c.CreatedAt
if err := h.r.ClientManager().CreateClient(r.Context(), &c); err != nil {
h.r.Writer().WriteError(w, r, err)
return
}
c.Secret = ""
if !c.IsPublic() {
c.Secret = secret
}
h.r.Writer().WriteCreated(w, r, ClientsHandlerPath+"/"+c.GetID(), &c)
}
// swagger:route PUT /clients/{id} admin updateOAuth2Client
//
// Update an OAuth 2.0 Client
//
// Update an existing OAuth 2.0 Client. If you pass `client_secret` the secret will be updated and returned via the API. This is the only time you will be able to retrieve the client secret, so write it down and keep it safe.
//
// OAuth 2.0 clients are used to perform OAuth 2.0 and OpenID Connect flows. Usually, OAuth 2.0 clients are generated for applications which want to consume your OAuth 2.0 or OpenID Connect capabilities. To manage ORY Hydra, you will need an OAuth 2.0 Client as well. Make sure that this endpoint is well protected and only callable by first-party components.
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Schemes: http, https
//
// Responses:
// 200: oAuth2Client
// 500: jsonError
func (h *Handler) Update(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var c models.Client
if err := json.NewDecoder(r.Body).Decode(&c); err != nil {
h.r.Writer().WriteError(w, r, errorsx.WithStack(err))
return
}
c.OutfacingID = ps.ByName("id")
if err := h.updateClient(r.Context(), &c); err != nil {
h.r.Writer().WriteError(w, r, err)
return
}
h.r.Writer().Write(w, r, &c)
}
// swagger:route PATCH /clients/{id} admin patchOAuth2Client
//
// Patch an OAuth 2.0 Client
//
// Patch an existing OAuth 2.0 Client. If you pass `client_secret` the secret will be updated and returned via the API. This is the only time you will be able to retrieve the client secret, so write it down and keep it safe.
//
// OAuth 2.0 clients are used to perform OAuth 2.0 and OpenID Connect flows. Usually, OAuth 2.0 clients are generated for applications which want to consume your OAuth 2.0 or OpenID Connect capabilities. To manage ORY Hydra, you will need an OAuth 2.0 Client as well. Make sure that this endpoint is well protected and only callable by first-party components.
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Schemes: http, https
//
// Responses:
// 200: oAuth2Client
// 500: jsonError
func (h *Handler) Patch(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
patchJSON, err := io.ReadAll(r.Body)
if err != nil {
h.r.Writer().WriteError(w, r, err)
return
}
id := ps.ByName("id")
c, err := h.r.ClientManager().GetConcreteClient(r.Context(), id)
if err != nil {
h.r.Writer().WriteError(w, r, err)
return
}
if err := helpers.ApplyJSONPatch(patchJSON, c, "/id"); err != nil {
h.r.Writer().WriteError(w, r, err)
return
}
if err := h.updateClient(r.Context(), c); err != nil {
h.r.Writer().WriteError(w, r, err)
return
}
h.r.Writer().Write(w, r, c)
}
func (h *Handler) updateClient(ctx context.Context, c *models.Client) error {
var secret string
if len(c.Secret) > 0 {
secret = c.Secret
}
if err := h.r.ClientValidator().Validate(c); err != nil {
return err
}
c.UpdatedAt = time.Now().UTC().Round(time.Second)
if err := h.r.ClientManager().UpdateClient(ctx, c); err != nil {
return err
}
c.Secret = secret
return nil
}
// swagger:parameters listOAuth2Clients
type Filter struct {
// The maximum amount of clients to returned, upper bound is 500 clients.
// in: query
Limit int `json:"limit"`
// The offset from where to start looking.
// in: query
Offset int `json:"offset"`
// The name of the clients to filter by.
// in: query
Name string `json:"name"`
// The owner of the clients to filter by.
// in: query
Owner string `json:"owner"`
}
// swagger:route GET /clients admin listOAuth2Clients
//
// List OAuth 2.0 Clients
//
// This endpoint lists all clients in the database, and never returns client secrets. As a default it lists the first 100 clients. The `limit` parameter can be used to retrieve more clients, but it has an upper bound at 500 objects. Pagination should be used to retrieve more than 500 objects.
//
// OAuth 2.0 clients are used to perform OAuth 2.0 and OpenID Connect flows. Usually, OAuth 2.0 clients are generated for applications which want to consume your OAuth 2.0 or OpenID Connect capabilities. To manage ORY Hydra, you will need an OAuth 2.0 Client as well. Make sure that this endpoint is well protected and only callable by first-party components.
// The "Link" header is also included in successful responses, which contains one or more links for pagination, formatted like so: '<https://hydra-url/admin/clients?limit={limit}&offset={offset}>; rel="{page}"', where page is one of the following applicable pages: 'first', 'next', 'last', and 'previous'.
// Multiple links can be included in this header, and will be separated by a comma.
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Schemes: http, https
//
// Responses:
// 200: oAuth2ClientList
// 500: jsonError
func (h *Handler) List(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
limit, offset := pagination.Parse(r, 100, 0, 500)
filters := Filter{
Limit: limit,
Offset: offset,
Name: r.URL.Query().Get("client_name"),
Owner: r.URL.Query().Get("owner"),
}
c, err := h.r.ClientManager().GetClients(r.Context(), filters)
if err != nil {
h.r.Writer().WriteError(w, r, err)
return
}
for k := range c {
c[k].Secret = ""
}
n, err := h.r.ClientManager().CountClients(r.Context())
if err != nil {
h.r.Writer().WriteError(w, r, err)
return
}
pagination.Header(w, r.URL, n, limit, offset)
if c == nil {
c = []models.Client{}
}
h.r.Writer().Write(w, r, c)
}
// swagger:route GET /clients/{id} admin getOAuth2Client
//
// Get an OAuth 2.0 Client.
//
// Get an OAUth 2.0 client by its ID. This endpoint never returns passwords.
//
// OAuth 2.0 clients are used to perform OAuth 2.0 and OpenID Connect flows. Usually, OAuth 2.0 clients are generated for applications which want to consume your OAuth 2.0 or OpenID Connect capabilities. To manage ORY Hydra, you will need an OAuth 2.0 Client as well. Make sure that this endpoint is well protected and only callable by first-party components.
//
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Schemes: http, https
//
// Responses:
// 200: oAuth2Client
// 401: jsonError
// 500: jsonError
func (h *Handler) Get(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var id = ps.ByName("id")
c, err := h.r.ClientManager().GetConcreteClient(r.Context(), id)
if err != nil {
if errors.Is(err, sqlcon.ErrNoRows) {
err = herodot.ErrUnauthorized.WithReason("The requested OAuth 2.0 client does not exist or you did not provide the necessary credentials")
}
h.r.Writer().WriteError(w, r, err)
return
}
c.Secret = ""
h.r.Writer().Write(w, r, c)
}
// swagger:route DELETE /clients/{id} admin deleteOAuth2Client
//
// Deletes an OAuth 2.0 Client
//
// Delete an existing OAuth 2.0 Client by its ID.
//
// OAuth 2.0 clients are used to perform OAuth 2.0 and OpenID Connect flows. Usually, OAuth 2.0 clients are generated for applications which want to consume your OAuth 2.0 or OpenID Connect capabilities. To manage ORY Hydra, you will need an OAuth 2.0 Client as well. Make sure that this endpoint is well protected and only callable by first-party components.
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Schemes: http, https
//
// Responses:
// 204: emptyResponse
// 404: jsonError
// 500: jsonError
func (h *Handler) Delete(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var id = ps.ByName("id")
if err := h.r.ClientManager().DeleteClient(r.Context(), id); err != nil {
h.r.Writer().WriteError(w, r, err)
return
}
w.WriteHeader(http.StatusNoContent)
}