rixiv 0.1.0

Pixiv client writen in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
# Authentication System

## Overview

PixivPy uses OAuth2 authentication with refresh tokens as the primary authentication method. The password-based authentication has been deprecated. This document details the authentication flow, security requirements, and implementation details for porting to Rust.

## Authentication Methods

### 1. OAuth2 with Refresh Token (Current)
```python
api = AppPixivAPI()
api.auth(refresh_token="YOUR_REFRESH_TOKEN")
```

### 2. OAuth2 with Refresh Token + Device ID
```python
api.auth(refresh_token="YOUR_REFRESH_TOKEN", device_token="DEVICE_TOKEN")
```

### 3. Deprecated: Username/Password
```python
# NO LONGER SUPPORTED
api.login("USERNAME", "PASSWORD")
```

## OAuth2 Flow Details

### Step 1: Initial Authentication
The user must obtain a refresh token through:
- Pixiv's official OAuth flow
- Browser authentication
- Third-party OAuth clients

### Step 2: Token Exchange
Using the refresh token to obtain an access token:
- Endpoint: `https://oauth.secure.pixiv.net/auth/token`
- Method: POST
- Content-Type: `application/x-www-form-urlencoded`

### Step 3: Access Token Usage
- Access tokens are used in API calls via `Authorization: Bearer {access_token}`
- Tokens have limited lifetime (typically 3600 seconds)
- Automatic refresh using the refresh token

## Security Headers

### Required Headers for Authentication
All authentication requests must include:

```http
X-Client-Time: 1234567890
X-Client-Hash: md5(timestamp + hash_secret)
```

### Implementation Details
```python
import time
import hashlib

def generate_client_headers():
    timestamp = str(int(time.time()))
    md5_hash = hashlib.md5(f"{timestamp}{hash_secret}".encode()).hexdigest()
    return {
        "X-Client-Time": timestamp,
        "X-Client-Hash": md5_hash
    }
```

### Constants
```python
client_id = "MOBrBDS8blbauoSck0ZfDbtuzpyT"
client_secret = "lsACyCD94FhDUtGTXi3QzcFE2uU1hqtDaKeqrdwj"
hash_secret = "28c1fdd170a5204386cb1313c7077b34f83e4aaf4aa829ce78c231e05b0bae2c"
```

## Authentication API Endpoint

### Request Format
```http
POST https://oauth.secure.pixiv.net/auth/token
Content-Type: application/x-www-form-urlencoded
User-Agent: PixivIOSApp/7.13.3 (iOS 14.6; iPhone13,2)

X-Client-Time: {timestamp}
X-Client-Hash: {md5_hash}

client_id={client_id}&client_secret={client_secret}&grant_type=refresh_token&refresh_token={refresh_token}
```

### Response Format
```json
{
    "access_token": "aCCESS_TOKEN_HERE",
    "expires_in": 3600,
    "token_type": "Bearer",
    "scope": "",
    "refresh_token": "nEW_REFRESH_TOKEN_HERE",
    "user": {
        "profile_image_urls": {
            "medium": "https://s.pximg.net/common/images/no_profile.png"
        },
        "id": 12345678,
        "name": "username",
        "account": "username",
        "mail_address": "user@example.com",
        "is_premium": false,
        "x_restrict": 0,
        "has_mail": false,
        "require_agreement": false
    }
}
```

## Token Management

### Access Token
- Lifetime: 3600 seconds (1 hour)
- Used for API requests
- Must be included in `Authorization` header

### Refresh Token
- Longer lifetime
- Used to obtain new access tokens
- Can be rotated by the server
- Should be stored securely

### Token Storage Pattern
```python
class TokenManager:
    def __init__(self):
        self.access_token = None
        self.refresh_token = None
        self.expires_at = None

    def is_access_token_valid(self):
        if not self.access_token or not self.expires_at:
            return False
        return time.time() < self.expires_at

    def refresh_if_needed(self):
        if not self.is_access_token_valid():
            self.refresh_tokens()
```

## Authentication Implementation in BasePixivAPI

