forked from loafle/openapi-generator-original
add aws v4 signature support to go-experimental (#7326)
This commit is contained in:
parent
d50d31cd5e
commit
966b87e66c
@ -24,6 +24,10 @@ import (
|
|||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
|
|
||||||
"golang.org/x/oauth2"
|
"golang.org/x/oauth2"
|
||||||
|
{{#withAWSV4Signature}}
|
||||||
|
awsv4 "github.com/aws/aws-sdk-go/aws/signer/v4"
|
||||||
|
awscredentials "github.com/aws/aws-sdk-go/aws/credentials"
|
||||||
|
{{/withAWSV4Signature}}
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -351,6 +355,25 @@ func (c *APIClient) prepareRequest(
|
|||||||
if auth, ok := ctx.Value(ContextAccessToken).(string); ok {
|
if auth, ok := ctx.Value(ContextAccessToken).(string); ok {
|
||||||
localVarRequest.Header.Add("Authorization", "Bearer "+auth)
|
localVarRequest.Header.Add("Authorization", "Bearer "+auth)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{{#withAWSV4Signature}}
|
||||||
|
// AWS Signature v4 Authentication
|
||||||
|
if auth, ok := ctx.Value(ContextAWSv4).(AWSv4); ok {
|
||||||
|
creds := awscredentials.NewStaticCredentials(auth.AccessKey, auth.SecretKey, "")
|
||||||
|
signer := awsv4.NewSigner(creds)
|
||||||
|
var reader *strings.Reader
|
||||||
|
if body == nil {
|
||||||
|
reader = strings.NewReader("")
|
||||||
|
} else {
|
||||||
|
reader = strings.NewReader(body.String())
|
||||||
|
}
|
||||||
|
timestamp := time.Now()
|
||||||
|
_, err := signer.Sign(localVarRequest, reader, "oapi", "eu-west-2", timestamp)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{{/withAWSV4Signature}}
|
||||||
}
|
}
|
||||||
|
|
||||||
for header, value := range c.cfg.DefaultHeader {
|
for header, value := range c.cfg.DefaultHeader {
|
||||||
|
@ -31,6 +31,11 @@ var (
|
|||||||
// ContextAPIKeys takes a string apikey as authentication for the request
|
// ContextAPIKeys takes a string apikey as authentication for the request
|
||||||
ContextAPIKeys = contextKey("apiKeys")
|
ContextAPIKeys = contextKey("apiKeys")
|
||||||
|
|
||||||
|
{{#withAWSV4Signature}}
|
||||||
|
// ContextAWSv4 takes an Access Key and a Secret Key for signing AWS Signature v4
|
||||||
|
ContextAWSv4 = contextKey("awsv4")
|
||||||
|
|
||||||
|
{{/withAWSV4Signature}}
|
||||||
// ContextHttpSignatureAuth takes HttpSignatureAuth as authentication for the request.
|
// ContextHttpSignatureAuth takes HttpSignatureAuth as authentication for the request.
|
||||||
ContextHttpSignatureAuth = contextKey("httpsignature")
|
ContextHttpSignatureAuth = contextKey("httpsignature")
|
||||||
|
|
||||||
@ -59,6 +64,15 @@ type APIKey struct {
|
|||||||
Prefix string
|
Prefix string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{{#withAWSV4Signature}}
|
||||||
|
// AWSv4 provides AWS Signature to a request passed via context using ContextAWSv4
|
||||||
|
// https://docs.aws.amazon.com/general/latest/gr/signature-version-4.html
|
||||||
|
type AWSv4 struct {
|
||||||
|
AccessKey string
|
||||||
|
SecretKey string
|
||||||
|
}
|
||||||
|
|
||||||
|
{{/withAWSV4Signature}}
|
||||||
// ServerVariable stores the information about a server variable
|
// ServerVariable stores the information about a server variable
|
||||||
type ServerVariable struct {
|
type ServerVariable struct {
|
||||||
Description string
|
Description string
|
||||||
|
@ -4,4 +4,7 @@ go 1.13
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
|
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
|
||||||
|
{{#withAWSV4Signature}}
|
||||||
|
github.com/aws/aws-sdk-go v1.34.14
|
||||||
|
{{/withAWSV4Signature}}
|
||||||
)
|
)
|
||||||
|
@ -3,5 +3,7 @@ module {{gitHost}}/{{gitUserId}}/{{gitRepoId}}{{#isGoSubmodule}}/{{packageName}}
|
|||||||
require (
|
require (
|
||||||
github.com/antihax/optional v1.0.0
|
github.com/antihax/optional v1.0.0
|
||||||
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
|
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
|
||||||
{{#withAWSV4Signature}}github.com/aws/aws-sdk-go v1.26.3{{/withAWSV4Signature}}
|
{{#withAWSV4Signature}}
|
||||||
|
github.com/aws/aws-sdk-go v1.34.14
|
||||||
|
{{/withAWSV4Signature}}
|
||||||
)
|
)
|
||||||
|
@ -357,6 +357,7 @@ func (c *APIClient) prepareRequest(
|
|||||||
if auth, ok := ctx.Value(ContextAccessToken).(string); ok {
|
if auth, ok := ctx.Value(ContextAccessToken).(string); ok {
|
||||||
localVarRequest.Header.Add("Authorization", "Bearer "+auth)
|
localVarRequest.Header.Add("Authorization", "Bearer "+auth)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for header, value := range c.cfg.DefaultHeader {
|
for header, value := range c.cfg.DefaultHeader {
|
||||||
|
@ -3,5 +3,4 @@ module github.com/GIT_USER_ID/GIT_REPO_ID
|
|||||||
require (
|
require (
|
||||||
github.com/antihax/optional v1.0.0
|
github.com/antihax/optional v1.0.0
|
||||||
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
|
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
|
||||||
|
|
||||||
)
|
)
|
||||||
|
@ -3,5 +3,4 @@ module github.com/GIT_USER_ID/GIT_REPO_ID
|
|||||||
require (
|
require (
|
||||||
github.com/antihax/optional v1.0.0
|
github.com/antihax/optional v1.0.0
|
||||||
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
|
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
|
||||||
|
|
||||||
)
|
)
|
||||||
|
@ -342,6 +342,7 @@ func (c *APIClient) prepareRequest(
|
|||||||
if auth, ok := ctx.Value(ContextAccessToken).(string); ok {
|
if auth, ok := ctx.Value(ContextAccessToken).(string); ok {
|
||||||
localVarRequest.Header.Add("Authorization", "Bearer "+auth)
|
localVarRequest.Header.Add("Authorization", "Bearer "+auth)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for header, value := range c.cfg.DefaultHeader {
|
for header, value := range c.cfg.DefaultHeader {
|
||||||
|
@ -360,6 +360,7 @@ func (c *APIClient) prepareRequest(
|
|||||||
if auth, ok := ctx.Value(ContextAccessToken).(string); ok {
|
if auth, ok := ctx.Value(ContextAccessToken).(string); ok {
|
||||||
localVarRequest.Header.Add("Authorization", "Bearer "+auth)
|
localVarRequest.Header.Add("Authorization", "Bearer "+auth)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for header, value := range c.cfg.DefaultHeader {
|
for header, value := range c.cfg.DefaultHeader {
|
||||||
|
@ -3,5 +3,4 @@ module github.com/GIT_USER_ID/GIT_REPO_ID
|
|||||||
require (
|
require (
|
||||||
github.com/antihax/optional v1.0.0
|
github.com/antihax/optional v1.0.0
|
||||||
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
|
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
|
||||||
|
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user