# queshex/aiokaspi architecture

> Architecture diagram of the GitHub repository queshex/aiokaspi, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/queshex/aiokaspi
- Repository: https://github.com/queshex/aiokaspi

## Overview

#### Purpose and Entry Points
`aiokaspi` is an async Python SDK for Kaspi Pay’s mobile authentication protocol, intended to become a broader client framework. The implemented core is device registration and session acquisition via a four-step OTP flow. Public package and client entry points are `aiokaspi/__init__.py`, `aiokaspi/client.py`, and the authentication service in `aiokaspi/auth/service.py`. A runnable usage pattern is provided in `examples/auth.py`.

#### Authentication Orchestration
`AuthClient` coordinates the stateful protocol: session initialization, OTP delivery, OTP confirmation, and final device-registration/session-key retrieval. It owns the control-flow boundary between caller interaction (phone number/OTP) and Kaspi API calls, while preserving protocol state across steps.
- `aiokaspi/auth/service.py`
- `aiokaspi/auth/schemas.py`
- `aiokaspi/auth/validation.py`

#### Protocol Schemas, Ordering, and Validation
Authentication request/response models encode the mobile protocol’s required payload fields and serialization behavior, including ordering-sensitive signed data. Response validation translates Kaspi-specific server failures into domain exceptions instead of exposing raw transport errors.
- `aiokaspi/auth/schemas.py`
- `aiokaspi/auth/validation.py`
- `aiokaspi/auth/exceptions.py`

#### Cryptographic Identity and Signing
The SDK generates and manages device EC keys, derives hashes, and signs protocol data using `cryptography`. This is a security boundary: private key material and device identity must remain local, while only signed requests and relevant public/device metadata cross to Kaspi.
- `aiokaspi/core/keys.py`
- `aiokaspi/auth/utils.py`
- `aiokaspi/core/config.py`

#### HTTP Transport Boundary
An asynchronous transport layer isolates Kaspi API communication from authentication logic. It is expected to operate over a caller-provided `aiohttp` session, making connection lifecycle, proxying, and network policy external to the SDK while centralizing request execution internally.
- `aiokaspi/core/transport.py`
- `aiokaspi/auth/service.py`
- `aiokaspi/client.py`

#### Persistent Session and Device State
Storage abstracts persistence of session credentials, generated keys, and device parameters. The default file-backed implementation supports JSON state files; an in-memory implementation supports ephemeral operation and testing. Callers can therefore resume authenticated sessions without rerunning OTP registration.
- `aiokaspi/core/storage/base.py`
- `aiokaspi/core/storage/file.py`
- `aiokaspi/core/storage/memory.py`

#### Shared Domain Layer and Expansion Surface
Shared configuration, schemas, storage, transport, and exceptions form the reusable platform layer beneath auth. Package areas for invoices, QR, refunds, and history exist but have no implementation shown; planners should treat them as reserved extension boundaries rather than active capabilities.
- `aiokaspi/core/schemas.py`
- `aiokaspi/exceptions.py`
- `aiokaspi/invoice/__init__.py`

