How queshex/aiokaspi works
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: service.py AuthClient · client.py Client · transport.py Async transport · base.py Storage interface
Components 18
Public SDK
- aiokaspi package
aiokaspi/__init__.py - Client · SDK entry point using caller-provided aiohttp session.
aiokaspi/client.py - Auth usage
examples/auth.py
Authentication Protocol
- AuthClient · Maintains state across the four-step OTP flow.
aiokaspi/auth/service.py - Auth schemas · Builds ordering-sensitive signed payloads and parses responses.
aiokaspi/auth/schemas.py - Response validation · Maps Kaspi failures to domain exceptions.
aiokaspi/auth/validation.py - Auth exceptions
aiokaspi/auth/exceptions.py - Auth utilities
aiokaspi/auth/utils.py
Core Platform
- Async transport · Executes Kaspi API requests over the caller-owned aiohttp session.
aiokaspi/core/transport.py - Device keys · Generates EC keys, hashes data, and signs protocol inputs locally.
aiokaspi/core/keys.py - Device config
aiokaspi/core/config.py - Shared schemas
aiokaspi/core/schemas.py - Shared exceptions
aiokaspi/exceptions.py
Persistent State
- Storage interface · Persists credentials, keys, and device parameters.
aiokaspi/core/storage/base.py - File storage · Resumes sessions from file-backed state.
aiokaspi/core/storage/file.py - Memory storage
aiokaspi/core/storage/memory.py
Other components
- Kaspi Pay API · Receives signed registration and OTP requests.
- Future service areas · Invoice, QR, refunds, and history are placeholders, not active capabilities.
aiokaspi/invoice/__init__.py
Connections 18
- 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