-
Notifications
You must be signed in to change notification settings - Fork 3
/
user_handler.go
369 lines (329 loc) · 11.3 KB
/
user_handler.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
362
363
364
365
366
367
368
369
// Copyright 2021 Monoskope Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package scimserver
import (
"fmt"
"io"
"net/http"
"github.com/elimity-com/scim"
scim_errors "github.com/elimity-com/scim/errors"
"github.com/finleap-connect/monoskope/pkg/api/domain"
cmdData "github.com/finleap-connect/monoskope/pkg/api/domain/commanddata"
"github.com/finleap-connect/monoskope/pkg/api/domain/projections"
"github.com/finleap-connect/monoskope/pkg/api/eventsourcing"
cmd "github.com/finleap-connect/monoskope/pkg/domain/commands"
commandTypes "github.com/finleap-connect/monoskope/pkg/domain/constants/commands"
"github.com/finleap-connect/monoskope/pkg/domain/constants/roles"
"github.com/finleap-connect/monoskope/pkg/domain/constants/users"
"github.com/finleap-connect/monoskope/pkg/domain/errors"
es_errors "github.com/finleap-connect/monoskope/pkg/eventsourcing/errors"
"github.com/finleap-connect/monoskope/pkg/logger"
m8scim "github.com/finleap-connect/monoskope/pkg/scim"
"github.com/google/uuid"
"github.com/scim2/filter-parser/v2"
"google.golang.org/protobuf/types/known/wrapperspb"
)
type userHandler struct {
cmdHandlerClient eventsourcing.CommandHandlerClient
userClient domain.UserClient
log logger.Logger
}
// NewUserHandler creates a new scim.ResourceHandler for handling User resources
func NewUserHandler(cmdHandlerClient eventsourcing.CommandHandlerClient, userClient domain.UserClient) scim.ResourceHandler {
return &userHandler{
cmdHandlerClient, userClient, logger.WithName("scim-user-handler"),
}
}
func (h *userHandler) getBy(f func() (*projections.User, error)) (scim.Resource, error) {
user, err := f()
if err != nil {
err = errors.TranslateFromGrpcError(err)
if err == errors.ErrUserNotFound {
return scim.Resource{}, scim_errors.ScimError{
Status: http.StatusNotFound,
}
}
return scim.Resource{}, scim_errors.ScimError{
Status: http.StatusInternalServerError,
Detail: err.Error(),
}
}
if user.GetMetadata().Deleted != nil && user.GetMetadata().Deleted.IsValid() {
return scim.Resource{}, scim_errors.ScimError{
Status: http.StatusNotFound,
}
}
return toScimUser(user), nil
}
// Create stores given attributes. Returns a resource with the attributes that are stored and a (new) unique identifier.
func (h *userHandler) Create(r *http.Request, attributes scim.ResourceAttributes) (scim.Resource, error) {
logDebug(h.log, r)
ctx, err := users.CreateUserContextGrpc(r.Context(), users.SCIMServerUser)
if err != nil {
return scim.Resource{}, scim_errors.ScimError{
Status: http.StatusInternalServerError,
Detail: err.Error(),
}
}
userAttributes, err := m8scim.NewUserAttribute(attributes)
if err != nil {
return scim.Resource{}, scim_errors.ScimError{
Status: http.StatusInternalServerError,
Detail: err.Error(),
}
}
command, err := cmd.AddCommandData(cmd.CreateCommand(uuid.Nil, commandTypes.CreateUser), &cmdData.CreateUserCommandData{
Email: userAttributes.UserName,
Name: userAttributes.DisplayName,
})
if err != nil {
return scim.Resource{}, scim_errors.ScimError{
Status: http.StatusInternalServerError,
Detail: err.Error(),
}
}
reply, err := h.cmdHandlerClient.Execute(ctx, command)
if err != nil {
err = errors.TranslateFromGrpcError(err)
if err == errors.ErrUserAlreadyExists {
return h.getBy(func() (*projections.User, error) {
return h.userClient.GetByEmail(r.Context(), wrapperspb.String(userAttributes.UserName))
})
}
return scim.Resource{}, scim_errors.ScimError{
Status: http.StatusInternalServerError,
Detail: err.Error(),
}
}
return scim.Resource{
ID: reply.AggregateId,
Attributes: attributes,
}, nil
}
// Get returns the resource corresponding with the given identifier.
func (h *userHandler) Get(r *http.Request, id string) (scim.Resource, error) {
logDebug(h.log, r)
return h.getBy(func() (*projections.User, error) {
return h.userClient.GetById(r.Context(), wrapperspb.String(id))
})
}
// GetAll returns a paginated list of resources.
// An empty list of resources will be represented as `null` in the JSON response if `nil` is assigned to the
// Page.Resources. Otherwise, is an empty slice is assigned, an empty list will be represented as `[]`.
func (h *userHandler) GetAll(r *http.Request, params scim.ListRequestParams) (scim.Page, error) {
logDebug(h.log, r)
// Get total user count initially
userCount, err := h.userClient.GetCount(r.Context(), &domain.GetCountRequest{IncludeDeleted: true})
if err != nil {
return scim.Page{}, scim_errors.ScimError{
Status: http.StatusInternalServerError,
Detail: err.Error(),
}
}
// If count is less than one just return total count
if params.Count < 1 {
return scim.Page{
TotalResults: int(userCount.Count),
}, nil
}
var filterByName string
if params.Filter != nil {
switch e := params.Filter.(type) {
case *filter.AttributeExpression:
if e.AttributePath.AttributeName == m8scim.UserNameAttribute && e.Operator == filter.EQ {
filterByName = e.CompareValue.(string)
}
default:
err := fmt.Errorf("unknown expression type: %s", e)
h.log.Error(err, "unknown expression type", "type", e)
return scim.Page{}, scim_errors.ScimError{
Status: http.StatusInternalServerError,
Detail: err.Error(),
}
}
}
resources := make([]scim.Resource, 0)
if len(filterByName) > 0 {
user, err := h.userClient.GetByEmail(r.Context(), wrapperspb.String(filterByName))
if err != nil {
err = errors.TranslateFromGrpcError(err)
if err == errors.ErrUserNotFound {
return scim.Page{
TotalResults: int(userCount.Count),
Resources: resources,
}, nil
}
return scim.Page{}, scim_errors.ScimError{
Status: http.StatusInternalServerError,
Detail: err.Error(),
}
}
resources = append(resources, toScimUser(user))
} else {
// Get stream of users
userStream, err := h.userClient.GetAll(r.Context(), &domain.GetAllRequest{IncludeDeleted: true})
if err != nil {
err = errors.TranslateFromGrpcError(err)
return scim.Page{}, scim_errors.ScimError{
Status: http.StatusInternalServerError,
Detail: err.Error(),
}
}
// Seek through the stream
i := 1
for {
if i > (params.StartIndex + params.Count - 1) {
break // We're done
}
// Read next
user, err := userStream.Recv()
// End of stream
if err == io.EOF {
break // No further users to query
}
if err != nil { // Some other error
return scim.Page{}, scim_errors.ScimError{
Status: http.StatusInternalServerError,
Detail: err.Error(),
}
}
// Skip users which are not in the current page
if i >= params.StartIndex {
resources = append(resources, toScimUser(user))
}
i++
}
}
return scim.Page{
TotalResults: int(userCount.Count),
Resources: resources,
}, nil
}
// Replace replaces ALL existing attributes of the resource with given identifier. Given attributes that are empty
// are to be deleted. Returns a resource with the attributes that are stored.
func (h *userHandler) Replace(r *http.Request, id string, attributes scim.ResourceAttributes) (scim.Resource, error) {
logDebug(h.log, r)
ctx, err := users.CreateUserContextGrpc(r.Context(), users.SCIMServerUser)
if err != nil {
return scim.Resource{}, scim_errors.ScimError{
Status: http.StatusInternalServerError,
Detail: err.Error(),
}
}
user, err := h.userClient.GetById(ctx, wrapperspb.String(id))
if err != nil {
err = errors.TranslateFromGrpcError(err)
if err == errors.ErrUserNotFound || err == es_errors.ErrProjectionNotFound {
return scim.Resource{}, scim_errors.ScimError{
Status: http.StatusNotFound,
}
}
return scim.Resource{}, scim_errors.ScimError{
Status: http.StatusInternalServerError,
Detail: err.Error(),
}
}
userAttributes, err := m8scim.NewUserAttribute(attributes)
if err != nil {
return scim.Resource{}, scim_errors.ScimError{
Status: http.StatusInternalServerError,
Detail: err.Error(),
}
}
command, err := cmd.AddCommandData(cmd.CreateCommand(uuid.MustParse(user.Id), commandTypes.UpdateUser), &cmdData.UpdateUserCommandData{
Name: wrapperspb.String(userAttributes.DisplayName),
})
if err != nil {
return scim.Resource{}, scim_errors.ScimError{
Status: http.StatusInternalServerError,
Detail: err.Error(),
}
}
_, err = h.cmdHandlerClient.Execute(ctx, command)
if err != nil {
return scim.Resource{}, scim_errors.ScimError{
Status: http.StatusInternalServerError,
Detail: err.Error(),
}
}
user.Name = userAttributes.DisplayName
return toScimUser(user), nil
}
// Delete removes the resource with corresponding ID.
func (h *userHandler) Delete(r *http.Request, id string) error {
logDebug(h.log, r)
uid, err := uuid.Parse(id)
if err != nil {
h.log.Error(err, "Failed to parse id")
return scim_errors.ScimError{
Status: http.StatusBadRequest,
Detail: err.Error(),
}
}
ctx, err := users.CreateUserContextGrpc(r.Context(), users.SCIMServerUser)
if err != nil {
return scim_errors.ScimError{
Status: http.StatusInternalServerError,
Detail: err.Error(),
}
}
_, err = h.cmdHandlerClient.Execute(ctx, cmd.CreateCommand(uid, commandTypes.DeleteUser))
if err != nil {
return scim_errors.ScimError{
Status: http.StatusInternalServerError,
Detail: err.Error(),
}
}
return nil
}
// Patch update one or more attributes of a SCIM resource using a sequence of
// operations to "add", "remove", or "replace" values.
// If you return no Resource.Attributes, a 204 No Content status code will be returned.
// This case is only valid in the following scenarios:
// 1. the Add/Replace operation should return No Content only when the value already exists AND is the same.
// 2. the Remove operation should return No Content when the value to be remove is already absent.
// More information in Section 3.5.2 of RFC 7644: https://tools.ietf.org/html/rfc7644#section-3.5.2
func (h *userHandler) Patch(r *http.Request, id string, operations []scim.PatchOperation) (scim.Resource, error) {
return scim.Resource{}, scim_errors.ScimError{
Status: http.StatusNotImplemented,
}
}
// toScimUser converts a projections.User to it's scim.Resource representation
func toScimUser(user *projections.User) scim.Resource {
created := user.Metadata.Created.AsTime()
lastModified := user.Metadata.LastModified.AsTime()
groups := make([]map[string]string, 0)
for _, r := range user.Roles {
role, err := roles.ToRole(r.Role)
if err != nil {
continue
}
groups = append(groups, map[string]string{
m8scim.GroupMemberValueAttribute: roles.IdFromRole(role).String(),
m8scim.GroupMemberDisplayAttribute: r.Role,
})
}
return scim.Resource{
ID: user.Id,
Meta: scim.Meta{
Created: &created,
LastModified: &lastModified,
},
Attributes: scim.ResourceAttributes{
m8scim.UserNameAttribute: user.Email,
m8scim.DisplayNameAttribute: user.Name,
m8scim.GroupAttribute: groups,
},
}
}