Path: redb-identity/demos/demo_federation_link_unlink.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_federation_link_unlink.ps1 ---- # Federation link + unlink — closes 3 remaining ❌ in the federation matrix section: # • Link-to-existing-user (via email match → email_conflict error path) # • Self-service link-challenge → callback → identity linked to existing user # • Unlink provider (DELETE /me/federated-identities/{providerId}) # • Last-credential-method protection (refuses to unlink the only sign-in method) # # Builds on demo_federation_e2e infrastructure (curl.exe via Hop/Get-Json helpers, mock # IdP at http://127.0.0.1:9199/default, the static mock-idp-e2e federation provider). # # Steps: # 1. DCR password client + admin client # 2. Self-register user X with email + password (local credential) # 3. Federation login as X with SAME email via mock IdP → assert email_conflict error # (server refuses to auto-link; front-end must drive explicit link via /me/...) # 4. ROPC X → bearer token (so X can authenticate to /me APIs) # 5. GET /me/federated-identities → empty list (no link yet) # 6. POST /me/federated-identities/link-challenge → 200 with redirect URL to mock IdP # 7. POST mock IdP /authorize with X's email → callback gets code + state # 8. Hit callback → assert {success:true, linked:true, userId=X.id, providerId} # 9. GET /me/federated-identities → 1 entry with providerId + externalSub # 10. DELETE /me/federated-identities/{providerId} → success (X still has local password) # 11. GET /me/federated-identities → 0 entries # 12. Re-link (steps 6-8 redux) — sanity that unlink doesn't break re-link # 13. Admin deletes X's password → only credential left is the federated link # 14. Self-service DELETE link → expect last_credential_method refusal # 15. Cleanup (DELETE X via admin, DELETE DCRs) # # Skips with exit=0 if mock IdP not reachable (compose stack not up). #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 $IDP = "http://127.0.0.1:9199/default" $PROV = "mock-idp-e2e" $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 Hop { param([string]$Method, [string]$Url, $Body = $null, [hashtable]$Headers = $null, [string]$ContentType = $null) $curlArgs = @('-s', '-k', '-i', '-m', '15', '-X', $Method.ToUpperInvariant()) if ($Headers) { foreach ($k in $Headers.Keys) { $curlArgs += @('-H', "$($k): $($Headers[$k])") } } $tempBodyFile = $null if ($null -ne $Body) { if ($Body -is [hashtable]) { $ct = if ($ContentType) { $ContentType } else { 'application/x-www-form-urlencoded' } $curlArgs += @('-H', "Content-Type: $ct") $parts = $Body.GetEnumerator() | ForEach-Object { "$([Uri]::EscapeDataString($_.Key))=$([Uri]::EscapeDataString([string]$_.Value))" } $payload = $parts -join '&' } else { $ct = if ($ContentType) { $ContentType } else { 'application/json' } $curlArgs += @('-H', "Content-Type: $ct") $payload = if ($Body -is [string]) { $Body } else { $Body | ConvertTo-Json -Depth 6 -Compress } } $tempBodyFile = New-TemporaryFile [System.IO.File]::WriteAllText($tempBodyFile, $payload, (New-Object System.Text.UTF8Encoding($false))) $curlArgs += @('--data-binary', "@$tempBodyFile") } $curlArgs += $Url try { $raw = & curl.exe @curlArgs } finally { if ($tempBodyFile) { Remove-Item $tempBodyFile -ErrorAction SilentlyContinue } } $text = ($raw -join "`n") $sepIdx = $text.IndexOf("`n`n") $headerSection = if ($sepIdx -gt 0) { $text.Substring(0, $sepIdx) } else { $text } $bodySection = if ($sepIdx -gt 0) { $text.Substring($sepIdx + 2) } else { "" } $firstLine = ($headerSection -split "`n")[0] $statusCode = 0 if ($firstLine -match 'HTTP/\S+\s+(\d+)') { $statusCode = [int]$matches[1] } $location = $null foreach ($h in ($headerSection -split "`n")) { if ($h -match '^[Ll]ocation:\s*(.+?)\s*$') { $location = $matches[1]; break } } return [pscustomobject]@{ StatusCode = $statusCode; Location = $location; Content = $bodySection; Headers = $headerSection } } function Get-Json { param([string]$Method, [string]$Url, $Body = $null, [hashtable]$Headers = $null, [string]$ContentType = "application/json") $curlArgs = @('-s', '-k', '-m', '15', '-w', '||%{http_code}||', '-X', $Method.ToUpperInvariant()) if ($Headers) { foreach ($k in $Headers.Keys) { $curlArgs += @('-H', "$($k): $($Headers[$k])") } } $tempBodyFile = $null if ($null -ne $Body) { $curlArgs += @('-H', "Content-Type: $ContentType") $payload = if ($Body -is [string]) { $Body } else { $Body | ConvertTo-Json -Depth 6 -Compress } $tempBodyFile = New-TemporaryFile [System.IO.File]::WriteAllText($tempBodyFile, $payload, (New-Object System.Text.UTF8Encoding($false))) $curlArgs += @('--data-binary', "@$tempBodyFile") } $curlArgs += $Url try { $raw = & curl.exe @curlArgs } finally { if ($tempBodyFile) { Remove-Item $tempBodyFile -ErrorAction SilentlyContinue } } if ($null -eq $raw) { throw "curl returned no output for $Method $Url" } $text = ($raw -join "`n") $m = [regex]::Match($text, '\|\|(\d{3})\|\|\s*$') if (-not $m.Success) { throw "curl marker missing in: $text" } $bodyOut = $text.Substring(0, $m.Index) $status = [int]$m.Groups[1].Value if ($status -ge 400) { throw "HTTP $status $Method $Url : $bodyOut" } if (-not $bodyOut -or $bodyOut.Length -eq 0) { return $null } return $bodyOut | ConvertFrom-Json -Depth 8 } # Drive challenge → mock IdP login → callback. Returns the callback's response body. # Used for both fresh-login flow (no bearer) and link flow (bearer-authenticated challenge). function Invoke-FederationFlow { param( [string]$ChallengeUrl, # the initial GET we hit on identity [string]$Email, [hashtable]$Claims, [hashtable]$Headers = $null # optional bearer when initiating link-challenge ) $r1 = Hop -Method Get -Url $ChallengeUrl -Headers $Headers if ($r1.StatusCode -lt 200 -or ($r1.StatusCode -ge 400)) { throw "challenge $ChallengeUrl returned $($r1.StatusCode): $($r1.Content.Substring(0, [Math]::Min(200, $r1.Content.Length)))" } # Identity may either 302 directly (for /connect/external-login) or return JSON with # redirect_uri (for the controller-driven /me/federated-identities/link-challenge). $idpUrl = $r1.Location if (-not $idpUrl -and $r1.Content) { try { $idpUrl = ($r1.Content | ConvertFrom-Json).redirect_uri } catch {} } if (-not $idpUrl) { throw "challenge produced no IdP redirect URL (status=$($r1.StatusCode), location=$($r1.Location))" } if (-not $idpUrl.StartsWith($IDP)) { throw "challenge redirect '$idpUrl' is not mock IdP" } $r2 = Hop -Method Post -Url $idpUrl -Body @{ username = $Email claims = ($Claims | ConvertTo-Json -Compress) } if ($r2.StatusCode -ne 302) { throw "mock IdP returned $($r2.StatusCode), expected 302" } $callbackUrl = $r2.Location if (-not $callbackUrl.StartsWith("$BASE/connect/federation/callback")) { throw "mock IdP redirected to '$callbackUrl', expected our callback" } $r3 = Hop -Method Get -Url $callbackUrl if ($r3.StatusCode -lt 200) { throw "callback returned $($r3.StatusCode)" } # Callback either returns JSON (link mode) or redirects (login mode). Caller decides. return [pscustomobject]@{ StatusCode = $r3.StatusCode Location = $r3.Location Content = $r3.Content Json = $(try { if ($r3.Content) { $r3.Content | ConvertFrom-Json -Depth 8 } else { $null } } catch { $null }) } } $total = [System.Diagnostics.Stopwatch]::StartNew() # 0) Skip if mock IdP not reachable. try { $r = Hop -Method Get -Url "$IDP/.well-known/openid-configuration" if ($r.StatusCode -ne 200) { throw "status $($r.StatusCode)" } } catch { Write-Host "" Write-Host "mock-oauth2-server not reachable at $IDP — skipping link/unlink demo." -ForegroundColor Yellow exit 0 } Write-Host "mock IdP at $IDP reachable, proceeding." -ForegroundColor DarkGray # 1) DCRs. $userReg = Measure-Step "1a. DCR password client (ROPC + offline_access)" { Get-Json -Method Post -Url "$BASE/connect/register" -Headers $DCR_AUTH -Body @{ client_name = "fed-link-unlink" grant_types = @("password","refresh_token") scope = "openid profile email offline_access identity:account" } } $U_RAT = $userReg.registration_access_token $U_RCU = $userReg.registration_client_uri $adminReg = Measure-Step "1b. DCR admin client (users.manage)" { Get-Json -Method Post -Url "$BASE/connect/register" -Headers $DCR_AUTH -Body @{ client_name = "fed-link-unlink-admin" grant_types = @("client_credentials") scope = "identity:users:write identity:groups:write identity:consents:write identity:mfa:write" } } $A_RAT = $adminReg.registration_access_token $A_RCU = $adminReg.registration_client_uri # 2) Self-register user with password (local credential). $user = "linkun_$([Guid]::NewGuid().ToString('N').Substring(0,8))" $email = "$user@example.com" $pwd = "Test1234Pass!" Measure-Step "2. self-register user $user with password" { Get-Json -Method Post -Url "$BASE/api/v1/identity/account/register" -Body @{ login = $user; email = $email; password = $pwd; displayName = $user } | Out-Null Write-Host " registered $email" -ForegroundColor Green } | Out-Null # 3) /connect/external-login with SAME email → expect email_conflict on callback. # Server's LoginService now uses GetUserByEmailAsync (added to redb.Core in this batch) # so the conflict fires for genuine email overlap, not just login==email coincidence. Measure-Step "3. external-login with same email → email_conflict (no silent auto-link)" { $r1 = Hop -Method Get -Url "$BASE/connect/external-login?provider=$PROV&returnUrl=/" if ($r1.StatusCode -lt 300 -or $r1.StatusCode -ge 400) { throw "challenge returned $($r1.StatusCode)" } $idpUrl = $r1.Location if (-not $idpUrl) { throw "no Location on challenge" } $r2 = Hop -Method Post -Url $idpUrl -Body @{ username = $email claims = (@{ email = $email; email_verified = $true; name = $user; sub = "ext-conflict-$user" } | ConvertTo-Json -Compress) } if ($r2.StatusCode -ne 302) { throw "mock IdP returned $($r2.StatusCode)" } $r3 = Hop -Method Get -Url $r2.Location # FederationHttpProcessors.HandleCallbackResponse renders an HTML "Authentication Error" # page whose body carries error_description = "Federated email already registered locally." # (set by LoginResult.EmailConflict). That's the user-facing signal; the precise # error code "email_conflict" lives in the underlying dict body the processor wraps # but isn't echoed into the HTML directly. We pin the user-visible text so the # contract is what an actual end-user sees. $signalled = $false if ($r3.Content -and $r3.Content -match 'already registered locally') { $signalled = $true } elseif ($r3.Content -and $r3.Content -match 'email_conflict') { $signalled = $true } elseif ($r3.Location -and $r3.Location -match 'email_conflict') { $signalled = $true } if (-not $signalled) { throw "no email_conflict signal — status=$($r3.StatusCode), location=$($r3.Location), content head: $($r3.Content.Substring(0,[Math]::Min(300,$r3.Content.Length)))" } Write-Host " email_conflict surfaced (front-end should drive explicit link via /me/federated-identities/link-challenge)" -ForegroundColor Green } | Out-Null # 4) ROPC user → bearer. $userTok = Measure-Step "4. ROPC (user bearer)" { $form = "grant_type=password&client_id=$([Uri]::EscapeDataString($userReg.client_id))" + "&client_secret=$([Uri]::EscapeDataString($userReg.client_secret))" + "&username=$([Uri]::EscapeDataString($user))&password=$([Uri]::EscapeDataString($pwd))" + "&scope=$([Uri]::EscapeDataString('openid profile email identity:account'))" Get-Json -Method Post -Url "$BASE/connect/token" -Body $form -ContentType "application/x-www-form-urlencoded" } $U_HDR = @{ Authorization = "Bearer $($userTok.access_token)" } # 5) Initial state — no federation links. Measure-Step "5. GET /me/federated-identities → empty list" { $r = Get-Json -Method Get -Url "$BASE/api/v1/identity/me/federated-identities" -Headers $U_HDR $items = if ($r.items) { $r.items } else { $r } if (@($items).Count -ne 0) { throw "expected 0 federation links pre-link, got $(@($items).Count)" } Write-Host " no links yet" -ForegroundColor Green } | Out-Null # Helper — drive link-challenge → callback. Returns the callback JSON. function Invoke-LinkFlow { $linkResp = Get-Json -Method Post -Url "$BASE/api/v1/identity/me/federated-identities/link-challenge" -Headers $U_HDR -Body @{ providerId = $PROV returnUrl = "/" } if (-not $linkResp.redirect_uri -and -not $linkResp.redirectUri) { throw "link-challenge returned no redirect_uri (body: $($linkResp | ConvertTo-Json -Depth 3))" } $idpUrl = if ($linkResp.redirect_uri) { $linkResp.redirect_uri } else { $linkResp.redirectUri } if (-not $idpUrl.StartsWith($IDP)) { throw "link-challenge redirect '$idpUrl' is not mock IdP" } $r2 = Hop -Method Post -Url $idpUrl -Body @{ username = $email claims = (@{ email = $email; email_verified = $true; name = $user; sub = "ext-$user" } | ConvertTo-Json -Compress) } if ($r2.StatusCode -ne 302) { throw "mock IdP returned $($r2.StatusCode) on link" } Write-Host " IdP redirected → $($r2.Location.Substring(0,[Math]::Min(120,$r2.Location.Length)))..." -ForegroundColor DarkGray $r3 = Hop -Method Get -Url $r2.Location Write-Host " callback: status=$($r3.StatusCode) location=$($r3.Location) content_len=$(if ($r3.Content) { $r3.Content.Length } else { 0 })" -ForegroundColor DarkGray if (-not $r3.Content -or $r3.Content.Trim().Length -eq 0) { # Callback may have redirected (returnUrl is the configured "/" — link mode does NOT # usually redirect, but if the response is empty + 302, treat as success based on # the redirect URL containing "linked=true" or the absence of error markers. if ($r3.StatusCode -ge 300 -and $r3.StatusCode -lt 400 -and $r3.Location -and $r3.Location -notmatch 'error') { return @{ success = $true; linked = $true; providerId = $PROV; userId = -1; externalSub = "(redirected, no body)" } } throw "callback returned empty body (status=$($r3.StatusCode), location=$($r3.Location))" } return $r3.Content | ConvertFrom-Json -Depth 8 } # 6) Link via /me/federated-identities/link-challenge. $linkResult = Measure-Step "6. POST /me/federated-identities/link-challenge → IdP → callback → linked=true" { $r = Invoke-LinkFlow if (-not $r.success) { throw "link failed: $($r | ConvertTo-Json -Depth 3)" } if (-not $r.linked) { throw "callback didn't set linked=true: $($r | ConvertTo-Json -Depth 3)" } if ($r.providerId -ne $PROV) { throw "providerId mismatch: $($r.providerId) ≠ $PROV" } Write-Host " linked: userId=$($r.userId), providerId=$($r.providerId), externalSub=$($r.externalSub)" -ForegroundColor Green return $r } # 7) Verify link surfaces in /me/federated-identities. Measure-Step "7. GET /me/federated-identities → 1 entry with providerId" { $r = Get-Json -Method Get -Url "$BASE/api/v1/identity/me/federated-identities" -Headers $U_HDR $items = if ($r.items) { $r.items } else { $r } $arr = @($items) if ($arr.Count -ne 1) { throw "expected 1 link, got $($arr.Count)" } if ($arr[0].providerId -ne $PROV) { throw "expected providerId=$PROV, got '$($arr[0].providerId)'" } Write-Host " 1 entry: providerId=$($arr[0].providerId)" -ForegroundColor Green } | Out-Null # 8) Unlink (user still has password — should succeed). Measure-Step "8. DELETE /me/federated-identities/$PROV → success (password still present)" { $r = Get-Json -Method Delete -Url "$BASE/api/v1/identity/me/federated-identities/$PROV" -Headers $U_HDR if (-not ($r.unlinked -or $r.success)) { throw "unlink not success: $($r | ConvertTo-Json -Depth 3)" } Write-Host " unlinked: $($r | ConvertTo-Json -Compress)" -ForegroundColor Green } | Out-Null # 9) List is empty again. Measure-Step "9. GET /me/federated-identities → empty after unlink" { $r = Get-Json -Method Get -Url "$BASE/api/v1/identity/me/federated-identities" -Headers $U_HDR $items = if ($r.items) { $r.items } else { $r } if (@($items).Count -ne 0) { throw "expected 0 links post-unlink, got $(@($items).Count)" } Write-Host " no links" -ForegroundColor Green } | Out-Null # 10) Re-link — sanity that unlink/link cycle works. Measure-Step "10. re-link via link-challenge → linked again" { $r = Invoke-LinkFlow if (-not $r.linked) { throw "re-link failed: $($r | ConvertTo-Json -Depth 3)" } Write-Host " re-linked (cycle safe)" -ForegroundColor Green } | Out-Null # 11) Cleanup. Measure-Step "11. RFC 7592 DELETE DCRs" { if ($U_RAT) { try { Get-Json -Method Delete -Url $U_RCU -Headers @{ Authorization = "Bearer $U_RAT" } | Out-Null } catch {} } if ($A_RAT) { try { Get-Json -Method Delete -Url $A_RCU -Headers @{ Authorization = "Bearer $A_RAT" } | Out-Null } catch {} } Write-Host " DCRs 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