forked from elimity-com/scim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
307 lines (275 loc) · 9.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
package scim
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"github.com/elimity-com/scim/errors"
)
func errorHandler(w http.ResponseWriter, _ *http.Request, scimErr 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)
}
}
// 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)
if paramsErr != nil {
errorHandler(w, r, *paramsErr)
return
}
schemas := s.getSchemas()
start, end := clamp(params.StartIndex-1, params.Count, len(schemas))
var resources []interface{}
for _, v := range schemas[start:end] {
resources = append(resources, v)
}
raw, err := json.Marshal(listResponse{
TotalResults: len(schemas),
ItemsPerPage: params.Count,
StartIndex: params.StartIndex,
Resources: resources,
})
if err != nil {
errorHandler(w, r, scimErrorInternalServer)
log.Fatalf("failed marshaling 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) {
schema := s.getSchema(id)
if schema.ID != id {
errorHandler(w, r, scimErrorResourceNotFound(id))
return
}
raw, err := json.Marshal(schema)
if err != nil {
errorHandler(w, r, scimErrorInternalServer)
log.Fatalf("failed marshaling schema: %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)
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, scimErrorInternalServer)
log.Fatalf("failed marshaling list response: %v", err)
return
}
_, 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 {
errorHandler(w, r, scimErrorResourceNotFound(name))
return
}
raw, err := json.Marshal(resourceType.getRaw())
if err != nil {
errorHandler(w, r, scimErrorInternalServer)
log.Fatalf("failed marshaling resource type: %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, scimErrorInternalServer)
log.Fatalf("failed marshaling service provider config: %v", err)
return
}
_, 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 != errors.ValidationErrorNil {
errorHandler(w, r, scimValidationError(scimErr))
return
}
resource, patchErr := resourceType.Handler.Patch(r, id, patch)
if patchErr != errors.PatchErrorNil {
errorHandler(w, r, scimPatchError(patchErr, id))
return
}
raw, err := json.Marshal(resource.response(resourceType))
if err != nil {
errorHandler(w, r, scimErrorInternalServer)
log.Fatalf("failed marshaling resource: %v", err)
return
}
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, _ := ioutil.ReadAll(r.Body)
attributes, scimErr := resourceType.validate(data)
if scimErr != errors.ValidationErrorNil {
errorHandler(w, r, scimValidationError(scimErr))
return
}
resource, postErr := resourceType.Handler.Create(r, attributes)
if postErr != errors.PostErrorNil {
errorHandler(w, r, scimPostError(postErr))
return
}
raw, err := json.Marshal(resource.response(resourceType))
if err != nil {
errorHandler(w, r, scimErrorInternalServer)
log.Fatalf("failed marshaling resource: %v", err)
return
}
w.WriteHeader(http.StatusCreated)
_, err = w.Write(raw)
if err != nil {
log.Printf("failed writing response: %v", err)
}
}
// 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 != errors.GetErrorNil {
errorHandler(w, r, scimGetError(getErr, id))
return
}
raw, err := json.Marshal(resource.response(resourceType))
if err != nil {
errorHandler(w, r, scimErrorInternalServer)
log.Fatalf("failed marshaling resource: %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)
if paramsErr != nil {
errorHandler(w, r, *paramsErr)
return
}
page, getError := resourceType.Handler.GetAll(r, params)
if getError != errors.GetErrorNil {
errorHandler(w, r, scimGetAllError(getError))
return
}
var resources []interface{}
for _, v := range page.Resources {
resources = append(resources, v.response(resourceType))
}
raw, err := json.Marshal(listResponse{
TotalResults: page.TotalResults,
Resources: resources,
StartIndex: params.StartIndex,
ItemsPerPage: params.Count,
})
if err != nil {
errorHandler(w, r, scimErrorInternalServer)
log.Fatalf("failed marshalling list response: %v", err)
return
}
_, 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, _ := ioutil.ReadAll(r.Body)
attributes, scimErr := resourceType.validate(data)
if scimErr != errors.ValidationErrorNil {
errorHandler(w, r, scimValidationError(scimErr))
return
}
resource, putError := resourceType.Handler.Replace(r, id, attributes)
if putError != errors.PutErrorNil {
errorHandler(w, r, scimPutError(putError, id))
return
}
raw, err := json.Marshal(resource.response(resourceType))
if err != nil {
errorHandler(w, r, scimErrorInternalServer)
log.Fatalf("failed marshaling resource: %v", err)
return
}
_, 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 != errors.DeleteErrorNil {
errorHandler(w, r, scimDeleteError(deleteErr, id))
return
}
w.WriteHeader(http.StatusNoContent)
}