clawspec-core 0.4.4

Core library for generating OpenAPI specifications from tests
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
//! OAuth2 token provider for acquiring and refreshing tokens.

use std::time::Duration;

use oauth2::{AccessToken, TokenResponse};

use super::config::{OAuth2Config, OAuth2GrantType};
use super::error::OAuth2Error;
use super::token::OAuth2Token;

impl OAuth2Config {
    /// Acquires a new access token using the configured grant type.
    ///
    /// This method handles:
    /// - Client Credentials grant: fetches a new token from the token endpoint
    /// - Pre-Acquired token: returns the cached token if available
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - Network request fails
    /// - Token endpoint returns an error
    /// - Response cannot be parsed
    pub async fn acquire_token(&self) -> Result<OAuth2Token, OAuth2Error> {
        match self.grant_type {
            OAuth2GrantType::ClientCredentials => self.acquire_client_credentials_token().await,
            OAuth2GrantType::PreAcquired => self.get_pre_acquired_token().await,
        }
    }

    /// Acquires a token using the Client Credentials grant.
    async fn acquire_client_credentials_token(&self) -> Result<OAuth2Token, OAuth2Error> {
        // Create HTTP client with redirect disabled for SSRF prevention
        // Use oauth2::reqwest to ensure version compatibility
        let http_client = oauth2::reqwest::ClientBuilder::new()
            .redirect(oauth2::reqwest::redirect::Policy::none())
            .build()
            .map_err(|e| OAuth2Error::TokenAcquisitionFailed {
                reason: format!("Failed to create HTTP client: {e}"),
            })?;

        self.acquire_client_credentials_token_with_client(&http_client)
            .await
    }

    /// Internal method for acquiring tokens with a custom HTTP client.
    ///
    /// This enables testing without making real network requests by injecting
    /// mock HTTP clients that return predefined responses.
    pub(crate) async fn acquire_client_credentials_token_with_client(
        &self,
        http_client: &oauth2::reqwest::Client,
    ) -> Result<OAuth2Token, OAuth2Error> {
        use oauth2::basic::BasicClient;
        use oauth2::{AuthUrl, ClientId, ClientSecret, Scope, TokenUrl};

        let client_id = ClientId::new(self.client_id.clone());

        // Use a dummy auth URL if not specified (client_credentials doesn't need it)
        let auth_url_str = self
            .auth_url
            .as_ref()
            .map(|u| u.to_string())
            .unwrap_or_else(|| format!("{}/../authorize", self.token_url));

        let auth_url = AuthUrl::new(auth_url_str).map_err(|e| OAuth2Error::ConfigurationError {
            reason: format!("Invalid authorization URL: {e}"),
        })?;

        let token_url = TokenUrl::new(self.token_url.to_string()).map_err(|e| {
            OAuth2Error::ConfigurationError {
                reason: format!("Invalid token URL: {e}"),
            }
        })?;

        // Build client using the new builder pattern (oauth2 5.x)
        // The type-state pattern ensures exchange_client_credentials() is available
        // only after set_token_uri() is called
        let mut client = BasicClient::new(client_id)
            .set_auth_uri(auth_url)
            .set_token_uri(token_url);

        // Set client secret if provided
        if let Some(ref secret) = self.client_secret {
            client = client.set_client_secret(ClientSecret::new(secret.as_str().to_string()));
        }

        let mut request = client.exchange_client_credentials();

        // Add scopes
        for scope in self.scopes.iter().map(|s| Scope::new(s.clone())) {
            request = request.add_scope(scope);
        }

        // Execute the request
        let token_result = request.request_async(http_client).await.map_err(|e| {
            OAuth2Error::TokenAcquisitionFailed {
                reason: format!("{e}"),
            }
        })?;

        // Convert to our token type
        let token =
            Self::convert_token_response(token_result.access_token(), token_result.expires_in());

        // Cache the token
        self.set_token(token.clone()).await;

        Ok(token)
    }

    /// Returns the pre-acquired token if available.
    async fn get_pre_acquired_token(&self) -> Result<OAuth2Token, OAuth2Error> {
        self.get_token().await.ok_or(OAuth2Error::TokenExpired)
    }

    /// Converts an oauth2 token response to our token type.
    fn convert_token_response(
        access_token: &AccessToken,
        expires_in: Option<Duration>,
    ) -> OAuth2Token {
        if let Some(duration) = expires_in {
            OAuth2Token::with_expiry(access_token.secret().clone(), duration)
        } else {
            OAuth2Token::new(access_token.secret().clone())
        }
    }

