-
-
Notifications
You must be signed in to change notification settings - Fork 930
/
handler.go
330 lines (295 loc) · 8.32 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
// nolint:deadcode,unused
package identity
import (
"encoding/json"
"net/http"
"github.com/julienschmidt/httprouter"
"github.com/pkg/errors"
"github.com/ory/x/jsonx"
"github.com/ory/x/urlx"
"github.com/ory/kratos/x"
)
const IdentitiesPath = "/identities"
type (
handlerDependencies interface {
PoolProvider
PrivilegedPoolProvider
ManagementProvider
x.WriterProvider
}
HandlerProvider interface {
IdentityHandler() *Handler
}
Handler struct {
c Configuration
r handlerDependencies
}
)
func NewHandler(
c Configuration,
r handlerDependencies,
) *Handler {
return &Handler{
c: c,
r: r,
}
}
func (h *Handler) RegisterAdminRoutes(admin *x.RouterAdmin) {
admin.GET(IdentitiesPath, h.list)
admin.GET(IdentitiesPath+"/:id", h.get)
admin.DELETE(IdentitiesPath+"/:id", h.delete)
admin.POST(IdentitiesPath, h.create)
admin.PUT(IdentitiesPath+"/:id", h.update)
}
// A single identity.
//
// swagger:response identityResponse
type identityResponse struct {
// required: true
// in: body
Body *Identity
}
// A list of identities.
// swagger:response identityList
type identitiesListResponse struct {
// in: body
// required: true
// type: array
Body []Identity
}
// swagger:parameters listIdentities
type listIdentityParameters struct {
// Items per Page
//
// This is the number of items per page.
//
// required: false
// in: query
// default: 100
// min: 1
// max: 500
PerPage int `json:"per_page"`
// Pagination Page
//
// required: false
// in: query
// default: 0
// min: 0
Page int `json:"page"`
}
// swagger:route GET /identities admin listIdentities
//
// List Identities
//
// Lists all identities. Does not support search at the moment.
//
// Learn how identities work in [ORY Kratos' User And Identity Model Documentation](https://www.ory.sh/docs/next/kratos/concepts/identity-user-model).
//
// Produces:
// - application/json
//
// Schemes: http, https
//
// Responses:
// 200: identityList
// 500: genericError
func (h *Handler) list(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
page, itemsPerPage := x.ParsePagination(r)
is, err := h.r.IdentityPool().ListIdentities(r.Context(), page, itemsPerPage)
if err != nil {
h.r.Writer().WriteError(w, r, err)
return
}
total, err := h.r.IdentityPool().CountIdentities(r.Context())
if err != nil {
h.r.Writer().WriteError(w, r, err)
return
}
x.PaginationHeader(w, urlx.AppendPaths(h.c.SelfAdminURL(), IdentitiesPath), total, page, itemsPerPage)
h.r.Writer().Write(w, r, is)
}
// swagger:parameters getIdentity
type getIdentityParameters struct {
// ID must be set to the ID of identity you want to get
//
// required: true
// in: path
ID string `json:"id"`
}
// swagger:route GET /identities/{id} admin getIdentity
//
// Get an Identity
//
// Learn how identities work in [ORY Kratos' User And Identity Model Documentation](https://www.ory.sh/docs/next/kratos/concepts/identity-user-model).
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Schemes: http, https
//
// Responses:
// 200: identityResponse
// 400: genericError
// 500: genericError
func (h *Handler) get(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
i, err := h.r.IdentityPool().GetIdentity(r.Context(), x.ParseUUID(ps.ByName("id")))
if err != nil {
h.r.Writer().WriteError(w, r, err)
return
}
h.r.Writer().Write(w, r, i)
}
// swagger:parameters createIdentity
type createIdentityRequest struct {
// in: body
Body CreateIdentityRequestPayload
}
type CreateIdentityRequestPayload struct {
// SchemaID is the ID of the JSON Schema to be used for validating the identity's traits.
//
// required: true
// in: body
SchemaID string `json:"schema_id"`
// Traits represent an identity's traits. The identity is able to create, modify, and delete traits
// in a self-service manner. The input will always be validated against the JSON Schema defined
// in `schema_url`.
//
// required: true
// in: body
Traits json.RawMessage `json:"traits"`
}
// swagger:route POST /identities admin createIdentity
//
// Create an Identity
//
// This endpoint creates an identity. It is NOT possible to set an identity's credentials (password, ...)
// using this method! A way to achieve that will be introduced in the future.
//
// Learn how identities work in [ORY Kratos' User And Identity Model Documentation](https://www.ory.sh/docs/next/kratos/concepts/identity-user-model).
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Schemes: http, https
//
// Responses:
// 201: identityResponse
// 400: genericError
// 500: genericError
func (h *Handler) create(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
var cr CreateIdentityRequestPayload
if err := jsonx.NewStrictDecoder(r.Body).Decode(&cr); err != nil {
h.r.Writer().WriteErrorCode(w, r, http.StatusBadRequest, errors.WithStack(err))
return
}
i := &Identity{SchemaID: cr.SchemaID, Traits: []byte(cr.Traits)}
if err := h.r.IdentityManager().Create(r.Context(), i); err != nil {
h.r.Writer().WriteError(w, r, err)
return
}
h.r.Writer().WriteCreated(w, r,
urlx.AppendPaths(
h.c.SelfAdminURL(),
"identities",
i.ID.String(),
).String(),
i,
)
}
// swagger:parameters updateIdentity
type updateIdentityRequest struct {
// in: body
Body UpdateIdentityRequestPayload
}
type UpdateIdentityRequestPayload struct {
// SchemaID is the ID of the JSON Schema to be used for validating the identity's traits. If set
// will update the Identity's SchemaID.
SchemaID string `json:"schema_id"`
// Traits represent an identity's traits. The identity is able to create, modify, and delete traits
// in a self-service manner. The input will always be validated against the JSON Schema defined
// in `schema_id`.
//
// required: true
Traits json.RawMessage `json:"traits"`
}
// swagger:route PUT /identities/{id} admin updateIdentity
//
// Update an Identity
//
// This endpoint updates an identity. It is NOT possible to set an identity's credentials (password, ...)
// using this method! A way to achieve that will be introduced in the future.
//
// The full identity payload (except credentials) is expected. This endpoint does not support patching.
//
// Learn how identities work in [ORY Kratos' User And Identity Model Documentation](https://www.ory.sh/docs/next/kratos/concepts/identity-user-model).
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Schemes: http, https
//
// Responses:
// 200: identityResponse
// 400: genericError
// 404: genericError
// 500: genericError
func (h *Handler) update(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var ur UpdateIdentityRequestPayload
if err := errors.WithStack(jsonx.NewStrictDecoder(r.Body).Decode(&ur)); err != nil {
h.r.Writer().WriteError(w, r, err)
return
}
id := x.ParseUUID(ps.ByName("id"))
identity, err := h.r.PrivilegedIdentityPool().GetIdentityConfidential(r.Context(), id)
if err != nil {
h.r.Writer().WriteError(w, r, err)
return
}
if ur.SchemaID != "" {
identity.SchemaID = ur.SchemaID
}
identity.Traits = []byte(ur.Traits)
if err := h.r.IdentityManager().Update(
r.Context(),
identity,
ManagerAllowWriteProtectedTraits,
); err != nil {
h.r.Writer().WriteError(w, r, err)
return
}
h.r.Writer().Write(w, r, identity)
}
// swagger:route DELETE /identities/{id} admin deleteIdentity
//
// Delete an Identity
//
// Calling this endpoint irrecoverably and permanently deletes the identity given its ID. This action can not be undone.
// This endpoint returns 204 when the identity was deleted or when the identity was not found, in which case it is
// assumed that is has been deleted already.
//
// Learn how identities work in [ORY Kratos' User And Identity Model Documentation](https://www.ory.sh/docs/next/kratos/concepts/identity-user-model).
//
// Produces:
// - application/json
//
// Schemes: http, https
//
// Responses:
// 204: emptyResponse
// 500: genericError
func (h *Handler) delete(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if err := h.r.IdentityPool().(PrivilegedPool).DeleteIdentity(r.Context(), x.ParseUUID(ps.ByName("id"))); err != nil {
h.r.Writer().WriteError(w, r, err)
return
}
w.WriteHeader(http.StatusNoContent)
}