forked from loafle/openapi-generator-original
[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:
parent
c215bc681d
commit
c252216700
@ -107,6 +107,7 @@ var supportedSigningSchemes = map[string]bool{
|
|||||||
type HttpSignatureAuth struct {
|
type HttpSignatureAuth struct {
|
||||||
KeyId string // A key identifier.
|
KeyId string // A key identifier.
|
||||||
PrivateKeyPath string // The path to the private key.
|
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.
|
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'.
|
SigningScheme string // The signature scheme, when signing HTTP requests. Supported value is 'hs2019'.
|
||||||
// The signature algorithm, when signing HTTP requests.
|
// The signature algorithm, when signing HTTP requests.
|
||||||
@ -134,9 +135,12 @@ func (h *HttpSignatureAuth) ContextWithValue(ctx context.Context) (context.Conte
|
|||||||
if h.KeyId == "" {
|
if h.KeyId == "" {
|
||||||
return nil, fmt.Errorf("key ID must be specified")
|
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")
|
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 {
|
if _, ok := supportedSigningSchemes[h.SigningScheme]; !ok {
|
||||||
return nil, fmt.Errorf("invalid signing scheme: '%v'", h.SigningScheme)
|
return nil, fmt.Errorf("invalid signing scheme: '%v'", h.SigningScheme)
|
||||||
}
|
}
|
||||||
@ -184,17 +188,21 @@ func (h *HttpSignatureAuth) loadPrivateKey() (err error) {
|
|||||||
if h.privateKey != nil {
|
if h.privateKey != nil {
|
||||||
return 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
|
var priv []byte
|
||||||
priv, err = io.ReadAll(file)
|
keyReader := h.PrivateKeyReader
|
||||||
if err != nil {
|
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 err
|
||||||
}
|
}
|
||||||
return h.parsePrivateKey(priv)
|
return h.parsePrivateKey(priv)
|
||||||
|
@ -116,6 +116,7 @@ var supportedSigningSchemes = map[string]bool{
|
|||||||
type HttpSignatureAuth struct {
|
type HttpSignatureAuth struct {
|
||||||
KeyId string // A key identifier.
|
KeyId string // A key identifier.
|
||||||
PrivateKeyPath string // The path to the private key.
|
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.
|
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'.
|
SigningScheme string // The signature scheme, when signing HTTP requests. Supported value is 'hs2019'.
|
||||||
// The signature algorithm, when signing HTTP requests.
|
// The signature algorithm, when signing HTTP requests.
|
||||||
@ -143,9 +144,12 @@ func (h *HttpSignatureAuth) ContextWithValue(ctx context.Context) (context.Conte
|
|||||||
if h.KeyId == "" {
|
if h.KeyId == "" {
|
||||||
return nil, fmt.Errorf("key ID must be specified")
|
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")
|
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 {
|
if _, ok := supportedSigningSchemes[h.SigningScheme]; !ok {
|
||||||
return nil, fmt.Errorf("invalid signing scheme: '%v'", h.SigningScheme)
|
return nil, fmt.Errorf("invalid signing scheme: '%v'", h.SigningScheme)
|
||||||
}
|
}
|
||||||
@ -193,17 +197,21 @@ func (h *HttpSignatureAuth) loadPrivateKey() (err error) {
|
|||||||
if h.privateKey != nil {
|
if h.privateKey != nil {
|
||||||
return 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
|
var priv []byte
|
||||||
priv, err = io.ReadAll(file)
|
keyReader := h.PrivateKeyReader
|
||||||
if err != nil {
|
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 err
|
||||||
}
|
}
|
||||||
return h.parsePrivateKey(priv)
|
return h.parsePrivateKey(priv)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user