Skip to content

Commit

Permalink
[Go][Client] Secret key content string in http signing support (#8570)
Browse files Browse the repository at this point in the history
* accept private key content string

* sample update

* Add comments to new methods

* update samples with comments

* Update modules/openapi-generator/src/main/resources/go/signing.mustache

Co-authored-by: Jiri Kuncar <jiri.kuncar@gmail.com>

* Update modules/openapi-generator/src/main/resources/go/signing.mustache

Co-authored-by: Jiri Kuncar <jiri.kuncar@gmail.com>

* Update signing.mustache

* update sample comments

* Update modules/openapi-generator/src/main/resources/go/signing.mustache

Co-authored-by: Sebastien Rosset <serosset@cisco.com>

* Update modules/openapi-generator/src/main/resources/go/signing.mustache

Co-authored-by: Sebastien Rosset <serosset@cisco.com>

* update empty checks for privateKey

Co-authored-by: Vikrant Balyan <vvb@users.noreply.github.com>
Co-authored-by: Jiri Kuncar <jiri.kuncar@gmail.com>
Co-authored-by: Sebastien Rosset <serosset@cisco.com>
  • Loading branch information
4 people committed Feb 4, 2021
1 parent d7bdd7f commit d869544
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
17 changes: 16 additions & 1 deletion modules/openapi-generator/src/main/resources/go/signing.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,19 @@ type HttpSignatureAuth struct {
privateKey crypto.PrivateKey // The private key used to sign HTTP requests.
}

// SetPrivateKey accepts a private key string and sets it.
func (h *HttpSignatureAuth) SetPrivateKey(privateKey string) error {
return h.parsePrivateKey([]byte(privateKey))
}

// ContextWithValue validates the HttpSignatureAuth configuration parameters and returns a context
// suitable for HTTP signature. An error is returned if the HttpSignatureAuth configuration parameters
// are invalid.
func (h *HttpSignatureAuth) ContextWithValue(ctx context.Context) (context.Context, error) {
if h.KeyId == "" {
return nil, fmt.Errorf("Key ID must be specified")
}
if h.PrivateKeyPath == "" {
if h.PrivateKeyPath == "" && h.privateKey == nil {
return nil, fmt.Errorf("Private key path must be specified")
}
if _, ok := supportedSigningSchemes[h.SigningScheme]; !ok {
Expand Down Expand Up @@ -168,7 +173,11 @@ func (h *HttpSignatureAuth) GetPublicKey() (crypto.PublicKey, error) {
}

// loadPrivateKey reads the private key from the file specified in the HttpSignatureAuth.
// The key is loaded only when privateKey is not already set.
func (h *HttpSignatureAuth) loadPrivateKey() (err error) {
if h.privateKey != nil {
return nil
}
var file *os.File
file, err = os.Open(h.PrivateKeyPath)
if err != nil {
Expand All @@ -182,12 +191,18 @@ func (h *HttpSignatureAuth) loadPrivateKey() (err error) {
if err != nil {
return err
}
return h.parsePrivateKey(priv)
}

// parsePrivateKey decodes privateKey byte array to crypto.PrivateKey type.
func (h *HttpSignatureAuth) parsePrivateKey(priv []byte) error {
pemBlock, _ := pem.Decode(priv)
if pemBlock == nil {
// No PEM data has been found.
return fmt.Errorf("File '%s' does not contain PEM data", h.PrivateKeyPath)
}
var privKey []byte
var err error
if x509.IsEncryptedPEMBlock(pemBlock) {
// The PEM data is encrypted.
privKey, err = x509.DecryptPEMBlock(pemBlock, []byte(h.Passphrase))
Expand Down
17 changes: 16 additions & 1 deletion samples/openapi3/client/petstore/go/go-petstore/signing.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d869544

Please sign in to comment.