@@ -27,8 +27,10 @@ import (
2727 "golang.org/x/oauth2"
2828 { {/hasOAuthMethods} }
2929 { {#withAWSV4Signature} }
30- awsv4 "github.com/aws/aws-sdk-go/aws/signer/v4"
31- awscredentials "github.com/aws/aws-sdk-go/aws/credentials"
30+ awsv4 "github.com/aws/aws-sdk-go-v2/aws/signer/v4"
31+ awscredentials "github.com/aws/aws-sdk-go-v2/credentials"
32+ "crypto/sha256"
33+ "encoding/hex"
3234 { {/withAWSV4Signature} }
3335)
3436
@@ -458,13 +460,10 @@ func (c *APIClient) prepareRequest(
458460 { { #withAWSV4Signature} }
459461 // AWS Signature v4 Authentication
460462 if auth, ok := ctx.Value(ContextAWSv4).(AWSv4); ok {
461- creds := awscredentials.NewStaticCredentials(auth.AccessKey, auth.SecretKey, auth.SessionToken)
462- signer := awsv4.NewSigner(creds)
463- var reader *strings.Reader
464- if body == nil {
465- reader = strings.NewReader(" " )
466- } else {
467- reader = strings.NewReader(body.String())
463+ credsProvider := awscredentials.NewStaticCredentialsProvider(auth.AccessKey, auth.SecretKey, auth.SessionToken)
464+ creds, err := credsProvider.Retrieve(ctx)
465+ if err != nil {
466+ return nil, err
468467 }
469468
470469 // Define default values for region and service to maintain backward compatibility
@@ -477,8 +476,22 @@ func (c *APIClient) prepareRequest(
477476 service = " oapi"
478477 }
479478
480- timestamp := time.Now()
481- _, err := signer.Sign(localVarRequest, reader, service, region, timestamp)
479+ // Compute payload hash from the request body
480+ var payloadHash string
481+ if body == nil {
482+ // Empty body
483+ hash := sha256.Sum256([]byte(" " ))
484+ payloadHash = hex.EncodeToString(hash[:])
485+ } else {
486+ // Hash the actual body content
487+ bodyBytes := []byte(body.String())
488+ hash := sha256.Sum256(bodyBytes)
489+ payloadHash = hex.EncodeToString(hash[:])
490+ }
491+
492+ // Sign the request with the computed payload hash
493+ signer := awsv4.NewSigner()
494+ err = signer.SignHTTP(ctx, creds, localVarRequest, payloadHash, service, region, time.Now())
482495 if err != nil {
483496 return nil, err
484497 }
0 commit comments