@@ -235,7 +235,7 @@ def sign_request(request)
235235
236236 return crt_sign_request ( request ) if Signer . use_crt?
237237
238- creds = fetch_credentials
238+ creds , _ = fetch_credentials
239239
240240 http_method = extract_http_method ( request )
241241 url = extract_url ( request )
@@ -314,7 +314,7 @@ def sign_request(request)
314314 # hex-encoded string using #unpack
315315 def sign_event ( prior_signature , payload , encoder )
316316 # Note: CRT does not currently provide event stream signing, so we always use the ruby implementation.
317- creds = fetch_credentials
317+ creds , _ = fetch_credentials
318318 time = Time . now
319319 headers = { }
320320
@@ -403,7 +403,7 @@ def presign_url(options)
403403
404404 return crt_presign_url ( options ) if Signer . use_crt?
405405
406- creds = fetch_credentials
406+ creds , expiration = fetch_credentials
407407
408408 http_method = extract_http_method ( options )
409409 url = extract_url ( options )
@@ -423,7 +423,7 @@ def presign_url(options)
423423 params [ 'X-Amz-Algorithm' ] = 'AWS4-HMAC-SHA256'
424424 params [ 'X-Amz-Credential' ] = credential ( creds , date )
425425 params [ 'X-Amz-Date' ] = datetime
426- params [ 'X-Amz-Expires' ] = extract_expires_in ( options )
426+ params [ 'X-Amz-Expires' ] = presigned_url_expiration ( options , expiration ) . to_s
427427 params [ 'X-Amz-Security-Token' ] = creds . session_token if creds . session_token
428428 params [ 'X-Amz-SignedHeaders' ] = signed_headers ( headers )
429429
@@ -526,7 +526,6 @@ def event_signature(secret_access_key, date, string_to_sign)
526526 hmac ( k_credentials , string_to_sign )
527527 end
528528
529-
530529 def path ( url )
531530 path = url . path
532531 path = '/' if path == ''
@@ -682,8 +681,8 @@ def downcase_headers(headers)
682681
683682 def extract_expires_in ( options )
684683 case options [ :expires_in ]
685- when nil then 900 . to_s
686- when Integer then options [ :expires_in ] . to_s
684+ when nil then 900
685+ when Integer then options [ :expires_in ]
687686 else
688687 msg = "expected :expires_in to be a number of seconds"
689688 raise ArgumentError , msg
@@ -698,11 +697,14 @@ def uri_escape_path(string)
698697 self . class . uri_escape_path ( string )
699698 end
700699
701-
702700 def fetch_credentials
703701 credentials = @credentials_provider . credentials
704702 if credentials_set? ( credentials )
705- credentials
703+ expiration = nil
704+ if @credentials_provider . respond_to? ( :expiration )
705+ expiration = @credentials_provider . expiration
706+ end
707+ [ credentials , expiration ]
706708 else
707709 raise Errors ::MissingCredentialsError ,
708710 'unable to sign request without credentials set'
@@ -720,21 +722,30 @@ def credentials_set?(credentials)
720722 !credentials . secret_access_key . empty?
721723 end
722724
725+ def presigned_url_expiration ( options , expiration )
726+ expires_in = extract_expires_in ( options )
727+ return expires_in unless expiration
728+
729+ expiration_seconds = ( expiration - Time . now ) . to_i
730+ [ expires_in , expiration_seconds ] . min
731+ end
732+
723733 ### CRT Code
724734
725735 # the credentials used by CRT must be a
726736 # CRT StaticCredentialsProvider object
727737 def crt_fetch_credentials
728- creds = fetch_credentials
729- Aws ::Crt ::Auth ::StaticCredentialsProvider . new (
738+ creds , expiration = fetch_credentials
739+ crt_creds = Aws ::Crt ::Auth ::StaticCredentialsProvider . new (
730740 creds . access_key_id ,
731741 creds . secret_access_key ,
732742 creds . session_token
733743 )
744+ [ crt_creds , expiration ]
734745 end
735746
736747 def crt_sign_request ( request )
737- creds = crt_fetch_credentials
748+ creds , _ = crt_fetch_credentials
738749 http_method = extract_http_method ( request )
739750 url = extract_url ( request )
740751 headers = downcase_headers ( request [ :headers ] )
@@ -793,7 +804,7 @@ def crt_sign_request(request)
793804 end
794805
795806 def crt_presign_url ( options )
796- creds = crt_fetch_credentials
807+ creds , expiration = crt_fetch_credentials
797808
798809 http_method = extract_http_method ( options )
799810 url = extract_url ( options )
@@ -821,7 +832,7 @@ def crt_presign_url(options)
821832 use_double_uri_encode : @uri_escape_path ,
822833 should_normalize_uri_path : @normalize_path ,
823834 omit_session_token : @omit_session_token ,
824- expiration_in_seconds : options . fetch ( :expires_in , 900 )
835+ expiration_in_seconds : presigned_url_expiration ( options , expiration )
825836 )
826837 http_request = Aws ::Crt ::Http ::Message . new (
827838 http_method , url . to_s , headers
0 commit comments