|
2 | 2 |
|
3 | 3 | # CipherToken |
4 | 4 |
|
5 | | -**CipherToken** is a modern, high-performance Python library for cryptography and JWT (JSON Web Token) management. |
6 | | -It is implemented in **Rust** using **PyO3**, providing both speed and security for generating, encoding, decoding, and verifying tokens. |
| 5 | +**CipherToken** is a **next-generation** token engine built for developers who demand speed, security, and reliability. Currently focused on **JWT**, it provides a complete token lifecycle — from key generation and token minting to verification, rotation, and expiry tracking — all backed by the raw performance of **Rust**. |
7 | 6 |
|
8 | | -The library is designed to be modular, organized into **submodules** for secret management, time utilities, algorithms, and token handling. |
9 | | -It supports both **synchronous** and **asynchronous** operations |
| 7 | +- Python 3.8+ |
| 8 | +- HMAC, RSA, ECDSA, RSA-PSS, EdDSA |
| 9 | +- Sync and Async APIs via **Tokio** |
10 | 10 |
|
11 | | ---- |
12 | | - |
13 | | -## Library Overview |
14 | | - |
15 | | -CipherToken is structured into the following main parts: |
16 | | - |
17 | | -### 1. `secret` Module |
18 | | -Handles key generation for symmetric (HMAC) and asymmetric (RSA) algorithms: |
19 | | - |
20 | | -- `secret_key()` – Generate a random HMAC key (default 32 bytes) |
21 | | -- `secret_key_with_size(size: int)` – Generate HMAC key with a custom size |
22 | | -- `generate_hmac_secret(size: int)` – Generate an HMAC secret key |
23 | | -- `generate_hmac_secret_async(size: int)` – Async version of HMAC key generation |
24 | | -- `generate_rsa_keypair(bits: Optional[int] = 2048)` – Generate RSA private/public key pair |
25 | | - |
26 | | ---- |
27 | | - |
28 | | -### 2. `time` Module |
29 | | -Provides utility functions for time conversion and timestamps: |
30 | | - |
31 | | -- `now()` – Current UNIX timestamp |
32 | | -- `seconds(n: int)` – Convert n seconds to seconds |
33 | | -- `minutes(n: int)` – Convert n minutes to seconds |
34 | | -- `hours(n: int)` – Convert n hours to seconds |
35 | | -- `days(n: int)` – Convert n days to seconds |
36 | | -- `weeks(n: int)` – Convert n weeks to seconds |
37 | | - |
38 | | ---- |
39 | | - |
40 | | -### 3. `utils` Module |
41 | | -Contains useful constants: |
42 | | - |
43 | | -- `DEFAULT_SECRET_SIZE` – Default key size |
44 | | -- `MIN_SECRET_SIZE` – Minimum allowed key size |
45 | | -- `TOKEN_ACCESS` – Access token type |
46 | | -- `TOKEN_REFRESH` – Refresh token type |
| 11 | +Built by **[Cipher-Unit](https://cipherunit.xyz/)**. |
47 | 12 |
|
48 | 13 | --- |
49 | 14 |
|
50 | | -### 4. `algorithms` Module |
51 | | -Defines algorithm constants for token signing: |
| 15 | +## Documentation |
52 | 16 |
|
53 | | -- HMAC: `HS256`, `HS384`, `HS512` |
54 | | -- RSA: `RS256`, `RS384`, `RS512` |
55 | | -- ECDSA: `ES256`, `ES384` |
56 | | -- RSA-PSS: `PS256`, `PS384`, `PS512` |
57 | | -- Edwards Curve: `EDDSA` |
| 17 | +📖 **Official Documentation:** [https://cipherunits.github.io/CipherToken/](https://cipherunits.github.io/CipherToken/) |
58 | 18 |
|
59 | 19 | --- |
60 | 20 |
|
61 | | -### 5. `CipherToken` Class |
62 | | -Main class for managing JWTs: |
63 | | - |
64 | | -- **Constructor:** |
65 | | -```python |
66 | | -from ciphertoken import CipherToken |
67 | | - |
68 | | -CipherToken(secret: str, algorithm: str, access_ttl: int, refresh_ttl: int) |
69 | | -``` |
70 | | - |
71 | | -### Synchronous methods: |
72 | | -```python |
73 | | -access(user_id: int, extra_payload: Optional[dict] = None) -> str |
74 | | -refresh(user_id: int, extra_payload: Optional[dict] = None) -> str |
75 | | -decode(token: str) -> dict |
76 | | -verify(token: str) -> bool |
77 | | -rotation(refresh_token: str, extra_payload: Optional[dict] = None) -> Tuple[str, str] |
78 | | -inspect(token: str) -> dict |
79 | | -remaining_time(token: str) -> Optional[int] |
80 | | -algorithm_name() -> str |
81 | | -``` |
82 | | - |
83 | | -### Asynchronous methods: |
84 | | -```python |
85 | | -await access_async(user_id: int, extra_payload: Optional[dict] = None) -> str |
86 | | -await refresh_async(user_id: int, extra_payload: Optional[dict] = None) -> str |
87 | | -await decode_async(token: str) -> dict |
88 | | -await verify_async(token: str) -> bool |
89 | | -await rotation_async(refresh_token: str, extra_payload: Optional[dict] = None) -> Tuple[str, str] |
90 | | -``` |
91 | | - |
92 | | -### Helper Functions: |
93 | | - |
94 | | -`is_jwt_format(token: str) – Check if a string is a valid JWT` |
95 | | - |
96 | | -`validate_jwt_format(token: str) – Validate JWT structure` |
97 | | - |
98 | 21 | ## Installation |
99 | 22 |
|
100 | | -CipherToken is available via PyPI. Install it with: |
101 | | - |
102 | | -`pip install ciphertoken` |
| 23 | +```bash |
| 24 | +pip install ciphertoken |
| 25 | +``` |
103 | 26 |
|
104 | 27 | --- |
105 | 28 |
|
106 | | -### Usage Example |
| 29 | +## Quick Example |
107 | 30 |
|
108 | 31 | ```python |
109 | | -from ciphertoken.secret import secret_key |
110 | 32 | from ciphertoken import CipherToken |
111 | | -from ciphertoken.time import minutes, days |
112 | | -from ciphertoken.utils import TOKEN_REFRESH |
113 | 33 | from ciphertoken.algorithms import HS256 |
| 34 | +from ciphertoken.time import minutes, days |
| 35 | +from ciphertoken.jwt import access, refresh, rotation |
114 | 36 |
|
115 | | -# Generate a secret key |
116 | | -key = secret_key() |
117 | | - |
118 | | -# Create a CipherToken instance |
119 | | -token = CipherToken(secret=key, algorithm=HS256, access_ttl=minutes(10), refresh_ttl=days(7)) |
120 | | - |
121 | | -# Generate access and refresh tokens |
122 | | -access_token = token.access(user_id=123) |
123 | | -refresh_token = token.refresh(user_id=123) |
124 | | - |
125 | | -# Verify and decode tokens |
126 | | -payload = token.decode(access_token) |
127 | | -is_valid = token.verify(access_token) |
128 | | - |
129 | | -# Async usage |
130 | | -import asyncio |
131 | | - |
132 | | -async def generate_async_token(): |
133 | | - access_token = await token.access_async(user_id=123) |
134 | | - payload = await token.decode_async(access_token) |
135 | | - return payload |
| 37 | +token = CipherToken( |
| 38 | + secret="your-strong-secret-key", |
| 39 | + algorithm=HS256, |
| 40 | + access_ttl=minutes(10), |
| 41 | + refresh_ttl=days(7), |
| 42 | +) |
136 | 43 |
|
137 | | -asyncio.run(generate_async_token()) |
| 44 | +access_token = access(token, payload={"user_id": 42}) |
| 45 | +refresh_token = refresh(token, payload={"user_id": 42}) |
| 46 | +new_access, new_refresh = rotation(token, refresh_token) |
138 | 47 |
|
| 48 | +print(token.verify(access_token)) # True |
| 49 | +print(token.decode(access_token)) |
139 | 50 | ``` |
140 | 51 |
|
141 | | -### Implementation Notes |
142 | | - |
143 | | -- Built in Rust using PyO3 for maximum speed and memory safety |
| 52 | +--- |
144 | 53 |
|
145 | | -- Supports synchronous and asynchronous workflows |
| 54 | +## Contribute |
146 | 55 |
|
147 | | -- Fully modular: secret, time, utils, algorithms, and CipherToken class |
| 56 | +Contributions are welcome! Whether it's bug fixes, new algorithms, documentation improvements, or feature suggestions — feel free to open an issue or pull request. |
148 | 57 |
|
149 | | -- Secure key handling with HMAC and RSA support |
| 58 | +- **Repository:** [https://github.com/cipherunits/CipherToken](https://github.com/cipherunits/CipherToken) |
| 59 | +- **Issues:** [https://github.com/cipherunits/CipherToken/issues](https://github.com/cipherunits/CipherToken/issues) |
150 | 60 |
|
151 | | -- Easily extendable for additional algorithms |
| 61 | +Please read the code of conduct and contribution guidelines before submitting. All contributions must follow the MIT license. |
152 | 62 |
|
| 63 | +--- |
153 | 64 |
|
154 | | -### Summary |
| 65 | +## License |
155 | 66 |
|
156 | | -CipherToken is a modern, Rust-backed Python library for authentication and JWT management. |
157 | | -It is ideal for web APIs, microservices, or any system that requires secure token generation, verification, and rotation. |
| 67 | +**MIT** — see [LICENSE](LICENSE) for details. |
0 commit comments