    /// Gets a valid token, acquiring a new one if necessary.
    ///
    /// This is the main entry point for getting an access token.
    /// It checks the cache first and only acquires a new token if needed.
    pub async fn get_valid_token(&self) -> Result<OAuth2Token, OAuth2Error> {
        // Check if we have a valid cached token
        if !self.needs_token().await
            && let Some(token) = self.get_token().await
        {
            return Ok(token);
        }

        // Need to acquire a new token
        self.acquire_token().await
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // =========================================
    // Pre-acquired token tests
    // =========================================

    #[tokio::test]
    async fn should_return_pre_acquired_token() {
        let config = OAuth2Config::pre_acquired(
            "client-id",
            "https://auth.example.com/token",
            "pre-acquired-access-token",
        )
        .expect("Should create builder")
        .build()
        .expect("Should build config");

        let token = config.get_valid_token().await.expect("Should get token");
        assert_eq!(token.access_token(), "pre-acquired-access-token");
    }

    #[tokio::test]
    async fn should_fail_when_no_pre_acquired_token() {
        let config =
            OAuth2Config::pre_acquired("client-id", "https://auth.example.com/token", "token")
                .expect("Should create builder")
                .build()
                .expect("Should build config");

        config.token_cache.clear().await;

        let result = config.get_pre_acquired_token().await;
        assert!(result.is_err());
        match result.expect_err("Should fail") {
            OAuth2Error::TokenExpired => {}
            _ => panic!("Expected TokenExpired error"),
        }
    }

    // =========================================
    // Cache behavior tests
    // =========================================

    #[tokio::test]
    async fn should_return_cached_token_without_network_call() {
        let config = OAuth2Config::client_credentials(
            "test-client",
            "test-secret",
            "https://auth.example.com/token",
        )
        .expect("Should create builder")
        .build()
        .expect("Should build config");

        // Pre-populate cache with a valid token (long expiry)
        let token = OAuth2Token::with_expiry("cached-valid-token", Duration::from_secs(3600));
        config.set_token(token).await;

        // get_valid_token should return cached token
        let result = config.get_valid_token().await;
        let token = result.expect("Should return cached token");
        assert_eq!(token.access_token(), "cached-valid-token");
    }

    // =========================================
    // convert_token_response tests
    // =========================================

    #[test]
    fn should_convert_token_with_expiry() {
        let access_token = oauth2::AccessToken::new("test-token".to_string());
        let expires_in = Some(Duration::from_secs(3600));

        let token = OAuth2Config::convert_token_response(&access_token, expires_in);

        assert_eq!(token.access_token(), "test-token");
        assert!(token.time_until_expiry().is_some());
    }

    #[test]
    fn should_convert_token_without_expiry() {
        let access_token = oauth2::AccessToken::new("no-expiry".to_string());
        let expires_in = None;

        let token = OAuth2Config::convert_token_response(&access_token, expires_in);

        assert_eq!(token.access_token(), "no-expiry");
        assert!(token.time_until_expiry().is_none());
    }

    // =========================================
    // Scope configuration tests
    // =========================================

    #[tokio::test]
    async fn should_configure_scopes() {
        let config = OAuth2Config::client_credentials(
            "test-client",
            "test-secret",
            "https://auth.example.com/token",
        )
        .expect("Should create builder")
        .add_scope("read:users")
        .add_scope("write:users")
        .build()
        .expect("Should build config");

        assert_eq!(config.scopes, vec!["read:users", "write:users"]);
    }

    // =========================================
    // Mock server tests for token acquisition
    // =========================================

    mod mock_server_tests {
        use super::*;
        use wiremock::matchers::{method, path};
        use wiremock::{Mock, MockServer, ResponseTemplate};

        #[tokio::test]
        async fn should_acquire_client_credentials_token() {
            let mock_server = MockServer::start().await;

            Mock::given(method("POST"))
                .and(path("/oauth/token"))
                .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                    "access_token": "test-access-token-12345",
                    "token_type": "Bearer",
                    "expires_in": 3600
                })))
                .expect(1)
                .mount(&mock_server)
                .await;

            let token_url = format!("{}/oauth/token", mock_server.uri());
            let config = OAuth2Config::client_credentials("test-client", "test-secret", &token_url)
                .expect("Should create builder")
                .build()
                .expect("Should build config");

            let token = config
                .acquire_token()
                .await
                .expect("Should acquire token successfully");

            assert_eq!(token.access_token(), "test-access-token-12345");
            assert!(token.time_until_expiry().is_some());
        }

        #[tokio::test]
        async fn should_include_scopes_in_token_request() {
            let mock_server = MockServer::start().await;

            Mock::given(method("POST"))
                .and(path("/oauth/token"))
                .and(wiremock::matchers::body_string_contains(
                    "scope=read%3Ausers",
                ))
                .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                    "access_token": "scoped-token",
                    "token_type": "Bearer",
                    "expires_in": 3600
                })))
                .expect(1)
                .mount(&mock_server)
                .await;

            let token_url = format!("{}/oauth/token", mock_server.uri());
            let config = OAuth2Config::client_credentials("test-client", "test-secret", &token_url)
                .expect("Should create builder")
                .add_scope("read:users")
                .build()
                .expect("Should build config");

            let token = config
                .acquire_token()
                .await
                .expect("Should acquire token with scopes");

            assert_eq!(token.access_token(), "scoped-token");
        }

        #[tokio::test]
        async fn should_handle_token_request_failure() {
            let mock_server = MockServer::start().await;

            Mock::given(method("POST"))
                .and(path("/oauth/token"))
                .respond_with(ResponseTemplate::new(400).set_body_json(serde_json::json!({
                    "error": "invalid_client",
                    "error_description": "Client authentication failed"
                })))
                .expect(1)
                .mount(&mock_server)
                .await;

            let token_url = format!("{}/oauth/token", mock_server.uri());
            let config =
                OAuth2Config::client_credentials("invalid-client", "wrong-secret", &token_url)
                    .expect("Should create builder")
                    .build()
                    .expect("Should build config");

            let result = config.acquire_token().await;

            assert!(result.is_err());
            match result.expect_err("Should fail") {
                OAuth2Error::TokenAcquisitionFailed { reason } => {
                    assert!(
                        reason.contains("invalid_client") || reason.contains("Client"),
                        "Error should contain client error info: {reason}"
                    );
                }
                other => panic!("Expected TokenAcquisitionFailed, got {:?}", other),
            }
        }

        #[tokio::test]
        async fn should_handle_invalid_token_url() {
            // Use an invalid URL that will fail URL parsing within the oauth2 crate
            let result =
                OAuth2Config::client_credentials("test-client", "test-secret", "not-a-valid-url");

            assert!(result.is_err());
        }

        #[tokio::test]
        async fn should_acquire_token_with_multiple_scopes() {
            let mock_server = MockServer::start().await;

            Mock::given(method("POST"))
                .and(path("/oauth/token"))
                .and(wiremock::matchers::body_string_contains(
                    "scope=read%3Ausers",
                ))
                .and(wiremock::matchers::body_string_contains("write%3Ausers"))
                .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                    "access_token": "multi-scope-token",
                    "token_type": "Bearer",
                    "expires_in": 3600
                })))
                .expect(1)
                .mount(&mock_server)
                .await;

            let token_url = format!("{}/oauth/token", mock_server.uri());
            let config = OAuth2Config::client_credentials("test-client", "test-secret", &token_url)
                .expect("Should create builder")
                .add_scope("read:users")
                .add_scope("write:users")
                .build()
                .expect("Should build config");

            let token = config
                .acquire_token()
                .await
                .expect("Should acquire token with multiple scopes");

            assert_eq!(token.access_token(), "multi-scope-token");
        }

        #[tokio::test]
        async fn should_handle_token_without_expiry() {
            let mock_server = MockServer::start().await;

            Mock::given(method("POST"))
                .and(path("/oauth/token"))
                .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                    "access_token": "no-expiry-token",
                    "token_type": "Bearer"
                })))
                .expect(1)
                .mount(&mock_server)
                .await;

            let token_url = format!("{}/oauth/token", mock_server.uri());
            let config = OAuth2Config::client_credentials("test-client", "test-secret", &token_url)
                .expect("Should create builder")
                .build()
                .expect("Should build config");

            let token = config
                .acquire_token()
                .await
                .expect("Should acquire token without expiry");

            assert_eq!(token.access_token(), "no-expiry-token");
            assert!(
                token.time_until_expiry().is_none(),
                "Token without expires_in should have no expiry"
            );
        }

        #[tokio::test]
        async fn should_cache_token_after_acquisition() {
            let mock_server = MockServer::start().await;

            Mock::given(method("POST"))
                .and(path("/oauth/token"))
                .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                    "access_token": "cached-token-value",
                    "token_type": "Bearer",
                    "expires_in": 3600
                })))
                .expect(1) // Should only be called once due to caching
                .mount(&mock_server)
                .await;

            let token_url = format!("{}/oauth/token", mock_server.uri());
            let config = OAuth2Config::client_credentials("test-client", "test-secret", &token_url)
                .expect("Should create builder")
                .build()
                .expect("Should build config");

            // First call - should hit the server
            let token1 = config
                .acquire_token()
                .await
                .expect("First token acquisition should succeed");

            // Second call - should use cached token (get_valid_token checks cache first)
            let token2 = config
                .get_valid_token()
                .await
                .expect("Second call should use cached token");

            assert_eq!(token1.access_token(), "cached-token-value");
            assert_eq!(token2.access_token(), "cached-token-value");
        }
    }
}