peppa/docs/egov-reference/README.md
Асель ed49767c31 PEP-65: eGov QR login through the KazEDS relay (sign.aitu.uz)
Login page and Settings → eGov: relay session with a server-side challenge/nonce, QR + eGov Mobile
deep link, polling, one-shot completion; IIN from the signer's certificate (own DER reader),
users.egov_iin. The relay is trusted (PM decision): no own XMLDSig check. CLI peppa user egov,
mock-kazeds-relay, tests. Dev mode: /_next/static is no-store (Cloudflare cached stale chunks).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 01:14:10 +05:00

3.4 KiB

KazEDS eGov sign-in widget

This integration asks eGov Mobile to sign a short-lived XML challenge and gives the signed XML to the relying website. The website owns signature verification and the login decision.

Install the backend adapter

Copy signWidget.server.js into the site's Node backend and install:

npm install express qrcode

Mount it in an existing Express app:

const { makeRouter } = require('./signWidget.server');
app.use('/api/egov-login', makeRouter({
  onComplete: async ({ challenge, nonce, signedDocuments, request }) => {
    // Implement with YOUR XMLDSig verifier and account policy.
    // 1. Verify the signature over the returned XML.
    // 2. Confirm the signed XML contains this exact challenge and nonce.
    // 3. Validate the certificate chain, validity and revocation.
    // 4. Resolve/link the account and create YOUR site's session.
    // `challenge` and `nonce` come from server memory, not the browser.
    const identity = await verifyEgovLogin({ challenge, nonce, signedDocuments });
    if (!identity) throw new Error('Invalid eGov signature or challenge');
    await createSiteSession(request, identity);
    return { authenticated: true };
  },
}));

Set KAZEDS_RELAY_BASE if needed (default https://sign.aitu.uz/relay/v1), plus EGOV_ORGANISATION_NAME and EGOV_ORGANISATION_BIN for the relying site. Use the registered business identity and eGov-approved domain details required for your integration.

Add the browser widget

Serve signWidget.js from the relying site and add:

<div id="egov-login"></div>
<script src="/signWidget.js"></script>
<script>
  SignWidget.mount({
    container: '#egov-login',
    apiBase: '/api/egov-login',
    onSigned: async (result) => {
      const response = await fetch('/api/egov-login/complete', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        credentials: 'same-origin',
        body: JSON.stringify({ verificationHandle: result.verificationHandle }),
      });
      if (!response.ok) throw new Error('The site could not verify this signature');
      // The relying site's configured onComplete callback decides whether to log in.
    },
    onError: (error) => console.error('eGov login flow failed', error),
  });
</script>

Implement /api/egov-login/verify on the relying site's backend. Verify the XMLDSig cryptographically, ensure the signed content contains the exact server-side challenge and nonce, check the certificate chain, validity and revocation, and only then map the certified identity to an account or ask the user to link/create one. Do not trust identity fields sent by the browser, and do not create a login session just because the widget reports completed.

The adapter stores short-lived correlation state in memory and is suitable for a single-process integration prototype. For multiple backend instances, replace its Map with shared storage and bind each flow to the site's authenticated/CSRF-protected browser session. The eGov Relay session store is also currently in memory.

Test mode

The Relay's eGov routes are the real API shim, not a deployed mock signer. For local end-to-end UI testing, point KAZEDS_RELAY_BASE at a local mock implementing POST /egov/sessions and GET /egov/:id/status; have it return a completed sample XML document. Real signing still requires eGov Mobile and its domain allowlist. Never use a fabricated signature to grant an account login.