/
handler.go
267 lines (237 loc) · 7.94 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
/*
* 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 2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
* @license Apache-2.0
*/
package client
import (
"encoding/json"
"net/http"
"github.com/julienschmidt/httprouter"
"github.com/ory/herodot"
"github.com/ory/hydra/rand/sequence"
"github.com/ory/pagination"
"github.com/pkg/errors"
)
type Handler struct {
Manager Manager
H herodot.Writer
}
const (
ClientsHandlerPath = "/clients"
)
func (h *Handler) SetRoutes(r *httprouter.Router) {
r.GET(ClientsHandlerPath, h.List)
r.POST(ClientsHandlerPath, h.Create)
r.GET(ClientsHandlerPath+"/:id", h.Get)
r.PUT(ClientsHandlerPath+"/:id", h.Update)
r.DELETE(ClientsHandlerPath+"/:id", h.Delete)
}
// swagger:route POST /clients oAuth2 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:
// 200: oAuth2Client
// 401: genericError
// 403: genericError
// 500: genericError
func (h *Handler) Create(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
var c Client
if err := json.NewDecoder(r.Body).Decode(&c); err != nil {
h.H.WriteError(w, r, errors.WithStack(err))
return
}
if len(c.Secret) == 0 {
secret, err := sequence.RuneSequence(12, []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_-.~"))
if err != nil {
h.H.WriteError(w, r, errors.WithStack(err))
return
}
c.Secret = string(secret)
} else if len(c.Secret) < 6 {
h.H.WriteError(w, r, errors.New("The client secret must be at least 6 characters long"))
return
}
// has to be 0 because it is not supposed to be set
c.SecretExpiresAt = 0
secret := c.Secret
if err := h.Manager.CreateClient(&c); err != nil {
h.H.WriteError(w, r, err)
return
}
c.Secret = ""
if !c.Public {
c.Secret = secret
}
h.H.WriteCreated(w, r, ClientsHandlerPath+"/"+c.GetID(), &c)
}
// swagger:route PUT /clients/{id} oAuth2 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
// 401: genericError
// 403: genericError
// 500: genericError
func (h *Handler) Update(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var c Client
if err := json.NewDecoder(r.Body).Decode(&c); err != nil {
h.H.WriteError(w, r, errors.WithStack(err))
return
}
var secret string
if len(c.Secret) > 0 && len(c.Secret) < 6 {
h.H.WriteError(w, r, errors.New("The client secret must be at least 6 characters long"))
return
} else {
secret = c.Secret
}
c.ID = ps.ByName("id")
// has to be 0 because it is not supposed to be set
c.SecretExpiresAt = 0
if err := h.Manager.UpdateClient(&c); err != nil {
h.H.WriteError(w, r, err)
return
}
c.Secret = secret
h.H.WriteCreated(w, r, ClientsHandlerPath+"/"+c.GetID(), &c)
}
// swagger:route GET /clients oAuth2 listOAuth2Clients
//
// List OAuth 2.0 Clients
//
// This endpoint lists all clients in the database, and never returns client secrets.
//
// 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: oAuth2ClientList
// 401: genericError
// 403: genericError
// 500: genericError
func (h *Handler) List(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
limit, offset := pagination.Parse(r, 100, 0, 500)
c, err := h.Manager.GetClients(limit, offset)
if err != nil {
h.H.WriteError(w, r, err)
return
}
clients := make([]Client, len(c))
k := 0
for _, cc := range c {
clients[k] = cc
clients[k].Secret = ""
k++
}
h.H.Write(w, r, clients)
}
// swagger:route GET /clients/{id} oAuth2 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: genericError
// 403: genericError
// 500: genericError
func (h *Handler) Get(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var id = ps.ByName("id")
c, err := h.Manager.GetConcreteClient(id)
if err != nil {
h.H.WriteError(w, r, err)
return
}
c.Secret = ""
h.H.Write(w, r, c)
}
// swagger:route DELETE /clients/{id} oAuth2 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
// 401: genericError
// 403: genericError
// 500: genericError
func (h *Handler) Delete(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var id = ps.ByName("id")
if err := h.Manager.DeleteClient(id); err != nil {
h.H.WriteError(w, r, err)
return
}
w.WriteHeader(http.StatusNoContent)
}