Skip to content

Commit c3c1583

Browse files
committed
Add secure integration guide documentation
1 parent c62ed1a commit c3c1583

1 file changed

Lines changed: 236 additions & 0 deletions

File tree

Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
# Secure Integration Guide
2+
3+
This guide explains how to properly integrate LicenseChain Python SDK into your application to prevent license bypassing and ensure secure validation.
4+
5+
## Overview
6+
7+
The `secure_integration.py` example demonstrates a production-ready implementation that:
8+
9+
1. **Validates licenses on startup** - Ensures the application cannot run without a valid license
10+
2. **Implements periodic re-validation** - Regularly checks license validity to catch revocations
11+
3. **Uses hardware ID validation** - Prevents license sharing between machines
12+
4. **Protects critical operations** - Requires valid license before executing sensitive operations
13+
5. **Manages state securely** - Uses thread locks to prevent race conditions
14+
6. **Handles errors gracefully** - Proper error handling with fallback strategies
15+
16+
## Key Components
17+
18+
### SecureLicenseManager Class
19+
20+
The `SecureLicenseManager` class provides:
21+
22+
- **Automatic validation**: Validates license on initialization and periodically
23+
- **Hardware ID generation**: Creates unique machine fingerprint
24+
- **State management**: Thread-safe license state management
25+
- **Error handling**: Graceful handling of network errors with cached results
26+
27+
### MyApplication Class
28+
29+
The `MyApplication` class demonstrates:
30+
31+
- **Startup validation**: Validates license before application starts
32+
- **Operation protection**: Requires valid license for critical operations
33+
- **Regular operations**: Validates license for regular operations (uses cache)
34+
35+
## Usage
36+
37+
### Basic Setup
38+
39+
```python
40+
import os
41+
from licensechain.client import LicenseChainClient
42+
43+
# Get credentials from environment (NEVER hardcode!)
44+
api_key = os.getenv("LICENSECHAIN_API_KEY")
45+
license_key = os.getenv("LICENSECHAIN_LICENSE_KEY")
46+
app_id = os.getenv("LICENSECHAIN_APP_ID") # Optional
47+
48+
# Initialize secure license manager
49+
license_manager = SecureLicenseManager(
50+
api_key=api_key,
51+
license_key=license_key,
52+
app_id=app_id,
53+
validation_interval=300, # Re-validate every 5 minutes
54+
)
55+
56+
# Initialize application
57+
app = MyApplication(license_manager)
58+
59+
# Start application (validates license)
60+
await app.startup()
61+
```
62+
63+
### Protecting Critical Operations
64+
65+
```python
66+
async def critical_operation(self, data):
67+
# CRITICAL: Validate license before operation
68+
await self.license_manager.require_valid()
69+
70+
# Perform the operation
71+
# ... your code here ...
72+
```
73+
74+
### Regular Operations
75+
76+
```python
77+
async def regular_operation(self, data):
78+
# Validate license (uses cache if recent)
79+
is_valid = await self.license_manager.ensure_valid()
80+
if not is_valid:
81+
return None # Operation blocked
82+
83+
# Perform the operation
84+
# ... your code here ...
85+
```
86+
87+
## Security Best Practices
88+
89+
### 1. Environment Variables
90+
91+
**✅ DO:**
92+
```python
93+
api_key = os.getenv("LICENSECHAIN_API_KEY")
94+
```
95+
96+
**❌ DON'T:**
97+
```python
98+
api_key = "hardcoded-key-here" # NEVER do this!
99+
```
100+
101+
### 2. Startup Validation
102+
103+
**✅ DO:**
104+
```python
105+
async def startup(self):
106+
is_valid = await self.license_manager.validate_license(force=True)
107+
if not is_valid:
108+
sys.exit(1) # Exit if invalid
109+
```
110+
111+
**❌ DON'T:**
112+
```python
113+
async def startup(self):
114+
# Don't skip validation!
115+
pass
116+
```
117+
118+
### 3. Periodic Re-validation
119+
120+
**✅ DO:**
121+
```python
122+
await license_manager.start_periodic_validation()
123+
```
124+
125+
**❌ DON'T:**
126+
```python
127+
# Don't validate only once
128+
is_valid = await license_manager.validate_license()
129+
# ... never validate again ...
130+
```
131+
132+
### 4. Hardware ID Binding
133+
134+
**✅ DO:**
135+
```python
136+
# Hardware ID is automatically generated and validated
137+
hardware_id = license_manager._hardware_id
138+
```
139+
140+
**❌ DON'T:**
141+
```python
142+
# Don't allow license sharing
143+
# Hardware ID validation prevents this automatically
144+
```
145+
146+
### 5. Critical Operation Protection
147+
148+
**✅ DO:**
149+
```python
150+
async def critical_operation(self):
151+
await self.license_manager.require_valid()
152+
# ... operation code ...
153+
```
154+
155+
**❌ DON'T:**
156+
```python
157+
async def critical_operation(self):
158+
# Don't skip validation!
159+
# ... operation code ...
160+
```
161+
162+
## Testing
163+
164+
Use the `test_connection.py` script to verify your setup:
165+
166+
```bash
167+
# Set environment variables
168+
export LICENSECHAIN_API_KEY='your-api-key'
169+
export LICENSECHAIN_LICENSE_KEY='your-license-key'
170+
export LICENSECHAIN_APP_ID='your-app-id' # Optional
171+
172+
# Run connection test
173+
python examples/test_connection.py
174+
```
175+
176+
## Workflow Verification
177+
178+
The secure integration ensures:
179+
180+
1. **License Validation Workflow**:
181+
- ✅ Validates license on startup
182+
- ✅ Re-validates periodically
183+
- ✅ Validates before critical operations
184+
- ✅ Handles network errors gracefully
185+
186+
2. **Hardware ID Validation**:
187+
- ✅ Generates unique hardware ID
188+
- ✅ Validates hardware ID matches license
189+
- ✅ Prevents license sharing
190+
191+
3. **API Connection**:
192+
- ✅ Connects to `https://api.licensechain.app/v1`
193+
- ✅ Uses correct endpoint: `POST /licenses/verify`
194+
- ✅ Sends correct payload: `{"key": "license-key"}`
195+
- ✅ Handles authentication errors
196+
- ✅ Handles network errors
197+
198+
## Response Format
199+
200+
The API returns:
201+
202+
```json
203+
{
204+
"valid": true,
205+
"status": "ACTIVE",
206+
"expiresAt": "2025-12-31T23:59:59Z",
207+
"email": "user@example.com",
208+
"metadata": {
209+
"hardware_id": "abc123..."
210+
}
211+
}
212+
```
213+
214+
## Error Handling
215+
216+
The implementation handles:
217+
218+
- **AuthenticationError**: Invalid API key
219+
- **ValidationError**: Invalid license key
220+
- **NetworkError**: Connection issues (uses cached result if recent)
221+
- **LicenseChainException**: Other SDK errors
222+
223+
## Next Steps
224+
225+
1. Review `examples/secure_integration.py` for complete implementation
226+
2. Run `examples/test_connection.py` to verify your setup
227+
3. Integrate `SecureLicenseManager` into your application
228+
4. Protect critical operations with `require_valid()`
229+
5. Set up periodic validation in your application lifecycle
230+
231+
## Support
232+
233+
For issues or questions:
234+
- GitHub Issues: https://github.com/LicenseChain/LicenseChain-Python-SDK/issues
235+
- Documentation: https://docs.licensechain.app/sdk/python
236+
- Email: support@licensechain.app

0 commit comments

Comments
 (0)