|
5 | 5 | use std::num::NonZeroU32;
|
6 | 6 |
|
7 | 7 | use chrono::Utc;
|
8 |
| -use dropshot::ResultsPage; |
9 | 8 | use dropshot::test_util::ClientTestContext;
|
| 9 | +use dropshot::{HttpErrorResponseBody, ResultsPage}; |
10 | 10 | use nexus_auth::authn::USER_TEST_UNPRIVILEGED;
|
11 | 11 | use nexus_db_queries::db::fixed_data::silo::DEFAULT_SILO;
|
12 | 12 | use nexus_db_queries::db::identity::{Asset, Resource};
|
@@ -55,7 +55,9 @@ async fn test_device_auth_flow(cptestctx: &ControlPlaneTestContext) {
|
55 | 55 | .expect("failed to reject device auth start without client_id");
|
56 | 56 |
|
57 | 57 | let client_id = Uuid::new_v4();
|
58 |
| - let authn_params = DeviceAuthRequest { client_id }; |
| 58 | + // note that this exercises ttl_seconds being omitted from the body because |
| 59 | + // it's URL encoded, so None means it's omitted |
| 60 | + let authn_params = DeviceAuthRequest { client_id, ttl_seconds: None }; |
59 | 61 |
|
60 | 62 | // Using a JSON encoded body fails.
|
61 | 63 | RequestBuilder::new(testctx, Method::POST, "/device/auth")
|
@@ -241,7 +243,7 @@ async fn test_device_auth_flow(cptestctx: &ControlPlaneTestContext) {
|
241 | 243 | /// as a string
|
242 | 244 | async fn get_device_token(testctx: &ClientTestContext) -> String {
|
243 | 245 | let client_id = Uuid::new_v4();
|
244 |
| - let authn_params = DeviceAuthRequest { client_id }; |
| 246 | + let authn_params = DeviceAuthRequest { client_id, ttl_seconds: None }; |
245 | 247 |
|
246 | 248 | // Start a device authentication flow
|
247 | 249 | let auth_response: DeviceAuthResponse =
|
@@ -431,6 +433,184 @@ async fn test_device_token_expiration(cptestctx: &ControlPlaneTestContext) {
|
431 | 433 | assert_eq!(settings.device_token_max_ttl_seconds, None);
|
432 | 434 | }
|
433 | 435 |
|
| 436 | +// lets me stick whatever I want in this thing to be URL-encoded |
| 437 | +#[derive(serde::Serialize)] |
| 438 | +struct BadAuthReq { |
| 439 | + client_id: String, |
| 440 | + ttl_seconds: String, |
| 441 | +} |
| 442 | + |
| 443 | +/// Test that 0 and negative values for ttl_seconds give immediate 400s |
| 444 | +#[nexus_test] |
| 445 | +async fn test_device_token_request_ttl_invalid( |
| 446 | + cptestctx: &ControlPlaneTestContext, |
| 447 | +) { |
| 448 | + let testctx = &cptestctx.external_client; |
| 449 | + |
| 450 | + let auth_response = NexusRequest::new( |
| 451 | + RequestBuilder::new(testctx, Method::POST, "/device/auth") |
| 452 | + .allow_non_dropshot_errors() |
| 453 | + .body_urlencoded(Some(&BadAuthReq { |
| 454 | + client_id: Uuid::new_v4().to_string(), |
| 455 | + ttl_seconds: "0".to_string(), |
| 456 | + })) |
| 457 | + .expect_status(Some(StatusCode::BAD_REQUEST)), |
| 458 | + ) |
| 459 | + .execute() |
| 460 | + // .execute_and_parse_unwrap::<DeviceAuthResponse>() |
| 461 | + .await |
| 462 | + .expect("expected an Ok(TestResponse)"); |
| 463 | + |
| 464 | + let error_body: serde_json::Value = |
| 465 | + serde_json::from_slice(&auth_response.body).unwrap(); |
| 466 | + assert_eq!( |
| 467 | + error_body.get("message").unwrap().to_string(), |
| 468 | + "\"unable to parse URL-encoded body: ttl_seconds: \ |
| 469 | + invalid value: integer `0`, expected a nonzero u32\"" |
| 470 | + ); |
| 471 | + |
| 472 | + let auth_response = NexusRequest::new( |
| 473 | + RequestBuilder::new(testctx, Method::POST, "/device/auth") |
| 474 | + .allow_non_dropshot_errors() |
| 475 | + .body_urlencoded(Some(&BadAuthReq { |
| 476 | + client_id: Uuid::new_v4().to_string(), |
| 477 | + ttl_seconds: "-3".to_string(), |
| 478 | + })) |
| 479 | + .expect_status(Some(StatusCode::BAD_REQUEST)), |
| 480 | + ) |
| 481 | + .execute() |
| 482 | + // .execute_and_parse_unwrap::<DeviceAuthResponse>() |
| 483 | + .await |
| 484 | + .expect("expected an Ok(TestResponse)"); |
| 485 | + |
| 486 | + let error_body: serde_json::Value = |
| 487 | + serde_json::from_slice(&auth_response.body).unwrap(); |
| 488 | + assert_eq!( |
| 489 | + error_body.get("message").unwrap().to_string(), |
| 490 | + "\"unable to parse URL-encoded body: ttl_seconds: \ |
| 491 | + invalid digit found in string\"" |
| 492 | + ); |
| 493 | +} |
| 494 | + |
| 495 | +#[nexus_test] |
| 496 | +async fn test_device_token_request_ttl(cptestctx: &ControlPlaneTestContext) { |
| 497 | + let testctx = &cptestctx.external_client; |
| 498 | + |
| 499 | + // Set silo max TTL to 10 seconds |
| 500 | + let settings = params::SiloAuthSettingsUpdate { |
| 501 | + device_token_max_ttl_seconds: NonZeroU32::new(10).into(), |
| 502 | + }; |
| 503 | + let _: views::SiloAuthSettings = |
| 504 | + object_put(testctx, "/v1/auth-settings", &settings).await; |
| 505 | + |
| 506 | + // Request TTL above the max should fail at verification time |
| 507 | + let invalid_ttl = DeviceAuthRequest { |
| 508 | + client_id: Uuid::new_v4(), |
| 509 | + ttl_seconds: NonZeroU32::new(20), // Above the 10 second max |
| 510 | + }; |
| 511 | + |
| 512 | + let auth_response = NexusRequest::new( |
| 513 | + RequestBuilder::new(testctx, Method::POST, "/device/auth") |
| 514 | + .body_urlencoded(Some(&invalid_ttl)) |
| 515 | + .expect_status(Some(StatusCode::OK)), |
| 516 | + ) |
| 517 | + .execute_and_parse_unwrap::<DeviceAuthResponse>() |
| 518 | + .await; |
| 519 | + |
| 520 | + let confirm_params = |
| 521 | + DeviceAuthVerify { user_code: auth_response.user_code }; |
| 522 | + |
| 523 | + // Confirmation fails because requested TTL exceeds max |
| 524 | + let confirm_error = NexusRequest::new( |
| 525 | + RequestBuilder::new(testctx, Method::POST, "/device/confirm") |
| 526 | + .body(Some(&confirm_params)) |
| 527 | + .expect_status(Some(StatusCode::BAD_REQUEST)), |
| 528 | + ) |
| 529 | + .authn_as(AuthnMode::PrivilegedUser) |
| 530 | + .execute_and_parse_unwrap::<HttpErrorResponseBody>() |
| 531 | + .await; |
| 532 | + |
| 533 | + // Check that the error message mentions TTL |
| 534 | + assert_eq!(confirm_error.error_code, Some("InvalidRequest".to_string())); |
| 535 | + assert_eq!( |
| 536 | + confirm_error.message, |
| 537 | + "Requested TTL 20 seconds exceeds maximum allowed TTL \ |
| 538 | + for this silo of 10 seconds" |
| 539 | + ); |
| 540 | + |
| 541 | + // Request TTL below the max should succeed and be used |
| 542 | + let valid_ttl = DeviceAuthRequest { |
| 543 | + client_id: Uuid::new_v4(), |
| 544 | + ttl_seconds: NonZeroU32::new(3), // Below the 10 second max |
| 545 | + }; |
| 546 | + |
| 547 | + let auth_response = NexusRequest::new( |
| 548 | + RequestBuilder::new(testctx, Method::POST, "/device/auth") |
| 549 | + .body_urlencoded(Some(&valid_ttl)) |
| 550 | + .expect_status(Some(StatusCode::OK)), |
| 551 | + ) |
| 552 | + .execute_and_parse_unwrap::<DeviceAuthResponse>() |
| 553 | + .await; |
| 554 | + |
| 555 | + let device_code = auth_response.device_code; |
| 556 | + let user_code = auth_response.user_code; |
| 557 | + let confirm_params = DeviceAuthVerify { user_code }; |
| 558 | + |
| 559 | + // this time will be pretty close to the now() used on the server when |
| 560 | + // calculating expiration time |
| 561 | + let t0 = Utc::now(); |
| 562 | + |
| 563 | + // Confirmation should succeed |
| 564 | + NexusRequest::new( |
| 565 | + RequestBuilder::new(testctx, Method::POST, "/device/confirm") |
| 566 | + .body(Some(&confirm_params)) |
| 567 | + .expect_status(Some(StatusCode::NO_CONTENT)), |
| 568 | + ) |
| 569 | + .authn_as(AuthnMode::PrivilegedUser) |
| 570 | + .execute() |
| 571 | + .await |
| 572 | + .expect("failed to confirm"); |
| 573 | + |
| 574 | + let token_params = DeviceAccessTokenRequest { |
| 575 | + grant_type: "urn:ietf:params:oauth:grant-type:device_code".to_string(), |
| 576 | + device_code, |
| 577 | + client_id: valid_ttl.client_id, |
| 578 | + }; |
| 579 | + |
| 580 | + // Get the token |
| 581 | + let token_grant = NexusRequest::new( |
| 582 | + RequestBuilder::new(testctx, Method::POST, "/device/token") |
| 583 | + .allow_non_dropshot_errors() |
| 584 | + .body_urlencoded(Some(&token_params)) |
| 585 | + .expect_status(Some(StatusCode::OK)), |
| 586 | + ) |
| 587 | + .authn_as(AuthnMode::PrivilegedUser) |
| 588 | + .execute_and_parse_unwrap::<DeviceAccessTokenGrant>() |
| 589 | + .await; |
| 590 | + |
| 591 | + // Verify the token has roughly the correct expiration time. One second |
| 592 | + // threshold is sufficient to confirm it's not getting the silo max of 10 |
| 593 | + // seconds. Locally, I saw diffs as low as 14ms. |
| 594 | + let tokens = get_tokens_priv(testctx).await; |
| 595 | + let time_expires = tokens[0].time_expires.unwrap(); |
| 596 | + let expected_expires = t0 + Duration::from_secs(3); |
| 597 | + let diff_ms = (time_expires - expected_expires).num_milliseconds().abs(); |
| 598 | + assert!(diff_ms <= 1000, "time diff was {diff_ms} ms. should be near zero"); |
| 599 | + |
| 600 | + // Token should work initially |
| 601 | + project_list(&testctx, &token_grant.access_token, StatusCode::OK) |
| 602 | + .await |
| 603 | + .expect("token should work initially"); |
| 604 | + |
| 605 | + // Wait for token to expire |
| 606 | + sleep(Duration::from_secs(4)).await; |
| 607 | + |
| 608 | + // Token is expired |
| 609 | + project_list(&testctx, &token_grant.access_token, StatusCode::UNAUTHORIZED) |
| 610 | + .await |
| 611 | + .expect("token should be expired"); |
| 612 | +} |
| 613 | + |
434 | 614 | async fn get_tokens_priv(
|
435 | 615 | testctx: &ClientTestContext,
|
436 | 616 | ) -> Vec<views::DeviceAccessToken> {
|
|
0 commit comments