forked from elimity-com/scim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
361 lines (310 loc) · 10.5 KB
/
handlers.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package scim
import (
"encoding/json"
"log"
"net/http"
"github.com/elimity-com/scim/errors"
f "github.com/elimity-com/scim/internal/filter"
"github.com/elimity-com/scim/schema"
)
func errorHandler(w http.ResponseWriter, _ *http.Request, scimErr *errors.ScimError) {
raw, err := json.Marshal(scimErr)
if err != nil {
log.Fatalf("failed marshaling scim error: %v", err)
}
w.WriteHeader(scimErr.Status)
_, err = w.Write(raw)
if err != nil {
log.Printf("failed writing response: %v", err)
}
}
// resourceDeleteHandler receives an HTTP DELETE request to the resource endpoint, e.g., "/Users/{id}" or "/Groups/{id}",
// where "{id}" is a resource identifier to delete a known resource.
func (s Server) resourceDeleteHandler(w http.ResponseWriter, r *http.Request, id string, resourceType ResourceType) {
deleteErr := resourceType.Handler.Delete(r, id)
if deleteErr != nil {
scimErr := errors.CheckScimError(deleteErr, http.MethodDelete)
errorHandler(w, r, &scimErr)
return
}
w.WriteHeader(http.StatusNoContent)
}
// resourceGetHandler receives an HTTP GET request to the resource endpoint, e.g., "/Users/{id}" or "/Groups/{id}",
// where "{id}" is a resource identifier to retrieve a known resource.
func (s Server) resourceGetHandler(w http.ResponseWriter, r *http.Request, id string, resourceType ResourceType) {
resource, getErr := resourceType.Handler.Get(r, id)
if getErr != nil {
scimErr := errors.CheckScimError(getErr, http.MethodGet)
errorHandler(w, r, &scimErr)
return
}
raw, err := json.Marshal(resource.response(resourceType))
if err != nil {
errorHandler(w, r, &errors.ScimErrorInternal)
log.Fatalf("failed marshaling resource: %v", err)
return
}
if resource.Meta.Version != "" {
w.Header().Set("Etag", resource.Meta.Version)
}
_, err = w.Write(raw)
if err != nil {
log.Printf("failed writing response: %v", err)
}
}
// resourcePatchHandler receives an HTTP PATCH to the resource endpoint, e.g., "/Users/{id}" or "/Groups/{id}", where
// "{id}" is a resource identifier to replace a resource's attributes.
func (s Server) resourcePatchHandler(w http.ResponseWriter, r *http.Request, id string, resourceType ResourceType) {
patch, scimErr := resourceType.validatePatch(r)
if scimErr != nil {
errorHandler(w, r, scimErr)
return
}
resource, patchErr := resourceType.Handler.Patch(r, id, patch)
if patchErr != nil {
scimErr := errors.CheckScimError(patchErr, http.MethodPatch)
errorHandler(w, r, &scimErr)
return
}
if len(resource.Attributes) == 0 {
w.WriteHeader(http.StatusNoContent)
return
}
raw, err := json.Marshal(resource.response(resourceType))
if err != nil {
errorHandler(w, r, &errors.ScimErrorInternal)
log.Fatalf("failed marshaling resource: %v", err)
return
}
if resource.Meta.Version != "" {
w.Header().Set("Etag", resource.Meta.Version)
}
w.WriteHeader(http.StatusOK)
_, err = w.Write(raw)
if err != nil {
log.Printf("failed writing response: %v", err)
}
}
// resourcePostHandler receives an HTTP POST request to the resource endpoint, such as "/Users" or "/Groups", as
// defined by the associated resource type endpoint discovery to create new resources.
func (s Server) resourcePostHandler(w http.ResponseWriter, r *http.Request, resourceType ResourceType) {
data, _ := readBody(r)
attributes, scimErr := resourceType.validate(data)
if scimErr != nil {
errorHandler(w, r, scimErr)
return
}
resource, postErr := resourceType.Handler.Create(r, attributes)
if postErr != nil {
scimErr := errors.CheckScimError(postErr, http.MethodPost)
errorHandler(w, r, &scimErr)
return
}
raw, err := json.Marshal(resource.response(resourceType))
if err != nil {
errorHandler(w, r, &errors.ScimErrorInternal)
log.Fatalf("failed marshaling resource: %v", err)
return
}
if resource.Meta.Version != "" {
w.Header().Set("Etag", resource.Meta.Version)
}
w.WriteHeader(http.StatusCreated)
_, err = w.Write(raw)
if err != nil {
log.Printf("failed writing response: %v", err)
}
}
// resourcePutHandler receives an HTTP PUT to the resource endpoint, e.g., "/Users/{id}" or "/Groups/{id}", where
// "{id}" is a resource identifier to replace a resource's attributes.
func (s Server) resourcePutHandler(w http.ResponseWriter, r *http.Request, id string, resourceType ResourceType) {
data, _ := readBody(r)
attributes, scimErr := resourceType.validate(data)
if scimErr != nil {
errorHandler(w, r, scimErr)
return
}
resource, putError := resourceType.Handler.Replace(r, id, attributes)
if putError != nil {
scimErr := errors.CheckScimError(putError, http.MethodPut)
errorHandler(w, r, &scimErr)
return
}
raw, err := json.Marshal(resource.response(resourceType))
if err != nil {
errorHandler(w, r, &errors.ScimErrorInternal)
log.Fatalf("failed marshaling resource: %v", err)
return
}
if resource.Meta.Version != "" {
w.Header().Set("Etag", resource.Meta.Version)
}
_, err = w.Write(raw)
if err != nil {
log.Printf("failed writing response: %v", err)
}
}
// resourceTypeHandler receives an HTTP GET to retrieve individual resource types which can be returned by appending the
// resource types name to the /ResourceTypes endpoint. For example: "/ResourceTypes/User".
func (s Server) resourceTypeHandler(w http.ResponseWriter, r *http.Request, name string) {
var resourceType ResourceType
for _, r := range s.ResourceTypes {
if r.Name == name {
resourceType = r
break
}
}
if resourceType.Name != name {
scimErr := errors.ScimErrorResourceNotFound(name)
errorHandler(w, r, &scimErr)
return
}
raw, err := json.Marshal(resourceType.getRaw())
if err != nil {
errorHandler(w, r, &errors.ScimErrorInternal)
log.Fatalf("failed marshaling resource type: %v", err)
return
}
_, err = w.Write(raw)
if err != nil {
log.Printf("failed writing response: %v", err)
}
}
// resourceTypesHandler receives an HTTP GET to this endpoint, "/ResourceTypes", which is used to discover the types of
// resources available on a SCIM service provider (e.g., Users and Groups). Each resource type defines the endpoints,
// the core schema URI that defines the resource, and any supported schema extensions.
func (s Server) resourceTypesHandler(w http.ResponseWriter, r *http.Request) {
params, paramsErr := s.parseRequestParams(r, schema.ResourceTypeSchema())
if paramsErr != nil {
errorHandler(w, r, paramsErr)
return
}
start, end := clamp(params.StartIndex-1, params.Count, len(s.ResourceTypes))
var resources []interface{}
for _, v := range s.ResourceTypes[start:end] {
resources = append(resources, v.getRaw())
}
raw, err := json.Marshal(listResponse{
TotalResults: len(s.ResourceTypes),
ItemsPerPage: params.Count,
StartIndex: params.StartIndex,
Resources: resources,
})
if err != nil {
errorHandler(w, r, &errors.ScimErrorInternal)
log.Fatalf("failed marshaling list response: %v", err)
return
}
_, err = w.Write(raw)
if err != nil {
log.Printf("failed writing response: %v", err)
}
}
// resourcesGetHandler receives an HTTP GET request to the resource endpoint, e.g., "/Users" or "/Groups", to retrieve
// all known resources.
func (s Server) resourcesGetHandler(w http.ResponseWriter, r *http.Request, resourceType ResourceType) {
params, paramsErr := s.parseRequestParams(r, resourceType.Schema, resourceType.getSchemaExtensions()...)
if paramsErr != nil {
errorHandler(w, r, paramsErr)
return
}
page, getError := resourceType.Handler.GetAll(r, params)
if getError != nil {
scimErr := errors.CheckScimError(getError, http.MethodGet)
errorHandler(w, r, &scimErr)
return
}
raw, err := json.Marshal(listResponse{
TotalResults: page.TotalResults,
Resources: page.resources(resourceType),
StartIndex: params.StartIndex,
ItemsPerPage: params.Count,
})
if err != nil {
errorHandler(w, r, &errors.ScimErrorInternal)
log.Fatalf("failed marshalling list response: %v", err)
return
}
_, err = w.Write(raw)
if err != nil {
log.Printf("failed writing response: %v", err)
}
}
// schemaHandler receives an HTTP GET to retrieve individual schema definitions which can be returned by appending the
// schema URI to the /Schemas endpoint. For example: "/Schemas/urn:ietf:params:scim:schemas:core:2.0:User".
func (s Server) schemaHandler(w http.ResponseWriter, r *http.Request, id string) {
getSchema := s.getSchema(id)
if getSchema.ID != id {
scimErr := errors.ScimErrorResourceNotFound(id)
errorHandler(w, r, &scimErr)
return
}
raw, err := json.Marshal(getSchema)
if err != nil {
errorHandler(w, r, &errors.ScimErrorInternal)
log.Fatalf("failed marshaling schema: %v", err)
return
}
_, err = w.Write(raw)
if err != nil {
log.Printf("failed writing response: %v", err)
}
}
// schemasHandler receives an HTTP GET to retrieve information about resource schemas supported by a SCIM service
// provider. An HTTP GET to the endpoint "/Schemas" returns all supported schemas in ListResponse format.
func (s Server) schemasHandler(w http.ResponseWriter, r *http.Request) {
params, paramsErr := s.parseRequestParams(r, schema.Definition())
if paramsErr != nil {
errorHandler(w, r, paramsErr)
return
}
var (
validator = f.NewFilterValidator(params.Filter, schema.Definition())
start, end = clamp(params.StartIndex-1, params.Count, len(s.getSchemas()))
resources []interface{}
)
if params.Filter != nil {
if err := validator.Validate(); err != nil {
errorHandler(w, r, &errors.ScimErrorInvalidFilter)
return
}
}
for _, v := range s.getSchemas()[start:end] {
resource := v.ToMap()
if params.Filter != nil {
if err := validator.PassesFilter(resource); err != nil {
continue
}
}
resources = append(resources, resource)
}
raw, err := json.Marshal(listResponse{
TotalResults: len(s.getSchemas()),
ItemsPerPage: params.Count,
StartIndex: params.StartIndex,
Resources: resources,
})
if err != nil {
errorHandler(w, r, &errors.ScimErrorInternal)
log.Fatalf("failed marshaling list response: %v", err)
return
}
_, err = w.Write(raw)
if err != nil {
log.Printf("failed writing response: %v", err)
}
}
// serviceProviderConfigHandler receives an HTTP GET to this endpoint will return a JSON structure that describes the
// SCIM specification features available on a service provider.
func (s Server) serviceProviderConfigHandler(w http.ResponseWriter, r *http.Request) {
raw, err := json.Marshal(s.Config.getRaw())
if err != nil {
errorHandler(w, r, &errors.ScimErrorInternal)
log.Fatalf("failed marshaling service provider config: %v", err)
return
}
_, err = w.Write(raw)
if err != nil {
log.Printf("failed writing response: %v", err)
}
}