### Core Method: `auth()`
```python
def auth(self, refresh_token=None, device_token=None):
    """
    Authenticate with refresh token

    Args:
        refresh_token: The refresh token obtained from OAuth flow
        device_token: Optional device token for additional security

    Returns:
        Authentication response containing tokens and user info

    Raises:
        PixivError: If authentication fails
    """
```

### Authentication Flow Code
```python
def auth(self, refresh_token=None, device_token=None):
    if not refresh_token:
        raise PixivError("Refresh token is required")

    # Prepare headers
    headers = self.generate_client_headers()
    headers["User-Agent"] = self.USER_AGENT

    # Prepare form data
    data = {
        "client_id": self.client_id,
        "client_secret": self.client_secret,
        "grant_type": "refresh_token",
        "refresh_token": refresh_token,
    }

    if device_token:
        data["device_token"] = device_token

    # Make authentication request
    response = self.session.post(
        self.AUTH_URL,
        data=data,
        headers=headers
    )

    if response.status_code != 200:
        raise PixivError("Authentication failed", response.headers, response.text)

    auth_info = response.json()

    # Store tokens
    self.access_token = auth_info["access_token"]
    self.refresh_token = auth_info.get("refresh_token", refresh_token)
    self.expires_in = auth_info["expires_in"]
    self.user_info = auth_info["user"]

    # Update session headers for future requests
    self.session.headers.update({
        "Authorization": f"Bearer {self.access_token}"
    })

    return auth_info
```

## API Request Authentication

### Bearer Token Header
All API requests after authentication must include:
```http
Authorization: Bearer {access_token}
```

### Example Request
```python
def make_authenticated_request(self, endpoint):
    headers = {
        "Authorization": f"Bearer {self.access_token}",
        "User-Agent": self.USER_AGENT,
        "Accept-Language": "en-us"
    }

    response = self.session.get(endpoint, headers=headers)
    return response.json()
```

## Error Handling

### Authentication Errors

#### 400 Bad Request
```json
{
    "error": "invalid_request",
    "error_description": "Missing required parameter"
}
```

#### 401 Unauthorized
```json
{
    "error": "invalid_grant",
    "error_description": "Invalid refresh token"
}
```

#### 403 Forbidden
```json
{
    "error": "invalid_client",
    "error_description": "Invalid client credentials"
}
```

### Error Handling Pattern
```python
try:
    auth_info = api.auth(refresh_token=token)
except PixivError as e:
    if "invalid_grant" in str(e):
        # Refresh token invalid, user must re-authenticate
        print("Please obtain a new refresh token")
    else:
        # Other authentication error
        print(f"Authentication failed: {e}")
```

## Device Tokens

### Optional Device Token
- Generated per device/user combination
- Additional security layer
- Persists across sessions
- Can be stored and reused

### Device Token Generation
```python
import uuid
import hashlib

def generate_device_token(user_id=None):
    """
    Generate a device token for additional security

    Args:
        user_id: Optional user ID for deterministic generation

    Returns:
        String device token
    """
    if user_id:
        # Deterministic token based on user ID
        data = f"{user_id}{hash_secret}".encode()
    else:
        # Random token
        data = f"{uuid.uuid4()}{hash_secret}".encode()

    return hashlib.sha256(data).hexdigest()
```

## Security Considerations

### 1. Token Storage
- Store refresh tokens securely (encrypted storage)
- Consider using OS keychain/secure storage
- Never log or expose tokens

### 2. Hash Secret
- Keep the hash secret secure in the client
- Do not expose in logs or error messages
- Consider obfuscation in production builds

### 3. Request Validation
- Always include required security headers
- Validate token expiration before use
- Implement proper error handling for expired tokens

### 4. Network Security
- Use HTTPS for all requests
- Validate SSL certificates (except for bypass scenarios)
- Consider certificate pinning for additional security

## Rate Limiting

### Authentication Rate Limits
- Too many failed attempts may trigger temporary bans
- Implement exponential backoff for retries
- Monitor authentication success/failure rates

