Metadata-Version: 2.4
Name: licensify-sdk
Version: 1.0.1
Summary: Python SDK for Licensify license authentication — one call to authenticate, with encrypted key exchange, server-identity verification and heartbeat handled for you.
Project-URL: Homepage, https://licensify.kr
Project-URL: Documentation, https://licensify.kr
Author: Licensify
License: MIT
License-File: LICENSE
Keywords: authentication,drm,license,licensify,licensing,sdk
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Security :: Cryptography
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Typing :: Typed
Requires-Python: >=3.8
Requires-Dist: cryptography>=42.0
Requires-Dist: httpx>=0.27
Provides-Extra: dev
Requires-Dist: build>=1.0; extra == 'dev'
Requires-Dist: mypy>=1.0; extra == 'dev'
Requires-Dist: pytest>=7.0; extra == 'dev'
Requires-Dist: ruff>=0.1; extra == 'dev'
Requires-Dist: twine>=5.0; extra == 'dev'
Description-Content-Type: text/markdown

# Licensify — Python SDK

The simplest way to add Licensify license authentication to your Python app.

The complexity all lives inside the SDK. Encrypted key exchange, server-identity
verification, session keep-alive (heartbeat) and retries are handled for you, so you
just call **`authenticate` once**. Pass the license key and you get an authenticated
session back. The whole integration is a few lines.

