55use std:: num:: NonZeroU32 ;
66
77use chrono:: Utc ;
8- use dropshot:: ResultsPage ;
98use dropshot:: test_util:: ClientTestContext ;
9+ use dropshot:: { HttpErrorResponseBody , ResultsPage } ;
1010use nexus_auth:: authn:: USER_TEST_UNPRIVILEGED ;
1111use nexus_db_queries:: db:: fixed_data:: silo:: DEFAULT_SILO ;
1212use nexus_db_queries:: db:: identity:: { Asset , Resource } ;
@@ -55,6 +55,8 @@ async fn test_device_auth_flow(cptestctx: &ControlPlaneTestContext) {
5555 . expect ( "failed to reject device auth start without client_id" ) ;
5656
5757 let client_id = Uuid :: new_v4 ( ) ;
58+ // note that this exercises ttl_seconds being omitted from the body because
59+ // it's URL encoded, so None means it's omitted
5860 let authn_params = DeviceAuthRequest { client_id, ttl_seconds : None } ;
5961
6062 // Using a JSON encoded body fails.
@@ -426,72 +428,68 @@ async fn test_device_token_request_ttl(cptestctx: &ControlPlaneTestContext) {
426428 let testctx = & cptestctx. external_client ;
427429
428430 // Set silo max TTL to 10 seconds
429- let _settings: views:: SiloSettings = object_put (
430- testctx,
431- "/v1/settings" ,
432- & params:: SiloSettingsUpdate {
433- device_token_max_ttl_seconds : NonZeroU32 :: new ( 10 ) ,
434- } ,
435- )
436- . await ;
437-
438- let client_id = Uuid :: new_v4 ( ) ;
431+ let settings = params:: SiloSettingsUpdate {
432+ device_token_max_ttl_seconds : NonZeroU32 :: new ( 10 ) . into ( ) ,
433+ } ;
434+ let _: views:: SiloSettings =
435+ object_put ( testctx, "/v1/settings" , & settings) . await ;
439436
440437 // Test 1: Request TTL above the max should fail at verification time
441- let authn_params_invalid = DeviceAuthRequest {
442- client_id,
443- ttl_seconds : Some ( 20 ) // Above the 10 second max
438+ let invalid_ttl = DeviceAuthRequest {
439+ client_id : Uuid :: new_v4 ( ) ,
440+ ttl_seconds : Some ( 20 ) , // Above the 10 second max
444441 } ;
445442
446- let auth_response: DeviceAuthResponse =
443+ let auth_response = NexusRequest :: new (
447444 RequestBuilder :: new ( testctx, Method :: POST , "/device/auth" )
448- . allow_non_dropshot_errors ( )
449- . body_urlencoded ( Some ( & authn_params_invalid) )
450- . expect_status ( Some ( StatusCode :: OK ) )
451- . execute ( )
452- . await
453- . expect ( "failed to start client authentication flow" )
454- . parsed_body ( )
455- . expect ( "client authentication response" ) ;
445+ . body_urlencoded ( Some ( & invalid_ttl) )
446+ . expect_status ( Some ( StatusCode :: OK ) ) ,
447+ )
448+ . execute_and_parse_unwrap :: < DeviceAuthResponse > ( )
449+ . await ;
456450
457- let confirm_params = DeviceAuthVerify { user_code : auth_response. user_code } ;
451+ let confirm_params =
452+ DeviceAuthVerify { user_code : auth_response. user_code } ;
458453
459- // Confirmation should fail because requested TTL exceeds max
460- let confirm_response = NexusRequest :: new (
454+ // Confirmation fails because requested TTL exceeds max
455+ let confirm_error = NexusRequest :: new (
461456 RequestBuilder :: new ( testctx, Method :: POST , "/device/confirm" )
462457 . body ( Some ( & confirm_params) )
463458 . expect_status ( Some ( StatusCode :: BAD_REQUEST ) ) ,
464459 )
465460 . authn_as ( AuthnMode :: PrivilegedUser )
466- . execute ( )
467- . await
468- . expect ( "confirmation should fail for TTL above max" ) ;
461+ . execute_and_parse_unwrap :: < HttpErrorResponseBody > ( )
462+ . await ;
469463
470464 // Check that the error message mentions TTL
471- let error_body = String :: from_utf8_lossy ( & confirm_response. body ) ;
472- assert ! ( error_body. contains( "TTL" ) || error_body. contains( "ttl" ) ) ;
465+ assert_eq ! ( confirm_error. error_code, Some ( "InvalidRequest" . to_string( ) ) ) ;
466+ assert_eq ! (
467+ confirm_error. message,
468+ "Requested TTL 20 seconds exceeds maximum allowed TTL 10 seconds for this silo"
469+ ) ;
473470
474471 // Test 2: Request TTL below the max should succeed and be used
475- let authn_params_valid = DeviceAuthRequest {
476- client_id : Uuid :: new_v4 ( ) , // New client ID for new flow
477- ttl_seconds : Some ( 5 ) // Below the 10 second max
472+ let valid_ttl = DeviceAuthRequest {
473+ client_id : Uuid :: new_v4 ( ) ,
474+ ttl_seconds : Some ( 3 ) , // Below the 10 second max
478475 } ;
479476
480- let auth_response: DeviceAuthResponse =
477+ let auth_response = NexusRequest :: new (
481478 RequestBuilder :: new ( testctx, Method :: POST , "/device/auth" )
482- . allow_non_dropshot_errors ( )
483- . body_urlencoded ( Some ( & authn_params_valid) )
484- . expect_status ( Some ( StatusCode :: OK ) )
485- . execute ( )
486- . await
487- . expect ( "failed to start client authentication flow" )
488- . parsed_body ( )
489- . expect ( "client authentication response" ) ;
479+ . body_urlencoded ( Some ( & valid_ttl) )
480+ . expect_status ( Some ( StatusCode :: OK ) ) ,
481+ )
482+ . execute_and_parse_unwrap :: < DeviceAuthResponse > ( )
483+ . await ;
490484
491485 let device_code = auth_response. device_code ;
492486 let user_code = auth_response. user_code ;
493487 let confirm_params = DeviceAuthVerify { user_code } ;
494488
489+ // this time will be pretty close to the now() used on the server when
490+ // calculating expiration time
491+ let t0 = Utc :: now ( ) ;
492+
495493 // Confirmation should succeed
496494 NexusRequest :: new (
497495 RequestBuilder :: new ( testctx, Method :: POST , "/device/confirm" )
@@ -506,44 +504,39 @@ async fn test_device_token_request_ttl(cptestctx: &ControlPlaneTestContext) {
506504 let token_params = DeviceAccessTokenRequest {
507505 grant_type : "urn:ietf:params:oauth:grant-type:device_code" . to_string ( ) ,
508506 device_code,
509- client_id : authn_params_valid . client_id ,
507+ client_id : valid_ttl . client_id ,
510508 } ;
511509
512510 // Get the token
513- let token : DeviceAccessTokenGrant = NexusRequest :: new (
511+ let token_grant = NexusRequest :: new (
514512 RequestBuilder :: new ( testctx, Method :: POST , "/device/token" )
515513 . allow_non_dropshot_errors ( )
516514 . body_urlencoded ( Some ( & token_params) )
517515 . expect_status ( Some ( StatusCode :: OK ) ) ,
518516 )
519517 . authn_as ( AuthnMode :: PrivilegedUser )
520- . execute ( )
521- . await
522- . expect ( "failed to get token" )
523- . parsed_body ( )
524- . expect ( "failed to deserialize token response" ) ;
518+ . execute_and_parse_unwrap :: < DeviceAccessTokenGrant > ( )
519+ . await ;
525520
526- // Verify the token has the correct expiration (5 seconds from now)
521+ // Verify the token has roughly the correct expiration time. One second
522+ // threshold is sufficient to confirm it's not getting the silo max of 10
523+ // seconds. Locally, I saw diffs as low as 14ms.
527524 let tokens = get_tokens_priv ( testctx) . await ;
528- let our_token = tokens. iter ( ) . find ( |t| t. time_expires . is_some ( ) ) . unwrap ( ) ;
529- let expires_at = our_token. time_expires . unwrap ( ) ;
530- let now = Utc :: now ( ) ;
531-
532- // Should expire approximately 5 seconds from now (allow some tolerance for test timing)
533- let expected_expiry = now + chrono:: Duration :: seconds ( 5 ) ;
534- let time_diff = ( expires_at - expected_expiry) . num_seconds ( ) . abs ( ) ;
535- assert ! ( time_diff <= 2 , "Token expiry should be close to requested TTL" ) ;
525+ let time_expires = tokens[ 0 ] . time_expires . unwrap ( ) ;
526+ let expected_expires = t0 + Duration :: from_secs ( 3 ) ;
527+ let diff_ms = ( time_expires - expected_expires) . num_milliseconds ( ) . abs ( ) ;
528+ assert ! ( diff_ms <= 1000 , "time diff was {diff_ms} ms. should be near zero" ) ;
536529
537530 // Token should work initially
538- project_list ( & testctx, & token . access_token , StatusCode :: OK )
531+ project_list ( & testctx, & token_grant . access_token , StatusCode :: OK )
539532 . await
540533 . expect ( "token should work initially" ) ;
541534
542535 // Wait for token to expire
543- sleep ( Duration :: from_secs ( 6 ) ) . await ;
536+ sleep ( Duration :: from_secs ( 4 ) ) . await ;
544537
545- // Token should be expired now
546- project_list ( & testctx, & token . access_token , StatusCode :: UNAUTHORIZED )
538+ // Token is expired
539+ project_list ( & testctx, & token_grant . access_token , StatusCode :: UNAUTHORIZED )
547540 . await
548541 . expect ( "token should be expired" ) ;
549542}
0 commit comments