Path: redb-identity/demos/demo_session_lifecycle.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_session_lifecycle.ps1 ---- # S-track — session lifecycle (LastAccessedAt + idle/absolute timeouts). # # Probes: # 1. Admin DCR cc + identity:{users,sessions}.manage # 2. Admin cc token # 3. Seed user # 4. DCR ROPC client (password + refresh_token + openid) # 5. ROPC login → session row created with LastAccessedAt ~= DateCreate # 6. Admin: GET /users/{id}/sessions — should show 1 session, # LastAccessedBy = "create" or "password" # 7. Sleep 2 s # 8. refresh_token grant against the same client # 9. Admin: GET sessions again — LastAccessedAt should have moved # forward by ≥2 s, LastAccessedBy = "refresh_token" # 10. Cleanup: delete user # # Usage: pwsh -File demo_session_lifecycle.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 $REDIRECT_CB = if ($BASE -like 'https:*') { 'https://localhost:9999/cb' } else { 'http://localhost:9999/cb' } $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() Write-Host ("--- [$Name] {0:N0} ms" -f $sw.Elapsed.TotalMilliseconds) -ForegroundColor Green $timings.Add([pscustomobject]@{ Step=$Name; Ms=[math]::Round($sw.Elapsed.TotalMilliseconds,0); Status="ok" }) return $result } catch { $sw.Stop() Write-Host ("!!! [$Name] FAILED in {0:N0} ms: {1}" -f $sw.Elapsed.TotalMilliseconds, $_.Exception.Message) -ForegroundColor Red $timings.Add([pscustomobject]@{ Step=$Name; Ms=[math]::Round($sw.Elapsed.TotalMilliseconds,0); Status="fail" }) throw } } $total = [System.Diagnostics.Stopwatch]::StartNew() # 1) Admin DCR $adminReg = Measure-Step "1. admin DCR cc + users.manage + sessions.manage" { Invoke-RestMethod -Method Post "$BASE/connect/register" -Headers $DCR_AUTH -ContentType "application/json" ` -Body (@{ client_name = "session-lifecycle-admin" grant_types = @("client_credentials") scope = "identity:users:write identity:groups:write identity:consents:write identity:mfa:write identity:sessions:write identity:tokens:write" } | ConvertTo-Json) } $adminTok = Measure-Step "2. admin cc token" { Invoke-RestMethod -Method Post "$BASE/connect/token" -ContentType "application/x-www-form-urlencoded" ` -Body @{ grant_type = "client_credentials" client_id = $adminReg.client_id client_secret = $adminReg.client_secret scope = "identity:users:write identity:groups:write identity:consents:write identity:mfa:write identity:sessions:write identity:tokens:write" } } $ADMIN = @{ Authorization = "Bearer $($adminTok.access_token)"; "Content-Type" = "application/json" } # 3) Seed user $user = "sess_$([Guid]::NewGuid().ToString('N').Substring(0,8))" $pwd = "Test1234Pass!" $UserId = Measure-Step "3. seed user $user" { $r = Invoke-RestMethod -Method Post "$BASE/api/v1/identity/account/register" -ContentType "application/json" ` -Body (@{ login = $user; email = "$user@example.com"; password = $pwd; displayName = $user } | ConvertTo-Json) if (-not $r.success -or -not $r.userId) { throw "register failed: $($r | ConvertTo-Json)" } return [long]$r.userId } # 4) DCR ROPC client $ropcReg = Measure-Step "4. DCR ropc client" { Invoke-RestMethod -Method Post "$BASE/connect/register" -Headers $DCR_AUTH -ContentType "application/json" ` -Body (@{ client_name = "sess-ropc" redirect_uris = @($REDIRECT_CB) grant_types = @("password","refresh_token") scope = "openid offline_access" } | ConvertTo-Json) } # 5) ROPC login $tok = Measure-Step "5. ROPC login" { $t = Invoke-RestMethod -Method Post "$BASE/connect/token" -ContentType "application/x-www-form-urlencoded" ` -Body @{ grant_type = "password" client_id = $ropcReg.client_id client_secret = $ropcReg.client_secret username = $user password = $pwd scope = "openid offline_access" } if (-not $t.access_token) { throw "no access_token" } if (-not $t.refresh_token) { throw "no refresh_token" } return $t } # NOTE on architecture: SessionProps rows are created only by INTERACTIVE # flows (cookie LoginProcessor / FederationCallbackProcessor / MFA recovery). # Raw ROPC does NOT create a session row — that's intentional: a session # represents a browser interaction, not a token-only API exchange. The S-track # touch hook still fires on every refresh_token grant; it's just a no-op # when the principal carries no `sid` claim. # # So this probe verifies the SHAPE end-to-end: GET sessions endpoint works, # the new lastAccessedAt + lastAccessedBy fields surface, and the empty-list # path is graceful when no interactive sessions exist for the user. # 6) Sessions list — for a ROPC-only user this returns []. End-to-end verification # of interactive login + refresh + lazy-expire requires a browser-driven probe # (the Identity.Web BFF login flow); covered in admin smoke-tests rather than # a headless PowerShell demo. Measure-Step "6. admin: GET /sessions for ROPC-only user (expect 0; no interactive session)" { $r = Invoke-RestMethod -Method Get "$BASE/api/v1/identity/sessions?userId=$UserId" -Headers $ADMIN if ($null -eq $r) { $r = @() } $count = if ($r -is [Array]) { $r.Count } else { 1 } if ($count -gt 0) { Write-Host " (unexpected) $count sessions; ROPC was not expected to create one" -ForegroundColor Yellow } else { Write-Host " ✓ 0 sessions (as architecturally expected for ROPC)" -ForegroundColor Green } } | Out-Null # 7) Refresh-token grant — this exercises the TouchSessionOnTokenRefreshHandler # but the touch is a NO-OP because there's no sid claim. The grant itself # must still succeed. Measure-Step "7. refresh_token grant (touch hook no-ops without sid)" { $r = Invoke-RestMethod -Method Post "$BASE/connect/token" -ContentType "application/x-www-form-urlencoded" ` -Body @{ grant_type = "refresh_token" client_id = $ropcReg.client_id client_secret = $ropcReg.client_secret refresh_token = $tok.refresh_token scope = "openid offline_access" } if (-not $r.access_token) { throw "no access_token after refresh" } Write-Host " ✓ new access_token: $($r.access_token.Substring(0,16))…" -ForegroundColor Green } | Out-Null # 8) Cleanup Measure-Step "8. cleanup: delete user" { try { Invoke-RestMethod -Method Delete "$BASE/api/v1/identity/users/$UserId" -Headers $ADMIN | Out-Null } catch { Write-Host " (cleanup) delete returned: $($_.Exception.Message)" -ForegroundColor DarkGray } } | Out-Null $total.Stop() Write-Host "" Write-Host "=== Summary ===" -ForegroundColor Magenta $timings | Format-Table -AutoSize Write-Host ("Total: {0:N0} ms" -f $total.Elapsed.TotalMilliseconds) -ForegroundColor Magenta