# Customer sign-in

> Customers sign in with a passkey or with the operator's own customer login, no customer number typed, and share their account read-only with a partner or a Betreuer until a date.

konto signs customers in itself, with passkeys: the customer confirms with a fingerprint,
their face or the device PIN, and types no password and no customer number. An operator
whose customers already have a login (its portal, its app) lets them
[sign in with it](#sign-in-with-your-customer-login) instead. A customer can share an
account read-only with a person they trust, until a date, and take it back at any time.
A clerk sees who has access and locks a lost device out.

## Turn it on

Set the origin of the page your customers open:

```sh
export KONTO_SIGNIN_ORIGIN='https://kunden.stadtwerke-musterstadt.de'
export KONTO_SIGNIN_NAME='Stadtwerke Musterstadt'   # shown in the passkey dialog
export KONTO_SIGNIN_CREDITORS='stadtwerke-musterstadt'   # required with several creditors
```

A passkey is bound to this host name, so choose it once: moving to another host means
setting up passkeys again. kontod refuses to start with an origin that is not `https`
(plain `http` only for `localhost`), an IP address, or a path. Serve kontod's `/v1`
routes from that origin, for example behind your reverse proxy, so the browser sends the
session cookie with each request. Without the origin, customer sign-in is off
(`signin_off`) and tokens are unaffected. `KONTO_SIGNIN_CREDITORS` names the creditors whose
customers sign in there; for a group of companies see
[several companies](several-creditors.md).

## Invite the holder

A clerk issues an invitation for the account's holder:

```sh
curl -s -X POST -H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' \
  -d '{}' https://konto.example/v1/accounts/VK-1000123/invitations
```

```json
{ "id": "…", "account": "VK-1000123", "kind": "holder", "name": "Erika Mustermann",
  "code": "7K3M-Q2XW-…-HJ4P", "expires": "2027-03-15T09:00:00Z" }
```

The name is the recorded holder's unless you send one; it is what your clerks see for the
person. The code is shown only in this
answer: konto stores its SHA-256, never the code. It works once and for 14 days, and a
newer holder invitation for the account ends the older one. Delivering it is yours: print
it in the welcome letter, or send a link `https://kunden…/#invitation=7K3M-…`. The
fragment never reaches a server, and the portal removes it from the address bar.

A code has 160 random bits, in 32 characters from Crockford's alphabet (no I, L, O or U).
Typing it back ignores case, spaces and dashes, and reads O as 0 and I or L as 1.

## Set up a passkey and sign in

The [Kundenportal](portal.md) runs both ceremonies in the browser; this is what it calls:

| Step | Call |
|---|---|
| set up: start | `POST /v1/signin/register` with `{invitation}` → `{ceremony, options}` |
| set up: the browser creates a passkey | `navigator.credentials.create` with the options |
| set up: finish | `POST /v1/signin/register/finish` with `{ceremony, credential}` |
| sign in: start | `POST /v1/signin` → `{ceremony, options}` |
| sign in: the browser asks for a passkey | `navigator.credentials.get` with the options |
| sign in: finish | `POST /v1/signin/finish` with `{ceremony, credential}` |

Sign-in names no user: the passkey says who the person is. A passkey requires user
verification and is a resident key, so it signs in from the device alone. kontod checks
origin, challenge, signature, user verification and the signature counter with
[webauthn-rs](https://github.com/kanidm/webauthn-rs). A ceremony waits 5 minutes on the
server and finishes once; any failure gives the same `signin_failed`. At most 10,000
ceremonies are open at once across the deployment; beyond that a start answers
`signin_unavailable`, and capacity returns as they expire. Limit the start routes per
address at your proxy too.

A finish starts a session and sets one cookie:

```text
Set-Cookie: __Host-konto=…; Path=/; Secure; HttpOnly; SameSite=Strict; Max-Age=28800
```

No script on the page can read it. The session lasts 8 hours from sign-in and ends at
`POST /v1/signout`. konto stores its SHA-256 only. Calls that change something (sharing,
revoking, signing out) must also carry an `Origin` equal to `KONTO_SIGNIN_ORIGIN`; the
browser adds it, a page of another origin cannot.

## Sign in with your customer login

kontod is an OpenID Connect client of the operator's provider (Keycloak, Microsoft Entra
External ID, cidaas, Auth0 and others). Register it there as a confidential client with
the redirect address `{KONTO_SIGNIN_ORIGIN}/v1/signin/oidc/callback` and, for logout,
`{KONTO_SIGNIN_ORIGIN}/` as the address after logout. Then:

```sh
export KONTO_SIGNIN_OIDC_ISSUER='https://login.stadtwerke-musterstadt.de/realms/kunden'
export KONTO_SIGNIN_OIDC_CLIENT_ID='konto'
export KONTO_SIGNIN_OIDC_CLIENT_SECRET='…'
export KONTO_SIGNIN_OIDC_NAME='Stadtwerke-Login'   # „Weiter mit Stadtwerke-Login“
```

The issuer must be `https` (plain `http` only on this machine) and equal, character for
character, the `issuer` of the provider's discovery document. Passkeys stay on; the
portal offers the login first.

| Step | Call |
|---|---|
| start | `POST /v1/signin/oidc` with `{invitation?, path?, fresh?}` → `{url}`; the browser goes there |
| back | the provider sends the browser to `GET /v1/signin/oidc/callback`, which goes on to `{origin}{path}` |
| fails | the same redirect with `#signin_error=signin_failed`, `signin_cancelled`, `signin_unlinked` or `invitation_invalid` |

konto asks for the scope `openid` only and keeps of a login its issuer and subject; no
e-mail address, no name, no token. The first sign-in links the login with an invitation
code, like a passkey; without one, the portal asks for the code (`signin_unlinked`).
No claim of the provider decides an account.

Each sign-in uses the authorization code flow with PKCE (S256) and a nonce, as RFC 9700
recommends. A second random value in the cookie `__Host-konto-oidc`
(HttpOnly, Secure, SameSite=Lax, 5 minutes) binds the sign-in to the browser that started
it. kontod exchanges the code with its client secret and checks the ID token on its own
clock. It accepts only asymmetric signatures (RS, PS, ES or EdDSA) by a key the provider
publishes, and checks the issuer, the audience and authorized party, the expiry (60
seconds leeway) and the nonce. It also checks `iss` in the response where the provider
announces it [RFC 9207]. The discovery document and keys are kept for an hour; keys for
an unknown key id are read again at most once a minute.

A change of payment or delivery needs a login within the last 15 minutes. The portal
then sends the customer to the provider with `prompt=login` and `max_age=0`, and kontod
accepts only an ID token whose `auth_time` is not older than that request. A provider
that sends no `auth_time` cannot confirm changes; in Microsoft Entra, add it as an
optional claim. Sign-out answers the provider's logout address
(`{"end_session": "https://…"}`), so a shared device does not sign the next person in
through the provider's own session.

## What a customer reads

A session reaches the accounts of the person's current grants: the overview, statement,
items, and the bills of those accounts with their HTML, PDF and XRechnung, plus
`GET /v1/me`. Any other account or bill answers like one that does not exist. A holder
also manages the account's [payment and delivery](self-service.md). Grants are
read on every request, so a revoked access stops at once.

```json
{ "person": "…", "name": "Erika Mustermann",
  "signin": { "method": "passkey", "confirmed_until": "2027-03-01T09:15:00Z" },
  "accounts": [{ "creditor": "stadtwerke-musterstadt", "company": "Stadtwerke Musterstadt GmbH",
    "account": "VK-1000123", "kind": "holder", "ends": null, "ends_de": null,
    "open": { "amount": "196.65", "text_de": "Offen 196,65 €", "due": "2027-01-18", "hint_de": "fällig am 18.01.2027" },
    "payment_de": "Wird per SEPA-Lastschrift von DE89 … 3000 eingezogen (Mandat VK-1000123-01)." }] }
```

A person who holds two accounts uses one passkey: a further invitation redeemed while
signed in (`POST /v1/me/invitations`) adds its account, also one of another company in the
sign-in group. Each account in `GET /v1/me` names its `creditor` and `company`, what is
`open` and how it is paid (`payment_de`); a session with accounts at several companies
names one with `?creditor=` on account and bill routes.

## Share an account

A holder shares an account read-only with another person, until a date at most 366 days
ahead:

| Call | Does |
|---|---|
| `POST /v1/me/delegations` with `{account, name, ends}` | issues the delegate's invitation, shown once |
| `GET /v1/me/delegations` | pending and active delegations of the accounts the person holds |
| `DELETE /v1/me/delegations/{id}` | revokes one, pending or active |

The delegate sets up their own passkey with the code and reads only that account, like
its holder. A delegate cannot share on. Access ends after the last day (UTC) or when the
holder revokes it, also within an open session. To extend it, the holder shares again.
The Kundenportal offers all of this under „Zugang teilen“.

## Lock access

A lost phone is a clerk's case:

| Call | Does |
|---|---|
| `GET /v1/accounts/{account}/access` | people with kind and end date, their passkeys and provider logins with creation and last use, pending invitations |
| `DELETE /v1/passkeys/{id}` | removes a passkey and ends every session started with it |
| `DELETE /v1/identities/{id}` | removes a provider login's link and ends its sessions; the login then needs a new invitation |
| `DELETE /v1/accounts/{account}/access/{id}` | revokes a grant or ends a pending invitation |

Then issue a new holder invitation. A revoked grant and an ended invitation keep their
rows with who did it and when.

## What konto stores

For each person: a name, their passkeys (credential id, public key, counter, when
created and last used) and their provider logins (issuer and subject, when linked and
last used). For each grant and invitation: the creditor, the account, the
kind, the name the invitation gave, the end date and who issued and ended it. Codes and session tokens only as SHA-256. No password,
no e-mail address, no token of the provider and no device data. Erasing an account
removes the passkeys and logins of every person left without access.

## Clock

Expiry reads kontod's one clock; booking dates come with each request
([Run kontod](kontod.md#one-clock-for-the-customer-s-own-actions)).