Main entry points: [`aiokaspi/auth/service.py`](https://github.com/queshex/aiokaspi/blob/main/aiokaspi/auth/service.py) (AuthClient), [`aiokaspi/client.py`](https://github.com/queshex/aiokaspi/blob/main/aiokaspi/client.py) (Client), [`aiokaspi/core/transport.py`](https://github.com/queshex/aiokaspi/blob/main/aiokaspi/core/transport.py) (Async transport), [`aiokaspi/core/storage/base.py`](https://github.com/queshex/aiokaspi/blob/main/aiokaspi/core/storage/base.py) (Storage interface)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_public["Public SDK"]
  node_package["aiokaspi package<br/>package entry<br/>[__init__.py]"]
  node_client["Client<br/>[client.py]"]
  node_example["Auth usage<br/>runnable example<br/>[auth.py]"]
end

subgraph group_auth["Authentication Protocol"]
  node_auth_service["AuthClient<br/>protocol orchestrator<br/>[service.py]"]
  node_auth_schemas["Auth schemas<br/>protocol models<br/>[schemas.py]"]
  node_validation["Response validation<br/>domain validation<br/>[validation.py]"]
  node_auth_exceptions["Auth exceptions<br/>domain errors<br/>[exceptions.py]"]
  node_auth_utils["Auth utilities<br/>signing support<br/>[utils.py]"]
end

subgraph group_core["Core Platform"]
  node_transport["Async transport<br/>HTTP boundary<br/>[transport.py]"]
  node_keys{{"Device keys<br/>crypto identity<br/>[keys.py]"}}
  node_config["Device config<br/>device parameters<br/>[config.py]"]
  node_core_schemas["Shared schemas<br/>domain models<br/>[schemas.py]"]
  node_shared_exceptions["Shared exceptions<br/>SDK errors<br/>[exceptions.py]"]
end

subgraph group_state["Persistent State"]
  node_storage_base["Storage interface<br/>persistence abstraction<br/>[base.py]"]
  node_file_storage[("File storage<br/>JSON persistence<br/>[file.py]")]
  node_memory_storage[("Memory storage<br/>ephemeral persistence<br/>[memory.py]")]
end

node_kaspi_api(("Kaspi Pay API<br/>external mobile API"))
node_extensions["Future service areas<br/>reserved extension boundary<br/>[__init__.py]"]

node_package -->|"exports"| node_client
node_example -->|"uses"| node_client
node_client -->|"starts auth"| node_auth_service
node_client -->|"provides session to"| node_transport
node_auth_service -->|"builds/parses protocol data"| node_auth_schemas
node_auth_service -->|"validates responses"| node_validation
node_validation -->|"raises"| node_auth_exceptions
node_auth_service -->|"prepares signed inputs"| node_auth_utils
node_auth_utils -->|"signs with"| node_keys
node_keys -->|"uses device parameters"| node_config
node_auth_service -->|"loads and saves state"| node_storage_base
node_file_storage -->|"implements"| node_storage_base
node_memory_storage -->|"implements"| node_storage_base
node_auth_service -->|"sends protocol calls"| node_transport
node_transport -->|"async HTTP"| node_kaspi_api
node_auth_schemas -.->|"shares models"| node_core_schemas
node_shared_exceptions -.->|"base error layer"| node_auth_exceptions
node_client -.->|"future services"| node_extensions

click node_package "https://github.com/queshex/aiokaspi/blob/main/aiokaspi/__init__.py"
click node_client "https://github.com/queshex/aiokaspi/blob/main/aiokaspi/client.py"
click node_example "https://github.com/queshex/aiokaspi/blob/main/examples/auth.py"
click node_auth_service "https://github.com/queshex/aiokaspi/blob/main/aiokaspi/auth/service.py"
click node_auth_schemas "https://github.com/queshex/aiokaspi/blob/main/aiokaspi/auth/schemas.py"
click node_validation "https://github.com/queshex/aiokaspi/blob/main/aiokaspi/auth/validation.py"
click node_auth_exceptions "https://github.com/queshex/aiokaspi/blob/main/aiokaspi/auth/exceptions.py"
click node_auth_utils "https://github.com/queshex/aiokaspi/blob/main/aiokaspi/auth/utils.py"
click node_transport "https://github.com/queshex/aiokaspi/blob/main/aiokaspi/core/transport.py"
click node_keys "https://github.com/queshex/aiokaspi/blob/main/aiokaspi/core/keys.py"
click node_config "https://github.com/queshex/aiokaspi/blob/main/aiokaspi/core/config.py"
click node_core_schemas "https://github.com/queshex/aiokaspi/blob/main/aiokaspi/core/schemas.py"
click node_shared_exceptions "https://github.com/queshex/aiokaspi/blob/main/aiokaspi/exceptions.py"
click node_storage_base "https://github.com/queshex/aiokaspi/blob/main/aiokaspi/core/storage/base.py"
click node_file_storage "https://github.com/queshex/aiokaspi/blob/main/aiokaspi/core/storage/file.py"
click node_memory_storage "https://github.com/queshex/aiokaspi/blob/main/aiokaspi/core/storage/memory.py"
click node_extensions "https://github.com/queshex/aiokaspi/blob/main/aiokaspi/invoice/__init__.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_package,node_client,node_example toneBlue
class node_auth_service,node_auth_schemas,node_validation,node_auth_exceptions,node_auth_utils toneAmber
class node_transport,node_keys,node_config,node_core_schemas,node_shared_exceptions toneMint
class node_storage_base,node_file_storage,node_memory_storage toneRose
class node_kaspi_api,node_extensions toneNeutral
```

## Components

### Public SDK

- [aiokaspi package](https://github.com/queshex/aiokaspi/blob/main/aiokaspi/__init__.py): package entry · `aiokaspi/__init__.py`
- [Client](https://github.com/queshex/aiokaspi/blob/main/aiokaspi/client.py): public client · SDK entry point using caller-provided aiohttp session. · `aiokaspi/client.py`
- [Auth usage](https://github.com/queshex/aiokaspi/blob/main/examples/auth.py): runnable example · `examples/auth.py`

### Authentication Protocol

- [AuthClient](https://github.com/queshex/aiokaspi/blob/main/aiokaspi/auth/service.py): protocol orchestrator · Maintains state across the four-step OTP flow. · `aiokaspi/auth/service.py`
- [Auth schemas](https://github.com/queshex/aiokaspi/blob/main/aiokaspi/auth/schemas.py): protocol models · Builds ordering-sensitive signed payloads and parses responses. · `aiokaspi/auth/schemas.py`
- [Response validation](https://github.com/queshex/aiokaspi/blob/main/aiokaspi/auth/validation.py): domain validation · Maps Kaspi failures to domain exceptions. · `aiokaspi/auth/validation.py`
- [Auth exceptions](https://github.com/queshex/aiokaspi/blob/main/aiokaspi/auth/exceptions.py): domain errors · `aiokaspi/auth/exceptions.py`
- [Auth utilities](https://github.com/queshex/aiokaspi/blob/main/aiokaspi/auth/utils.py): signing support · `aiokaspi/auth/utils.py`

### Core Platform

- [Async transport](https://github.com/queshex/aiokaspi/blob/main/aiokaspi/core/transport.py): HTTP boundary · Executes Kaspi API requests over the caller-owned aiohttp session. · `aiokaspi/core/transport.py`
- [Device keys](https://github.com/queshex/aiokaspi/blob/main/aiokaspi/core/keys.py): crypto identity · Generates EC keys, hashes data, and signs protocol inputs locally. · `aiokaspi/core/keys.py`
- [Device config](https://github.com/queshex/aiokaspi/blob/main/aiokaspi/core/config.py): device parameters · `aiokaspi/core/config.py`
- [Shared schemas](https://github.com/queshex/aiokaspi/blob/main/aiokaspi/core/schemas.py): domain models · `aiokaspi/core/schemas.py`
- [Shared exceptions](https://github.com/queshex/aiokaspi/blob/main/aiokaspi/exceptions.py): SDK errors · `aiokaspi/exceptions.py`

### Persistent State

- [Storage interface](https://github.com/queshex/aiokaspi/blob/main/aiokaspi/core/storage/base.py): persistence abstraction · Persists credentials, keys, and device parameters. · `aiokaspi/core/storage/base.py`
- [File storage](https://github.com/queshex/aiokaspi/blob/main/aiokaspi/core/storage/file.py): JSON persistence · Resumes sessions from file-backed state. · `aiokaspi/core/storage/file.py`
- [Memory storage](https://github.com/queshex/aiokaspi/blob/main/aiokaspi/core/storage/memory.py): ephemeral persistence · `aiokaspi/core/storage/memory.py`

### Other components

- **Kaspi Pay API**: external mobile API · Receives signed registration and OTP requests.
- [Future service areas](https://github.com/queshex/aiokaspi/blob/main/aiokaspi/invoice/__init__.py): reserved extension boundary · Invoice, QR, refunds, and history are placeholders, not active capabilities. · `aiokaspi/invoice/__init__.py`

## Connections

- aiokaspi package → Client: exports
- Auth usage → Client: uses
- Client → AuthClient: starts auth
- Client → Async transport: provides session to
- AuthClient → Auth schemas: builds/parses protocol data
- AuthClient → Response validation: validates responses
- Response validation → Auth exceptions: raises
- AuthClient → Auth utilities: prepares signed inputs
- Auth utilities → Device keys: signs with
- Device keys → Device config: uses device parameters
- AuthClient → Storage interface: loads and saves state
- File storage → Storage interface: implements
- Memory storage → Storage interface: implements
- AuthClient → Async transport: sends protocol calls
- Async transport → Kaspi Pay API: async HTTP
- Auth schemas → Shared schemas: shares models
- Shared exceptions → Auth exceptions: base error layer
- Client → Future service areas: future services
