forked from elimity-com/scim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
188 lines (164 loc) · 4.97 KB
/
server.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
package scim
import (
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
scim "github.com/di-wu/scim-filter-parser"
"github.com/elimity-com/scim/schema"
)
const (
defaultStartIndex = 1
fallbackCount = 100
)
// Server represents a SCIM server which implements the HTTP-based SCIM protocol that makes managing identities in multi-
// domain scenarios easier to support via a standardized service.
type Server struct {
Config ServiceProviderConfig
ResourceTypes []ResourceType
}
// getSchemas extracts all the schemas from the resources types defined in the server. Duplicate IDs will be ignored.
func (s Server) getSchemas() []schema.Schema {
ids := make([]string, 0)
schemas := make([]schema.Schema, 0)
for _, resourceType := range s.ResourceTypes {
if !contains(ids, resourceType.Schema.ID) {
schemas = append(schemas, resourceType.Schema)
}
ids = append(ids, resourceType.Schema.ID)
for _, extension := range resourceType.SchemaExtensions {
if !contains(ids, extension.Schema.ID) {
schemas = append(schemas, extension.Schema)
}
ids = append(ids, extension.Schema.ID)
}
}
return schemas
}
// getSchema extracts the schemas from the resources types defined in the server with given id.
func (s Server) getSchema(id string) schema.Schema {
for _, resourceType := range s.ResourceTypes {
if resourceType.Schema.ID == id {
return resourceType.Schema
}
for _, extension := range resourceType.SchemaExtensions {
if extension.Schema.ID == id {
return extension.Schema
}
}
}
return schema.Schema{}
}
// ServeHTTP dispatches the request to the handler whose pattern most closely matches the request URL.
func (s Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/scim+json")
path := strings.TrimPrefix(r.URL.Path, "/v2")
switch {
case path == "/Schemas" && r.Method == http.MethodGet:
s.schemasHandler(w, r)
return
case strings.HasPrefix(path, "/Schemas/") && r.Method == http.MethodGet:
s.schemaHandler(w, r, strings.TrimPrefix(path, "/Schemas/"))
return
case path == "/ResourceTypes" && r.Method == http.MethodGet:
s.resourceTypesHandler(w, r)
return
case strings.HasPrefix(path, "/ResourceTypes/") && r.Method == http.MethodGet:
s.resourceTypeHandler(w, r, strings.TrimPrefix(path, "/ResourceTypes/"))
return
case path == "/ServiceProviderConfig":
s.serviceProviderConfigHandler(w, r)
return
}
for _, resourceType := range s.ResourceTypes {
if path == resourceType.Endpoint {
switch r.Method {
case http.MethodPost:
s.resourcePostHandler(w, r, resourceType)
return
case http.MethodGet:
s.resourcesGetHandler(w, r, resourceType)
return
}
}
if strings.HasPrefix(path, resourceType.Endpoint+"/") {
id, err := parseIdentifier(path, resourceType.Endpoint)
if err != nil {
break
}
switch r.Method {
case http.MethodGet:
s.resourceGetHandler(w, r, id, resourceType)
return
case http.MethodPut:
s.resourcePutHandler(w, r, id, resourceType)
return
case http.MethodPatch:
s.resourcePatchHandler(w, r, id, resourceType)
return
case http.MethodDelete:
s.resourceDeleteHandler(w, r, id, resourceType)
return
}
}
}
errorHandler(w, r, scimError{
detail: "Specified endpoint does not exist.",
status: http.StatusNotFound,
})
}
func parseIdentifier(path, endpoint string) (string, error) {
return url.PathUnescape(strings.TrimPrefix(path, endpoint+"/"))
}
func getIntQueryParam(r *http.Request, key string, def int) (int, error) {
strVal := r.URL.Query().Get(key)
if strVal == "" {
return def, nil
}
if intVal, err := strconv.Atoi(strVal); err == nil {
return intVal, nil
}
return 0, fmt.Errorf("invalid query parameter, \"%s\" must be an integer", key)
}
func (s Server) parseRequestParams(r *http.Request) (ListRequestParams, *scimError) {
invalidParams := make([]string, 0)
defaultCount := s.Config.getItemsPerPage()
count, countErr := getIntQueryParam(r, "count", defaultCount)
if countErr != nil {
invalidParams = append(invalidParams, "count")
}
startIndex, indexErr := getIntQueryParam(r, "startIndex", defaultStartIndex)
if indexErr != nil {
invalidParams = append(invalidParams, "startIndex")
}
if len(invalidParams) > 1 {
err := scimErrorBadParams(invalidParams)
return ListRequestParams{}, &err
}
// Ensure the count isn't more then the allowable max and not less then 1.
if count > defaultCount || count < 1 {
count = defaultCount
}
if startIndex < 1 {
startIndex = defaultStartIndex
}
filter, filterErr := getFilter(r)
if filterErr != nil {
err := scimErrorBadParams([]string{"filter"})
return ListRequestParams{}, &err
}
return ListRequestParams{
Count: count,
Filter: filter,
StartIndex: startIndex,
}, nil
}
func getFilter(r *http.Request) (scim.Expression, error) {
rawFilter := strings.TrimSpace(r.URL.Query().Get("filter"))
if rawFilter != "" {
parser := scim.NewParser(strings.NewReader(rawFilter))
return parser.Parse()
}
return nil, nil
}