Skip to content

Commit 966b87e

Browse files
authored
add aws v4 signature support to go-experimental (#7326)
1 parent d50d31c commit 966b87e

File tree

10 files changed

+46
-4
lines changed

10 files changed

+46
-4
lines changed

modules/openapi-generator/src/main/resources/go-experimental/client.mustache

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ import (
2424
"unicode/utf8"
2525

2626
"golang.org/x/oauth2"
27+
{{#withAWSV4Signature}}
28+
awsv4 "github.com/aws/aws-sdk-go/aws/signer/v4"
29+
awscredentials "github.com/aws/aws-sdk-go/aws/credentials"
30+
{{/withAWSV4Signature}}
2731
)
2832

2933
var (
@@ -351,6 +355,25 @@ func (c *APIClient) prepareRequest(
351355
if auth, ok := ctx.Value(ContextAccessToken).(string); ok {
352356
localVarRequest.Header.Add("Authorization", "Bearer "+auth)
353357
}
358+
359+
{{#withAWSV4Signature}}
360+
// AWS Signature v4 Authentication
361+
if auth, ok := ctx.Value(ContextAWSv4).(AWSv4); ok {
362+
creds := awscredentials.NewStaticCredentials(auth.AccessKey, auth.SecretKey, "")
363+
signer := awsv4.NewSigner(creds)
364+
var reader *strings.Reader
365+
if body == nil {
366+
reader = strings.NewReader("")
367+
} else {
368+
reader = strings.NewReader(body.String())
369+
}
370+
timestamp := time.Now()
371+
_, err := signer.Sign(localVarRequest, reader, "oapi", "eu-west-2", timestamp)
372+
if err != nil {
373+
return nil, err
374+
}
375+
}
376+
{{/withAWSV4Signature}}
354377
}
355378

356379
for header, value := range c.cfg.DefaultHeader {

modules/openapi-generator/src/main/resources/go-experimental/configuration.mustache

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ var (
3131
// ContextAPIKeys takes a string apikey as authentication for the request
3232
ContextAPIKeys = contextKey("apiKeys")
3333

34+
{{#withAWSV4Signature}}
35+
// ContextAWSv4 takes an Access Key and a Secret Key for signing AWS Signature v4
36+
ContextAWSv4 = contextKey("awsv4")
37+
38+
{{/withAWSV4Signature}}
3439
// ContextHttpSignatureAuth takes HttpSignatureAuth as authentication for the request.
3540
ContextHttpSignatureAuth = contextKey("httpsignature")
3641

@@ -59,6 +64,15 @@ type APIKey struct {
5964
Prefix string
6065
}
6166

67+
{{#withAWSV4Signature}}
68+
// AWSv4 provides AWS Signature to a request passed via context using ContextAWSv4
69+
// https://docs.aws.amazon.com/general/latest/gr/signature-version-4.html
70+
type AWSv4 struct {
71+
AccessKey string
72+
SecretKey string
73+
}
74+
75+
{{/withAWSV4Signature}}
6276
// ServerVariable stores the information about a server variable
6377
type ServerVariable struct {
6478
Description string

modules/openapi-generator/src/main/resources/go-experimental/go.mod.mustache

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@ go 1.13
44

55
require (
66
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
7+
{{#withAWSV4Signature}}
8+
github.com/aws/aws-sdk-go v1.34.14
9+
{{/withAWSV4Signature}}
710
)

modules/openapi-generator/src/main/resources/go/go.mod.mustache

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@ module {{gitHost}}/{{gitUserId}}/{{gitRepoId}}{{#isGoSubmodule}}/{{packageName}}
33
require (
44
github.com/antihax/optional v1.0.0
55
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
6-
{{#withAWSV4Signature}}github.com/aws/aws-sdk-go v1.26.3{{/withAWSV4Signature}}
6+
{{#withAWSV4Signature}}
7+
github.com/aws/aws-sdk-go v1.34.14
8+
{{/withAWSV4Signature}}
79
)

samples/client/petstore/go-experimental/go-petstore/client.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ func (c *APIClient) prepareRequest(
357357
if auth, ok := ctx.Value(ContextAccessToken).(string); ok {
358358
localVarRequest.Header.Add("Authorization", "Bearer "+auth)
359359
}
360+
360361
}
361362

362363
for header, value := range c.cfg.DefaultHeader {

samples/client/petstore/go/go-petstore-withXml/go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@ module github.com/GIT_USER_ID/GIT_REPO_ID
33
require (
44
github.com/antihax/optional v1.0.0
55
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
6-
76
)

samples/client/petstore/go/go-petstore/go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@ module github.com/GIT_USER_ID/GIT_REPO_ID
33
require (
44
github.com/antihax/optional v1.0.0
55
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
6-
76
)

samples/openapi3/client/extensions/x-auth-id-alias/go-experimental/client.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ func (c *APIClient) prepareRequest(
342342
if auth, ok := ctx.Value(ContextAccessToken).(string); ok {
343343
localVarRequest.Header.Add("Authorization", "Bearer "+auth)
344344
}
345+
345346
}
346347

347348
for header, value := range c.cfg.DefaultHeader {

samples/openapi3/client/petstore/go-experimental/go-petstore/client.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ func (c *APIClient) prepareRequest(
360360
if auth, ok := ctx.Value(ContextAccessToken).(string); ok {
361361
localVarRequest.Header.Add("Authorization", "Bearer "+auth)
362362
}
363+
363364
}
364365

365366
for header, value := range c.cfg.DefaultHeader {

samples/openapi3/client/petstore/go/go-petstore/go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@ module github.com/GIT_USER_ID/GIT_REPO_ID
33
require (
44
github.com/antihax/optional v1.0.0
55
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
6-
76
)

0 commit comments

Comments
 (0)