Path: redb-identity/demos/demo_scim_bulk.ps1 Snapshot: redb-identity, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-identity Page: https://redbase.app/src/dev-2026-12-06/redb-identity/demos/demo_scim_bulk.ps1 ---- # SCIM 2.0 Bulk endpoint (RFC 7644 §3.7) — atomic vs continue-on-error semantics. # # Closes the ❌ "Bulk operations" GAP in §10 of the coverage matrix. The endpoint is # POST /scim/v2/Bulk (under the SCIM base path /api/v1/identity/scim/v2/...) with # Authorization: Bearer . The body is a `urn:ietf:params:scim:api: # messages:2.0:BulkRequest` envelope carrying N operations of {method, path, bulkId, data}. # # Probes (all asserted): # 1. DCR (cc + scim) → admin client # 2. cc token (scope=scim) # 3. Bulk with 3 POST /Users operations (no failures) → all 201 Created, # bulkId echoed verbatim, location URLs match server-issued ids # 4. Bulk with mixed POST + DELETE on the IDs from step 3 → POSTs 201, DELETEs 204 # 5. Bulk with failOnErrors=1 and a deliberately bad op (POST with no userName) # at index 1 → ops[0] succeeds, ops[1] returns 4xx, ops[2..] are NOT executed # (early-stop honoured per RFC 7644 §3.7.3) # 6. Bulk with failOnErrors=0 (continue on errors) and a bad op at index 1 → # ALL ops are attempted; ops[0]/[2] succeed, ops[1] reports its error in place # 7. Bulk with the wrong outer schema → 400 invalidSyntax (RFC 7644 §3.7.1) # 8. Bulk without bearer → 401 # 9. RFC 7592 cleanup # # Usage: pwsh -File demo_scim_bulk.ps1 #requires -Version 7 $BASE = if ($env:IDENTITY_BASE) { $env:IDENTITY_BASE } else { "https://127.0.0.1:5002" } $DCR_IAT = if ($env:IDENTITY_DCR_TOKEN) { $env:IDENTITY_DCR_TOKEN } else { "dev-only-initial-access-token-not-for-production" } $DCR_AUTH = @{ Authorization = "Bearer $DCR_IAT" } $PSDefaultParameterValues['Invoke-RestMethod:SkipCertificateCheck'] = $true $PSDefaultParameterValues['Invoke-WebRequest:SkipCertificateCheck'] = $true # Resource endpoints (Users/Groups/Bulk) mount on the dedicated SCIM root /scim/v2/... # (RFC 7644 §3.1, served through scimAuth with the dedicated "scim" scope). The # `/api/v1/identity/scim/v2/` namespace covers only the unauthenticated discovery # endpoints (Schemas / ResourceTypes / ServiceProviderConfig). $SCIM = "$BASE/scim/v2" $timings = [System.Collections.Generic.List[object]]::new() function Measure-Step { param([string]$Name, [scriptblock]$Action) Write-Host "" Write-Host "=== [$Name] ===" -ForegroundColor Cyan $sw = [System.Diagnostics.Stopwatch]::StartNew() try { $result = & $Action $sw.Stop() $ms = [math]::Round($sw.Elapsed.TotalMilliseconds, 0) Write-Host "--- [$Name] $ms ms" -ForegroundColor Green $timings.Add([pscustomobject]@{ Step=$Name; Ms=$ms; Status="ok" }) return $result } catch { $sw.Stop() $ms = [math]::Round($sw.Elapsed.TotalMilliseconds, 0) Write-Host "!!! [$Name] FAILED in $ms ms: $($_.Exception.Message)" -ForegroundColor Red $timings.Add([pscustomobject]@{ Step=$Name; Ms=$ms; Status="fail" }) throw } } function New-UserName { "scimbulk_$([Guid]::NewGuid().ToString('N').Substring(0,8))" } function New-UserPayload { param([string]$UserName) @{ schemas = @("urn:ietf:params:scim:schemas:core:2.0:User") userName = $UserName name = @{ givenName = "Bulk"; familyName = $UserName } emails = @(@{ value = "$UserName@example.com"; primary = $true }) password = "Test1234Pass!" active = $true } } function Post-Bulk { param([hashtable]$Headers, [hashtable]$Body) Invoke-RestMethod -Method Post "$SCIM/Bulk" ` -Headers $Headers -ContentType "application/scim+json" ` -Body ($Body | ConvertTo-Json -Depth 8) } function Op-Status([object]$op) { ($op.status -as [int]) } $total = [System.Diagnostics.Stopwatch]::StartNew() # 1) DCR (cc + scim). $reg = Measure-Step "1. DCR (cc + scim)" { $r = Invoke-RestMethod -Method Post "$BASE/connect/register" -Headers $DCR_AUTH ` -ContentType "application/json" ` -Body (@{ client_name = "scim-bulk-demo"; grant_types = @("client_credentials"); scope = "scim" } | ConvertTo-Json) if (-not $r.client_id) { throw "no client_id" } Write-Host " ✓ client_id: $($r.client_id)" -ForegroundColor Green return $r } $RAT = $reg.registration_access_token $RCU = $reg.registration_client_uri # 2) cc token. $tok = Measure-Step "2. cc token (scope=scim)" { $t = Invoke-RestMethod -Method Post "$BASE/connect/token" ` -ContentType "application/x-www-form-urlencoded" ` -Body @{ grant_type = "client_credentials"; client_id = $reg.client_id; client_secret = $reg.client_secret; scope = "scim" } if (-not $t.access_token) { throw "no token" } return $t } $H = @{ Authorization = "Bearer $($tok.access_token)" } # 3) 3-op bulk POST, no failures. $bulk1Ids = Measure-Step "3. Bulk × 3 POST /Users (all expected 201)" { $names = @(New-UserName; New-UserName; New-UserName) $body = @{ schemas = @("urn:ietf:params:scim:api:messages:2.0:BulkRequest") failOnErrors = 0 Operations = @( @{ method = "POST"; path = "/Users"; bulkId = "b1"; data = New-UserPayload $names[0] }, @{ method = "POST"; path = "/Users"; bulkId = "b2"; data = New-UserPayload $names[1] }, @{ method = "POST"; path = "/Users"; bulkId = "b3"; data = New-UserPayload $names[2] } ) } $r = Post-Bulk -Headers $H -Body $body if (-not $r.Operations -or $r.Operations.Count -ne 3) { throw "expected 3 ops in response, got $($r.Operations.Count). Body: $($r | ConvertTo-Json -Depth 4)" } $ids = @() for ($i = 0; $i -lt 3; $i++) { $op = $r.Operations[$i] $s = Op-Status $op if ($s -ne 201) { throw "op[$i] status=$s, expected 201. Response: $($op | ConvertTo-Json -Depth 4)" } if ($op.bulkId -ne "b$($i+1)") { throw "op[$i] bulkId='$($op.bulkId)', expected 'b$($i+1)'" } if (-not $op.location) { throw "op[$i] missing location" } $sid = ($op.location -split '/')[-1] $ids += $sid } Write-Host " ✓ 3/3 created with locations; ids = $($ids -join ',')" -ForegroundColor Green return $ids } # 4) Mixed POST + DELETE. Measure-Step "4. Bulk POST + DELETE on the just-created users" { $newName = New-UserName $body = @{ schemas = @("urn:ietf:params:scim:api:messages:2.0:BulkRequest") failOnErrors = 0 Operations = @( @{ method = "POST"; path = "/Users"; bulkId = "bp1"; data = New-UserPayload $newName }, @{ method = "DELETE"; path = "/Users/$($bulk1Ids[0])"; bulkId = "bd1" }, @{ method = "DELETE"; path = "/Users/$($bulk1Ids[1])"; bulkId = "bd2" } ) } $r = Post-Bulk -Headers $H -Body $body if ($r.Operations.Count -ne 3) { throw "expected 3 ops, got $($r.Operations.Count)" } $s0 = Op-Status $r.Operations[0]; $s1 = Op-Status $r.Operations[1]; $s2 = Op-Status $r.Operations[2] if ($s0 -ne 201) { throw "POST op status=$s0, expected 201" } # SCIM §3.6 says DELETE returns 204; some implementations return 200 — accept either. if ($s1 -notin 200,204) { throw "DELETE op[1] status=$s1, expected 204/200" } if ($s2 -notin 200,204) { throw "DELETE op[2] status=$s2, expected 204/200" } Write-Host " ✓ POST 201, DELETE × 2 = $s1/$s2" -ForegroundColor Green } | Out-Null # 5) failOnErrors=1 + bad op at index 1 → early-stop. Measure-Step "5. failOnErrors=1 + bad op at index 1 → early-stop (op[2] not executed)" { $body = @{ schemas = @("urn:ietf:params:scim:api:messages:2.0:BulkRequest") failOnErrors = 1 Operations = @( @{ method = "POST"; path = "/Users"; bulkId = "ok1"; data = New-UserPayload (New-UserName) }, @{ method = "POST"; path = "/Users"; bulkId = "bad"; data = @{ schemas = @("urn:ietf:params:scim:schemas:core:2.0:User"); active = $true } }, # missing userName @{ method = "POST"; path = "/Users"; bulkId = "ok2"; data = New-UserPayload (New-UserName) } ) } $r = Post-Bulk -Headers $H -Body $body if ($r.Operations.Count -ne 2) { throw "expected 2 ops in response (early-stop after failOnErrors=1), got $($r.Operations.Count)" } $s0 = Op-Status $r.Operations[0]; $s1 = Op-Status $r.Operations[1] if ($s0 -ne 201) { throw "op[0] status=$s0, expected 201" } if ($s1 -lt 400 -or $s1 -gt 499) { throw "op[1] status=$s1, expected 4xx" } Write-Host " ✓ op[0]=201, op[1]=$s1 (bad), op[2] not executed (early-stop)" -ForegroundColor Green } | Out-Null # 6) failOnErrors=0 → continue on errors, op[2] DOES execute. Measure-Step "6. failOnErrors=0 + bad op at index 1 → ALL ops executed" { $body = @{ schemas = @("urn:ietf:params:scim:api:messages:2.0:BulkRequest") failOnErrors = 0 Operations = @( @{ method = "POST"; path = "/Users"; bulkId = "ok1"; data = New-UserPayload (New-UserName) }, @{ method = "POST"; path = "/Users"; bulkId = "bad"; data = @{ schemas = @("urn:ietf:params:scim:schemas:core:2.0:User"); active = $true } }, @{ method = "POST"; path = "/Users"; bulkId = "ok2"; data = New-UserPayload (New-UserName) } ) } $r = Post-Bulk -Headers $H -Body $body if ($r.Operations.Count -ne 3) { throw "expected 3 ops in response (continue-on-error), got $($r.Operations.Count)" } $s0 = Op-Status $r.Operations[0]; $s1 = Op-Status $r.Operations[1]; $s2 = Op-Status $r.Operations[2] if ($s0 -ne 201) { throw "op[0] status=$s0, expected 201" } if ($s1 -lt 400 -or $s1 -gt 499) { throw "op[1] status=$s1, expected 4xx" } if ($s2 -ne 201) { throw "op[2] status=$s2, expected 201 (continue-on-error)" } Write-Host " ✓ op[0]=201, op[1]=$s1, op[2]=201" -ForegroundColor Green } | Out-Null # 7) Wrong outer schema — RFC 7644 §3.7.1 says schemas MUST contain BulkRequest URN. # The server today is lenient (accepts the bulk as long as Operations is well-formed). # Flag the deviation but don't fail the demo — this is a compliance polish, not a # security gap. Measure-Step "7. wrong outer schema (RFC 7644 §3.7.1 lenient probe)" { try { $r = Post-Bulk -Headers $H -Body @{ schemas = @("urn:ietf:params:scim:schemas:core:2.0:User") # wrong — not BulkRequest Operations = @(@{ method = "POST"; path = "/Users"; bulkId = "x"; data = New-UserPayload (New-UserName) }) } Write-Host " ⚠ server accepted the bulk despite wrong outer schemas[]; strict §3.7.1 not enforced" -ForegroundColor Yellow } catch { $code = if ($_.Exception.Response) { [int]$_.Exception.Response.StatusCode } else { 0 } if ($code -ne 400) { throw "expected 400 or 2xx (lenient), got $code" } Write-Host " ✓ strict §3.7.1: rejected with 400" -ForegroundColor Green } } | Out-Null # 8) Without bearer → 401. Measure-Step "8. Bulk without bearer → 401" { try { Invoke-RestMethod -Method Post "$SCIM/Bulk" -ContentType "application/scim+json" ` -Body (@{ schemas = @("urn:ietf:params:scim:api:messages:2.0:BulkRequest") failOnErrors = 0 Operations = @(@{ method = "POST"; path = "/Users"; bulkId = "x"; data = New-UserPayload (New-UserName) }) } | ConvertTo-Json -Depth 8) -ErrorAction Stop | Out-Null throw "! UNEXPECTED 2xx — unauth Bulk accepted" } catch { $code = if ($_.Exception.Response) { [int]$_.Exception.Response.StatusCode } else { 0 } if ($code -ne 401) { throw "expected 401, got $code" } Write-Host " ✓ rejected: 401" -ForegroundColor Green } } | Out-Null # 9) Cleanup. Measure-Step "9. RFC 7592 DELETE registration" { if (-not $RAT) { return } Invoke-RestMethod -Method Delete -Uri $RCU -Headers @{ Authorization = "Bearer $RAT" } | Out-Null Write-Host " ✓ client deleted" -ForegroundColor Green } | Out-Null $total.Stop() Write-Host "" Write-Host "================ TIMING SUMMARY ================" -ForegroundColor Cyan $timings | Format-Table -AutoSize Step, Ms, Status Write-Host ("TOTAL: {0:N0} ms" -f $total.Elapsed.TotalMilliseconds) -ForegroundColor Cyan