Repository navigation
Expand file tree
/
Copy pathscim_handler.go
More file actions
561 lines (482 loc) · 17.9 KB
/
Copy pathscim_handler.go
File metadata and controls
561 lines (482 loc) · 17.9 KB
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
package scim
import (
"encoding/json"
"errors"
"net/http"
"strings"
"time"
"github.com/xraph/forge"
"github.com/xraph/authsome/apitypes"
"github.com/xraph/authsome/bridge"
"github.com/xraph/authsome/id"
"github.com/xraph/authsome/middleware"
"github.com/xraph/authsome/user"
)
// ──────────────────────────────────────────────────
// Request types
// ──────────────────────────────────────────────────
type scimUserPathParam struct {
UserID string `path:"userId"`
}
type scimGroupPathParam struct {
GroupID string `path:"groupId"`
}
// ──────────────────────────────────────────────────
// SCIM Discovery endpoints
// ──────────────────────────────────────────────────
func (p *Plugin) handleServiceProviderConfig(_ forge.Context, _ *apitypes.Empty) (*ServiceProviderConfig, error) {
config := ServiceProviderConfig{
"schemas": []string{SchemaServiceConfig},
"patch": map[string]any{
"supported": true,
},
"bulk": map[string]any{
"supported": false,
"maxPayloadSize": 0,
},
"filter": map[string]any{
"supported": true,
"maxResults": 100,
},
"changePassword": map[string]any{
"supported": false,
},
"sort": map[string]any{
"supported": false,
},
"etag": map[string]any{
"supported": false,
},
"authenticationSchemes": []map[string]any{
{
"type": "oauthbearertoken",
"name": "OAuth Bearer Token",
"description": "Authentication scheme using the OAuth Bearer Token Standard",
},
},
}
return &config, nil
}
func (p *Plugin) handleSchemas(_ forge.Context, _ *apitypes.Empty) (*SchemaList, error) {
schemas := SchemaList{
"schemas": []string{SchemaListResponse},
"totalResults": 2,
"Resources": []map[string]any{
{
"id": SchemaUser,
"name": "User",
},
{
"id": SchemaGroup,
"name": "Group",
},
},
}
return &schemas, nil
}
func (p *Plugin) handleResourceTypes(_ forge.Context, _ *apitypes.Empty) (*ResourceTypeList, error) {
types := ResourceTypeList{
"schemas": []string{SchemaListResponse},
"totalResults": 2,
"Resources": []map[string]any{
{
"schemas": []string{SchemaResourceType},
"id": "User",
"name": "User",
"endpoint": "/Users",
"schema": SchemaUser,
},
{
"schemas": []string{SchemaResourceType},
"id": "Group",
"name": "Group",
"endpoint": "/Groups",
"schema": SchemaGroup,
},
},
}
return &types, nil
}
// ──────────────────────────────────────────────────
// User SCIM endpoints
// ──────────────────────────────────────────────────
func (p *Plugin) handleListUsers(ctx forge.Context, _ *apitypes.Empty) (*ListResponse, error) {
cfg, err := p.authenticateSCIM(ctx)
if err != nil {
return nil, err
}
baseURL := p.config.BasePath
// List users. For org-scoped configs, list org members; otherwise all users.
var users []*UserResource
if !cfg.OrgID.IsNil() && p.authStore != nil {
members, err := p.authStore.ListMembers(ctx.Context(), cfg.OrgID)
if err != nil {
return nil, middleware.InternalError(ctx, err)
}
for _, m := range members {
u, err := p.authStore.GetUser(ctx.Context(), m.UserID)
if err != nil {
continue
}
users = append(users, UserToSCIM(u, baseURL))
}
} else if p.authStore != nil {
result, err := p.authStore.ListUsers(ctx.Context(), &user.Query{
AppID: cfg.AppID,
})
if err != nil {
return nil, middleware.InternalError(ctx, err)
}
for _, u := range result.Users {
users = append(users, UserToSCIM(u, baseURL))
}
}
resources := make([]any, 0, len(users))
for _, u := range users {
resources = append(resources, u)
}
return &ListResponse{
Schemas: []string{SchemaListResponse},
TotalResults: len(resources),
StartIndex: 1,
ItemsPerPage: len(resources),
Resources: resources,
}, nil
}
// scimError maps a service failure to the SCIM response. A row outside the
// configuration's scope is answered exactly like a missing one.
func scimError(ctx forge.Context, err error, notFound string) error {
if errors.Is(err, ErrOutOfScope) {
return forge.NotFound(notFound)
}
return middleware.InternalError(ctx, err)
}
func (p *Plugin) handleGetUser(ctx forge.Context, req *scimUserPathParam) (*UserResource, error) {
cfg, err := p.authenticateSCIM(ctx)
if err != nil {
return nil, err
}
u, err := p.service.LoadUser(ctx.Context(), cfg, req.UserID)
if err != nil {
return nil, scimError(ctx, err, "user not found")
}
return UserToSCIM(u, p.config.BasePath), nil
}
func (p *Plugin) handleCreateUser(ctx forge.Context, _ *apitypes.Empty) (*UserResource, error) {
cfg, err := p.authenticateSCIM(ctx)
if err != nil {
return nil, err
}
var scimUser UserResource
if decodeErr := json.NewDecoder(ctx.Request().Body).Decode(&scimUser); decodeErr != nil {
return nil, forge.BadRequest("invalid SCIM user payload")
}
scimUser.Active = true
u, action, err := p.service.ProvisionUser(ctx.Context(), cfg, &scimUser)
if err != nil {
p.service.RecordLog(ctx.Context(), cfg.ID, action, "User", scimUser.ExternalID, "", LogStatusError, err.Error())
return nil, scimError(ctx, err, "user not found")
}
p.service.RecordLog(ctx.Context(), cfg.ID, action, "User", scimUser.ExternalID, u.ID.String(), LogStatusSuccess, "")
p.audit(ctx.Context(), "scim."+action, "user", u.ID.String(), "", cfg.ID.String(), bridge.OutcomeSuccess)
result := UserToSCIM(u, p.config.BasePath)
return nil, ctx.JSON(http.StatusCreated, result)
}
func (p *Plugin) handleReplaceUser(ctx forge.Context, req *scimUserPathParam) (*UserResource, error) {
cfg, err := p.authenticateSCIM(ctx)
if err != nil {
return nil, err
}
var scimUser UserResource
if decodeErr := json.NewDecoder(ctx.Request().Body).Decode(&scimUser); decodeErr != nil {
return nil, forge.BadRequest("invalid SCIM user payload")
}
scimUser.ID = req.UserID
// PUT targets the path id, never whichever account the body's email
// happens to resolve to.
u, err := p.service.LoadUser(ctx.Context(), cfg, req.UserID)
if err != nil {
return nil, scimError(ctx, err, "user not found")
}
if err := p.service.ReplaceUser(ctx.Context(), cfg, u, &scimUser); err != nil {
p.service.RecordLog(ctx.Context(), cfg.ID, ActionUpdateUser, "User", scimUser.ExternalID, req.UserID, LogStatusError, err.Error())
return nil, scimError(ctx, err, "user not found")
}
p.service.RecordLog(ctx.Context(), cfg.ID, ActionUpdateUser, "User", scimUser.ExternalID, u.ID.String(), LogStatusSuccess, "")
return UserToSCIM(u, p.config.BasePath), nil
}
func (p *Plugin) handlePatchUser(ctx forge.Context, req *scimUserPathParam) (*UserResource, error) {
cfg, err := p.authenticateSCIM(ctx)
if err != nil {
return nil, err
}
var patch PatchOp
if decodeErr := json.NewDecoder(ctx.Request().Body).Decode(&patch); decodeErr != nil {
return nil, forge.BadRequest("invalid SCIM patch payload")
}
u, err := p.service.LoadUser(ctx.Context(), cfg, req.UserID)
if err != nil {
return nil, scimError(ctx, err, "user not found")
}
// Apply SCIM PATCH operations.
wasBanned := u.Banned
newUserName := ""
for _, op := range patch.Operations {
if strings.EqualFold(op.Op, "replace") {
if name := p.applyUserPatchReplace(u, op); name != "" {
newUserName = name
}
}
}
// userName is the primary email and goes through the email-record API,
// not through the user's email field.
if newUserName != "" {
if err := p.service.ChangeUserName(ctx.Context(), cfg, u, newUserName); err != nil {
p.service.RecordLog(ctx.Context(), cfg.ID, ActionUpdateUser, "User", "", req.UserID, LogStatusError, err.Error())
return nil, scimError(ctx, err, "user not found")
}
}
u.UpdatedAt = time.Now()
if err := p.authStore.UpdateUser(ctx.Context(), u); err != nil {
p.service.RecordLog(ctx.Context(), cfg.ID, ActionUpdateUser, "User", "", req.UserID, LogStatusError, err.Error())
return nil, middleware.InternalError(ctx, err)
}
if u.Banned && !wasBanned {
if err := p.service.enforceDeactivation(ctx.Context(), cfg, u); err != nil {
p.service.RecordLog(ctx.Context(), cfg.ID, ActionSuspendUser, "User", "", req.UserID, LogStatusError, err.Error())
return nil, middleware.InternalError(ctx, err)
}
}
// PATCH {"path":"active","value":false} (and the pathless bulk-replace
// shape) is the deactivation Okta and Entra ID send by default, and it
// bans the user (see applyUserPatchReplace) without going through
// AdminBanUser or DeactivateUser. Plugins watching AfterUserUpdate need
// to see every path that can flip Banned, not just the two named ones.
if p.plugins != nil {
p.plugins.EmitAfterUserUpdate(ctx.Context(), u)
}
// Check if user was deactivated.
if u.Banned {
p.service.RecordLog(ctx.Context(), cfg.ID, ActionSuspendUser, "User", "", u.ID.String(), LogStatusSuccess, "")
} else {
p.service.RecordLog(ctx.Context(), cfg.ID, ActionUpdateUser, "User", "", u.ID.String(), LogStatusSuccess, "")
}
return UserToSCIM(u, p.config.BasePath), nil
}
func (p *Plugin) handleDeleteUser(ctx forge.Context, req *scimUserPathParam) (*apitypes.Empty, error) {
cfg, err := p.authenticateSCIM(ctx)
if err != nil {
return nil, err
}
userID, err := id.ParseUserID(req.UserID)
if err != nil {
return nil, forge.NotFound("user not found")
}
if err := p.service.DeactivateUser(ctx.Context(), cfg, userID); err != nil {
p.service.RecordLog(ctx.Context(), cfg.ID, ActionSuspendUser, "User", "", req.UserID, LogStatusError, err.Error())
return nil, scimError(ctx, err, "user not found")
}
p.service.RecordLog(ctx.Context(), cfg.ID, ActionSuspendUser, "User", "", req.UserID, LogStatusSuccess, "")
p.audit(ctx.Context(), "scim.suspend_user", "user", req.UserID, "", cfg.ID.String(), bridge.OutcomeSuccess)
return nil, ctx.NoContent(http.StatusNoContent)
}
// ──────────────────────────────────────────────────
// Group SCIM endpoints
// ──────────────────────────────────────────────────
func (p *Plugin) handleListGroups(ctx forge.Context, _ *apitypes.Empty) (*ListResponse, error) {
cfg, err := p.authenticateSCIM(ctx)
if err != nil {
return nil, err
}
if !cfg.GroupSync || cfg.OrgID.IsNil() {
return &ListResponse{
Schemas: []string{SchemaListResponse},
TotalResults: 0,
StartIndex: 1,
ItemsPerPage: 0,
Resources: []any{},
}, nil
}
baseURL := p.config.BasePath
teams, err := p.authStore.ListTeams(ctx.Context(), cfg.OrgID)
if err != nil {
return nil, middleware.InternalError(ctx, err)
}
resources := make([]any, 0, len(teams))
for _, t := range teams {
resources = append(resources, TeamToSCIMGroup(t, nil, baseURL))
}
return &ListResponse{
Schemas: []string{SchemaListResponse},
TotalResults: len(resources),
StartIndex: 1,
ItemsPerPage: len(resources),
Resources: resources,
}, nil
}
func (p *Plugin) handleGetGroup(ctx forge.Context, req *scimGroupPathParam) (*GroupResource, error) {
cfg, err := p.authenticateSCIM(ctx)
if err != nil {
return nil, err
}
team, err := p.service.LoadTeam(ctx.Context(), cfg, req.GroupID)
if err != nil {
return nil, scimError(ctx, err, "group not found")
}
return TeamToSCIMGroup(team, nil, p.config.BasePath), nil
}
func (p *Plugin) handleCreateGroup(ctx forge.Context, _ *apitypes.Empty) (*GroupResource, error) {
cfg, err := p.authenticateSCIM(ctx)
if err != nil {
return nil, err
}
var scimGroup GroupResource
if decodeErr := json.NewDecoder(ctx.Request().Body).Decode(&scimGroup); decodeErr != nil {
return nil, forge.BadRequest("invalid SCIM group payload")
}
team, action, err := p.service.ProvisionGroup(ctx.Context(), cfg, &scimGroup)
if err != nil {
p.service.RecordLog(ctx.Context(), cfg.ID, action, "Group", scimGroup.ExternalID, "", LogStatusError, err.Error())
return nil, middleware.InternalError(ctx, err)
}
p.service.RecordLog(ctx.Context(), cfg.ID, action, "Group", scimGroup.ExternalID, team.ID.String(), LogStatusSuccess, "")
p.audit(ctx.Context(), "scim."+action, "group", team.ID.String(), "", cfg.ID.String(), bridge.OutcomeSuccess)
result := TeamToSCIMGroup(team, nil, p.config.BasePath)
return nil, ctx.JSON(http.StatusCreated, result)
}
func (p *Plugin) handleReplaceGroup(ctx forge.Context, req *scimGroupPathParam) (*GroupResource, error) {
cfg, err := p.authenticateSCIM(ctx)
if err != nil {
return nil, err
}
var scimGroup GroupResource
if decodeErr := json.NewDecoder(ctx.Request().Body).Decode(&scimGroup); decodeErr != nil {
return nil, forge.BadRequest("invalid SCIM group payload")
}
scimGroup.ID = req.GroupID
// PUT targets the path id inside the configuration's organization.
team, err := p.service.LoadTeam(ctx.Context(), cfg, req.GroupID)
if err != nil {
return nil, scimError(ctx, err, "group not found")
}
if name := strings.TrimSpace(scimGroup.DisplayName); name != "" {
team.Name = name
}
team.UpdatedAt = time.Now()
if err := p.authStore.UpdateTeam(ctx.Context(), team); err != nil {
p.service.RecordLog(ctx.Context(), cfg.ID, ActionUpdateGroup, "Group", scimGroup.ExternalID, req.GroupID, LogStatusError, err.Error())
return nil, middleware.InternalError(ctx, err)
}
p.service.RecordLog(ctx.Context(), cfg.ID, ActionUpdateGroup, "Group", scimGroup.ExternalID, team.ID.String(), LogStatusSuccess, "")
return TeamToSCIMGroup(team, nil, p.config.BasePath), nil
}
func (p *Plugin) handlePatchGroup(ctx forge.Context, req *scimGroupPathParam) (*GroupResource, error) {
cfg, err := p.authenticateSCIM(ctx)
if err != nil {
return nil, err
}
var patch PatchOp
if decodeErr := json.NewDecoder(ctx.Request().Body).Decode(&patch); decodeErr != nil {
return nil, forge.BadRequest("invalid SCIM patch payload")
}
team, err := p.service.LoadTeam(ctx.Context(), cfg, req.GroupID)
if err != nil {
return nil, scimError(ctx, err, "group not found")
}
// Apply PATCH operations (simplified: handle displayName changes).
for _, op := range patch.Operations {
if strings.EqualFold(op.Op, "replace") {
if op.Path == "displayName" {
if name, ok := op.Value.(string); ok {
team.Name = name
}
}
}
}
team.UpdatedAt = time.Now()
if err := p.authStore.UpdateTeam(ctx.Context(), team); err != nil {
p.service.RecordLog(ctx.Context(), cfg.ID, ActionUpdateGroup, "Group", "", req.GroupID, LogStatusError, err.Error())
return nil, middleware.InternalError(ctx, err)
}
p.service.RecordLog(ctx.Context(), cfg.ID, ActionUpdateGroup, "Group", "", team.ID.String(), LogStatusSuccess, "")
return TeamToSCIMGroup(team, nil, p.config.BasePath), nil
}
func (p *Plugin) handleDeleteGroup(ctx forge.Context, req *scimGroupPathParam) (*apitypes.Empty, error) {
cfg, err := p.authenticateSCIM(ctx)
if err != nil {
return nil, err
}
team, err := p.service.LoadTeam(ctx.Context(), cfg, req.GroupID)
if err != nil {
return nil, scimError(ctx, err, "group not found")
}
if err := p.authStore.DeleteTeam(ctx.Context(), team.ID); err != nil {
p.service.RecordLog(ctx.Context(), cfg.ID, ActionDeleteGroup, "Group", "", req.GroupID, LogStatusError, err.Error())
return nil, middleware.InternalError(ctx, err)
}
p.service.RecordLog(ctx.Context(), cfg.ID, ActionDeleteGroup, "Group", "", req.GroupID, LogStatusSuccess, "")
p.audit(ctx.Context(), "scim.delete_group", "group", req.GroupID, "", cfg.ID.String(), bridge.OutcomeSuccess)
return nil, ctx.NoContent(http.StatusNoContent)
}
// ──────────────────────────────────────────────────
// Auth + Helpers
// ──────────────────────────────────────────────────
// authenticateSCIM validates the Bearer token from the request.
func (p *Plugin) authenticateSCIM(ctx forge.Context) (*SCIMConfig, error) {
auth := ctx.Request().Header.Get("Authorization")
if auth == "" || !strings.HasPrefix(auth, "Bearer ") {
return nil, forge.Unauthorized("SCIM bearer token required")
}
token := strings.TrimPrefix(auth, "Bearer ")
_, cfg, err := p.service.ValidateToken(ctx.Context(), token)
if err != nil {
return nil, forge.Unauthorized("invalid or expired SCIM token")
}
if !cfg.Enabled {
return nil, forge.Forbidden("SCIM configuration is disabled")
}
return cfg, nil
}
// applyUserPatchReplace handles SCIM PATCH replace operations for a user.
// applyUserPatchReplace applies one replace operation to u and returns the
// userName it named, if any, for the caller to change through the
// email-record API rather than by writing the user's email field.
func (p *Plugin) applyUserPatchReplace(u *user.User, op Operation) (userName string) {
switch op.Path {
case "active":
if active, ok := op.Value.(bool); ok {
u.Banned = !active
}
case "name.givenName":
if v, ok := op.Value.(string); ok {
u.FirstName = v
}
case "name.familyName":
if v, ok := op.Value.(string); ok {
u.LastName = v
}
case "userName":
if v, ok := op.Value.(string); ok {
userName = v
}
case "":
// Bulk replace: value is a map of attributes.
if m, ok := op.Value.(map[string]any); ok {
if active, ok := m["active"].(bool); ok {
u.Banned = !active
}
if v, ok := m["userName"].(string); ok {
userName = v
}
if name, ok := m["name"].(map[string]any); ok {
if gn, ok := name["givenName"].(string); ok {
u.FirstName = gn
}
if fn, ok := name["familyName"].(string); ok {
u.LastName = fn
}
}
}
}
return userName
}