// packages/ldap_saml/examples/demo.vk // Self-contained verification suite & runnable example for `ldap_saml` and `ldap_saml/scim`. use [ sso = ldap_saml scim = ldap_saml/scim web url = codec/url hex = codec/hex x25 = crypto/x25519 sig = crypto/sig ] assert-true = fn [cond bool, label str] enum [ if not cond [ println (str/join ['[FAIL] ' label] '') os/exit 1 ] println (str/join ['[PASS] ' label] '') .ok ] // --- 1. SAML 2.0 `AuthnRequest` (DEFLATE + Base64 + HTTP-Redirect) & SP Metadata XML --- idp_seed = bytes/from-str 'okta-idp-ed25519-signing-seed-32' idp_pub = x25/ed25519-pubkey idp_seed sp = sso/SpConfig { entity_id = 'https://app.acme.io/saml/metadata' acs_url = 'https://app.acme.io/saml/acs' slo_url = 'https://app.acme.io/saml/slo' idp_entity_id = 'http://www.okta.com/exk99acme' idp_sso_url = 'https://acme.okta.com/app/sso/saml' idp_alg = 'ed25519' idp_pubkey = idp_pub clock_skew_sec = 30 } authn = sso/build-authn-request/relay-state sp '_req_8842' '2026-12-05T02:00:00Z' '/dashboard' parsed_redir = url/parse authn.redirect_url q_params = url/parse-query parsed_redir.query inflated_xml = sso/inflate-authn-request (map/get q_params 'SAMLRequest') meta_xml = sso/build-sp-metadata sp authn_ok_1 = inflated_xml == authn.xml and (map/get q_params 'RelayState') == '/dashboard' authn_ok_2 = (str/contains? meta_xml 'SPSSODescriptor') and (str/contains? meta_xml 'https://app.acme.io/saml/acs') _ = assert-true (authn_ok_1 and authn_ok_2) 'SAML 2.0 AuthnRequest (DEFLATE + Base64 HTTP-Redirect) and SP EntityDescriptor XML' // --- 2. SAML 2.0 Response Sign & Verify (Ed25519 + ECDSA P-256) & Security Rejections --- saml_b64 = sso/sign-test-saml-response/assertion-id 'http://www.okta.com/exk99acme' 'https://app.acme.io/saml/metadata' 'https://app.acme.io/saml/acs' '_req_8842' 'ada@acme.io' '2026-12-05T02:00:00Z' '2026-12-05T02:10:00Z' { email = 'ada@acme.io' firstName = 'Ada' groups = ['eng-leads' 'sre-prod'] } 'ed25519' idp_seed '_assert_1001' // Valid verification at 2026-12-05T02:03:00Z (unix 1796436180) now_valid = 1796436180 mut replay_cache = {} principal = sso/verify-saml-response/expected-req-id/now-sec/seen-ids sp saml_b64 '_req_8842' now_valid replay_cache map/set mut replay_cache principal.assertion_id true // Also verify ECDSA P-256 IdP XML-DSig path! p256_priv = bytes/fill 32 0x2a p256_pub = sig/ecdsa-p256-pubkey p256_priv sp_p256 = sp \ struct/with { idp_alg = 'p256', idp_pubkey = p256_pub } saml_p256 = sso/sign-test-saml-response 'http://www.okta.com/exk99acme' 'https://app.acme.io/saml/metadata' 'https://app.acme.io/saml/acs' '_req_8842' 'grace@acme.io' '2026-12-05T02:00:00Z' '2026-12-05T02:10:00Z' { groups = ['security'] } 'p256' p256_priv prin_p256 = sso/verify-saml-response/expected-req-id/now-sec sp_p256 saml_p256 '_req_8842' now_valid saml_ok = principal.name_id == 'ada@acme.io' and principal.first_name == 'Ada' and principal.groups == ['eng-leads' 'sre-prod'] and prin_p256.name_id == 'grace@acme.io' _ = assert-true saml_ok 'SAML 2.0 XML-DSig verification (Ed25519 & ECDSA P-256) extracting NameID and multi-valued groups' // Security negative tests: // (a) Tampered XML attribute value inside Base64 SAMLResponse (privilege escalation attempt) raw_xml = bytes/to-str (hex/b64-decode saml_b64) tampered_xml = str/join (str/split raw_xml 'ada@acme.io') 'admin@acme.io' tampered_b64 = hex/b64-encode (bytes/from-str tampered_xml) rej_tamper = err? (sso/verify-saml-response/now-sec/or sp tampered_b64 now_valid [e -> e]) // (b) Wrong AudienceRestriction wrong_aud_b64 = sso/sign-test-saml-response 'http://www.okta.com/exk99acme' 'https://other-sp.evil.io/saml' 'https://app.acme.io/saml/acs' '_req_8842' 'ada@acme.io' '2026-12-05T02:00:00Z' '2026-12-05T02:10:00Z' {} 'ed25519' idp_seed rej_aud = err? (sso/verify-saml-response/now-sec/or sp wrong_aud_b64 now_valid [e -> e]) // (c) Expired NotOnOrAfter (1 hour later: 1796439780) rej_exp = err? (sso/verify-saml-response/now-sec/or sp saml_b64 1796439780 [e -> e]) // (d) Replayed Assertion ID against `replay_cache` rej_replay = err? (sso/verify-saml-response/now-sec/seen-ids/or sp saml_b64 now_valid replay_cache [e -> e]) _ = assert-true (rej_tamper and rej_aud and rej_exp and rej_replay) 'SAML 2.0 rejects tampered XML, wrong Audience, expired NotOnOrAfter, and replayed Assertion ID' // --- 3. OIDC Discovery, JWKS `kid` Lookup & ID-Token Claims Validation --- oidc_cfg = sso/build-oidc-configuration 'https://id.acme.io' jwk_ed = sso/jwk-from-ed25519 'key-ed-2026' idp_pub jwk_ec = sso/jwk-from-p256 'key-p256-2026' p256_pub jwks_doc = { keys = [jwk_ed jwk_ec] } picked_jwk = sso/select-jwk-by-kid jwks_doc 'key-p256-2026' missing_jwk = err? (sso/select-jwk-by-kid/or jwks_doc 'unknown-kid' [e -> e]) claims_ok = sso/validate-id-token-claims/now-sec { iss = 'https://id.acme.io' aud = ['client-web-01' 'client-cli'] sub = 'usr-ada' exp = 1796436900 nonce = 'n-7741' } 'https://id.acme.io' 'client-web-01' 'n-7741' 1796436180 claims_bad_nonce = err? (sso/validate-id-token-claims/now-sec/or { iss = 'https://id.acme.io' aud = 'client-web-01' exp = 1796436900 nonce = 'wrong-nonce' } 'https://id.acme.io' 'client-web-01' 'n-7741' 1796436180 [e -> e]) oidc_ok = (map/get oidc_cfg 'jwks_uri') == 'https://id.acme.io/.well-known/jwks.json' and (map/get picked_jwk 'crv') == 'P-256' and missing_jwk and claims_ok == .ok and claims_bad_nonce _ = assert-true oidc_ok 'OIDC Discovery metadata, JWKS Ed25519/P-256 kid selection, and ID-token claims validation' // --- 4. SCIM 2.0 Provisioning Lifecycle on `web/App` (Create -> Filter -> `PatchOp` Deactivate) --- mut app = web/new-app dir = scim/new-directory/base-url/bearer-token 'https://app.acme.io/scim/v2' 'scim-secret-tok-99' _ = scim/mount-directory mut app dir auth_hdrs = { 'Authorization' = 'Bearer scim-secret-tok-99' 'Content-Type' = 'application/scim+json' } // 4a. Unauthenticated request rejected with 401 r_unauth = web/handle-request app (web/Request { method = 'GET' path = '/scim/v2/Users' target = '/scim/v2/Users' query = {} params = {} headers = {} body = bytes/with-cap 0 }) // 4b. Provision two users (Ada @ acme.io and Contractor @ ext.org) create_ada_body = json/encode { schemas = [(scim/SCHEMA-USER)] userName = 'ada@acme.io' displayName = 'Ada Lovelace' active = true name = { givenName = 'Ada', familyName = 'Lovelace' } emails = [{ value = 'ada@acme.io', type = 'work', primary = true }] } r_create_ada = web/handle-request app (web/Request { method = 'POST' path = '/scim/v2/Users' target = '/scim/v2/Users' query = {} params = {} headers = auth_hdrs body = bytes/from-str create_ada_body }) ada_created = json/decode (bytes/to-str r_create_ada.body) ada_id = str/append '' (map/get ada_created 'id') create_ext_body = json/encode { schemas = [(scim/SCHEMA-USER)] userName = 'bob@partner.org' active = true emails = [{ value = 'bob@partner.org', type = 'work', primary = true }] } _ = web/handle-request app (web/Request { method = 'POST' path = '/scim/v2/Users' target = '/scim/v2/Users' query = {} params = {} headers = auth_hdrs body = bytes/from-str create_ext_body }) // 4c. Query `/scim/v2/Users` with compound RFC 7644 filter: `active eq true and emails.value co "@acme.io"` r_flt_before = web/handle-request app (web/Request { method = 'GET' path = '/scim/v2/Users' target = '/scim/v2/Users' query = { 'filter' = 'active eq true and emails.value co "@acme.io"' } params = {} headers = auth_hdrs body = bytes/with-cap 0 }) list_before = json/decode (bytes/to-str r_flt_before.body) // 4d. Instant Okta/Entra-style `PatchOp` deprovisioning (`active = false`) + title update! patch_body = json/encode { schemas = [(scim/SCHEMA-PATCH)] Operations = [ { op = 'Replace', value = { active = false } } { op = 'replace', path = 'title', value = 'Alumni Fellow' } ] } user_path = str/append '/scim/v2/Users/' ada_id r_patch = web/handle-request app (web/Request { method = 'PATCH' path = user_path target = user_path query = {} params = {} headers = auth_hdrs body = bytes/from-str patch_body }) ada_patched = json/decode (bytes/to-str r_patch.body) // 4e. Re-run active filter -> 0 active @acme.io users! And `userName eq "ada@acme.io"` returns deactivated Ada! r_flt_after = web/handle-request app (web/Request { method = 'GET' path = '/scim/v2/Users' target = '/scim/v2/Users' query = { 'filter' = 'active eq true and emails.value co "@acme.io"' } params = {} headers = auth_hdrs body = bytes/with-cap 0 }) list_after = json/decode (bytes/to-str r_flt_after.body) r_flt_eq = web/handle-request app (web/Request { method = 'GET' path = '/scim/v2/Users' target = '/scim/v2/Users' query = { 'filter' = 'userName eq "ada@acme.io"' } params = {} headers = auth_hdrs body = bytes/with-cap 0 }) list_eq = json/decode (bytes/to-str r_flt_eq.body) scim_ok_1 = r_unauth.status == 401 and r_create_ada.status == 201 and ada_id == 'usr-1' scim_ok_2 = (map/get list_before 'totalResults') == 1 and r_patch.status == 200 and (map/get ada_patched 'active') == false and (map/get ada_patched 'title') == 'Alumni Fellow' scim_ok_3 = (map/get list_after 'totalResults') == 0 and (map/get list_eq 'totalResults') == 1 _ = assert-true (scim_ok_1 and scim_ok_2 and scim_ok_3) 'SCIM 2.0 web/App User create, RFC 7644 filter evaluation, and PatchOp instant deprovisioning' println 'ldap_saml: all verification checks passed'