### API Rate Limits
- Authenticated requests have higher limits
- Rate limit information in response headers
- Implement polite request patterns

## Migration to Rust: Key Considerations

### 1. HTTP Client
- Use `reqwest` for HTTP requests
- Configure default headers properly
- Handle cookies and sessions

### 2. Time Management
- Use `chrono` for timestamp handling
- UTC timezone consistency
- Accurate timeout calculations

### 3. Cryptography
- Use `sha2` crate for MD5/SHA hashes
- Secure random number generation
- Token generation and validation

### 4. Error Types
- Custom error enum for authentication failures
- Detailed error context
- Proper error propagation

### 5. Async/Await
- Async authentication methods
- Non-blocking token refresh
- Concurrent API request handling

### 6. Storage Patterns
- Use `serde` for token serialization
- Consider encrypted storage libraries
- Secure memory handling

## Example Rust Implementation Outline

```rust
use chrono::{DateTime, Utc};
use reqwest::Client;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256, Md5};

#[derive(Debug, Serialize, Deserialize)]
pub struct AuthInfo {
    pub access_token: String,
    pub expires_in: u64,
    pub token_type: String,
    pub refresh_token: String,
    pub user: UserInfo,
}

#[derive(Debug)]
pub struct PixivAuth {
    client_id: String,
    client_secret: String,
    hash_secret: String,
    client: Client,
    access_token: Option<String>,
    refresh_token: Option<String>,
    expires_at: Option<DateTime<Utc>>,
}

impl PixivAuth {
    pub async fn auth(&mut self, refresh_token: &str) -> Result<AuthInfo, AuthError> {
        // Generate security headers
        let timestamp = Utc::now().timestamp().to_string();
        let hash_input = format!("{}{}", timestamp, self.hash_secret);
        let hash = format!("{:x}", Md5::digest(hash_input.as_bytes()));

        // Build request
        let mut params = HashMap::new();
        params.insert("client_id", &self.client_id);
        params.insert("client_secret", &self.client_secret);
        params.insert("grant_type", "refresh_token");
        params.insert("refresh_token", &refresh_token.to_string());

        let mut headers = HeaderMap::new();
        headers.insert("X-Client-Time", timestamp.parse()?);
        headers.insert("X-Client-Hash", hash.parse()?);
        headers.insert("User-Agent", "PixivIOSApp/7.13.3 (iOS 14.6; iPhone13,2)".parse()?);

        // Execute request
        let response = self.client
            .post("https://oauth.secure.pixiv.net/auth/token")
            .form(&params)
            .headers(headers)
            .send()
            .await?;

        if !response.status().is_success() {
            return Err(AuthError::InvalidCredentials);
        }

        let auth_info: AuthInfo = response.json().await?;

        // Update stored tokens
        self.access_token = Some(auth_info.access_token.clone());
        self.refresh_token = Some(auth_info.refresh_token.clone());
        self.expires_at = Some(Utc::now() + chrono::Duration::seconds(auth_info.expires_in as i64));

        Ok(auth_info)
    }

    pub async fn ensure_auth(&mut self) -> Result<(), AuthError> {
        if let Some(expires_at) = self.expires_at {
            if Utc::now() >= expires_at {
                self.refresh_tokens().await?;
            }
        }
        Ok(())
    }
}
```

## Testing Authentication

### Unit Tests
1. Test header generation
2. Test token parsing
3. Test error scenarios
4. Test token expiration logic

### Integration Tests
1. Test full authentication flow
2. Test token refresh
3. Test invalid credentials
4. Test expired token handling

### Mock Scenarios
- Successful authentication
- Invalid refresh token
- Network errors
- Malformed responses

## Best Practices

1. **Never hardcode tokens** in production code
2. **Implement proper logging** without exposing secrets
3. **Use secure storage** for refresh tokens
4. **Handle edge cases** gracefully
5. **Monitor authentication metrics**
6. **Implement retry logic** with exponential backoff
7. **Validate all inputs** and sanitize responses
8. **Keep dependencies updated** for security patches