Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions modules/openapi-generator/src/main/resources/go/client.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import (
"golang.org/x/oauth2"
{{/hasOAuthMethods}}
{{#withAWSV4Signature}}
awsv4 "github.com/aws/aws-sdk-go/aws/signer/v4"
awscredentials "github.com/aws/aws-sdk-go/aws/credentials"
awsv4 "github.com/aws/aws-sdk-go-v2/aws/signer/v4"
"github.com/aws/aws-sdk-go-v2/credentials"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you rename this import to awscredentials again?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed the credentials import to use the awscredentials alias as requested in commit 3c8d5ee4.

{{/withAWSV4Signature}}
)

Expand Down Expand Up @@ -458,15 +458,9 @@ func (c *APIClient) prepareRequest(
{{#withAWSV4Signature}}
// AWS Signature v4 Authentication
if auth, ok := ctx.Value(ContextAWSv4).(AWSv4); ok {
creds := awscredentials.NewStaticCredentials(auth.AccessKey, auth.SecretKey, auth.SessionToken)
signer := awsv4.NewSigner(creds)
var reader *strings.Reader
if body == nil {
reader = strings.NewReader("")
} else {
reader = strings.NewReader(body.String())
}

credsProvider := credentials.NewStaticCredentialsProvider(auth.AccessKey, auth.SecretKey, auth.SessionToken)
signer := awsv4.NewSigner()

// Define default values for region and service to maintain backward compatibility
region := auth.Region
if region == "" {
Expand All @@ -477,8 +471,21 @@ func (c *APIClient) prepareRequest(
service = "oapi"
}

timestamp := time.Now()
_, err := signer.Sign(localVarRequest, reader, service, region, timestamp)
// Compute payload hash from the request body
var payloadHash string
if body == nil {
// Empty body
hash := sha256.Sum256([]byte(""))
payloadHash = hex.EncodeToString(hash[:])
} else {
// Hash the actual body content
bodyBytes := []byte(body.String())
hash := sha256.Sum256(bodyBytes)
payloadHash = hex.EncodeToString(hash[:])
}

// Sign the request with the computed payload hash
err = signer.SignHTTP(ctx, creds, localVarRequest, payloadHash, service, region, time.Now())
if err != nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
golang.org/x/oauth2 v0.27.0
{{/hasOAuthMethods}}
{{#withAWSV4Signature}}
github.com/aws/aws-sdk-go v1.34.14
github.com/aws/aws-sdk-go-v2 v1.30.3
{{/withAWSV4Signature}}
{{#importValidator}}
gopkg.in/validator.v2 v2.0.1
Expand Down