> License issuing, software registration and session/usage management all happen on the
> admin site **[licensify.kr](https://licensify.kr)**. This SDK is the client that
> authenticates the licenses issued there.

## Requirements

- **Python 3.8+**
- [`cryptography`](https://cryptography.io) ≥ 42.0 and [`httpx`](https://www.python-httpx.org) ≥ 0.27 (installed automatically). A single dependency, `cryptography`, provides X25519, HKDF-SHA256, AES-256-GCM and Ed25519 — no extra curve library needed.

## Install

```bash
pip install licensify-sdk
```

## Quick start

```python
from licensify import LicenseClient, LicenseError, LicenseErrorCode

with LicenseClient(app_id="myapp", client_version="1.2.0") as client:
    try:
        session = client.authenticate(user_entered_license_key)

        print(f"authenticated; {session.remaining_millis} ms left")
        print("theme =", session.local_variables.get("theme"))

        # ... run your app; the heartbeat loop keeps the session alive automatically ...

        session.set_local_variable("highscore", "9001")

    except LicenseError as err:
        # Every failure flows through this one type. Branch on err.code.
        if err.code == LicenseErrorCode.LICENSE_BANNED:
            print(f"banned: {err.reason} (until {err.until})")
        elif err.code == LicenseErrorCode.UPDATE_REQUIRED:
            print(f"update to {err.latest_version}: {err.download_url}")
        elif err.code == LicenseErrorCode.NETWORK_ERROR and err.retryable:
            print("network problem — try again.")
        else:
            print(f"[{err.code.value}] {err}")
# leaving the `with` block releases the session (best-effort).
```

## With options

Every option is optional and covers only the customizable ("OPEN") surface. Everything
security-relevant is locked inside the SDK.

```python
from licensify import LicenseClient, LicenseClientOptions

options = LicenseClientOptions(
    file_hash=sha256_of_my_binary,                 # optional integrity check (≤ 64 chars)
    auto_reauthenticate=True,                       # one automatic re-auth on session loss
    on_session_lost=lambda err: lock_the_app(err),  # heartbeat failure / expiry / ban mid-session
    on_update_required=lambda info: show_update(info.latest_version, info.download_url),
    logger=lambda level, msg: print(f"[{level.value}] {msg}"),  # never logs secrets
    user_agent_suffix="MyApp",                      # appended to the User-Agent (server logging)
)
client = LicenseClient("myapp", "1.2.0", options)
```

## Public API

```python
# Construct — server URL and signing keys are internal constants, never arguments.
LicenseClient(app_id: str, client_version: str, options: LicenseClientOptions | None = None)

client.authenticate(license_key: str) -> Session   # init → verify → start heartbeat
client.release() -> None                            # best-effort, idempotent
client.is_active -> bool
client.close()                                      # release + dispose (or use `with`)

# Session — only what you need; all crypto state is hidden.
session.remaining_millis -> int        # authoritative, counted down from remainMs
session.local_variables  -> Mapping[str, str]
session.global_variables -> Mapping[str, str]
session.latest_version   -> str
session.exp              -> str        # display only
session.server_time      -> str        # display only
session.set_local_variable(key: str, value: str) -> None
```

`LicenseClient` is a context manager — a `with` block releases the session on exit. The
SDK registers no global shutdown hook, so on app exit call `release()` (or use `with`).
If the process dies without releasing, the server's 60-second session TTL cleans up.

## Error handling

Every failure flows through `LicenseError`. Branch on `code` (a `LicenseErrorCode`); use
`retryable`, `context`, and the convenience accessors (`reason`, `until`,
`latest_version`, `download_url`). `server_code` holds the raw code (e.g. `"SDK_102"`)
for logging. Invalid arguments (empty/too long) raise `ValueError` **before** any network
call — those are not `LicenseError`.

| `LicenseErrorCode` | Server code | Meaning | Retryable |
|---|---|---|:--:|
| `NETWORK_ERROR` | — | network/TLS/timeout | ✅ |
| `INTEGRITY_ERROR` | — | decryption/signature/verification failed | ❌ |
| `SERVER_ERROR` | SDK_001 / SERVER_001 | server-side error | ✅ |
| `BAD_REQUEST` | SDK_002 / COMMON_001 | malformed/tampered request | ❌ |
| `INVALID_SOFTWARE` | SDK_003 | unregistered software | ❌ |
| `INVALID_LICENSE` | SDK_004 | bad license | ❌ |
| `FILE_TAMPERED` | SDK_005 | file-hash mismatch | ❌ |
| `UPDATE_REQUIRED` | SDK_006 | unsupported version | ❌ |
| `VERIFY_IN_PROGRESS` | SDK_101 | another verify in progress | ✅ |
| `LICENSE_BANNED` | SDK_102 | banned license | ❌ |
| `LICENSE_EXPIRED` | SDK_103 | expired license | ❌ |
| `LICENSE_ACTIVATION_LIMIT` | SDK_104 | activation/seat limit | ❌ |
| `SOFTWARE_BANNED` | SDK_201 | banned software | ❌ |
| `SOFTWARE_INACTIVE` | SDK_202 | inactive software | ❌ |
| `SOFTWARE_MAINTENANCE` | SDK_204 | maintenance | ✅ |
| `SOFTWARE_UNSUPPORTED` | SDK_205 | unsupported software | ❌ |
| `SESSION_EXPIRED` | SDK_301 | session expired/not found | (re-auth) |
| `VARIABLE_INVALID` | SDK_401 | variable key/value null | ❌ |
| `VARIABLE_KEY_TOO_LONG` | SDK_402 | key too long | ❌ |
| `VARIABLE_VALUE_TOO_LONG` | SDK_403 | value too long | ❌ |
| `VARIABLE_LIMIT_EXCEEDED` | SDK_404 | too many variables | ❌ |
| `UNKNOWN` | (other) | unmapped code | ❌ |

## Security (in brief)

- The server signs responses with a key only it knows; the SDK verifies them with a
  built-in public key → fake servers and MITM are blocked, independent of TLS.
- Each session uses one-time keys for AES-256-GCM encryption, with sequence-based replay
  protection. TLS verification is always on and cannot be disabled.

## ⚠️ Protecting your app is your job

The SDK secures the *communication*, but it **cannot protect your built binary.** If an
attacker patches out the `authenticate` call, the `if` that checks its result, or your
hash checks, the SDK can't stop them — that's your code, not the SDK. So:

- **Obfuscate** your distributed binary.
- Don't centralize the auth/integrity checks — scatter them.
- Design the checks to be hard to bypass.

See [docs/guide.ko.md](docs/guide.ko.md) for the full Korean usage guide.
