Path: redb-identity/demos/demo_jwt.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_jwt.ps1 ---- # Per-step timing wrapper. Use: pwsh -File demo_jwt.ps1 # Each step prints its wall-clock duration; a final table summarises. $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) DCR — client registration $reg = Measure-Step "1. DCR /connect/register" { Invoke-RestMethod -Method Post "$BASE/connect/register" -Headers $DCR_AUTH ` -ContentType "application/json" ` -Body (@{ client_name = "jwt-demo" redirect_uris = @($REDIRECT_CB) grant_types = @("password","refresh_token") scope = "openid profile email offline_access" } | ConvertTo-Json) } $reg | Format-List client_id, client_secret # 2) Register the user (if not already present) Measure-Step "2. account/register" { try { Invoke-RestMethod -Method Post $BASE/api/v1/identity/account/register ` -ContentType "application/json" ` -Body (@{ login = "testuser2" email = "testuser2@example.com" password = "Test1234Pass!" displayName = "Test User 2" } | ConvertTo-Json) } catch { Write-Host " (already exists or non-fatal: $($_.Exception.Message))" -ForegroundColor Yellow } } | Out-Null # 3) Password grant → JWT $tok = Measure-Step "3. password grant /connect/token" { Invoke-RestMethod -Method Post $BASE/connect/token ` -ContentType "application/x-www-form-urlencoded" ` -Body @{ grant_type = "password" client_id = $reg.client_id client_secret = $reg.client_secret username = "testuser2" password = "Test1234Pass!" scope = "openid profile email offline_access" } } $tok | Format-List access_token, id_token, refresh_token, expires_in # 4) Decode id_token (JWS — readable without a key) function Decode-Jwt([string]$jwt) { $payload = $jwt.Split('.')[1].Replace('-','+').Replace('_','/') $payload += '=' * ((4 - $payload.Length % 4) % 4) [Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($payload)) | ConvertFrom-Json } Measure-Step "4. decode id_token (local)" { Decode-Jwt $tok.id_token | ConvertTo-Json } | Out-Host # 5) access_token — JWE-encrypted, claims via /connect/userinfo Measure-Step "5. /connect/userinfo" { Invoke-RestMethod $BASE/connect/userinfo ` -Headers @{ Authorization = "Bearer $($tok.access_token)" } | ConvertTo-Json } | Out-Host # 6) Refresh $refreshed = Measure-Step "6. refresh_token grant" { Invoke-RestMethod -Method Post $BASE/connect/token ` -ContentType "application/x-www-form-urlencoded" ` -Body @{ grant_type = "refresh_token" client_id = $reg.client_id client_secret = $reg.client_secret refresh_token = $tok.refresh_token } } $refreshed | Format-List access_token, refresh_token, expires_in $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