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
//! Twitch OAuth Token Library
//!
//! This library provides a comprehensive solution for Twitch OAuth 2.0 authentication,
//! supporting both **app authentication** and **user authentication** flows with built-in
//! CSRF protection, connection pooling, and type safety.
//!
//! ## Core Concepts
//!
//! ### Authentication Flows
//!
//! **App Authentication** ([`TwitchOauth<AppAuth>`])
//! - For server-to-server API calls that don't require user context
//! - Access public data (streams, games, public user info)
//! - No user interaction or redirect URI required
//! - Use [`TwitchOauth::app_access_token()`] to get tokens
//!
//! **User Authentication** ([`TwitchOauth<UserAuth>`])
//! - For applications that need to act on behalf of users
//! - Access user-specific data (follows, subscriptions, chat)
//! - Requires redirect URI and user consent flow
//! - Use [`TwitchOauth::authorization_url()`] and [`TwitchOauth::exchange_code()`]
//!
//! ### Type Safety
//!
//! The library uses Rust's type system to prevent common OAuth mistakes:
//! - [`TwitchOauth<AppAuth>`] only allows app authentication operations
//! - [`TwitchOauth<UserAuth>`] only allows user authentication operations
//! - No way to accidentally call user methods without a redirect URI
//!
//! ## Examples
//!
//! ### App Authentication Flow
//!
//! ```rust,no_run
//! use twitch_oauth_token::TwitchOauth;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let oauth = TwitchOauth::new("client_id", "client_secret");
//!
//! let token = oauth.app_access_token().await?;
//!
//! println!("App token: {}", token.access_token.secret());
//! println!("Expires in: {} seconds", token.expires_in);
//!
//! Ok(())
//! }
//! ```
//!
//! ### User Authentication Flow
//!
//! ```rust
//! use std::str::FromStr;
//! use twitch_oauth_token::{
//! scope::{ChannelScopes, ChatScopes},
//! AuthCallback,
//! RedirectUrl, TwitchOauth,
//! };
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Setup OAuth client with redirect URI
//! let oauth = TwitchOauth::new("your_client_id", "your_client_secret")
//! .with_redirect_uri(RedirectUrl::from_str("http://localhost:3000/auth/callback")?);
//!
//! // Create authorization URL with specific scopes
//! let mut auth_request = oauth.authorization_url();
//! auth_request.scopes_mut()
//! .send_chat_message()
//! .get_channel_emotes()
//! .modify_channel_info();
//!
//! let auth_url = auth_request.url();
//! println!("Visit: {}", auth_url);
//!
//! // In your callback handler:
//! // let callback: AuthCallback = /* parse from URL */;
//! // let token = oauth.exchange_code(callback.code, callback.state).await?;
//!
//! Ok(())
//! }
//! ```
//!
//! ### Handling OAuth Callbacks
//!
//! When the user grants access, Twitch redirects them back to your redirect URL with the authorization result.
//!
//! #### Raw HTTP Format
//! If you're not using a web framework, the callback arrives as a raw HTTP request:
//!
//! ```http
//! GET /?code=...&scope=...&state=... HTTP/1.1
//! ```
//!
//! The query parameters contain:
//! - `code`: Authorization code to exchange for tokens
//! - `state`: CSRF protection token (must match what you sent)
//! - `scope`: Space-separated scopes that were actually granted
//!
//! #### Processing the Callback
//!
//! Regardless of how you extract the parameters, the token exchange process is the same.
//! The library provides [`AuthCallback`] to parse OAuth callback parameters.
//! Use it to extract the authorization code and state
//!
//! ```rust
//! use twitch_oauth_token::{AuthCallback, TwitchOauth, UserAuth};
//!
//! async fn handle_oauth_callback(
//! oauth: &TwitchOauth<UserAuth>,
//! query_params: AuthCallback,
//! ) -> Result<(), twitch_oauth_token::Error> {
//! let token = oauth
//! .exchange_code(query_params.code, query_params.state)
//! .await?;
//!
//! // Store the tokens securely for future API calls
//! // store_user_tokens(user_id, &token).await?;
//!
//! Ok(())
//! }
//! ```
//!
//! ## Working with Scopes
//!
//! The library provides type-safe scope builders organized by API category:
//!
//! ```rust
//! use twitch_oauth_token::scope::{
//! ChannelScopes, ChatScopes, ModerationScopes,
//! SubscriptionScopes, UserScopes
//! };
//! # use twitch_oauth_token::{TwitchOauth, UserAuth};
//!
//! # fn run(oauth: TwitchOauth<UserAuth>) {
//! let mut auth_request = oauth.authorization_url();
//!
//! // Chat-related scopes
//! auth_request.scopes_mut()
//! .send_chat_message() // Send chat messages
//! .get_user_emotes() // Read user's emotes
//! .get_chatters() // Read chatters list
//! .send_chat_announcement(); // Send announcements
//!
//! // Channel management scopes
//! auth_request.scopes_mut()
//! .modify_channel_info() // Update channel info
//! .get_channel_followers() // Read followers
//! .channel_ban_unban(); // Ban/unban users
//!
//! // Moderation scopes
//! auth_request.scopes_mut()
//! .ban_user() // Ban users
//! .delete_chat_messages() // Delete messages
//! .update_automod_settings(); // Manage AutoMod
//!
//! // Or add entire API categories at once
//! auth_request.scopes_mut()
//! .chat_api() // All chat-related scopes
//! .moderation_api() // All moderation scopes
//! .users_api(); // All user-related scopes
//! # }
//! ```
//!
//! ## Token Management
//!
//! ### Refresh
//!
//! Returns [`UserToken`].
//!
//! ```rust
//! # use twitch_oauth_token::{Error, RefreshToken, TwitchOauth};
//! # async fn run(oauth: TwitchOauth, refresh_token: RefreshToken) -> Result<(), Error> {
//! let new_token = oauth.refresh_access_token(refresh_token).await?;
//! # Ok(())
//! # }
//! ```
//!
//! ### Validation
//!
//! Returns [`TokenInfo`].
//!
//! ```rust
//! # use twitch_oauth_token::{AccessToken, Error, TwitchOauth};
//! # async fn run(oauth: TwitchOauth, access_token: AccessToken) -> Result<(), Error> {
//! let token_info = oauth.validate_access_token(&access_token).await?;
//! # Ok(())
//! # }
//! ```
//!
//! ### Revocation
//!
//! ```rust
//! # use twitch_oauth_token::{AccessToken, Error, TwitchOauth};
//! # async fn run(oauth: TwitchOauth, access_token: AccessToken) -> Result<(), Error> {
//! oauth.revoke_access_token(&access_token).await?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Security Features
//!
//! ### CSRF Protection
//!
//! The library automatically generates and validates CSRF tokens using HMAC-SHA256:
//! - Tokens are cryptographically signed with a random secret key
//! - Automatic validation prevents CSRF attacks
//! - Configurable expiration and clock skew tolerance
//! - Self-contained validation without database storage
//!
//! **Important for multi-server deployments:**
//! - By default, each [`TwitchOauth`] instance generates a random secret key
//! - For load-balanced or clustered environments, use [`TwitchOauth<UserAuth>::with_secret_key`] to share the same secret across all instances
//! - Without a shared secret key, tokens generated on one server will fail validation on another
//!
//! #### CSRF Configuration
//!
//! You can customize CSRF token validation behavior:
//!
//! ```rust
//! use twitch_oauth_token::{csrf::CsrfConfig, TwitchOauth};
//!
//! // Default: 30 minutes expiry, no clock skew tolerance
//! let oauth = TwitchOauth::new("client_id", "client_secret");
//!
//! // Custom: 10 minutes expiry, 60 seconds clock skew tolerance
//! let oauth = TwitchOauth::new("client_id", "client_secret")
//! .with_csrf_config(CsrfConfig::new(60, 600));
//!
//! // Strict: 5 minutes expiry, no clock skew tolerance
//! let oauth = TwitchOauth::new("client_id", "client_secret")
//! .with_csrf_config(CsrfConfig::default().with_max_age(300));
//! ```
//!
//! **When to customize:**
//! - **High-security apps**: Shorter expiry times (300-900 seconds)
//! - **Mobile apps**: Clock skew tolerance (30-60 seconds) for device time differences
//! - **Server clusters**: Clock skew tolerance for distributed systems and shared secret key via [`TwitchOauth<UserAuth>::with_secret_key`]
//!
//! ### Secure Defaults
//!
//! - HTTPS enforcement (HTTP requests are blocked)
//! - Secure HTTP headers (no-cache, strict security)
//! - TLS 1.2+ minimum with certificate validation
//! - HTTP/2 required (no HTTP/1.1 fallback)
//! - Connection pooling with idle timeouts
//!
//! ## Error Handling
//!
//! The library provides comprehensive error types:
//!
//! ```rust
//! # use twitch_oauth_token::{TwitchOauth, AppAuth};
//!
//! # async fn run(oauth: TwitchOauth<AppAuth>) {
//!
//! match oauth.app_access_token().await {
//! Ok(response) => { /* success */ }
//! Err(e) => {
//! // Network/HTTP errors (connection issues, timeouts, DNS failures)
//! if e.is_request_error() {
//! eprintln!("Network error: {}", e);
//! // Common causes:
//! // - No internet connection
//! // - Twitch API is down
//! // - Firewall blocking requests
//! // - DNS resolution failure
//!
//! // OAuth-specific errors (invalid credentials, CSRF mismatch)
//! } else if e.is_oauth_error() {
//! eprintln!("OAuth error: {}", e);
//! // Common causes:
//! // - Invalid client_id or client_secret
//! // - CSRF token validation failed
//! // - Authorization code expired or invalid
//! // - Redirect URI mismatch
//!
//! // JSON deserialization errors (response doesn't match expected structure)
//! } else if e.is_decode() {
//! eprintln!("Deserialization error: {}", e);
//! if let Some(raw) = e.raw() {
//! eprintln!("Raw response body: {}", raw);
//! }
//! // Common causes:
//! // - Twitch API response schema changed
//! // - Missing or unexpected fields in JSON
//! // - Type mismatch (expected number, got string)
//! // - Invalid data format
//! }
//! }
//! }
//! # }
//! ```
//!
//! ## Development Server
//!
//! For local development, use the built-in oneshot server to handle OAuth callbacks:
//!
//! ```rust,no_run
//! # #[cfg(feature = "oneshot")]
//! # {
//! use std::{str::FromStr, time::Duration};
//! use twitch_oauth_token::{oneshot, scope::ChatScopes, AuthCallback, RedirectUrl, TwitchOauth};
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let oauth = TwitchOauth::new("client_id", "client_secret")
//! .with_redirect_uri(RedirectUrl::from_str("http://localhost:3000")?);
//!
//! let mut auth_request = oauth.authorization_url();
//! auth_request.scopes_mut().chat_api();
//!
//! println!("Visit this URL to authorize:");
//! println!("{}", auth_request.url());
//! println!("Waiting for callback...");
//!
//! let config = oneshot::Config::new()
//! .with_port(3000)
//! .with_duration(Duration::from_secs(120));
//!
//! // Wait up to 2 minutes for the user to complete OAuth flow
//! match oneshot::listen::<AuthCallback>(config).await {
//! Ok(callback) => {
//! let token = oauth
//! .exchange_code(callback.code, callback.state)
//! .await?;
//! println!("Successfully got user token!");
//! }
//! Err(e) => {
//! eprintln!("OAuth flow failed: {}", e);
//! }
//! }
//!
//! Ok(())
//! }
//! # }
//! ```
//!
//! ## HTTP Client Configuration
//!
//! The default HTTP client works for most applications.
//! For custom configuration, see [`client`].
//!
//! ## Testing
//!
//! For local testing with Twitch Mock API, see [`test_oauth`] (requires `test` feature).
pub use Error;
pub use ;
pub use ;
pub use Scope;
pub use ;
pub use AuthCallback;
pub use oneshot;
// Re-export
pub use ;