# KiteLedger auth.md

KiteLedger agents use OAuth 2.0 bearer access tokens for the KiteLedger API. Start with the public discovery documents:

- Protected Resource Metadata: https://kiteledger.de/.well-known/oauth-protected-resource
- Authorization Server Metadata: https://kiteledger.de/.well-known/oauth-authorization-server
- JSON Web Key Set: https://kiteledger.de/.well-known/jwks.json

## Registration

Registration URI: https://kiteledger.de/agent/auth. Send a JSON POST. Registration is not a passive-scan endpoint: it creates credentials or starts a verification email.

### Anonymous registration

Use this when no KiteLedger user identity is available. It creates a client with only the public capability scope.

```http
POST https://kiteledger.de/agent/auth
Content-Type: application/json

{"type":"anonymous"}
```

The response contains `client_id`, `client_secret`, `scope`, and an optional `claim_token`. Keep the client secret private. Exchange it using the client-credentials grant described below. Anonymous credentials can call only `GET /api/agent/capabilities`; they cannot read or change any user's workspace.

### Verified-email registration

The email must belong to an existing KiteLedger account with a verified email address. Include only the scopes the user wants the agent to have.

```http
POST https://kiteledger.de/agent/auth
Content-Type: application/json

{"type":"verified_email","email":"user@example.com","scopes":["workspace:read","invoices:read"]}
```

KiteLedger emails a one-time code to that verified address. Ask the user to provide that code, then complete the claim:

```http
POST https://kiteledger.de/agent/auth/claim
Content-Type: application/json

{"claim_token":"<claim_token_from_registration>","code":"<six_digit_email_code>"}
```

The successful response contains a one-time `client_secret`. Registration returns the same claim-pending response when no account matches; a code is sent only to an existing verified KiteLedger account. If no code arrives, create and verify an account at https://kiteledger.de/signup, then restart registration.

### ID-JAG registration

Trusted agent providers may register with an audience-bound ID-JAG. KiteLedger verifies the RS256 signature using that issuer's configured JWKS and requires the assertion audience to be `https://kiteledger.de`, a verified email, a fresh `auth_time`, and a short-lived `exp`. The user then confirms the email claim using the same one-time-code step above. Only issuers returned by https://kiteledger.de/agent/auth/providers are accepted. That list starts empty and must be configured by a KiteLedger administrator.

```http
POST https://kiteledger.de/agent/auth
Content-Type: application/json

{"type":"identity_assertion","assertion_type":"urn:ietf:params:oauth:token-type:id-jag","assertion":"<id_jag_jwt>","scopes":["workspace:read","invoices:read"]}
```

## Obtain an access token

All completed registrations use the OAuth 2.0 `client_credentials` grant. Use HTTP Basic authentication or send `client_id` and `client_secret` as form fields:

```http
POST https://kiteledger.de/oauth/token
Content-Type: application/x-www-form-urlencoded

grant_type=client_credentials&client_id=<client_id>&client_secret=<client_secret>
```

The response is an `access_token` with `token_type: Bearer`, the granted `scope`, and a 900-second lifetime. Tokens are signed with the RSA key published at the JWKS URL. The key is generated by KiteLedger on first use, encrypted at rest, and kept stable in the database.

## Use the credential

Send the access token in the Authorization header:

```http
GET https://kiteledger.de/api/customer/invoices
Authorization: Bearer <access_token>
```

Request only the scopes needed. `workspace:read` reads workspace context; `invoices:read` reads invoice, ledger, seller, accounting, and VAT resources; `invoices:write` permits their existing write operations; `invoices:export` is required for DATEV exports and still requires the workspace's existing paid entitlement; `billing:read` reads billing status; `billing:write` requests a checkout or customer-portal session and still requires workspace-manager access; `account:write` requests account deletion. Existing workspace membership, verified-email, admin, and paid-plan checks remain in force. No agent scope grants admin access. A valid token missing a required scope receives HTTP 403; invalid, expired, or revoked credentials receive HTTP 401.

## Revoke and renew

Revoke a client by posting to https://kiteledger.de/oauth/revoke with its client credentials. Revocation immediately invalidates its access tokens. Trusted ID-JAG providers may send the signed revocation event `https://schemas.workos.com/events/agent/auth/identity/assertion/revoked` to https://kiteledger.de/agent/auth/revoke. When an access token expires, exchange the client credentials again for a new 900-second token.

## Supported methods

- Anonymous registration: immediate, restricted to `agent:public`.
- Verified email: one-time email-code claim tied to an existing verified KiteLedger user.
- ID-JAG: trusted RS256 provider assertion followed by the same email-confirmation step.
- Bearer transport: `Authorization: Bearer <access_token>`.
