forked from elimity-com/scim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_type.go
181 lines (156 loc) · 5.41 KB
/
resource_type.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
package scim
import (
"bytes"
"encoding/json"
"net/http"
"github.com/elimity-com/scim/errors"
"github.com/elimity-com/scim/internal/patch"
"github.com/elimity-com/scim/optional"
"github.com/elimity-com/scim/schema"
)
// unmarshal unifies the unmarshal of the requests.
func unmarshal(data []byte, v interface{}) error {
d := json.NewDecoder(bytes.NewReader(data))
d.UseNumber()
return d.Decode(v)
}
// ResourceType specifies the metadata about a resource type.
type ResourceType struct {
// ID is the resource type's server unique id. This is often the same value as the "name" attribute.
ID optional.String
// Name is the resource type name. This name is referenced by the "meta.resourceType" attribute in all resources.
Name string
// Description is the resource type's human-readable description.
Description optional.String
// Endpoint is the resource type's HTTP-addressable endpoint relative to the Base URL of the service provider,
// e.g., "/Users".
Endpoint string
// Schema is the resource type's primary/base schema.
Schema schema.Schema
// SchemaExtensions is a list of the resource type's schema extensions.
SchemaExtensions []SchemaExtension
// Handler is the set of callback method that connect the SCIM server with a provider of the resource type.
Handler ResourceHandler
}
func (t ResourceType) getRaw() map[string]interface{} {
return map[string]interface{}{
"schemas": []string{"urn:ietf:params:scim:schemas:core:2.0:ResourceType"},
"id": t.ID.Value(),
"name": t.Name,
"description": t.Description.Value(),
"endpoint": t.Endpoint,
"schema": t.Schema.ID,
"schemaExtensions": t.getRawSchemaExtensions(),
}
}
func (t ResourceType) getRawSchemaExtensions() []map[string]interface{} {
schemas := make([]map[string]interface{}, 0)
for _, e := range t.SchemaExtensions {
schemas = append(schemas, map[string]interface{}{
"schema": e.Schema.ID,
"required": e.Required,
})
}
return schemas
}
func (t ResourceType) getSchemaExtensions() []schema.Schema {
var extensions []schema.Schema
for _, e := range t.SchemaExtensions {
extensions = append(extensions, e.Schema)
}
return extensions
}
func (t ResourceType) schemaWithCommon() schema.Schema {
s := t.Schema
externalID := schema.SimpleCoreAttribute(
schema.SimpleStringParams(schema.StringParams{
CaseExact: true,
Mutability: schema.AttributeMutabilityReadWrite(),
Name: schema.CommonAttributeExternalID,
Uniqueness: schema.AttributeUniquenessNone(),
}),
)
s.Attributes = append(s.Attributes, externalID)
return s
}
func (t ResourceType) validate(raw []byte) (ResourceAttributes, *errors.ScimError) {
var m map[string]interface{}
if err := unmarshal(raw, &m); err != nil {
return ResourceAttributes{}, &errors.ScimErrorInvalidSyntax
}
attributes, scimErr := t.schemaWithCommon().Validate(m)
if scimErr != nil {
return ResourceAttributes{}, scimErr
}
for _, extension := range t.SchemaExtensions {
extensionField := m[extension.Schema.ID]
if extensionField == nil {
if extension.Required {
return ResourceAttributes{}, &errors.ScimErrorInvalidValue
}
continue
}
extensionAttributes, scimErr := extension.Schema.Validate(extensionField)
if scimErr != nil {
return ResourceAttributes{}, scimErr
}
attributes[extension.Schema.ID] = extensionAttributes
}
return attributes, nil
}
// validatePatch parse and validate PATCH request.
func (t ResourceType) validatePatch(r *http.Request) ([]PatchOperation, *errors.ScimError) {
data, err := readBody(r)
if err != nil {
return nil, &errors.ScimErrorInvalidSyntax
}
var req struct {
Schemas []string
Operations []json.RawMessage
}
if err := unmarshal(data, &req); err != nil {
return nil, &errors.ScimErrorInvalidSyntax
}
// The body of each request MUST contain the "schemas" attribute with the URI value of
// "urn:ietf:params:scim:api:messages:2.0:PatchOp".
if len(req.Schemas) != 1 || req.Schemas[0] != "urn:ietf:params:scim:api:messages:2.0:PatchOp" {
return nil, &errors.ScimErrorInvalidValue
}
// The body of an HTTP PATCH request MUST contain the attribute "Operations",
// whose value is an array of one or more PATCH operations.
if len(req.Operations) < 1 {
return nil, &errors.ScimErrorInvalidValue
}
// Evaluation continues until all operations are successfully applied or until an error condition is encountered.
var operations []PatchOperation
for _, v := range req.Operations {
validator, err := patch.NewValidator(
string(v),
t.schemaWithCommon(),
t.getSchemaExtensions()...,
)
if err != nil {
return nil, &errors.ScimErrorInvalidPath
}
value, err := validator.Validate()
if err != nil {
return nil, &errors.ScimErrorInvalidValue
}
operations = append(operations, PatchOperation{
Op: string(validator.Op),
Path: validator.Path,
Value: value,
})
}
return operations, nil
}
// SchemaExtension is one of the resource type's schema extensions.
type SchemaExtension struct {
// Schema is the URI of an extended schema, e.g., "urn:edu:2.0:Staff".
Schema schema.Schema
// Required is a boolean value that specifies whether or not the schema extension is required for the resource
// type. If true, a resource of this type MUST include this schema extension and also include any attributes
// declared as required in this schema extension. If false, a resource of this type MAY omit this schema
// extension.
Required bool
}