[go]: Accept APIKey as string, byte array or stream using io.Reader interface (#17432)

* Added support for APIKey as string or bytes stream for golang sdk.

* updated the sample code

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

Co-authored-by: Vikrant Balyan <vvb@users.noreply.github.com>

* updated the sample code

---------

Co-authored-by: Aanisha Mishra <aanisha.mishra05@gmail.com>
Co-authored-by: Vikrant Balyan (vvb) <vvb@cisco.com>
Co-authored-by: Vikrant Balyan <vvb@users.noreply.github.com>
Co-authored-by: Sebastien Rosset <serosset@cisco.com>
This commit is contained in:
Ghufran Zahidi 2023-12-26 15:09:53 +05:30 committed by GitHub
parent c215bc681d
commit c252216700
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 22 deletions

View File

@ -107,6 +107,7 @@ var supportedSigningSchemes = map[string]bool{
type HttpSignatureAuth struct {
KeyId string // A key identifier.
PrivateKeyPath string // The path to the private key.
PrivateKeyReader io.Reader // provide the APIKey using the types which implement io.Reader interface.
Passphrase string // The passphrase to decrypt the private key, if the key is encrypted.
SigningScheme string // The signature scheme, when signing HTTP requests. Supported value is 'hs2019'.
// The signature algorithm, when signing HTTP requests.
@ -134,9 +135,12 @@ func (h *HttpSignatureAuth) ContextWithValue(ctx context.Context) (context.Conte
if h.KeyId == "" {
return nil, fmt.Errorf("key ID must be specified")
}
if h.PrivateKeyPath == "" && h.privateKey == nil {
if (len(h.PrivateKeyPath) == 0 && h.PrivateKeyReader == nil) && h.privateKey == nil {
return nil, fmt.Errorf("private key path must be specified")
}
if len(h.PrivateKeyPath) > 0 && h.PrivateKeyReader != nil{
return nil, fmt.Errorf("Specify only one of PrivateKeyPath or PrivateKeyReader")
}
if _, ok := supportedSigningSchemes[h.SigningScheme]; !ok {
return nil, fmt.Errorf("invalid signing scheme: '%v'", h.SigningScheme)
}
@ -184,17 +188,21 @@ 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 {
return fmt.Errorf("cannot load private key '%s'. Error: %v", h.PrivateKeyPath, err)
}
defer func() {
err = file.Close()
}()
var priv []byte
priv, err = io.ReadAll(file)
if err != nil {
keyReader := h.PrivateKeyReader
if keyReader == nil {
var file *os.File
file, err = os.Open(h.PrivateKeyPath)
if err != nil {
return fmt.Errorf("cannot load private key '%s'. Error: %v", h.PrivateKeyPath, err)
}
keyReader = file
defer func() {
err = file.Close()
}()
}
priv, err = io.ReadAll(keyReader)
if err != nil{
return err
}
return h.parsePrivateKey(priv)

View File

@ -116,6 +116,7 @@ var supportedSigningSchemes = map[string]bool{
type HttpSignatureAuth struct {
KeyId string // A key identifier.
PrivateKeyPath string // The path to the private key.
PrivateKeyReader io.Reader // provide the APIKey using the types which implement io.Reader interface.
Passphrase string // The passphrase to decrypt the private key, if the key is encrypted.
SigningScheme string // The signature scheme, when signing HTTP requests. Supported value is 'hs2019'.
// The signature algorithm, when signing HTTP requests.
@ -143,9 +144,12 @@ func (h *HttpSignatureAuth) ContextWithValue(ctx context.Context) (context.Conte
if h.KeyId == "" {
return nil, fmt.Errorf("key ID must be specified")
}
if h.PrivateKeyPath == "" && h.privateKey == nil {
if (len(h.PrivateKeyPath) == 0 && h.PrivateKeyReader == nil) && h.privateKey == nil {
return nil, fmt.Errorf("private key path must be specified")
}
if len(h.PrivateKeyPath) > 0 && h.PrivateKeyReader != nil{
return nil, fmt.Errorf("Specify only one of PrivateKeyPath or PrivateKeyReader")
}
if _, ok := supportedSigningSchemes[h.SigningScheme]; !ok {
return nil, fmt.Errorf("invalid signing scheme: '%v'", h.SigningScheme)
}
@ -193,17 +197,21 @@ 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 {
return fmt.Errorf("cannot load private key '%s'. Error: %v", h.PrivateKeyPath, err)
}
defer func() {
err = file.Close()
}()
var priv []byte
priv, err = io.ReadAll(file)
if err != nil {
keyReader := h.PrivateKeyReader
if keyReader == nil {
var file *os.File
file, err = os.Open(h.PrivateKeyPath)
if err != nil {
return fmt.Errorf("cannot load private key '%s'. Error: %v", h.PrivateKeyPath, err)
}
keyReader = file
defer func() {
err = file.Close()
}()
}
priv, err = io.ReadAll(keyReader)
if err != nil{
return err
}
return h.parsePrivateKey(priv)