// packages/webauthn/examples/demo.vk // Runnable verification suite for `webauthn` (FIDO2 Ed25519 + P-256 Passkeys) and `webauthn/policy` (RBAC/ABAC). use [ hex = codec/hex pol = webauthn/policy wa = webauthn ] assert-eq = fn [actual any, expected any, label str] any [ if actual != expected [ return (err (str/join ['ASSERT FAILED [' label ']: got ' (mold actual) ', expected ' (mold expected)] '')) ] .ok ] assert-true = fn [cond bool, label str] any [ if not cond [ return (err (str/join ['ASSERT FAILED [' label ']'] '')) ] .ok ] main = fn [] any [ println '=== webauthn 1.0.0 Verification Suite ===' rp = wa/RelyingParty { rp_id = 'portal.veko-lang.org' rp_name = 'Veko Identity Portal' origin = 'https://portal.veko-lang.org' } // 1. Ed25519 (`alg = -8`, EdDSA) Passkey Registration & Login Ceremony user_id = bytes/from-str 'usr_ada_001' reg_start = wa/begin-registration rp user_id 'ada@veko-lang.org' 'Ada Lovelace' reg_chal = str/append '' (map/get reg_start 'challenge') r1 = assert-true ((len reg_chal) >= 40) 'begin-registration 32B base64url challenge' if err? r1 [ return r1 ] ed_seed = hex/decode '9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60' cred_id1 = bytes/from-str 'cred-ed25519-passkey-01' sim_reg1 = wa/simulate-registration rp reg_chal cred_id1 (wa/cose-alg-eddsa) ed_seed 1 true if err? sim_reg1 [ return sim_reg1 ] mut stored_ed = wa/finish-registration/require-uv rp reg_chal sim_reg1 true if err? stored_ed [ return stored_ed ] r2 = assert-eq stored_ed.alg -8 'Ed25519 COSE alg -8' if err? r2 [ return r2 ] r3 = assert-eq (len stored_ed.public_key) 32 'Ed25519 32B public key extracted from COSE_Key' if err? r3 [ return r3 ] r4 = assert-eq stored_ed.sign_count 1 'initial signCount == 1' if err? r4 [ return r4 ] r5 = assert-eq stored_ed.user_verified true 'UV flag verified' if err? r5 [ return r5 ] // Valid Ed25519 login (`signCount` advances 1 -> 2) auth_start1 = wa/begin-authentication rp [stored_ed.credential_id] auth_chal1 = str/append '' (map/get auth_start1 'challenge') sim_auth1 = wa/simulate-authentication rp auth_chal1 stored_ed.credential_id (wa/cose-alg-eddsa) ed_seed 2 true stored_ed = wa/finish-authentication/require-uv rp auth_chal1 stored_ed sim_auth1 true if err? stored_ed [ return stored_ed ] r6 = assert-eq stored_ed.sign_count 2 'login updated signCount -> 2' if err? r6 [ return r6 ] // Security check A: Wrong challenge MUST be rejected auth_start_bad = wa/begin-authentication rp [stored_ed.credential_id] other_chal = str/append '' (map/get auth_start_bad 'challenge') sim_wrong_chal = wa/simulate-authentication rp auth_chal1 stored_ed.credential_id (wa/cose-alg-eddsa) ed_seed 3 true wrong_chal_res = wa/finish-authentication/or rp other_chal stored_ed sim_wrong_chal [ wa/StoredCredential { credential_id = 'REJECTED', public_key = (bytes/with-cap 0), alg = 0, sign_count = -1 } ] r7 = assert-eq wrong_chal_res.credential_id 'REJECTED' 'wrong challenge rejected' if err? r7 [ return r7 ] // Security check B: Phishing origin mismatch MUST be rejected phish_rp = wa/RelyingParty { rp_id = 'portal.veko-lang.org' rp_name = 'Evil Proxy' origin = 'https://phish.evil.example' } sim_phish = wa/simulate-authentication phish_rp other_chal stored_ed.credential_id (wa/cose-alg-eddsa) ed_seed 3 true phish_res = wa/finish-authentication/or rp other_chal stored_ed sim_phish [ wa/StoredCredential { credential_id = 'REJECTED_ORIGIN', public_key = (bytes/with-cap 0), alg = 0, sign_count = -1 } ] r8 = assert-eq phish_res.credential_id 'REJECTED_ORIGIN' 'phishing origin rejected' if err? r8 [ return r8 ] // Security check C: Cloned `signCount` (2 <= 2) MUST be rejected! sim_cloned = wa/simulate-authentication rp other_chal stored_ed.credential_id (wa/cose-alg-eddsa) ed_seed 2 true clone_res = wa/finish-authentication/or rp other_chal stored_ed sim_cloned [ wa/StoredCredential { credential_id = 'REJECTED_CLONE', public_key = (bytes/with-cap 0), alg = 0, sign_count = -1 } ] r9 = assert-eq clone_res.credential_id 'REJECTED_CLONE' 'cloned non-monotonic signCount rejected' if err? r9 [ return r9 ] println ' [PASS] FIDO2 Ed25519 (alg=-8) registration, login, challenge/origin & clone detection' // 2. ECDSA P-256 (`alg = -7`, ES256) Passkey Registration & Login Ceremony p256_priv = bytes/fill 32 0x2a cred_id2 = bytes/from-str 'cred-p256-hardware-key-02' reg_p256 = wa/begin-registration rp user_id 'ada@veko-lang.org' 'Ada Lovelace' chal_p256 = str/append '' (map/get reg_p256 'challenge') sim_reg2 = wa/simulate-registration rp chal_p256 cred_id2 (wa/cose-alg-es256) p256_priv 10 true stored_p256 = wa/finish-registration rp chal_p256 sim_reg2 if err? stored_p256 [ return stored_p256 ] r10 = assert-eq stored_p256.alg -7 'ECDSA P-256 COSE alg -7' if err? r10 [ return r10 ] r11 = assert-eq (len stored_p256.public_key) 65 'P-256 65B SEC1 public key reconstructed' if err? r11 [ return r11 ] auth_p256 = wa/begin-authentication rp [stored_p256.credential_id] achal_p256 = str/append '' (map/get auth_p256 'challenge') sim_auth2 = wa/simulate-authentication rp achal_p256 stored_p256.credential_id (wa/cose-alg-es256) p256_priv 11 true updated_p256 = wa/finish-authentication rp achal_p256 stored_p256 sim_auth2 if err? updated_p256 [ return updated_p256 ] r12 = assert-eq updated_p256.sign_count 11 'P-256 ES256 login verified & signCount -> 11' if err? r12 [ return r12 ] println ' [PASS] FIDO2 ECDSA P-256 (alg=-7) COSE_Key registration & RFC 6979 login verification' // 3. Homoiconic RBAC/ABAC Policy Engine (`webauthn/policy`) policy = pol/compile-policy (quote [ .role-inherits 'admin' ['editor'] .role-inherits 'editor' ['viewer'] .allow role 'viewer' action 'read' resource 'projects/*/docs/**' .allow role 'editor' action 'write' resource 'projects/*/docs/**' when [mfa == true and subject.id == resource.owner_id] .allow role 'admin' action '*' resource '**' when [mfa == true and clearance >= 3] .forbid role '*' action 'delete' resource 'projects/*/audit/**' .forbid role '*' action '*' resource '**' when [suspended == true] ]) if err? policy [ return policy ] // (a) Role hierarchy: `editor` inherits `viewer` -> allowed to `read` `projects/veko/docs/guide.md` d_inh = pol/evaluate policy (pol/AccessRequest { principal = 'usr_lin' roles = ['editor'] action = 'read' resource = 'projects/veko/docs/guide.md' attrs = { mfa = false } }) r13 = assert-eq d_inh.allowed true 'role hierarchy inheritance (editor -> viewer)' if err? r13 [ return r13 ] // (b) ABAC cross-attribute owner check + MFA: allowed when owner matches & mfa == true d_abac_ok = pol/evaluate policy (pol/AccessRequest { principal = 'usr_lin' roles = ['editor'] action = 'write' resource = 'projects/veko/docs/guide.md' attrs = { mfa = true subject = { id = 'usr_lin' } resource = { owner_id = 'usr_lin' } } }) r14 = assert-eq d_abac_ok.allowed true 'ABAC owner + MFA check permitted' if err? r14 [ return r14 ] // (c) ABAC owner mismatch -> default deny d_abac_no = pol/evaluate policy (pol/AccessRequest { principal = 'usr_lin' roles = ['editor'] action = 'write' resource = 'projects/veko/docs/guide.md' attrs = { mfa = true subject = { id = 'usr_lin' } resource = { owner_id = 'usr_ada' } } }) r15 = assert-eq d_abac_no.effect .deny_default 'ABAC owner mismatch denied by default' if err? r15 [ return r15 ] // (d) Explicit `.forbid` overrides `admin` `.allow` on audit trail deletion! d_forbid_audit = pol/evaluate policy (pol/AccessRequest { principal = 'usr_ada' roles = ['admin'] action = 'delete' resource = 'projects/veko/audit/2026-12.log' attrs = { mfa = true, clearance = 5, suspended = false } }) r16 = assert-eq d_forbid_audit.allowed false 'explicit .forbid overrides admin .allow' if err? r16 [ return r16 ] r17 = assert-eq d_forbid_audit.effect .forbid 'decision effect is .forbid' if err? r17 [ return r17 ] // (e) Suspended principal blocked everywhere by conditional `.forbid` d_suspended = pol/evaluate policy (pol/AccessRequest { principal = 'usr_ada' roles = ['admin'] action = 'read' resource = 'projects/veko/docs/guide.md' attrs = { mfa = true, clearance = 5, suspended = true } }) r18 = assert-eq d_suspended.effect .forbid 'suspended attribute triggers .forbid' if err? r18 [ return r18 ] println ' [PASS] Homoiconic RBAC/ABAC policy engine (.forbid override, role hierarchy, globs & ABAC)' println 'ALL webauthn TESTS PASSED' .ok ] res = main if err? res [ println (mold res) os/exit 1 ]