add aws v4 signature support to go-experimental (#7326)

This commit is contained in:
William Cheng 2020-09-02 15:28:22 +08:00 committed by GitHub
parent d50d31cd5e
commit 966b87e66c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 46 additions and 4 deletions

View File

@ -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 {

View File

@ -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

View File

@ -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}}
) )

View File

@ -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}}
) )

View File

@ -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 {

View File

@ -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
) )

View File

@ -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
) )

View File

@ -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 {

View File

@ -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 {

View File

@ -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
) )