-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscim.zod.ts
More file actions
1064 lines (934 loc) · 23.5 KB
/
scim.zod.ts
File metadata and controls
1064 lines (934 loc) · 23.5 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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { z } from 'zod';
/**
* # SCIM 2.0 Protocol Implementation
*
* System for Cross-domain Identity Management (SCIM) 2.0 specification
* implementation for ObjectStack.
*
* ## Overview
*
* SCIM 2.0 is an HTTP-based protocol for managing user and group identities
* across domains. It provides a standardized REST API for user provisioning,
* de-provisioning, and synchronization.
*
* ## Use Cases
*
* 1. **Enterprise SSO Integration**
* - Integrate with Okta, Azure AD, OneLogin
* - Automatic user provisioning from corporate directory
* - Just-in-Time (JIT) user creation on first login
*
* 2. **User Lifecycle Management**
* - Automatically create users when they join organization
* - Update user attributes when they change roles
* - Deactivate users when they leave organization
*
* 3. **Group/Department Synchronization**
* - Sync organizational structure from AD/LDAP
* - Maintain group memberships automatically
* - Map corporate roles to application permissions
*
* 4. **Compliance & Audit**
* - Maintain accurate user directory
* - Track all identity changes
* - Meet SOX/HIPAA requirements for user management
*
* ## Specification References
*
* - **RFC 7643**: SCIM Core Schema
* - **RFC 7644**: SCIM Protocol
* - **RFC 7642**: SCIM Requirements
*
* ## Industry Implementations
*
* - **Okta**: Leading SCIM provider
* - **Azure AD**: Microsoft's identity platform
* - **OneLogin**: Enterprise SSO provider
* - **Google Workspace**: Google's identity management
*
* @see https://datatracker.ietf.org/doc/html/rfc7643
* @see https://datatracker.ietf.org/doc/html/rfc7644
*/
/**
* SCIM Schema URIs
* Standard schema identifiers defined in RFC 7643
*/
export const SCIM_SCHEMAS = {
USER: 'urn:ietf:params:scim:schemas:core:2.0:User',
GROUP: 'urn:ietf:params:scim:schemas:core:2.0:Group',
ENTERPRISE_USER: 'urn:ietf:params:scim:schemas:extension:enterprise:2.0:User',
RESOURCE_TYPE: 'urn:ietf:params:scim:schemas:core:2.0:ResourceType',
SERVICE_PROVIDER_CONFIG: 'urn:ietf:params:scim:schemas:core:2.0:ServiceProviderConfig',
SCHEMA: 'urn:ietf:params:scim:schemas:core:2.0:Schema',
LIST_RESPONSE: 'urn:ietf:params:scim:api:messages:2.0:ListResponse',
PATCH_OP: 'urn:ietf:params:scim:api:messages:2.0:PatchOp',
BULK_REQUEST: 'urn:ietf:params:scim:api:messages:2.0:BulkRequest',
BULK_RESPONSE: 'urn:ietf:params:scim:api:messages:2.0:BulkResponse',
ERROR: 'urn:ietf:params:scim:api:messages:2.0:Error',
} as const;
/**
* SCIM Meta Schema
* Common metadata for all SCIM resources
*/
export const SCIMMetaSchema = z.object({
/**
* Resource type name
* @example "User", "Group"
*/
resourceType: z.string()
.optional()
.describe('Resource type'),
/**
* Resource creation timestamp (ISO 8601)
*/
created: z.string()
.datetime()
.optional()
.describe('Creation timestamp'),
/**
* Last modification timestamp (ISO 8601)
*/
lastModified: z.string()
.datetime()
.optional()
.describe('Last modification timestamp'),
/**
* Resource location URI
* Absolute URL to the resource
*/
location: z.string()
.url()
.optional()
.describe('Resource location URI'),
/**
* Entity tag for optimistic concurrency control
* Used with If-Match header for conditional updates
*/
version: z.string()
.optional()
.describe('Entity tag (ETag) for concurrency control'),
});
export type SCIMMeta = z.infer<typeof SCIMMetaSchema>;
/**
* SCIM Name Schema
* Structured name components
*/
export const SCIMNameSchema = z.object({
/**
* Full name formatted for display
* @example "Ms. Barbara Jane Jensen III"
*/
formatted: z.string()
.optional()
.describe('Formatted full name'),
/**
* Family name (surname)
* @example "Jensen"
*/
familyName: z.string()
.optional()
.describe('Family name (last name)'),
/**
* Given name (first name)
* @example "Barbara"
*/
givenName: z.string()
.optional()
.describe('Given name (first name)'),
/**
* Middle name
* @example "Jane"
*/
middleName: z.string()
.optional()
.describe('Middle name'),
/**
* Honorific prefix
* @example "Ms.", "Dr.", "Prof."
*/
honorificPrefix: z.string()
.optional()
.describe('Honorific prefix (Mr., Ms., Dr.)'),
/**
* Honorific suffix
* @example "III", "Jr.", "Sr."
*/
honorificSuffix: z.string()
.optional()
.describe('Honorific suffix (Jr., Sr.)'),
});
export type SCIMName = z.infer<typeof SCIMNameSchema>;
/**
* SCIM Email Schema
* Multi-valued email address
*/
export const SCIMEmailSchema = z.object({
/**
* Email address value
*/
value: z.string()
.email()
.describe('Email address'),
/**
* Email type
* @example "work", "home", "other"
*/
type: z.enum(['work', 'home', 'other'])
.optional()
.describe('Email type'),
/**
* Display label for the email
*/
display: z.string()
.optional()
.describe('Display label'),
/**
* Whether this is the primary email
*/
primary: z.boolean()
.optional()
.default(false)
.describe('Primary email indicator'),
});
export type SCIMEmail = z.infer<typeof SCIMEmailSchema>;
/**
* SCIM Phone Number Schema
* Multi-valued phone number
*/
export const SCIMPhoneNumberSchema = z.object({
/**
* Phone number value
* Format is not enforced to support international numbers
*/
value: z.string()
.describe('Phone number'),
/**
* Phone type
*/
type: z.enum(['work', 'home', 'mobile', 'fax', 'pager', 'other'])
.optional()
.describe('Phone number type'),
/**
* Display label for the phone number
*/
display: z.string()
.optional()
.describe('Display label'),
/**
* Whether this is the primary phone
*/
primary: z.boolean()
.optional()
.default(false)
.describe('Primary phone indicator'),
});
export type SCIMPhoneNumber = z.infer<typeof SCIMPhoneNumberSchema>;
/**
* SCIM Address Schema
* Multi-valued physical mailing address
*/
export const SCIMAddressSchema = z.object({
/**
* Full mailing address formatted for display
*/
formatted: z.string()
.optional()
.describe('Formatted address'),
/**
* Full street address
*/
streetAddress: z.string()
.optional()
.describe('Street address'),
/**
* City or locality
*/
locality: z.string()
.optional()
.describe('City/Locality'),
/**
* State or region
*/
region: z.string()
.optional()
.describe('State/Region'),
/**
* Zip code or postal code
*/
postalCode: z.string()
.optional()
.describe('Postal code'),
/**
* Country
*/
country: z.string()
.optional()
.describe('Country'),
/**
* Address type
*/
type: z.enum(['work', 'home', 'other'])
.optional()
.describe('Address type'),
/**
* Whether this is the primary address
*/
primary: z.boolean()
.optional()
.default(false)
.describe('Primary address indicator'),
});
export type SCIMAddress = z.infer<typeof SCIMAddressSchema>;
/**
* SCIM Group Reference
* Reference to a group the user belongs to
*/
export const SCIMGroupReferenceSchema = z.object({
/**
* Group identifier
*/
value: z.string()
.describe('Group ID'),
/**
* Direct reference to the group resource
*/
$ref: z.string()
.url()
.optional()
.describe('URI reference to the group'),
/**
* Human-readable group name
*/
display: z.string()
.optional()
.describe('Group display name'),
/**
* Type of group
*/
type: z.enum(['direct', 'indirect'])
.optional()
.describe('Membership type'),
});
export type SCIMGroupReference = z.infer<typeof SCIMGroupReferenceSchema>;
/**
* SCIM Enterprise User Extension
* Enterprise-specific user attributes
*/
export const SCIMEnterpriseUserSchema = z.object({
/**
* Employee number
*/
employeeNumber: z.string()
.optional()
.describe('Employee number'),
/**
* Cost center
*/
costCenter: z.string()
.optional()
.describe('Cost center'),
/**
* Organization unit
*/
organization: z.string()
.optional()
.describe('Organization'),
/**
* Division
*/
division: z.string()
.optional()
.describe('Division'),
/**
* Department
*/
department: z.string()
.optional()
.describe('Department'),
/**
* Manager reference
*/
manager: z.object({
value: z.string().describe('Manager ID'),
$ref: z.string().url().optional().describe('Manager URI'),
displayName: z.string().optional().describe('Manager name'),
})
.optional()
.describe('Manager reference'),
});
export type SCIMEnterpriseUser = z.infer<typeof SCIMEnterpriseUserSchema>;
/**
* SCIM User Schema (Core)
* Complete SCIM 2.0 User resource
*/
export const SCIMUserSchema = z.object({
/**
* SCIM schema URIs
* Must include at minimum the core User schema URI
*/
schemas: z.array(z.string())
.min(1)
.refine(
(schemas) => schemas.includes(SCIM_SCHEMAS.USER),
'Must include core User schema URI'
)
.default([SCIM_SCHEMAS.USER])
.describe('SCIM schema URIs (must include User schema)'),
/**
* Unique identifier
*/
id: z.string()
.optional()
.describe('Unique resource identifier'),
/**
* External identifier
* Identifier from the provisioning client
*/
externalId: z.string()
.optional()
.describe('External identifier from client system'),
/**
* Unique username
* REQUIRED for user creation
*/
userName: z.string()
.describe('Unique username (REQUIRED)'),
/**
* Structured name
*/
name: SCIMNameSchema
.optional()
.describe('Structured name components'),
/**
* Display name
*/
displayName: z.string()
.optional()
.describe('Display name for UI'),
/**
* Nickname or casual name
*/
nickName: z.string()
.optional()
.describe('Nickname'),
/**
* Profile URL
*/
profileUrl: z.string()
.url()
.optional()
.describe('Profile page URL'),
/**
* Job title
*/
title: z.string()
.optional()
.describe('Job title'),
/**
* User type (employee, contractor, etc.)
*/
userType: z.string()
.optional()
.describe('User type (employee, contractor)'),
/**
* Preferred language (ISO 639-1)
*/
preferredLanguage: z.string()
.optional()
.describe('Preferred language (ISO 639-1)'),
/**
* Locale (e.g., en-US)
*/
locale: z.string()
.optional()
.describe('Locale (e.g., en-US)'),
/**
* Timezone (e.g., America/Los_Angeles)
*/
timezone: z.string()
.optional()
.describe('Timezone'),
/**
* Account active status
*/
active: z.boolean()
.optional()
.default(true)
.describe('Account active status'),
/**
* Password (write-only, never returned)
*/
password: z.string()
.optional()
.describe('Password (write-only)'),
/**
* Email addresses (multi-valued)
*/
emails: z.array(SCIMEmailSchema)
.optional()
.describe('Email addresses'),
/**
* Phone numbers (multi-valued)
*/
phoneNumbers: z.array(SCIMPhoneNumberSchema)
.optional()
.describe('Phone numbers'),
/**
* Instant messaging addresses
*/
ims: z.array(z.object({
value: z.string(),
type: z.string().optional(),
primary: z.boolean().optional(),
}))
.optional()
.describe('IM addresses'),
/**
* Photos (profile pictures)
*/
photos: z.array(z.object({
value: z.string().url(),
type: z.enum(['photo', 'thumbnail']).optional(),
primary: z.boolean().optional(),
}))
.optional()
.describe('Photo URLs'),
/**
* Physical addresses
*/
addresses: z.array(SCIMAddressSchema)
.optional()
.describe('Physical addresses'),
/**
* Group memberships
*/
groups: z.array(SCIMGroupReferenceSchema)
.optional()
.describe('Group memberships'),
/**
* User entitlements
*/
entitlements: z.array(z.object({
value: z.string(),
type: z.string().optional(),
primary: z.boolean().optional(),
}))
.optional()
.describe('Entitlements'),
/**
* User roles
*/
roles: z.array(z.object({
value: z.string(),
type: z.string().optional(),
primary: z.boolean().optional(),
}))
.optional()
.describe('Roles'),
/**
* X509 certificates
*/
x509Certificates: z.array(z.object({
value: z.string(),
type: z.string().optional(),
primary: z.boolean().optional(),
}))
.optional()
.describe('X509 certificates'),
/**
* Resource metadata
*/
meta: SCIMMetaSchema
.optional()
.describe('Resource metadata'),
/**
* Enterprise user extension
* Only present when enterprise extension is used
*/
[SCIM_SCHEMAS.ENTERPRISE_USER]: SCIMEnterpriseUserSchema
.optional()
.describe('Enterprise user attributes'),
}).superRefine((data, ctx) => {
// Validate that enterprise extension schema URI is present when extension data is provided
const hasEnterpriseExtension = data[SCIM_SCHEMAS.ENTERPRISE_USER] != null;
if (!hasEnterpriseExtension) {
return;
}
const schemas = data.schemas || [];
if (!schemas.includes(SCIM_SCHEMAS.ENTERPRISE_USER)) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
path: ['schemas'],
message: `schemas must include "${SCIM_SCHEMAS.ENTERPRISE_USER}" when enterprise user extension attributes are present`,
});
}
});
export type SCIMUser = z.infer<typeof SCIMUserSchema>;
/**
* SCIM Member Reference
* Reference to a member in a group
*/
export const SCIMMemberReferenceSchema = z.object({
/**
* Member identifier
*/
value: z.string()
.describe('Member ID'),
/**
* Direct reference to the member resource
*/
$ref: z.string()
.url()
.optional()
.describe('URI reference to the member'),
/**
* Member type (User or Group for nested groups)
*/
type: z.enum(['User', 'Group'])
.optional()
.describe('Member type'),
/**
* Human-readable member name
*/
display: z.string()
.optional()
.describe('Member display name'),
});
export type SCIMMemberReference = z.infer<typeof SCIMMemberReferenceSchema>;
/**
* SCIM Group Schema
* Complete SCIM 2.0 Group resource
*/
export const SCIMGroupSchema = z.object({
/**
* SCIM schema URIs
* Must include at minimum the core Group schema URI
*/
schemas: z.array(z.string())
.min(1)
.refine(
(schemas) => schemas.includes(SCIM_SCHEMAS.GROUP),
'Must include core Group schema URI'
)
.default([SCIM_SCHEMAS.GROUP])
.describe('SCIM schema URIs (must include Group schema)'),
/**
* Unique identifier
*/
id: z.string()
.optional()
.describe('Unique resource identifier'),
/**
* External identifier
*/
externalId: z.string()
.optional()
.describe('External identifier from client system'),
/**
* Group display name
* REQUIRED for group creation
*/
displayName: z.string()
.describe('Group display name (REQUIRED)'),
/**
* Group members
*/
members: z.array(SCIMMemberReferenceSchema)
.optional()
.describe('Group members'),
/**
* Resource metadata
*/
meta: SCIMMetaSchema
.optional()
.describe('Resource metadata'),
});
export type SCIMGroup = z.infer<typeof SCIMGroupSchema>;
/**
* SCIM Resource Union Type
* Known SCIM resource types for type-safe list responses
*/
export type SCIMResource = SCIMUser | SCIMGroup;
/**
* SCIM List Response
* Paginated list of resources
*
* Generic type T allows for type-safe responses when the resource type is known.
* For mixed resource types, use SCIMResource union.
*/
export const SCIMListResponseSchema = z.object({
/**
* SCIM schema URI
*/
schemas: z.array(z.string())
.min(1)
.refine(
(schemas) => schemas.includes(SCIM_SCHEMAS.LIST_RESPONSE),
{ message: `schemas must include ${SCIM_SCHEMAS.LIST_RESPONSE}` }
)
.default([SCIM_SCHEMAS.LIST_RESPONSE])
.describe('SCIM schema URIs'),
/**
* Total number of results matching the query
*/
totalResults: z.number()
.int()
.min(0)
.describe('Total results count'),
/**
* Resources returned in this response
* Use SCIMListResponseOf<T> for type-safe responses
*/
Resources: z.array(z.union([SCIMUserSchema, SCIMGroupSchema, z.record(z.string(), z.unknown())]))
.describe('Resources array (Users, Groups, or custom resources)'),
/**
* 1-based index of the first result
*/
startIndex: z.number()
.int()
.min(1)
.optional()
.describe('Start index (1-based)'),
/**
* Number of resources per page
*/
itemsPerPage: z.number()
.int()
.min(0)
.optional()
.describe('Items per page'),
});
export type SCIMListResponse = z.infer<typeof SCIMListResponseSchema>;
/**
* SCIM Error Response
* Error response format
*/
export const SCIMErrorSchema = z.object({
/**
* SCIM schema URI
*/
schemas: z.array(z.string())
.min(1)
.refine(
(schemas) => schemas.includes(SCIM_SCHEMAS.ERROR),
{ message: `schemas must include ${SCIM_SCHEMAS.ERROR}` }
)
.default([SCIM_SCHEMAS.ERROR])
.describe('SCIM schema URIs'),
/**
* HTTP status code
*/
status: z.number()
.int()
.min(400)
.max(599)
.describe('HTTP status code'),
/**
* SCIM error type
*/
scimType: z.enum([
'invalidFilter',
'tooMany',
'uniqueness',
'mutability',
'invalidSyntax',
'invalidPath',
'noTarget',
'invalidValue',
'invalidVers',
'sensitive',
])
.optional()
.describe('SCIM error type'),
/**
* Human-readable error description
*/
detail: z.string()
.optional()
.describe('Error detail message'),
});
export type SCIMError = z.infer<typeof SCIMErrorSchema>;
/**
* SCIM Patch Operation
* For PATCH requests
*/
export const SCIMPatchOperationSchema = z.object({
/**
* Operation type
*/
op: z.enum(['add', 'remove', 'replace'])
.describe('Operation type'),
/**
* Attribute path to modify
*/
path: z.string()
.optional()
.describe('Attribute path (optional for add)'),
/**
* Value to set
*/
value: z.unknown()
.optional()
.describe('Value to set'),
});
export type SCIMPatchOperation = z.infer<typeof SCIMPatchOperationSchema>;
/**
* SCIM Patch Request
*/
export const SCIMPatchRequestSchema = z.object({
/**
* SCIM schema URI
*/
schemas: z.array(z.string())
.min(1)
.refine(
(schemas) => schemas.includes(SCIM_SCHEMAS.PATCH_OP),
{ message: 'SCIM PATCH requests must include the PatchOp schema URI' }
)
.default([SCIM_SCHEMAS.PATCH_OP])
.describe('SCIM schema URIs'),
/**
* Array of patch operations
*/
Operations: z.array(SCIMPatchOperationSchema)
.min(1)
.describe('Patch operations'),
});
export type SCIMPatchRequest = z.infer<typeof SCIMPatchRequestSchema>;
/**
* Helper factory for creating SCIM resources
*/
export const SCIM = {
/**
* Create a basic SCIM user
*/
user: (userName: string, email: string, givenName?: string, familyName?: string): SCIMUser => ({
schemas: [SCIM_SCHEMAS.USER],
userName,
emails: [{ value: email, type: 'work', primary: true }],
name: {
givenName,
familyName,
},
active: true,
}),
/**
* Create a SCIM group
*/
group: (displayName: string, members?: SCIMMemberReference[]): SCIMGroup => ({
schemas: [SCIM_SCHEMAS.GROUP],
displayName,
members: members || [],
}),
/**
* Create a list response
*/
listResponse: <T>(resources: T[], totalResults?: number): SCIMListResponse => ({
schemas: [SCIM_SCHEMAS.LIST_RESPONSE],
totalResults: totalResults ?? resources.length,
Resources: resources as Array<SCIMResource | Record<string, any>>,
startIndex: 1,
itemsPerPage: resources.length,
}),
/**
* Create an error response
*/
error: (
status: number,
detail: string,
scimType?: 'invalidFilter' | 'tooMany' | 'uniqueness' | 'mutability' |
'invalidSyntax' | 'invalidPath' | 'noTarget' | 'invalidValue' |
'invalidVers' | 'sensitive'
): SCIMError => ({
schemas: [SCIM_SCHEMAS.ERROR],
status,
detail,
scimType,
}),
} as const;
// ─── SCIM 2.0 Bulk Operations (RFC 7644 §3.7) ──────────────────────────────
/**
* SCIM Bulk Operation Schema
* A single operation within a bulk request
*/
export const SCIMBulkOperationSchema = z.object({
/** HTTP method for this operation */
method: z.enum(['POST', 'PUT', 'PATCH', 'DELETE'])
.describe('HTTP method for the bulk operation'),
/** Resource path (e.g. /Users, /Groups/{id}) */
path: z.string()
.describe('Resource endpoint path (e.g. /Users, /Groups/{id})'),
/** Client-assigned identifier for cross-referencing operations */
bulkId: z.string()
.optional()
.describe('Client-assigned ID for cross-referencing between operations'),
/** Request body for POST/PUT/PATCH operations */
data: z.record(z.string(), z.unknown())
.optional()
.describe('Request body for POST/PUT/PATCH operations'),
/** ETag value for optimistic concurrency control */
version: z.string()
.optional()
.describe('ETag for optimistic concurrency control'),
});
export type SCIMBulkOperation = z.infer<typeof SCIMBulkOperationSchema>;
/**
* SCIM Bulk Request Schema
* Batch multiple SCIM operations into a single HTTP request
*/
export const SCIMBulkRequestSchema = z.object({
/** SCIM schema URI for bulk request */
schemas: z.array(z.literal(SCIM_SCHEMAS.BULK_REQUEST))