-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler_user.go
200 lines (181 loc) · 4.58 KB
/
handler_user.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
package server
import (
"fmt"
"github.com/elimity-com/scim"
"github.com/elimity-com/scim/errors"
"github.com/elimity-com/scim/optional"
"github.com/elimity-com/scim/schema"
"github.com/google/uuid"
"github.com/scim2/example-server/mdb"
"github.com/scim2/tools/marshal"
"net/http"
"time"
)
var userType = scim.ResourceType{
ID: optional.NewString("User"),
Name: "User",
Endpoint: "/Users",
Description: optional.NewString("User Account"),
Schema: schema.CoreUserSchema(),
SchemaExtensions: []scim.SchemaExtension{
{Schema: schema.ExtensionEnterpriseUser()},
},
Handler: newUsersResourceHandler(),
}
type usersResourceHandler struct {
db *mdb.DB
}
func newUsersResourceHandler() *usersResourceHandler {
return &usersResourceHandler{
db: mdb.New(),
}
}
func (u usersResourceHandler) Create(r *http.Request, attributes scim.ResourceAttributes) (scim.Resource, error) {
// Convert attributes to a User struct.
user, err := attr2User(attributes)
if err != nil {
// The SCIM server will convert this error to a InternalServerError (500).
return scim.Resource{}, err
}
// Create a unique identifier.
user.ID = uuid.New().String()
// Meta data.
now := time.Now()
meta := scim.Meta{
Created: &now,
LastModified: &now,
Version: fmt.Sprintf("v%s", user.ID),
}
// Write to database.
if err := u.db.Update(func(tx *mdb.TX) error {
tx.Set(user.ID, mdb.Instance{
Value: user,
Meta: meta,
})
return nil
}); err != nil {
return scim.Resource{}, err
}
return user2Resource(user, meta)
}
func (u usersResourceHandler) Get(r *http.Request, id string) (scim.Resource, error) {
var user mdb.Instance
// Get user from database.
if err := u.db.View(func(tx *mdb.TX) error {
var ok bool
user, ok = tx.Get(id)
if !ok {
// Return NotFound (404) if not found.
return errors.ScimErrorResourceNotFound(id)
}
return nil
}); err != nil {
return scim.Resource{}, err
}
return user2Resource(user.Value.(User), user.Meta)
}
func (u usersResourceHandler) GetAll(r *http.Request, params scim.ListRequestParams) (scim.Page, error) {
var (
resources = make([]scim.Resource, 0)
total int
)
if err := u.db.View(func(tx *mdb.TX) error {
var (
i = 1
all = tx.GetAll()
)
total = len(all)
for _, v := range all {
if i > (params.StartIndex + params.Count - 1) {
break
}
if i >= params.StartIndex {
resource, err := user2Resource(v.Value.(User), v.Meta)
if err != nil {
return err
}
resources = append(resources, resource)
}
i++
}
return nil
}); err != nil {
return scim.Page{}, err
}
return scim.Page{
TotalResults: total,
Resources: resources,
}, nil
}
func (u usersResourceHandler) Replace(r *http.Request, id string, attributes scim.ResourceAttributes) (scim.Resource, error) {
user, err := attr2User(attributes)
if err != nil {
return scim.Resource{}, err
}
var meta scim.Meta
if err := u.db.Update(func(tx *mdb.TX) error {
previous, ok := tx.Get(id)
if !ok {
// Return NotFound (404) if not found.
return errors.ScimErrorResourceNotFound(id)
}
// Take over the identifier.
user.ID = previous.Value.(User).ID
// Update meta.
now := time.Now()
meta = scim.Meta{
Created: previous.Meta.Created,
LastModified: &now,
Version: previous.Meta.Version,
}
// Replace instance.
tx.Set(id, mdb.Instance{
Value: user,
Meta: meta,
})
return nil
}); err != nil {
return scim.Resource{}, err
}
return user2Resource(user, meta)
}
func (u usersResourceHandler) Delete(r *http.Request, id string) error {
return u.db.Update(func(tx *mdb.TX) error {
if ok := tx.Delete(id); !ok {
// Return NotFound (404) if not found.
return errors.ScimErrorResourceNotFound(id)
}
return nil
})
}
func (u usersResourceHandler) Patch(r *http.Request, id string, request scim.PatchRequest) (scim.Resource, error) {
return scim.Resource{}, &errors.ScimError{
Status: http.StatusNotImplemented,
}
}
func attr2User(attributes scim.ResourceAttributes) (User, error) {
var user User
if err := marshal.Unmarshal(attributes, &user); err != nil {
return User{}, err
}
return user, nil
}
func user2Resource(user User, meta scim.Meta) (scim.Resource, error) {
attributes, err := user2Attr(user)
if err != nil {
return scim.Resource{}, err
}
eID := optional.String{}
if user.ExternalID != "" {
eID = optional.NewString(user.ExternalID)
}
return scim.Resource{
ID: user.ID,
ExternalID: eID,
Attributes: attributes,
Meta: meta,
}, nil
}
func user2Attr(user User) (scim.ResourceAttributes, error) {
return marshal.Marshal(user)
}