From d692ea41394af9e073d4fe0c01c7988bbafbb9c1 Mon Sep 17 00:00:00 2001 From: Ghufran Zahidi <18732053+Ghufz@users.noreply.github.com> Date: Mon, 12 Jun 2023 14:22:15 +0530 Subject: [PATCH 1/7] added property in HttpSIgningConfiguration to accept API key in string format. --- .../HttpSigningConfiguration.mustache | 81 +++++++++++-------- 1 file changed, 49 insertions(+), 32 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/csharp-netcore/HttpSigningConfiguration.mustache b/modules/openapi-generator/src/main/resources/csharp-netcore/HttpSigningConfiguration.mustache index 6b773b8d596a..0ef145115c23 100644 --- a/modules/openapi-generator/src/main/resources/csharp-netcore/HttpSigningConfiguration.mustache +++ b/modules/openapi-generator/src/main/resources/csharp-netcore/HttpSigningConfiguration.mustache @@ -40,6 +40,11 @@ namespace {{packageName}}.Client /// public string KeyFilePath { get; set; } + /// + /// Specify the API key in the form of a string, either configure the KeyString property or configure the KeyFilePath property. + /// + public string KeyString { get; set; } + /// /// Gets the key pass phrase for password protected key /// @@ -104,6 +109,17 @@ namespace {{packageName}}.Client //the list of signed headers and a base64-encoded signature. const string HEADER_AUTHORIZATION = "Authorization"; + //Read the api key from the file + if(!string.IsNullOrEmpty(this.KeyFilePath)) + { + this.KeyString = ReadApiKeyFromFile(KeyFilePath); + } + + if(string.IsNullOrEmpty(KeyString)) + { + throw new Exception("No API key has been provided."); + } + //Hash table to store singed headers var HttpSignedRequestHeader = new Dictionary(); var HttpSignatureHeader = new Dictionary(); @@ -242,7 +258,7 @@ namespace {{packageName}}.Client var headerValuesString = string.Join("\n", headerValuesList); var signatureStringHash = GetStringHash(HashAlgorithm.ToString(), headerValuesString); string headerSignatureStr = null; - var keyType = GetKeyType(KeyFilePath); + var keyType = GetKeyType(KeyString); if (keyType == PrivateKeyType.RSA) { @@ -293,7 +309,7 @@ namespace {{packageName}}.Client private string GetRSASignature(byte[] stringToSign) { - RSA rsa = GetRSAProviderFromPemFile(KeyFilePath, KeyPassPhrase); + RSA rsa = GetRSAProviderFromPemFile(KeyString, KeyPassPhrase); if (SigningAlgorithm == "RSASSA-PSS") { var signedbytes = rsa.SignHash(stringToSign, HashAlgorithm, RSASignaturePadding.Pss); @@ -317,16 +333,7 @@ namespace {{packageName}}.Client /// ECDSA signature private string GetECDSASignature(byte[] dataToSign) { - string keyStr = string.Empty; - if (File.Exists(KeyFilePath)) - { - keyStr = File.ReadAllText(KeyFilePath); - } - else - { - keyStr = KeyFilePath; - } - + var keyStr = KeyString; const string ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----"; const string ecKeyFooter = "-----END EC PRIVATE KEY-----"; var ecKeyBase64String = keyStr.Replace(ecKeyHeader, "").Replace(ecKeyFooter, "").Trim(); @@ -419,22 +426,13 @@ namespace {{packageName}}.Client return derBytes.ToArray(); } - private RSACryptoServiceProvider GetRSAProviderFromPemFile(string pemfile, SecureString keyPassPhrase = null) + private RSACryptoServiceProvider GetRSAProviderFromPemFile(string keyString, SecureString keyPassPhrase = null) { const string pempubheader = "-----BEGIN PUBLIC KEY-----"; const string pempubfooter = "-----END PUBLIC KEY-----"; bool isPrivateKeyFile = true; byte[] pemkey = null; - - string pemstr = string.Empty; - if (File.Exists(pemfile)) - { - pemstr = File.ReadAllText(pemfile).Trim(); - } - else - { - pemstr = pemfile; - } + string pemstr = keyString; if (pemstr.StartsWith(pempubheader) && pemstr.EndsWith(pempubfooter)) { @@ -721,20 +719,15 @@ namespace {{packageName}}.Client /// /// Detect the key type from the pem file. /// - /// key file path in pem format + /// api key in string format /// Private Key Type - private PrivateKeyType GetKeyType(string keyFilePath) + private PrivateKeyType GetKeyType(string keyString) { string[] key = null; - if (File.Exists(keyFilePath)) - { - key = File.ReadAllLines(keyFilePath); - } - else + if (string.IsNullOrEmpty(keyString)) { - // The ApiKeyFilePath is passed as string - key = new string[] { keyFilePath }; + throw new Exception("No API key has been provided."); } const string ecPrivateKeyHeader = "BEGIN EC PRIVATE KEY"; @@ -744,6 +737,7 @@ namespace {{packageName}}.Client //var pkcs8Header = "BEGIN PRIVATE KEY"; //var pkcs8Footer = "END PRIVATE KEY"; PrivateKeyType keyType; + key = KeyString.TrimEnd().Split("\n"); if (key[0].Contains(rsaPrivateKeyHeader) && key[key.Length - 1].ToString().Contains(rsaPrivateFooter)) @@ -761,6 +755,29 @@ namespace {{packageName}}.Client } return keyType; } + + /// + /// Read the api key form the api key file path and stored it in KeyString property. + /// + /// api key file path + private string ReadApiKeyFromFile(string apiKeyFilePath) + { + string apiKeyString = null; + if(!string.IsNullOrEmpty(apiKeyFilePath) && !string.IsNullOrEmpty(this.KeyString)) + { + throw new Exception("Configure either the KeyFilePath or configure the KeyString property."); + } + + if(File.Exists(apiKeyFilePath)) + { + apiKeyString = File.ReadAllText(apiKeyFilePath); + } + else + { + throw new Exception("Provided API key file path does not exists."); + } + return apiKeyString; + } #endregion } } From c384fe3b41285d678d1342d08d586977a2f8595e Mon Sep 17 00:00:00 2001 From: William Cheng Date: Mon, 12 Jun 2023 18:34:45 +0800 Subject: [PATCH 2/7] remove trailing space, update samples --- .../HttpSigningConfiguration.mustache | 2 +- .../Client/HttpSigningConfiguration.cs | 81 +++++++++++-------- .../Client/HttpSigningConfiguration.cs | 81 +++++++++++-------- .../Client/HttpSigningConfiguration.cs | 81 +++++++++++-------- .../Client/HttpSigningConfiguration.cs | 81 +++++++++++-------- .../Client/HttpSigningConfiguration.cs | 81 +++++++++++-------- .../Client/HttpSigningConfiguration.cs | 81 +++++++++++-------- .../Client/HttpSigningConfiguration.cs | 81 +++++++++++-------- .../Client/HttpSigningConfiguration.cs | 81 +++++++++++-------- 9 files changed, 393 insertions(+), 257 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/csharp-netcore/HttpSigningConfiguration.mustache b/modules/openapi-generator/src/main/resources/csharp-netcore/HttpSigningConfiguration.mustache index 0ef145115c23..50d7bf35ab53 100644 --- a/modules/openapi-generator/src/main/resources/csharp-netcore/HttpSigningConfiguration.mustache +++ b/modules/openapi-generator/src/main/resources/csharp-netcore/HttpSigningConfiguration.mustache @@ -43,7 +43,7 @@ namespace {{packageName}}.Client /// /// Specify the API key in the form of a string, either configure the KeyString property or configure the KeyFilePath property. /// - public string KeyString { get; set; } + public string KeyString { get; set; } /// /// Gets the key pass phrase for password protected key diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient-ConditionalSerialization/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient-ConditionalSerialization/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 0b3e867d0f42..116c890aee58 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient-ConditionalSerialization/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient-ConditionalSerialization/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -48,6 +48,11 @@ public HttpSigningConfiguration() /// public string KeyFilePath { get; set; } + /// + /// Specify the API key in the form of a string, either configure the KeyString property or configure the KeyFilePath property. + /// + public string KeyString { get; set; } + /// /// Gets the key pass phrase for password protected key /// @@ -112,6 +117,17 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m //the list of signed headers and a base64-encoded signature. const string HEADER_AUTHORIZATION = "Authorization"; + //Read the api key from the file + if(!string.IsNullOrEmpty(this.KeyFilePath)) + { + this.KeyString = ReadApiKeyFromFile(KeyFilePath); + } + + if(string.IsNullOrEmpty(KeyString)) + { + throw new Exception("No API key has been provided."); + } + //Hash table to store singed headers var HttpSignedRequestHeader = new Dictionary(); var HttpSignatureHeader = new Dictionary(); @@ -250,7 +266,7 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m var headerValuesString = string.Join("\n", headerValuesList); var signatureStringHash = GetStringHash(HashAlgorithm.ToString(), headerValuesString); string headerSignatureStr = null; - var keyType = GetKeyType(KeyFilePath); + var keyType = GetKeyType(KeyString); if (keyType == PrivateKeyType.RSA) { @@ -301,7 +317,7 @@ private int GetUnixTime(DateTime date2) private string GetRSASignature(byte[] stringToSign) { - RSA rsa = GetRSAProviderFromPemFile(KeyFilePath, KeyPassPhrase); + RSA rsa = GetRSAProviderFromPemFile(KeyString, KeyPassPhrase); if (SigningAlgorithm == "RSASSA-PSS") { var signedbytes = rsa.SignHash(stringToSign, HashAlgorithm, RSASignaturePadding.Pss); @@ -325,16 +341,7 @@ private string GetRSASignature(byte[] stringToSign) /// ECDSA signature private string GetECDSASignature(byte[] dataToSign) { - string keyStr = string.Empty; - if (File.Exists(KeyFilePath)) - { - keyStr = File.ReadAllText(KeyFilePath); - } - else - { - keyStr = KeyFilePath; - } - + var keyStr = KeyString; const string ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----"; const string ecKeyFooter = "-----END EC PRIVATE KEY-----"; var ecKeyBase64String = keyStr.Replace(ecKeyHeader, "").Replace(ecKeyFooter, "").Trim(); @@ -427,22 +434,13 @@ private byte[] ConvertToECDSAANS1Format(byte[] signedBytes) return derBytes.ToArray(); } - private RSACryptoServiceProvider GetRSAProviderFromPemFile(string pemfile, SecureString keyPassPhrase = null) + private RSACryptoServiceProvider GetRSAProviderFromPemFile(string keyString, SecureString keyPassPhrase = null) { const string pempubheader = "-----BEGIN PUBLIC KEY-----"; const string pempubfooter = "-----END PUBLIC KEY-----"; bool isPrivateKeyFile = true; byte[] pemkey = null; - - string pemstr = string.Empty; - if (File.Exists(pemfile)) - { - pemstr = File.ReadAllText(pemfile).Trim(); - } - else - { - pemstr = pemfile; - } + string pemstr = keyString; if (pemstr.StartsWith(pempubheader) && pemstr.EndsWith(pempubfooter)) { @@ -729,20 +727,15 @@ private byte[] DecryptKey(byte[] cipherData, byte[] desKey, byte[] IV) /// /// Detect the key type from the pem file. /// - /// key file path in pem format + /// api key in string format /// Private Key Type - private PrivateKeyType GetKeyType(string keyFilePath) + private PrivateKeyType GetKeyType(string keyString) { string[] key = null; - if (File.Exists(keyFilePath)) - { - key = File.ReadAllLines(keyFilePath); - } - else + if (string.IsNullOrEmpty(keyString)) { - // The ApiKeyFilePath is passed as string - key = new string[] { keyFilePath }; + throw new Exception("No API key has been provided."); } const string ecPrivateKeyHeader = "BEGIN EC PRIVATE KEY"; @@ -752,6 +745,7 @@ private PrivateKeyType GetKeyType(string keyFilePath) //var pkcs8Header = "BEGIN PRIVATE KEY"; //var pkcs8Footer = "END PRIVATE KEY"; PrivateKeyType keyType; + key = KeyString.TrimEnd().Split("\n"); if (key[0].Contains(rsaPrivateKeyHeader) && key[key.Length - 1].ToString().Contains(rsaPrivateFooter)) @@ -769,6 +763,29 @@ private PrivateKeyType GetKeyType(string keyFilePath) } return keyType; } + + /// + /// Read the api key form the api key file path and stored it in KeyString property. + /// + /// api key file path + private string ReadApiKeyFromFile(string apiKeyFilePath) + { + string apiKeyString = null; + if(!string.IsNullOrEmpty(apiKeyFilePath) && !string.IsNullOrEmpty(this.KeyString)) + { + throw new Exception("Configure either the KeyFilePath or configure the KeyString property."); + } + + if(File.Exists(apiKeyFilePath)) + { + apiKeyString = File.ReadAllText(apiKeyFilePath); + } + else + { + throw new Exception("Provided API key file path does not exists."); + } + return apiKeyString; + } #endregion } } diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 0b3e867d0f42..116c890aee58 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -48,6 +48,11 @@ public HttpSigningConfiguration() /// public string KeyFilePath { get; set; } + /// + /// Specify the API key in the form of a string, either configure the KeyString property or configure the KeyFilePath property. + /// + public string KeyString { get; set; } + /// /// Gets the key pass phrase for password protected key /// @@ -112,6 +117,17 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m //the list of signed headers and a base64-encoded signature. const string HEADER_AUTHORIZATION = "Authorization"; + //Read the api key from the file + if(!string.IsNullOrEmpty(this.KeyFilePath)) + { + this.KeyString = ReadApiKeyFromFile(KeyFilePath); + } + + if(string.IsNullOrEmpty(KeyString)) + { + throw new Exception("No API key has been provided."); + } + //Hash table to store singed headers var HttpSignedRequestHeader = new Dictionary(); var HttpSignatureHeader = new Dictionary(); @@ -250,7 +266,7 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m var headerValuesString = string.Join("\n", headerValuesList); var signatureStringHash = GetStringHash(HashAlgorithm.ToString(), headerValuesString); string headerSignatureStr = null; - var keyType = GetKeyType(KeyFilePath); + var keyType = GetKeyType(KeyString); if (keyType == PrivateKeyType.RSA) { @@ -301,7 +317,7 @@ private int GetUnixTime(DateTime date2) private string GetRSASignature(byte[] stringToSign) { - RSA rsa = GetRSAProviderFromPemFile(KeyFilePath, KeyPassPhrase); + RSA rsa = GetRSAProviderFromPemFile(KeyString, KeyPassPhrase); if (SigningAlgorithm == "RSASSA-PSS") { var signedbytes = rsa.SignHash(stringToSign, HashAlgorithm, RSASignaturePadding.Pss); @@ -325,16 +341,7 @@ private string GetRSASignature(byte[] stringToSign) /// ECDSA signature private string GetECDSASignature(byte[] dataToSign) { - string keyStr = string.Empty; - if (File.Exists(KeyFilePath)) - { - keyStr = File.ReadAllText(KeyFilePath); - } - else - { - keyStr = KeyFilePath; - } - + var keyStr = KeyString; const string ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----"; const string ecKeyFooter = "-----END EC PRIVATE KEY-----"; var ecKeyBase64String = keyStr.Replace(ecKeyHeader, "").Replace(ecKeyFooter, "").Trim(); @@ -427,22 +434,13 @@ private byte[] ConvertToECDSAANS1Format(byte[] signedBytes) return derBytes.ToArray(); } - private RSACryptoServiceProvider GetRSAProviderFromPemFile(string pemfile, SecureString keyPassPhrase = null) + private RSACryptoServiceProvider GetRSAProviderFromPemFile(string keyString, SecureString keyPassPhrase = null) { const string pempubheader = "-----BEGIN PUBLIC KEY-----"; const string pempubfooter = "-----END PUBLIC KEY-----"; bool isPrivateKeyFile = true; byte[] pemkey = null; - - string pemstr = string.Empty; - if (File.Exists(pemfile)) - { - pemstr = File.ReadAllText(pemfile).Trim(); - } - else - { - pemstr = pemfile; - } + string pemstr = keyString; if (pemstr.StartsWith(pempubheader) && pemstr.EndsWith(pempubfooter)) { @@ -729,20 +727,15 @@ private byte[] DecryptKey(byte[] cipherData, byte[] desKey, byte[] IV) /// /// Detect the key type from the pem file. /// - /// key file path in pem format + /// api key in string format /// Private Key Type - private PrivateKeyType GetKeyType(string keyFilePath) + private PrivateKeyType GetKeyType(string keyString) { string[] key = null; - if (File.Exists(keyFilePath)) - { - key = File.ReadAllLines(keyFilePath); - } - else + if (string.IsNullOrEmpty(keyString)) { - // The ApiKeyFilePath is passed as string - key = new string[] { keyFilePath }; + throw new Exception("No API key has been provided."); } const string ecPrivateKeyHeader = "BEGIN EC PRIVATE KEY"; @@ -752,6 +745,7 @@ private PrivateKeyType GetKeyType(string keyFilePath) //var pkcs8Header = "BEGIN PRIVATE KEY"; //var pkcs8Footer = "END PRIVATE KEY"; PrivateKeyType keyType; + key = KeyString.TrimEnd().Split("\n"); if (key[0].Contains(rsaPrivateKeyHeader) && key[key.Length - 1].ToString().Contains(rsaPrivateFooter)) @@ -769,6 +763,29 @@ private PrivateKeyType GetKeyType(string keyFilePath) } return keyType; } + + /// + /// Read the api key form the api key file path and stored it in KeyString property. + /// + /// api key file path + private string ReadApiKeyFromFile(string apiKeyFilePath) + { + string apiKeyString = null; + if(!string.IsNullOrEmpty(apiKeyFilePath) && !string.IsNullOrEmpty(this.KeyString)) + { + throw new Exception("Configure either the KeyFilePath or configure the KeyString property."); + } + + if(File.Exists(apiKeyFilePath)) + { + apiKeyString = File.ReadAllText(apiKeyFilePath); + } + else + { + throw new Exception("Provided API key file path does not exists."); + } + return apiKeyString; + } #endregion } } diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient-net47/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient-net47/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 0b3e867d0f42..116c890aee58 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient-net47/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient-net47/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -48,6 +48,11 @@ public HttpSigningConfiguration() /// public string KeyFilePath { get; set; } + /// + /// Specify the API key in the form of a string, either configure the KeyString property or configure the KeyFilePath property. + /// + public string KeyString { get; set; } + /// /// Gets the key pass phrase for password protected key /// @@ -112,6 +117,17 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m //the list of signed headers and a base64-encoded signature. const string HEADER_AUTHORIZATION = "Authorization"; + //Read the api key from the file + if(!string.IsNullOrEmpty(this.KeyFilePath)) + { + this.KeyString = ReadApiKeyFromFile(KeyFilePath); + } + + if(string.IsNullOrEmpty(KeyString)) + { + throw new Exception("No API key has been provided."); + } + //Hash table to store singed headers var HttpSignedRequestHeader = new Dictionary(); var HttpSignatureHeader = new Dictionary(); @@ -250,7 +266,7 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m var headerValuesString = string.Join("\n", headerValuesList); var signatureStringHash = GetStringHash(HashAlgorithm.ToString(), headerValuesString); string headerSignatureStr = null; - var keyType = GetKeyType(KeyFilePath); + var keyType = GetKeyType(KeyString); if (keyType == PrivateKeyType.RSA) { @@ -301,7 +317,7 @@ private int GetUnixTime(DateTime date2) private string GetRSASignature(byte[] stringToSign) { - RSA rsa = GetRSAProviderFromPemFile(KeyFilePath, KeyPassPhrase); + RSA rsa = GetRSAProviderFromPemFile(KeyString, KeyPassPhrase); if (SigningAlgorithm == "RSASSA-PSS") { var signedbytes = rsa.SignHash(stringToSign, HashAlgorithm, RSASignaturePadding.Pss); @@ -325,16 +341,7 @@ private string GetRSASignature(byte[] stringToSign) /// ECDSA signature private string GetECDSASignature(byte[] dataToSign) { - string keyStr = string.Empty; - if (File.Exists(KeyFilePath)) - { - keyStr = File.ReadAllText(KeyFilePath); - } - else - { - keyStr = KeyFilePath; - } - + var keyStr = KeyString; const string ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----"; const string ecKeyFooter = "-----END EC PRIVATE KEY-----"; var ecKeyBase64String = keyStr.Replace(ecKeyHeader, "").Replace(ecKeyFooter, "").Trim(); @@ -427,22 +434,13 @@ private byte[] ConvertToECDSAANS1Format(byte[] signedBytes) return derBytes.ToArray(); } - private RSACryptoServiceProvider GetRSAProviderFromPemFile(string pemfile, SecureString keyPassPhrase = null) + private RSACryptoServiceProvider GetRSAProviderFromPemFile(string keyString, SecureString keyPassPhrase = null) { const string pempubheader = "-----BEGIN PUBLIC KEY-----"; const string pempubfooter = "-----END PUBLIC KEY-----"; bool isPrivateKeyFile = true; byte[] pemkey = null; - - string pemstr = string.Empty; - if (File.Exists(pemfile)) - { - pemstr = File.ReadAllText(pemfile).Trim(); - } - else - { - pemstr = pemfile; - } + string pemstr = keyString; if (pemstr.StartsWith(pempubheader) && pemstr.EndsWith(pempubfooter)) { @@ -729,20 +727,15 @@ private byte[] DecryptKey(byte[] cipherData, byte[] desKey, byte[] IV) /// /// Detect the key type from the pem file. /// - /// key file path in pem format + /// api key in string format /// Private Key Type - private PrivateKeyType GetKeyType(string keyFilePath) + private PrivateKeyType GetKeyType(string keyString) { string[] key = null; - if (File.Exists(keyFilePath)) - { - key = File.ReadAllLines(keyFilePath); - } - else + if (string.IsNullOrEmpty(keyString)) { - // The ApiKeyFilePath is passed as string - key = new string[] { keyFilePath }; + throw new Exception("No API key has been provided."); } const string ecPrivateKeyHeader = "BEGIN EC PRIVATE KEY"; @@ -752,6 +745,7 @@ private PrivateKeyType GetKeyType(string keyFilePath) //var pkcs8Header = "BEGIN PRIVATE KEY"; //var pkcs8Footer = "END PRIVATE KEY"; PrivateKeyType keyType; + key = KeyString.TrimEnd().Split("\n"); if (key[0].Contains(rsaPrivateKeyHeader) && key[key.Length - 1].ToString().Contains(rsaPrivateFooter)) @@ -769,6 +763,29 @@ private PrivateKeyType GetKeyType(string keyFilePath) } return keyType; } + + /// + /// Read the api key form the api key file path and stored it in KeyString property. + /// + /// api key file path + private string ReadApiKeyFromFile(string apiKeyFilePath) + { + string apiKeyString = null; + if(!string.IsNullOrEmpty(apiKeyFilePath) && !string.IsNullOrEmpty(this.KeyString)) + { + throw new Exception("Configure either the KeyFilePath or configure the KeyString property."); + } + + if(File.Exists(apiKeyFilePath)) + { + apiKeyString = File.ReadAllText(apiKeyFilePath); + } + else + { + throw new Exception("Provided API key file path does not exists."); + } + return apiKeyString; + } #endregion } } diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient-net48/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient-net48/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 0b3e867d0f42..116c890aee58 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient-net48/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient-net48/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -48,6 +48,11 @@ public HttpSigningConfiguration() /// public string KeyFilePath { get; set; } + /// + /// Specify the API key in the form of a string, either configure the KeyString property or configure the KeyFilePath property. + /// + public string KeyString { get; set; } + /// /// Gets the key pass phrase for password protected key /// @@ -112,6 +117,17 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m //the list of signed headers and a base64-encoded signature. const string HEADER_AUTHORIZATION = "Authorization"; + //Read the api key from the file + if(!string.IsNullOrEmpty(this.KeyFilePath)) + { + this.KeyString = ReadApiKeyFromFile(KeyFilePath); + } + + if(string.IsNullOrEmpty(KeyString)) + { + throw new Exception("No API key has been provided."); + } + //Hash table to store singed headers var HttpSignedRequestHeader = new Dictionary(); var HttpSignatureHeader = new Dictionary(); @@ -250,7 +266,7 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m var headerValuesString = string.Join("\n", headerValuesList); var signatureStringHash = GetStringHash(HashAlgorithm.ToString(), headerValuesString); string headerSignatureStr = null; - var keyType = GetKeyType(KeyFilePath); + var keyType = GetKeyType(KeyString); if (keyType == PrivateKeyType.RSA) { @@ -301,7 +317,7 @@ private int GetUnixTime(DateTime date2) private string GetRSASignature(byte[] stringToSign) { - RSA rsa = GetRSAProviderFromPemFile(KeyFilePath, KeyPassPhrase); + RSA rsa = GetRSAProviderFromPemFile(KeyString, KeyPassPhrase); if (SigningAlgorithm == "RSASSA-PSS") { var signedbytes = rsa.SignHash(stringToSign, HashAlgorithm, RSASignaturePadding.Pss); @@ -325,16 +341,7 @@ private string GetRSASignature(byte[] stringToSign) /// ECDSA signature private string GetECDSASignature(byte[] dataToSign) { - string keyStr = string.Empty; - if (File.Exists(KeyFilePath)) - { - keyStr = File.ReadAllText(KeyFilePath); - } - else - { - keyStr = KeyFilePath; - } - + var keyStr = KeyString; const string ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----"; const string ecKeyFooter = "-----END EC PRIVATE KEY-----"; var ecKeyBase64String = keyStr.Replace(ecKeyHeader, "").Replace(ecKeyFooter, "").Trim(); @@ -427,22 +434,13 @@ private byte[] ConvertToECDSAANS1Format(byte[] signedBytes) return derBytes.ToArray(); } - private RSACryptoServiceProvider GetRSAProviderFromPemFile(string pemfile, SecureString keyPassPhrase = null) + private RSACryptoServiceProvider GetRSAProviderFromPemFile(string keyString, SecureString keyPassPhrase = null) { const string pempubheader = "-----BEGIN PUBLIC KEY-----"; const string pempubfooter = "-----END PUBLIC KEY-----"; bool isPrivateKeyFile = true; byte[] pemkey = null; - - string pemstr = string.Empty; - if (File.Exists(pemfile)) - { - pemstr = File.ReadAllText(pemfile).Trim(); - } - else - { - pemstr = pemfile; - } + string pemstr = keyString; if (pemstr.StartsWith(pempubheader) && pemstr.EndsWith(pempubfooter)) { @@ -729,20 +727,15 @@ private byte[] DecryptKey(byte[] cipherData, byte[] desKey, byte[] IV) /// /// Detect the key type from the pem file. /// - /// key file path in pem format + /// api key in string format /// Private Key Type - private PrivateKeyType GetKeyType(string keyFilePath) + private PrivateKeyType GetKeyType(string keyString) { string[] key = null; - if (File.Exists(keyFilePath)) - { - key = File.ReadAllLines(keyFilePath); - } - else + if (string.IsNullOrEmpty(keyString)) { - // The ApiKeyFilePath is passed as string - key = new string[] { keyFilePath }; + throw new Exception("No API key has been provided."); } const string ecPrivateKeyHeader = "BEGIN EC PRIVATE KEY"; @@ -752,6 +745,7 @@ private PrivateKeyType GetKeyType(string keyFilePath) //var pkcs8Header = "BEGIN PRIVATE KEY"; //var pkcs8Footer = "END PRIVATE KEY"; PrivateKeyType keyType; + key = KeyString.TrimEnd().Split("\n"); if (key[0].Contains(rsaPrivateKeyHeader) && key[key.Length - 1].ToString().Contains(rsaPrivateFooter)) @@ -769,6 +763,29 @@ private PrivateKeyType GetKeyType(string keyFilePath) } return keyType; } + + /// + /// Read the api key form the api key file path and stored it in KeyString property. + /// + /// api key file path + private string ReadApiKeyFromFile(string apiKeyFilePath) + { + string apiKeyString = null; + if(!string.IsNullOrEmpty(apiKeyFilePath) && !string.IsNullOrEmpty(this.KeyString)) + { + throw new Exception("Configure either the KeyFilePath or configure the KeyString property."); + } + + if(File.Exists(apiKeyFilePath)) + { + apiKeyString = File.ReadAllText(apiKeyFilePath); + } + else + { + throw new Exception("Provided API key file path does not exists."); + } + return apiKeyString; + } #endregion } } diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient-net5.0/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient-net5.0/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 0b3e867d0f42..116c890aee58 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient-net5.0/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient-net5.0/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -48,6 +48,11 @@ public HttpSigningConfiguration() /// public string KeyFilePath { get; set; } + /// + /// Specify the API key in the form of a string, either configure the KeyString property or configure the KeyFilePath property. + /// + public string KeyString { get; set; } + /// /// Gets the key pass phrase for password protected key /// @@ -112,6 +117,17 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m //the list of signed headers and a base64-encoded signature. const string HEADER_AUTHORIZATION = "Authorization"; + //Read the api key from the file + if(!string.IsNullOrEmpty(this.KeyFilePath)) + { + this.KeyString = ReadApiKeyFromFile(KeyFilePath); + } + + if(string.IsNullOrEmpty(KeyString)) + { + throw new Exception("No API key has been provided."); + } + //Hash table to store singed headers var HttpSignedRequestHeader = new Dictionary(); var HttpSignatureHeader = new Dictionary(); @@ -250,7 +266,7 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m var headerValuesString = string.Join("\n", headerValuesList); var signatureStringHash = GetStringHash(HashAlgorithm.ToString(), headerValuesString); string headerSignatureStr = null; - var keyType = GetKeyType(KeyFilePath); + var keyType = GetKeyType(KeyString); if (keyType == PrivateKeyType.RSA) { @@ -301,7 +317,7 @@ private int GetUnixTime(DateTime date2) private string GetRSASignature(byte[] stringToSign) { - RSA rsa = GetRSAProviderFromPemFile(KeyFilePath, KeyPassPhrase); + RSA rsa = GetRSAProviderFromPemFile(KeyString, KeyPassPhrase); if (SigningAlgorithm == "RSASSA-PSS") { var signedbytes = rsa.SignHash(stringToSign, HashAlgorithm, RSASignaturePadding.Pss); @@ -325,16 +341,7 @@ private string GetRSASignature(byte[] stringToSign) /// ECDSA signature private string GetECDSASignature(byte[] dataToSign) { - string keyStr = string.Empty; - if (File.Exists(KeyFilePath)) - { - keyStr = File.ReadAllText(KeyFilePath); - } - else - { - keyStr = KeyFilePath; - } - + var keyStr = KeyString; const string ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----"; const string ecKeyFooter = "-----END EC PRIVATE KEY-----"; var ecKeyBase64String = keyStr.Replace(ecKeyHeader, "").Replace(ecKeyFooter, "").Trim(); @@ -427,22 +434,13 @@ private byte[] ConvertToECDSAANS1Format(byte[] signedBytes) return derBytes.ToArray(); } - private RSACryptoServiceProvider GetRSAProviderFromPemFile(string pemfile, SecureString keyPassPhrase = null) + private RSACryptoServiceProvider GetRSAProviderFromPemFile(string keyString, SecureString keyPassPhrase = null) { const string pempubheader = "-----BEGIN PUBLIC KEY-----"; const string pempubfooter = "-----END PUBLIC KEY-----"; bool isPrivateKeyFile = true; byte[] pemkey = null; - - string pemstr = string.Empty; - if (File.Exists(pemfile)) - { - pemstr = File.ReadAllText(pemfile).Trim(); - } - else - { - pemstr = pemfile; - } + string pemstr = keyString; if (pemstr.StartsWith(pempubheader) && pemstr.EndsWith(pempubfooter)) { @@ -729,20 +727,15 @@ private byte[] DecryptKey(byte[] cipherData, byte[] desKey, byte[] IV) /// /// Detect the key type from the pem file. /// - /// key file path in pem format + /// api key in string format /// Private Key Type - private PrivateKeyType GetKeyType(string keyFilePath) + private PrivateKeyType GetKeyType(string keyString) { string[] key = null; - if (File.Exists(keyFilePath)) - { - key = File.ReadAllLines(keyFilePath); - } - else + if (string.IsNullOrEmpty(keyString)) { - // The ApiKeyFilePath is passed as string - key = new string[] { keyFilePath }; + throw new Exception("No API key has been provided."); } const string ecPrivateKeyHeader = "BEGIN EC PRIVATE KEY"; @@ -752,6 +745,7 @@ private PrivateKeyType GetKeyType(string keyFilePath) //var pkcs8Header = "BEGIN PRIVATE KEY"; //var pkcs8Footer = "END PRIVATE KEY"; PrivateKeyType keyType; + key = KeyString.TrimEnd().Split("\n"); if (key[0].Contains(rsaPrivateKeyHeader) && key[key.Length - 1].ToString().Contains(rsaPrivateFooter)) @@ -769,6 +763,29 @@ private PrivateKeyType GetKeyType(string keyFilePath) } return keyType; } + + /// + /// Read the api key form the api key file path and stored it in KeyString property. + /// + /// api key file path + private string ReadApiKeyFromFile(string apiKeyFilePath) + { + string apiKeyString = null; + if(!string.IsNullOrEmpty(apiKeyFilePath) && !string.IsNullOrEmpty(this.KeyString)) + { + throw new Exception("Configure either the KeyFilePath or configure the KeyString property."); + } + + if(File.Exists(apiKeyFilePath)) + { + apiKeyString = File.ReadAllText(apiKeyFilePath); + } + else + { + throw new Exception("Provided API key file path does not exists."); + } + return apiKeyString; + } #endregion } } diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient-unityWebRequest/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient-unityWebRequest/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 0b3e867d0f42..116c890aee58 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient-unityWebRequest/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient-unityWebRequest/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -48,6 +48,11 @@ public HttpSigningConfiguration() /// public string KeyFilePath { get; set; } + /// + /// Specify the API key in the form of a string, either configure the KeyString property or configure the KeyFilePath property. + /// + public string KeyString { get; set; } + /// /// Gets the key pass phrase for password protected key /// @@ -112,6 +117,17 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m //the list of signed headers and a base64-encoded signature. const string HEADER_AUTHORIZATION = "Authorization"; + //Read the api key from the file + if(!string.IsNullOrEmpty(this.KeyFilePath)) + { + this.KeyString = ReadApiKeyFromFile(KeyFilePath); + } + + if(string.IsNullOrEmpty(KeyString)) + { + throw new Exception("No API key has been provided."); + } + //Hash table to store singed headers var HttpSignedRequestHeader = new Dictionary(); var HttpSignatureHeader = new Dictionary(); @@ -250,7 +266,7 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m var headerValuesString = string.Join("\n", headerValuesList); var signatureStringHash = GetStringHash(HashAlgorithm.ToString(), headerValuesString); string headerSignatureStr = null; - var keyType = GetKeyType(KeyFilePath); + var keyType = GetKeyType(KeyString); if (keyType == PrivateKeyType.RSA) { @@ -301,7 +317,7 @@ private int GetUnixTime(DateTime date2) private string GetRSASignature(byte[] stringToSign) { - RSA rsa = GetRSAProviderFromPemFile(KeyFilePath, KeyPassPhrase); + RSA rsa = GetRSAProviderFromPemFile(KeyString, KeyPassPhrase); if (SigningAlgorithm == "RSASSA-PSS") { var signedbytes = rsa.SignHash(stringToSign, HashAlgorithm, RSASignaturePadding.Pss); @@ -325,16 +341,7 @@ private string GetRSASignature(byte[] stringToSign) /// ECDSA signature private string GetECDSASignature(byte[] dataToSign) { - string keyStr = string.Empty; - if (File.Exists(KeyFilePath)) - { - keyStr = File.ReadAllText(KeyFilePath); - } - else - { - keyStr = KeyFilePath; - } - + var keyStr = KeyString; const string ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----"; const string ecKeyFooter = "-----END EC PRIVATE KEY-----"; var ecKeyBase64String = keyStr.Replace(ecKeyHeader, "").Replace(ecKeyFooter, "").Trim(); @@ -427,22 +434,13 @@ private byte[] ConvertToECDSAANS1Format(byte[] signedBytes) return derBytes.ToArray(); } - private RSACryptoServiceProvider GetRSAProviderFromPemFile(string pemfile, SecureString keyPassPhrase = null) + private RSACryptoServiceProvider GetRSAProviderFromPemFile(string keyString, SecureString keyPassPhrase = null) { const string pempubheader = "-----BEGIN PUBLIC KEY-----"; const string pempubfooter = "-----END PUBLIC KEY-----"; bool isPrivateKeyFile = true; byte[] pemkey = null; - - string pemstr = string.Empty; - if (File.Exists(pemfile)) - { - pemstr = File.ReadAllText(pemfile).Trim(); - } - else - { - pemstr = pemfile; - } + string pemstr = keyString; if (pemstr.StartsWith(pempubheader) && pemstr.EndsWith(pempubfooter)) { @@ -729,20 +727,15 @@ private byte[] DecryptKey(byte[] cipherData, byte[] desKey, byte[] IV) /// /// Detect the key type from the pem file. /// - /// key file path in pem format + /// api key in string format /// Private Key Type - private PrivateKeyType GetKeyType(string keyFilePath) + private PrivateKeyType GetKeyType(string keyString) { string[] key = null; - if (File.Exists(keyFilePath)) - { - key = File.ReadAllLines(keyFilePath); - } - else + if (string.IsNullOrEmpty(keyString)) { - // The ApiKeyFilePath is passed as string - key = new string[] { keyFilePath }; + throw new Exception("No API key has been provided."); } const string ecPrivateKeyHeader = "BEGIN EC PRIVATE KEY"; @@ -752,6 +745,7 @@ private PrivateKeyType GetKeyType(string keyFilePath) //var pkcs8Header = "BEGIN PRIVATE KEY"; //var pkcs8Footer = "END PRIVATE KEY"; PrivateKeyType keyType; + key = KeyString.TrimEnd().Split("\n"); if (key[0].Contains(rsaPrivateKeyHeader) && key[key.Length - 1].ToString().Contains(rsaPrivateFooter)) @@ -769,6 +763,29 @@ private PrivateKeyType GetKeyType(string keyFilePath) } return keyType; } + + /// + /// Read the api key form the api key file path and stored it in KeyString property. + /// + /// api key file path + private string ReadApiKeyFromFile(string apiKeyFilePath) + { + string apiKeyString = null; + if(!string.IsNullOrEmpty(apiKeyFilePath) && !string.IsNullOrEmpty(this.KeyString)) + { + throw new Exception("Configure either the KeyFilePath or configure the KeyString property."); + } + + if(File.Exists(apiKeyFilePath)) + { + apiKeyString = File.ReadAllText(apiKeyFilePath); + } + else + { + throw new Exception("Provided API key file path does not exists."); + } + return apiKeyString; + } #endregion } } diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 0b3e867d0f42..116c890aee58 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -48,6 +48,11 @@ public HttpSigningConfiguration() /// public string KeyFilePath { get; set; } + /// + /// Specify the API key in the form of a string, either configure the KeyString property or configure the KeyFilePath property. + /// + public string KeyString { get; set; } + /// /// Gets the key pass phrase for password protected key /// @@ -112,6 +117,17 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m //the list of signed headers and a base64-encoded signature. const string HEADER_AUTHORIZATION = "Authorization"; + //Read the api key from the file + if(!string.IsNullOrEmpty(this.KeyFilePath)) + { + this.KeyString = ReadApiKeyFromFile(KeyFilePath); + } + + if(string.IsNullOrEmpty(KeyString)) + { + throw new Exception("No API key has been provided."); + } + //Hash table to store singed headers var HttpSignedRequestHeader = new Dictionary(); var HttpSignatureHeader = new Dictionary(); @@ -250,7 +266,7 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m var headerValuesString = string.Join("\n", headerValuesList); var signatureStringHash = GetStringHash(HashAlgorithm.ToString(), headerValuesString); string headerSignatureStr = null; - var keyType = GetKeyType(KeyFilePath); + var keyType = GetKeyType(KeyString); if (keyType == PrivateKeyType.RSA) { @@ -301,7 +317,7 @@ private int GetUnixTime(DateTime date2) private string GetRSASignature(byte[] stringToSign) { - RSA rsa = GetRSAProviderFromPemFile(KeyFilePath, KeyPassPhrase); + RSA rsa = GetRSAProviderFromPemFile(KeyString, KeyPassPhrase); if (SigningAlgorithm == "RSASSA-PSS") { var signedbytes = rsa.SignHash(stringToSign, HashAlgorithm, RSASignaturePadding.Pss); @@ -325,16 +341,7 @@ private string GetRSASignature(byte[] stringToSign) /// ECDSA signature private string GetECDSASignature(byte[] dataToSign) { - string keyStr = string.Empty; - if (File.Exists(KeyFilePath)) - { - keyStr = File.ReadAllText(KeyFilePath); - } - else - { - keyStr = KeyFilePath; - } - + var keyStr = KeyString; const string ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----"; const string ecKeyFooter = "-----END EC PRIVATE KEY-----"; var ecKeyBase64String = keyStr.Replace(ecKeyHeader, "").Replace(ecKeyFooter, "").Trim(); @@ -427,22 +434,13 @@ private byte[] ConvertToECDSAANS1Format(byte[] signedBytes) return derBytes.ToArray(); } - private RSACryptoServiceProvider GetRSAProviderFromPemFile(string pemfile, SecureString keyPassPhrase = null) + private RSACryptoServiceProvider GetRSAProviderFromPemFile(string keyString, SecureString keyPassPhrase = null) { const string pempubheader = "-----BEGIN PUBLIC KEY-----"; const string pempubfooter = "-----END PUBLIC KEY-----"; bool isPrivateKeyFile = true; byte[] pemkey = null; - - string pemstr = string.Empty; - if (File.Exists(pemfile)) - { - pemstr = File.ReadAllText(pemfile).Trim(); - } - else - { - pemstr = pemfile; - } + string pemstr = keyString; if (pemstr.StartsWith(pempubheader) && pemstr.EndsWith(pempubfooter)) { @@ -729,20 +727,15 @@ private byte[] DecryptKey(byte[] cipherData, byte[] desKey, byte[] IV) /// /// Detect the key type from the pem file. /// - /// key file path in pem format + /// api key in string format /// Private Key Type - private PrivateKeyType GetKeyType(string keyFilePath) + private PrivateKeyType GetKeyType(string keyString) { string[] key = null; - if (File.Exists(keyFilePath)) - { - key = File.ReadAllLines(keyFilePath); - } - else + if (string.IsNullOrEmpty(keyString)) { - // The ApiKeyFilePath is passed as string - key = new string[] { keyFilePath }; + throw new Exception("No API key has been provided."); } const string ecPrivateKeyHeader = "BEGIN EC PRIVATE KEY"; @@ -752,6 +745,7 @@ private PrivateKeyType GetKeyType(string keyFilePath) //var pkcs8Header = "BEGIN PRIVATE KEY"; //var pkcs8Footer = "END PRIVATE KEY"; PrivateKeyType keyType; + key = KeyString.TrimEnd().Split("\n"); if (key[0].Contains(rsaPrivateKeyHeader) && key[key.Length - 1].ToString().Contains(rsaPrivateFooter)) @@ -769,6 +763,29 @@ private PrivateKeyType GetKeyType(string keyFilePath) } return keyType; } + + /// + /// Read the api key form the api key file path and stored it in KeyString property. + /// + /// api key file path + private string ReadApiKeyFromFile(string apiKeyFilePath) + { + string apiKeyString = null; + if(!string.IsNullOrEmpty(apiKeyFilePath) && !string.IsNullOrEmpty(this.KeyString)) + { + throw new Exception("Configure either the KeyFilePath or configure the KeyString property."); + } + + if(File.Exists(apiKeyFilePath)) + { + apiKeyString = File.ReadAllText(apiKeyFilePath); + } + else + { + throw new Exception("Provided API key file path does not exists."); + } + return apiKeyString; + } #endregion } } diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 0b3e867d0f42..116c890aee58 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -48,6 +48,11 @@ public HttpSigningConfiguration() /// public string KeyFilePath { get; set; } + /// + /// Specify the API key in the form of a string, either configure the KeyString property or configure the KeyFilePath property. + /// + public string KeyString { get; set; } + /// /// Gets the key pass phrase for password protected key /// @@ -112,6 +117,17 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m //the list of signed headers and a base64-encoded signature. const string HEADER_AUTHORIZATION = "Authorization"; + //Read the api key from the file + if(!string.IsNullOrEmpty(this.KeyFilePath)) + { + this.KeyString = ReadApiKeyFromFile(KeyFilePath); + } + + if(string.IsNullOrEmpty(KeyString)) + { + throw new Exception("No API key has been provided."); + } + //Hash table to store singed headers var HttpSignedRequestHeader = new Dictionary(); var HttpSignatureHeader = new Dictionary(); @@ -250,7 +266,7 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m var headerValuesString = string.Join("\n", headerValuesList); var signatureStringHash = GetStringHash(HashAlgorithm.ToString(), headerValuesString); string headerSignatureStr = null; - var keyType = GetKeyType(KeyFilePath); + var keyType = GetKeyType(KeyString); if (keyType == PrivateKeyType.RSA) { @@ -301,7 +317,7 @@ private int GetUnixTime(DateTime date2) private string GetRSASignature(byte[] stringToSign) { - RSA rsa = GetRSAProviderFromPemFile(KeyFilePath, KeyPassPhrase); + RSA rsa = GetRSAProviderFromPemFile(KeyString, KeyPassPhrase); if (SigningAlgorithm == "RSASSA-PSS") { var signedbytes = rsa.SignHash(stringToSign, HashAlgorithm, RSASignaturePadding.Pss); @@ -325,16 +341,7 @@ private string GetRSASignature(byte[] stringToSign) /// ECDSA signature private string GetECDSASignature(byte[] dataToSign) { - string keyStr = string.Empty; - if (File.Exists(KeyFilePath)) - { - keyStr = File.ReadAllText(KeyFilePath); - } - else - { - keyStr = KeyFilePath; - } - + var keyStr = KeyString; const string ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----"; const string ecKeyFooter = "-----END EC PRIVATE KEY-----"; var ecKeyBase64String = keyStr.Replace(ecKeyHeader, "").Replace(ecKeyFooter, "").Trim(); @@ -427,22 +434,13 @@ private byte[] ConvertToECDSAANS1Format(byte[] signedBytes) return derBytes.ToArray(); } - private RSACryptoServiceProvider GetRSAProviderFromPemFile(string pemfile, SecureString keyPassPhrase = null) + private RSACryptoServiceProvider GetRSAProviderFromPemFile(string keyString, SecureString keyPassPhrase = null) { const string pempubheader = "-----BEGIN PUBLIC KEY-----"; const string pempubfooter = "-----END PUBLIC KEY-----"; bool isPrivateKeyFile = true; byte[] pemkey = null; - - string pemstr = string.Empty; - if (File.Exists(pemfile)) - { - pemstr = File.ReadAllText(pemfile).Trim(); - } - else - { - pemstr = pemfile; - } + string pemstr = keyString; if (pemstr.StartsWith(pempubheader) && pemstr.EndsWith(pempubfooter)) { @@ -729,20 +727,15 @@ private byte[] DecryptKey(byte[] cipherData, byte[] desKey, byte[] IV) /// /// Detect the key type from the pem file. /// - /// key file path in pem format + /// api key in string format /// Private Key Type - private PrivateKeyType GetKeyType(string keyFilePath) + private PrivateKeyType GetKeyType(string keyString) { string[] key = null; - if (File.Exists(keyFilePath)) - { - key = File.ReadAllLines(keyFilePath); - } - else + if (string.IsNullOrEmpty(keyString)) { - // The ApiKeyFilePath is passed as string - key = new string[] { keyFilePath }; + throw new Exception("No API key has been provided."); } const string ecPrivateKeyHeader = "BEGIN EC PRIVATE KEY"; @@ -752,6 +745,7 @@ private PrivateKeyType GetKeyType(string keyFilePath) //var pkcs8Header = "BEGIN PRIVATE KEY"; //var pkcs8Footer = "END PRIVATE KEY"; PrivateKeyType keyType; + key = KeyString.TrimEnd().Split("\n"); if (key[0].Contains(rsaPrivateKeyHeader) && key[key.Length - 1].ToString().Contains(rsaPrivateFooter)) @@ -769,6 +763,29 @@ private PrivateKeyType GetKeyType(string keyFilePath) } return keyType; } + + /// + /// Read the api key form the api key file path and stored it in KeyString property. + /// + /// api key file path + private string ReadApiKeyFromFile(string apiKeyFilePath) + { + string apiKeyString = null; + if(!string.IsNullOrEmpty(apiKeyFilePath) && !string.IsNullOrEmpty(this.KeyString)) + { + throw new Exception("Configure either the KeyFilePath or configure the KeyString property."); + } + + if(File.Exists(apiKeyFilePath)) + { + apiKeyString = File.ReadAllText(apiKeyFilePath); + } + else + { + throw new Exception("Provided API key file path does not exists."); + } + return apiKeyString; + } #endregion } } From 5b945c7a59ef03782dc5f48d97784894d6b41614 Mon Sep 17 00:00:00 2001 From: Ghufran Zahidi <18732053+Ghufz@users.noreply.github.com> Date: Mon, 12 Jun 2023 16:14:43 +0530 Subject: [PATCH 3/7] updated the sample code. --- .../Client/HttpSigningConfiguration.cs | 81 +++++++++++-------- .../Client/HttpSigningConfiguration.cs | 81 +++++++++++-------- .../Client/HttpSigningConfiguration.cs | 81 +++++++++++-------- .../Client/HttpSigningConfiguration.cs | 81 +++++++++++-------- .../Client/HttpSigningConfiguration.cs | 81 +++++++++++-------- .../Client/HttpSigningConfiguration.cs | 81 +++++++++++-------- .../Client/HttpSigningConfiguration.cs | 81 +++++++++++-------- .../Client/HttpSigningConfiguration.cs | 81 +++++++++++-------- 8 files changed, 392 insertions(+), 256 deletions(-) diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient-ConditionalSerialization/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient-ConditionalSerialization/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 0b3e867d0f42..265b513e84c2 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient-ConditionalSerialization/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient-ConditionalSerialization/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -48,6 +48,11 @@ public HttpSigningConfiguration() /// public string KeyFilePath { get; set; } + /// + /// Specify the API key in the form of a string, either configure the KeyString property or configure the KeyFilePath property. + /// + public string KeyString { get; set; } + /// /// Gets the key pass phrase for password protected key /// @@ -112,6 +117,17 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m //the list of signed headers and a base64-encoded signature. const string HEADER_AUTHORIZATION = "Authorization"; + //Read the api key from the file + if(!string.IsNullOrEmpty(this.KeyFilePath)) + { + this.KeyString = ReadApiKeyFromFile(KeyFilePath); + } + + if(string.IsNullOrEmpty(KeyString)) + { + throw new Exception("No API key has been provided."); + } + //Hash table to store singed headers var HttpSignedRequestHeader = new Dictionary(); var HttpSignatureHeader = new Dictionary(); @@ -250,7 +266,7 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m var headerValuesString = string.Join("\n", headerValuesList); var signatureStringHash = GetStringHash(HashAlgorithm.ToString(), headerValuesString); string headerSignatureStr = null; - var keyType = GetKeyType(KeyFilePath); + var keyType = GetKeyType(KeyString); if (keyType == PrivateKeyType.RSA) { @@ -301,7 +317,7 @@ private int GetUnixTime(DateTime date2) private string GetRSASignature(byte[] stringToSign) { - RSA rsa = GetRSAProviderFromPemFile(KeyFilePath, KeyPassPhrase); + RSA rsa = GetRSAProviderFromPemFile(KeyString, KeyPassPhrase); if (SigningAlgorithm == "RSASSA-PSS") { var signedbytes = rsa.SignHash(stringToSign, HashAlgorithm, RSASignaturePadding.Pss); @@ -325,16 +341,7 @@ private string GetRSASignature(byte[] stringToSign) /// ECDSA signature private string GetECDSASignature(byte[] dataToSign) { - string keyStr = string.Empty; - if (File.Exists(KeyFilePath)) - { - keyStr = File.ReadAllText(KeyFilePath); - } - else - { - keyStr = KeyFilePath; - } - + var keyStr = KeyString; const string ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----"; const string ecKeyFooter = "-----END EC PRIVATE KEY-----"; var ecKeyBase64String = keyStr.Replace(ecKeyHeader, "").Replace(ecKeyFooter, "").Trim(); @@ -427,22 +434,13 @@ private byte[] ConvertToECDSAANS1Format(byte[] signedBytes) return derBytes.ToArray(); } - private RSACryptoServiceProvider GetRSAProviderFromPemFile(string pemfile, SecureString keyPassPhrase = null) + private RSACryptoServiceProvider GetRSAProviderFromPemFile(string keyString, SecureString keyPassPhrase = null) { const string pempubheader = "-----BEGIN PUBLIC KEY-----"; const string pempubfooter = "-----END PUBLIC KEY-----"; bool isPrivateKeyFile = true; byte[] pemkey = null; - - string pemstr = string.Empty; - if (File.Exists(pemfile)) - { - pemstr = File.ReadAllText(pemfile).Trim(); - } - else - { - pemstr = pemfile; - } + string pemstr = keyString; if (pemstr.StartsWith(pempubheader) && pemstr.EndsWith(pempubfooter)) { @@ -729,20 +727,15 @@ private byte[] DecryptKey(byte[] cipherData, byte[] desKey, byte[] IV) /// /// Detect the key type from the pem file. /// - /// key file path in pem format + /// api key in string format /// Private Key Type - private PrivateKeyType GetKeyType(string keyFilePath) + private PrivateKeyType GetKeyType(string keyString) { string[] key = null; - if (File.Exists(keyFilePath)) - { - key = File.ReadAllLines(keyFilePath); - } - else + if (string.IsNullOrEmpty(keyString)) { - // The ApiKeyFilePath is passed as string - key = new string[] { keyFilePath }; + throw new Exception("No API key has been provided."); } const string ecPrivateKeyHeader = "BEGIN EC PRIVATE KEY"; @@ -752,6 +745,7 @@ private PrivateKeyType GetKeyType(string keyFilePath) //var pkcs8Header = "BEGIN PRIVATE KEY"; //var pkcs8Footer = "END PRIVATE KEY"; PrivateKeyType keyType; + key = KeyString.TrimEnd().Split("\n"); if (key[0].Contains(rsaPrivateKeyHeader) && key[key.Length - 1].ToString().Contains(rsaPrivateFooter)) @@ -769,6 +763,29 @@ private PrivateKeyType GetKeyType(string keyFilePath) } return keyType; } + + /// + /// Read the api key form the api key file path and stored it in KeyString property. + /// + /// api key file path + private string ReadApiKeyFromFile(string apiKeyFilePath) + { + string apiKeyString = null; + if(!string.IsNullOrEmpty(apiKeyFilePath) && !string.IsNullOrEmpty(this.KeyString)) + { + throw new Exception("Configure either the KeyFilePath or configure the KeyString property."); + } + + if(File.Exists(apiKeyFilePath)) + { + apiKeyString = File.ReadAllText(apiKeyFilePath); + } + else + { + throw new Exception("Provided API key file path does not exists."); + } + return apiKeyString; + } #endregion } } diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 0b3e867d0f42..265b513e84c2 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -48,6 +48,11 @@ public HttpSigningConfiguration() /// public string KeyFilePath { get; set; } + /// + /// Specify the API key in the form of a string, either configure the KeyString property or configure the KeyFilePath property. + /// + public string KeyString { get; set; } + /// /// Gets the key pass phrase for password protected key /// @@ -112,6 +117,17 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m //the list of signed headers and a base64-encoded signature. const string HEADER_AUTHORIZATION = "Authorization"; + //Read the api key from the file + if(!string.IsNullOrEmpty(this.KeyFilePath)) + { + this.KeyString = ReadApiKeyFromFile(KeyFilePath); + } + + if(string.IsNullOrEmpty(KeyString)) + { + throw new Exception("No API key has been provided."); + } + //Hash table to store singed headers var HttpSignedRequestHeader = new Dictionary(); var HttpSignatureHeader = new Dictionary(); @@ -250,7 +266,7 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m var headerValuesString = string.Join("\n", headerValuesList); var signatureStringHash = GetStringHash(HashAlgorithm.ToString(), headerValuesString); string headerSignatureStr = null; - var keyType = GetKeyType(KeyFilePath); + var keyType = GetKeyType(KeyString); if (keyType == PrivateKeyType.RSA) { @@ -301,7 +317,7 @@ private int GetUnixTime(DateTime date2) private string GetRSASignature(byte[] stringToSign) { - RSA rsa = GetRSAProviderFromPemFile(KeyFilePath, KeyPassPhrase); + RSA rsa = GetRSAProviderFromPemFile(KeyString, KeyPassPhrase); if (SigningAlgorithm == "RSASSA-PSS") { var signedbytes = rsa.SignHash(stringToSign, HashAlgorithm, RSASignaturePadding.Pss); @@ -325,16 +341,7 @@ private string GetRSASignature(byte[] stringToSign) /// ECDSA signature private string GetECDSASignature(byte[] dataToSign) { - string keyStr = string.Empty; - if (File.Exists(KeyFilePath)) - { - keyStr = File.ReadAllText(KeyFilePath); - } - else - { - keyStr = KeyFilePath; - } - + var keyStr = KeyString; const string ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----"; const string ecKeyFooter = "-----END EC PRIVATE KEY-----"; var ecKeyBase64String = keyStr.Replace(ecKeyHeader, "").Replace(ecKeyFooter, "").Trim(); @@ -427,22 +434,13 @@ private byte[] ConvertToECDSAANS1Format(byte[] signedBytes) return derBytes.ToArray(); } - private RSACryptoServiceProvider GetRSAProviderFromPemFile(string pemfile, SecureString keyPassPhrase = null) + private RSACryptoServiceProvider GetRSAProviderFromPemFile(string keyString, SecureString keyPassPhrase = null) { const string pempubheader = "-----BEGIN PUBLIC KEY-----"; const string pempubfooter = "-----END PUBLIC KEY-----"; bool isPrivateKeyFile = true; byte[] pemkey = null; - - string pemstr = string.Empty; - if (File.Exists(pemfile)) - { - pemstr = File.ReadAllText(pemfile).Trim(); - } - else - { - pemstr = pemfile; - } + string pemstr = keyString; if (pemstr.StartsWith(pempubheader) && pemstr.EndsWith(pempubfooter)) { @@ -729,20 +727,15 @@ private byte[] DecryptKey(byte[] cipherData, byte[] desKey, byte[] IV) /// /// Detect the key type from the pem file. /// - /// key file path in pem format + /// api key in string format /// Private Key Type - private PrivateKeyType GetKeyType(string keyFilePath) + private PrivateKeyType GetKeyType(string keyString) { string[] key = null; - if (File.Exists(keyFilePath)) - { - key = File.ReadAllLines(keyFilePath); - } - else + if (string.IsNullOrEmpty(keyString)) { - // The ApiKeyFilePath is passed as string - key = new string[] { keyFilePath }; + throw new Exception("No API key has been provided."); } const string ecPrivateKeyHeader = "BEGIN EC PRIVATE KEY"; @@ -752,6 +745,7 @@ private PrivateKeyType GetKeyType(string keyFilePath) //var pkcs8Header = "BEGIN PRIVATE KEY"; //var pkcs8Footer = "END PRIVATE KEY"; PrivateKeyType keyType; + key = KeyString.TrimEnd().Split("\n"); if (key[0].Contains(rsaPrivateKeyHeader) && key[key.Length - 1].ToString().Contains(rsaPrivateFooter)) @@ -769,6 +763,29 @@ private PrivateKeyType GetKeyType(string keyFilePath) } return keyType; } + + /// + /// Read the api key form the api key file path and stored it in KeyString property. + /// + /// api key file path + private string ReadApiKeyFromFile(string apiKeyFilePath) + { + string apiKeyString = null; + if(!string.IsNullOrEmpty(apiKeyFilePath) && !string.IsNullOrEmpty(this.KeyString)) + { + throw new Exception("Configure either the KeyFilePath or configure the KeyString property."); + } + + if(File.Exists(apiKeyFilePath)) + { + apiKeyString = File.ReadAllText(apiKeyFilePath); + } + else + { + throw new Exception("Provided API key file path does not exists."); + } + return apiKeyString; + } #endregion } } diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient-net47/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient-net47/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 0b3e867d0f42..265b513e84c2 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient-net47/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient-net47/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -48,6 +48,11 @@ public HttpSigningConfiguration() /// public string KeyFilePath { get; set; } + /// + /// Specify the API key in the form of a string, either configure the KeyString property or configure the KeyFilePath property. + /// + public string KeyString { get; set; } + /// /// Gets the key pass phrase for password protected key /// @@ -112,6 +117,17 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m //the list of signed headers and a base64-encoded signature. const string HEADER_AUTHORIZATION = "Authorization"; + //Read the api key from the file + if(!string.IsNullOrEmpty(this.KeyFilePath)) + { + this.KeyString = ReadApiKeyFromFile(KeyFilePath); + } + + if(string.IsNullOrEmpty(KeyString)) + { + throw new Exception("No API key has been provided."); + } + //Hash table to store singed headers var HttpSignedRequestHeader = new Dictionary(); var HttpSignatureHeader = new Dictionary(); @@ -250,7 +266,7 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m var headerValuesString = string.Join("\n", headerValuesList); var signatureStringHash = GetStringHash(HashAlgorithm.ToString(), headerValuesString); string headerSignatureStr = null; - var keyType = GetKeyType(KeyFilePath); + var keyType = GetKeyType(KeyString); if (keyType == PrivateKeyType.RSA) { @@ -301,7 +317,7 @@ private int GetUnixTime(DateTime date2) private string GetRSASignature(byte[] stringToSign) { - RSA rsa = GetRSAProviderFromPemFile(KeyFilePath, KeyPassPhrase); + RSA rsa = GetRSAProviderFromPemFile(KeyString, KeyPassPhrase); if (SigningAlgorithm == "RSASSA-PSS") { var signedbytes = rsa.SignHash(stringToSign, HashAlgorithm, RSASignaturePadding.Pss); @@ -325,16 +341,7 @@ private string GetRSASignature(byte[] stringToSign) /// ECDSA signature private string GetECDSASignature(byte[] dataToSign) { - string keyStr = string.Empty; - if (File.Exists(KeyFilePath)) - { - keyStr = File.ReadAllText(KeyFilePath); - } - else - { - keyStr = KeyFilePath; - } - + var keyStr = KeyString; const string ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----"; const string ecKeyFooter = "-----END EC PRIVATE KEY-----"; var ecKeyBase64String = keyStr.Replace(ecKeyHeader, "").Replace(ecKeyFooter, "").Trim(); @@ -427,22 +434,13 @@ private byte[] ConvertToECDSAANS1Format(byte[] signedBytes) return derBytes.ToArray(); } - private RSACryptoServiceProvider GetRSAProviderFromPemFile(string pemfile, SecureString keyPassPhrase = null) + private RSACryptoServiceProvider GetRSAProviderFromPemFile(string keyString, SecureString keyPassPhrase = null) { const string pempubheader = "-----BEGIN PUBLIC KEY-----"; const string pempubfooter = "-----END PUBLIC KEY-----"; bool isPrivateKeyFile = true; byte[] pemkey = null; - - string pemstr = string.Empty; - if (File.Exists(pemfile)) - { - pemstr = File.ReadAllText(pemfile).Trim(); - } - else - { - pemstr = pemfile; - } + string pemstr = keyString; if (pemstr.StartsWith(pempubheader) && pemstr.EndsWith(pempubfooter)) { @@ -729,20 +727,15 @@ private byte[] DecryptKey(byte[] cipherData, byte[] desKey, byte[] IV) /// /// Detect the key type from the pem file. /// - /// key file path in pem format + /// api key in string format /// Private Key Type - private PrivateKeyType GetKeyType(string keyFilePath) + private PrivateKeyType GetKeyType(string keyString) { string[] key = null; - if (File.Exists(keyFilePath)) - { - key = File.ReadAllLines(keyFilePath); - } - else + if (string.IsNullOrEmpty(keyString)) { - // The ApiKeyFilePath is passed as string - key = new string[] { keyFilePath }; + throw new Exception("No API key has been provided."); } const string ecPrivateKeyHeader = "BEGIN EC PRIVATE KEY"; @@ -752,6 +745,7 @@ private PrivateKeyType GetKeyType(string keyFilePath) //var pkcs8Header = "BEGIN PRIVATE KEY"; //var pkcs8Footer = "END PRIVATE KEY"; PrivateKeyType keyType; + key = KeyString.TrimEnd().Split("\n"); if (key[0].Contains(rsaPrivateKeyHeader) && key[key.Length - 1].ToString().Contains(rsaPrivateFooter)) @@ -769,6 +763,29 @@ private PrivateKeyType GetKeyType(string keyFilePath) } return keyType; } + + /// + /// Read the api key form the api key file path and stored it in KeyString property. + /// + /// api key file path + private string ReadApiKeyFromFile(string apiKeyFilePath) + { + string apiKeyString = null; + if(!string.IsNullOrEmpty(apiKeyFilePath) && !string.IsNullOrEmpty(this.KeyString)) + { + throw new Exception("Configure either the KeyFilePath or configure the KeyString property."); + } + + if(File.Exists(apiKeyFilePath)) + { + apiKeyString = File.ReadAllText(apiKeyFilePath); + } + else + { + throw new Exception("Provided API key file path does not exists."); + } + return apiKeyString; + } #endregion } } diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient-net48/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient-net48/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 0b3e867d0f42..265b513e84c2 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient-net48/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient-net48/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -48,6 +48,11 @@ public HttpSigningConfiguration() /// public string KeyFilePath { get; set; } + /// + /// Specify the API key in the form of a string, either configure the KeyString property or configure the KeyFilePath property. + /// + public string KeyString { get; set; } + /// /// Gets the key pass phrase for password protected key /// @@ -112,6 +117,17 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m //the list of signed headers and a base64-encoded signature. const string HEADER_AUTHORIZATION = "Authorization"; + //Read the api key from the file + if(!string.IsNullOrEmpty(this.KeyFilePath)) + { + this.KeyString = ReadApiKeyFromFile(KeyFilePath); + } + + if(string.IsNullOrEmpty(KeyString)) + { + throw new Exception("No API key has been provided."); + } + //Hash table to store singed headers var HttpSignedRequestHeader = new Dictionary(); var HttpSignatureHeader = new Dictionary(); @@ -250,7 +266,7 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m var headerValuesString = string.Join("\n", headerValuesList); var signatureStringHash = GetStringHash(HashAlgorithm.ToString(), headerValuesString); string headerSignatureStr = null; - var keyType = GetKeyType(KeyFilePath); + var keyType = GetKeyType(KeyString); if (keyType == PrivateKeyType.RSA) { @@ -301,7 +317,7 @@ private int GetUnixTime(DateTime date2) private string GetRSASignature(byte[] stringToSign) { - RSA rsa = GetRSAProviderFromPemFile(KeyFilePath, KeyPassPhrase); + RSA rsa = GetRSAProviderFromPemFile(KeyString, KeyPassPhrase); if (SigningAlgorithm == "RSASSA-PSS") { var signedbytes = rsa.SignHash(stringToSign, HashAlgorithm, RSASignaturePadding.Pss); @@ -325,16 +341,7 @@ private string GetRSASignature(byte[] stringToSign) /// ECDSA signature private string GetECDSASignature(byte[] dataToSign) { - string keyStr = string.Empty; - if (File.Exists(KeyFilePath)) - { - keyStr = File.ReadAllText(KeyFilePath); - } - else - { - keyStr = KeyFilePath; - } - + var keyStr = KeyString; const string ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----"; const string ecKeyFooter = "-----END EC PRIVATE KEY-----"; var ecKeyBase64String = keyStr.Replace(ecKeyHeader, "").Replace(ecKeyFooter, "").Trim(); @@ -427,22 +434,13 @@ private byte[] ConvertToECDSAANS1Format(byte[] signedBytes) return derBytes.ToArray(); } - private RSACryptoServiceProvider GetRSAProviderFromPemFile(string pemfile, SecureString keyPassPhrase = null) + private RSACryptoServiceProvider GetRSAProviderFromPemFile(string keyString, SecureString keyPassPhrase = null) { const string pempubheader = "-----BEGIN PUBLIC KEY-----"; const string pempubfooter = "-----END PUBLIC KEY-----"; bool isPrivateKeyFile = true; byte[] pemkey = null; - - string pemstr = string.Empty; - if (File.Exists(pemfile)) - { - pemstr = File.ReadAllText(pemfile).Trim(); - } - else - { - pemstr = pemfile; - } + string pemstr = keyString; if (pemstr.StartsWith(pempubheader) && pemstr.EndsWith(pempubfooter)) { @@ -729,20 +727,15 @@ private byte[] DecryptKey(byte[] cipherData, byte[] desKey, byte[] IV) /// /// Detect the key type from the pem file. /// - /// key file path in pem format + /// api key in string format /// Private Key Type - private PrivateKeyType GetKeyType(string keyFilePath) + private PrivateKeyType GetKeyType(string keyString) { string[] key = null; - if (File.Exists(keyFilePath)) - { - key = File.ReadAllLines(keyFilePath); - } - else + if (string.IsNullOrEmpty(keyString)) { - // The ApiKeyFilePath is passed as string - key = new string[] { keyFilePath }; + throw new Exception("No API key has been provided."); } const string ecPrivateKeyHeader = "BEGIN EC PRIVATE KEY"; @@ -752,6 +745,7 @@ private PrivateKeyType GetKeyType(string keyFilePath) //var pkcs8Header = "BEGIN PRIVATE KEY"; //var pkcs8Footer = "END PRIVATE KEY"; PrivateKeyType keyType; + key = KeyString.TrimEnd().Split("\n"); if (key[0].Contains(rsaPrivateKeyHeader) && key[key.Length - 1].ToString().Contains(rsaPrivateFooter)) @@ -769,6 +763,29 @@ private PrivateKeyType GetKeyType(string keyFilePath) } return keyType; } + + /// + /// Read the api key form the api key file path and stored it in KeyString property. + /// + /// api key file path + private string ReadApiKeyFromFile(string apiKeyFilePath) + { + string apiKeyString = null; + if(!string.IsNullOrEmpty(apiKeyFilePath) && !string.IsNullOrEmpty(this.KeyString)) + { + throw new Exception("Configure either the KeyFilePath or configure the KeyString property."); + } + + if(File.Exists(apiKeyFilePath)) + { + apiKeyString = File.ReadAllText(apiKeyFilePath); + } + else + { + throw new Exception("Provided API key file path does not exists."); + } + return apiKeyString; + } #endregion } } diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient-net5.0/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient-net5.0/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 0b3e867d0f42..265b513e84c2 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient-net5.0/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient-net5.0/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -48,6 +48,11 @@ public HttpSigningConfiguration() /// public string KeyFilePath { get; set; } + /// + /// Specify the API key in the form of a string, either configure the KeyString property or configure the KeyFilePath property. + /// + public string KeyString { get; set; } + /// /// Gets the key pass phrase for password protected key /// @@ -112,6 +117,17 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m //the list of signed headers and a base64-encoded signature. const string HEADER_AUTHORIZATION = "Authorization"; + //Read the api key from the file + if(!string.IsNullOrEmpty(this.KeyFilePath)) + { + this.KeyString = ReadApiKeyFromFile(KeyFilePath); + } + + if(string.IsNullOrEmpty(KeyString)) + { + throw new Exception("No API key has been provided."); + } + //Hash table to store singed headers var HttpSignedRequestHeader = new Dictionary(); var HttpSignatureHeader = new Dictionary(); @@ -250,7 +266,7 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m var headerValuesString = string.Join("\n", headerValuesList); var signatureStringHash = GetStringHash(HashAlgorithm.ToString(), headerValuesString); string headerSignatureStr = null; - var keyType = GetKeyType(KeyFilePath); + var keyType = GetKeyType(KeyString); if (keyType == PrivateKeyType.RSA) { @@ -301,7 +317,7 @@ private int GetUnixTime(DateTime date2) private string GetRSASignature(byte[] stringToSign) { - RSA rsa = GetRSAProviderFromPemFile(KeyFilePath, KeyPassPhrase); + RSA rsa = GetRSAProviderFromPemFile(KeyString, KeyPassPhrase); if (SigningAlgorithm == "RSASSA-PSS") { var signedbytes = rsa.SignHash(stringToSign, HashAlgorithm, RSASignaturePadding.Pss); @@ -325,16 +341,7 @@ private string GetRSASignature(byte[] stringToSign) /// ECDSA signature private string GetECDSASignature(byte[] dataToSign) { - string keyStr = string.Empty; - if (File.Exists(KeyFilePath)) - { - keyStr = File.ReadAllText(KeyFilePath); - } - else - { - keyStr = KeyFilePath; - } - + var keyStr = KeyString; const string ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----"; const string ecKeyFooter = "-----END EC PRIVATE KEY-----"; var ecKeyBase64String = keyStr.Replace(ecKeyHeader, "").Replace(ecKeyFooter, "").Trim(); @@ -427,22 +434,13 @@ private byte[] ConvertToECDSAANS1Format(byte[] signedBytes) return derBytes.ToArray(); } - private RSACryptoServiceProvider GetRSAProviderFromPemFile(string pemfile, SecureString keyPassPhrase = null) + private RSACryptoServiceProvider GetRSAProviderFromPemFile(string keyString, SecureString keyPassPhrase = null) { const string pempubheader = "-----BEGIN PUBLIC KEY-----"; const string pempubfooter = "-----END PUBLIC KEY-----"; bool isPrivateKeyFile = true; byte[] pemkey = null; - - string pemstr = string.Empty; - if (File.Exists(pemfile)) - { - pemstr = File.ReadAllText(pemfile).Trim(); - } - else - { - pemstr = pemfile; - } + string pemstr = keyString; if (pemstr.StartsWith(pempubheader) && pemstr.EndsWith(pempubfooter)) { @@ -729,20 +727,15 @@ private byte[] DecryptKey(byte[] cipherData, byte[] desKey, byte[] IV) /// /// Detect the key type from the pem file. /// - /// key file path in pem format + /// api key in string format /// Private Key Type - private PrivateKeyType GetKeyType(string keyFilePath) + private PrivateKeyType GetKeyType(string keyString) { string[] key = null; - if (File.Exists(keyFilePath)) - { - key = File.ReadAllLines(keyFilePath); - } - else + if (string.IsNullOrEmpty(keyString)) { - // The ApiKeyFilePath is passed as string - key = new string[] { keyFilePath }; + throw new Exception("No API key has been provided."); } const string ecPrivateKeyHeader = "BEGIN EC PRIVATE KEY"; @@ -752,6 +745,7 @@ private PrivateKeyType GetKeyType(string keyFilePath) //var pkcs8Header = "BEGIN PRIVATE KEY"; //var pkcs8Footer = "END PRIVATE KEY"; PrivateKeyType keyType; + key = KeyString.TrimEnd().Split("\n"); if (key[0].Contains(rsaPrivateKeyHeader) && key[key.Length - 1].ToString().Contains(rsaPrivateFooter)) @@ -769,6 +763,29 @@ private PrivateKeyType GetKeyType(string keyFilePath) } return keyType; } + + /// + /// Read the api key form the api key file path and stored it in KeyString property. + /// + /// api key file path + private string ReadApiKeyFromFile(string apiKeyFilePath) + { + string apiKeyString = null; + if(!string.IsNullOrEmpty(apiKeyFilePath) && !string.IsNullOrEmpty(this.KeyString)) + { + throw new Exception("Configure either the KeyFilePath or configure the KeyString property."); + } + + if(File.Exists(apiKeyFilePath)) + { + apiKeyString = File.ReadAllText(apiKeyFilePath); + } + else + { + throw new Exception("Provided API key file path does not exists."); + } + return apiKeyString; + } #endregion } } diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient-unityWebRequest/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient-unityWebRequest/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 0b3e867d0f42..265b513e84c2 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient-unityWebRequest/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient-unityWebRequest/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -48,6 +48,11 @@ public HttpSigningConfiguration() /// public string KeyFilePath { get; set; } + /// + /// Specify the API key in the form of a string, either configure the KeyString property or configure the KeyFilePath property. + /// + public string KeyString { get; set; } + /// /// Gets the key pass phrase for password protected key /// @@ -112,6 +117,17 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m //the list of signed headers and a base64-encoded signature. const string HEADER_AUTHORIZATION = "Authorization"; + //Read the api key from the file + if(!string.IsNullOrEmpty(this.KeyFilePath)) + { + this.KeyString = ReadApiKeyFromFile(KeyFilePath); + } + + if(string.IsNullOrEmpty(KeyString)) + { + throw new Exception("No API key has been provided."); + } + //Hash table to store singed headers var HttpSignedRequestHeader = new Dictionary(); var HttpSignatureHeader = new Dictionary(); @@ -250,7 +266,7 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m var headerValuesString = string.Join("\n", headerValuesList); var signatureStringHash = GetStringHash(HashAlgorithm.ToString(), headerValuesString); string headerSignatureStr = null; - var keyType = GetKeyType(KeyFilePath); + var keyType = GetKeyType(KeyString); if (keyType == PrivateKeyType.RSA) { @@ -301,7 +317,7 @@ private int GetUnixTime(DateTime date2) private string GetRSASignature(byte[] stringToSign) { - RSA rsa = GetRSAProviderFromPemFile(KeyFilePath, KeyPassPhrase); + RSA rsa = GetRSAProviderFromPemFile(KeyString, KeyPassPhrase); if (SigningAlgorithm == "RSASSA-PSS") { var signedbytes = rsa.SignHash(stringToSign, HashAlgorithm, RSASignaturePadding.Pss); @@ -325,16 +341,7 @@ private string GetRSASignature(byte[] stringToSign) /// ECDSA signature private string GetECDSASignature(byte[] dataToSign) { - string keyStr = string.Empty; - if (File.Exists(KeyFilePath)) - { - keyStr = File.ReadAllText(KeyFilePath); - } - else - { - keyStr = KeyFilePath; - } - + var keyStr = KeyString; const string ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----"; const string ecKeyFooter = "-----END EC PRIVATE KEY-----"; var ecKeyBase64String = keyStr.Replace(ecKeyHeader, "").Replace(ecKeyFooter, "").Trim(); @@ -427,22 +434,13 @@ private byte[] ConvertToECDSAANS1Format(byte[] signedBytes) return derBytes.ToArray(); } - private RSACryptoServiceProvider GetRSAProviderFromPemFile(string pemfile, SecureString keyPassPhrase = null) + private RSACryptoServiceProvider GetRSAProviderFromPemFile(string keyString, SecureString keyPassPhrase = null) { const string pempubheader = "-----BEGIN PUBLIC KEY-----"; const string pempubfooter = "-----END PUBLIC KEY-----"; bool isPrivateKeyFile = true; byte[] pemkey = null; - - string pemstr = string.Empty; - if (File.Exists(pemfile)) - { - pemstr = File.ReadAllText(pemfile).Trim(); - } - else - { - pemstr = pemfile; - } + string pemstr = keyString; if (pemstr.StartsWith(pempubheader) && pemstr.EndsWith(pempubfooter)) { @@ -729,20 +727,15 @@ private byte[] DecryptKey(byte[] cipherData, byte[] desKey, byte[] IV) /// /// Detect the key type from the pem file. /// - /// key file path in pem format + /// api key in string format /// Private Key Type - private PrivateKeyType GetKeyType(string keyFilePath) + private PrivateKeyType GetKeyType(string keyString) { string[] key = null; - if (File.Exists(keyFilePath)) - { - key = File.ReadAllLines(keyFilePath); - } - else + if (string.IsNullOrEmpty(keyString)) { - // The ApiKeyFilePath is passed as string - key = new string[] { keyFilePath }; + throw new Exception("No API key has been provided."); } const string ecPrivateKeyHeader = "BEGIN EC PRIVATE KEY"; @@ -752,6 +745,7 @@ private PrivateKeyType GetKeyType(string keyFilePath) //var pkcs8Header = "BEGIN PRIVATE KEY"; //var pkcs8Footer = "END PRIVATE KEY"; PrivateKeyType keyType; + key = KeyString.TrimEnd().Split("\n"); if (key[0].Contains(rsaPrivateKeyHeader) && key[key.Length - 1].ToString().Contains(rsaPrivateFooter)) @@ -769,6 +763,29 @@ private PrivateKeyType GetKeyType(string keyFilePath) } return keyType; } + + /// + /// Read the api key form the api key file path and stored it in KeyString property. + /// + /// api key file path + private string ReadApiKeyFromFile(string apiKeyFilePath) + { + string apiKeyString = null; + if(!string.IsNullOrEmpty(apiKeyFilePath) && !string.IsNullOrEmpty(this.KeyString)) + { + throw new Exception("Configure either the KeyFilePath or configure the KeyString property."); + } + + if(File.Exists(apiKeyFilePath)) + { + apiKeyString = File.ReadAllText(apiKeyFilePath); + } + else + { + throw new Exception("Provided API key file path does not exists."); + } + return apiKeyString; + } #endregion } } diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 0b3e867d0f42..265b513e84c2 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -48,6 +48,11 @@ public HttpSigningConfiguration() /// public string KeyFilePath { get; set; } + /// + /// Specify the API key in the form of a string, either configure the KeyString property or configure the KeyFilePath property. + /// + public string KeyString { get; set; } + /// /// Gets the key pass phrase for password protected key /// @@ -112,6 +117,17 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m //the list of signed headers and a base64-encoded signature. const string HEADER_AUTHORIZATION = "Authorization"; + //Read the api key from the file + if(!string.IsNullOrEmpty(this.KeyFilePath)) + { + this.KeyString = ReadApiKeyFromFile(KeyFilePath); + } + + if(string.IsNullOrEmpty(KeyString)) + { + throw new Exception("No API key has been provided."); + } + //Hash table to store singed headers var HttpSignedRequestHeader = new Dictionary(); var HttpSignatureHeader = new Dictionary(); @@ -250,7 +266,7 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m var headerValuesString = string.Join("\n", headerValuesList); var signatureStringHash = GetStringHash(HashAlgorithm.ToString(), headerValuesString); string headerSignatureStr = null; - var keyType = GetKeyType(KeyFilePath); + var keyType = GetKeyType(KeyString); if (keyType == PrivateKeyType.RSA) { @@ -301,7 +317,7 @@ private int GetUnixTime(DateTime date2) private string GetRSASignature(byte[] stringToSign) { - RSA rsa = GetRSAProviderFromPemFile(KeyFilePath, KeyPassPhrase); + RSA rsa = GetRSAProviderFromPemFile(KeyString, KeyPassPhrase); if (SigningAlgorithm == "RSASSA-PSS") { var signedbytes = rsa.SignHash(stringToSign, HashAlgorithm, RSASignaturePadding.Pss); @@ -325,16 +341,7 @@ private string GetRSASignature(byte[] stringToSign) /// ECDSA signature private string GetECDSASignature(byte[] dataToSign) { - string keyStr = string.Empty; - if (File.Exists(KeyFilePath)) - { - keyStr = File.ReadAllText(KeyFilePath); - } - else - { - keyStr = KeyFilePath; - } - + var keyStr = KeyString; const string ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----"; const string ecKeyFooter = "-----END EC PRIVATE KEY-----"; var ecKeyBase64String = keyStr.Replace(ecKeyHeader, "").Replace(ecKeyFooter, "").Trim(); @@ -427,22 +434,13 @@ private byte[] ConvertToECDSAANS1Format(byte[] signedBytes) return derBytes.ToArray(); } - private RSACryptoServiceProvider GetRSAProviderFromPemFile(string pemfile, SecureString keyPassPhrase = null) + private RSACryptoServiceProvider GetRSAProviderFromPemFile(string keyString, SecureString keyPassPhrase = null) { const string pempubheader = "-----BEGIN PUBLIC KEY-----"; const string pempubfooter = "-----END PUBLIC KEY-----"; bool isPrivateKeyFile = true; byte[] pemkey = null; - - string pemstr = string.Empty; - if (File.Exists(pemfile)) - { - pemstr = File.ReadAllText(pemfile).Trim(); - } - else - { - pemstr = pemfile; - } + string pemstr = keyString; if (pemstr.StartsWith(pempubheader) && pemstr.EndsWith(pempubfooter)) { @@ -729,20 +727,15 @@ private byte[] DecryptKey(byte[] cipherData, byte[] desKey, byte[] IV) /// /// Detect the key type from the pem file. /// - /// key file path in pem format + /// api key in string format /// Private Key Type - private PrivateKeyType GetKeyType(string keyFilePath) + private PrivateKeyType GetKeyType(string keyString) { string[] key = null; - if (File.Exists(keyFilePath)) - { - key = File.ReadAllLines(keyFilePath); - } - else + if (string.IsNullOrEmpty(keyString)) { - // The ApiKeyFilePath is passed as string - key = new string[] { keyFilePath }; + throw new Exception("No API key has been provided."); } const string ecPrivateKeyHeader = "BEGIN EC PRIVATE KEY"; @@ -752,6 +745,7 @@ private PrivateKeyType GetKeyType(string keyFilePath) //var pkcs8Header = "BEGIN PRIVATE KEY"; //var pkcs8Footer = "END PRIVATE KEY"; PrivateKeyType keyType; + key = KeyString.TrimEnd().Split("\n"); if (key[0].Contains(rsaPrivateKeyHeader) && key[key.Length - 1].ToString().Contains(rsaPrivateFooter)) @@ -769,6 +763,29 @@ private PrivateKeyType GetKeyType(string keyFilePath) } return keyType; } + + /// + /// Read the api key form the api key file path and stored it in KeyString property. + /// + /// api key file path + private string ReadApiKeyFromFile(string apiKeyFilePath) + { + string apiKeyString = null; + if(!string.IsNullOrEmpty(apiKeyFilePath) && !string.IsNullOrEmpty(this.KeyString)) + { + throw new Exception("Configure either the KeyFilePath or configure the KeyString property."); + } + + if(File.Exists(apiKeyFilePath)) + { + apiKeyString = File.ReadAllText(apiKeyFilePath); + } + else + { + throw new Exception("Provided API key file path does not exists."); + } + return apiKeyString; + } #endregion } } diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 0b3e867d0f42..265b513e84c2 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -48,6 +48,11 @@ public HttpSigningConfiguration() /// public string KeyFilePath { get; set; } + /// + /// Specify the API key in the form of a string, either configure the KeyString property or configure the KeyFilePath property. + /// + public string KeyString { get; set; } + /// /// Gets the key pass phrase for password protected key /// @@ -112,6 +117,17 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m //the list of signed headers and a base64-encoded signature. const string HEADER_AUTHORIZATION = "Authorization"; + //Read the api key from the file + if(!string.IsNullOrEmpty(this.KeyFilePath)) + { + this.KeyString = ReadApiKeyFromFile(KeyFilePath); + } + + if(string.IsNullOrEmpty(KeyString)) + { + throw new Exception("No API key has been provided."); + } + //Hash table to store singed headers var HttpSignedRequestHeader = new Dictionary(); var HttpSignatureHeader = new Dictionary(); @@ -250,7 +266,7 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m var headerValuesString = string.Join("\n", headerValuesList); var signatureStringHash = GetStringHash(HashAlgorithm.ToString(), headerValuesString); string headerSignatureStr = null; - var keyType = GetKeyType(KeyFilePath); + var keyType = GetKeyType(KeyString); if (keyType == PrivateKeyType.RSA) { @@ -301,7 +317,7 @@ private int GetUnixTime(DateTime date2) private string GetRSASignature(byte[] stringToSign) { - RSA rsa = GetRSAProviderFromPemFile(KeyFilePath, KeyPassPhrase); + RSA rsa = GetRSAProviderFromPemFile(KeyString, KeyPassPhrase); if (SigningAlgorithm == "RSASSA-PSS") { var signedbytes = rsa.SignHash(stringToSign, HashAlgorithm, RSASignaturePadding.Pss); @@ -325,16 +341,7 @@ private string GetRSASignature(byte[] stringToSign) /// ECDSA signature private string GetECDSASignature(byte[] dataToSign) { - string keyStr = string.Empty; - if (File.Exists(KeyFilePath)) - { - keyStr = File.ReadAllText(KeyFilePath); - } - else - { - keyStr = KeyFilePath; - } - + var keyStr = KeyString; const string ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----"; const string ecKeyFooter = "-----END EC PRIVATE KEY-----"; var ecKeyBase64String = keyStr.Replace(ecKeyHeader, "").Replace(ecKeyFooter, "").Trim(); @@ -427,22 +434,13 @@ private byte[] ConvertToECDSAANS1Format(byte[] signedBytes) return derBytes.ToArray(); } - private RSACryptoServiceProvider GetRSAProviderFromPemFile(string pemfile, SecureString keyPassPhrase = null) + private RSACryptoServiceProvider GetRSAProviderFromPemFile(string keyString, SecureString keyPassPhrase = null) { const string pempubheader = "-----BEGIN PUBLIC KEY-----"; const string pempubfooter = "-----END PUBLIC KEY-----"; bool isPrivateKeyFile = true; byte[] pemkey = null; - - string pemstr = string.Empty; - if (File.Exists(pemfile)) - { - pemstr = File.ReadAllText(pemfile).Trim(); - } - else - { - pemstr = pemfile; - } + string pemstr = keyString; if (pemstr.StartsWith(pempubheader) && pemstr.EndsWith(pempubfooter)) { @@ -729,20 +727,15 @@ private byte[] DecryptKey(byte[] cipherData, byte[] desKey, byte[] IV) /// /// Detect the key type from the pem file. /// - /// key file path in pem format + /// api key in string format /// Private Key Type - private PrivateKeyType GetKeyType(string keyFilePath) + private PrivateKeyType GetKeyType(string keyString) { string[] key = null; - if (File.Exists(keyFilePath)) - { - key = File.ReadAllLines(keyFilePath); - } - else + if (string.IsNullOrEmpty(keyString)) { - // The ApiKeyFilePath is passed as string - key = new string[] { keyFilePath }; + throw new Exception("No API key has been provided."); } const string ecPrivateKeyHeader = "BEGIN EC PRIVATE KEY"; @@ -752,6 +745,7 @@ private PrivateKeyType GetKeyType(string keyFilePath) //var pkcs8Header = "BEGIN PRIVATE KEY"; //var pkcs8Footer = "END PRIVATE KEY"; PrivateKeyType keyType; + key = KeyString.TrimEnd().Split("\n"); if (key[0].Contains(rsaPrivateKeyHeader) && key[key.Length - 1].ToString().Contains(rsaPrivateFooter)) @@ -769,6 +763,29 @@ private PrivateKeyType GetKeyType(string keyFilePath) } return keyType; } + + /// + /// Read the api key form the api key file path and stored it in KeyString property. + /// + /// api key file path + private string ReadApiKeyFromFile(string apiKeyFilePath) + { + string apiKeyString = null; + if(!string.IsNullOrEmpty(apiKeyFilePath) && !string.IsNullOrEmpty(this.KeyString)) + { + throw new Exception("Configure either the KeyFilePath or configure the KeyString property."); + } + + if(File.Exists(apiKeyFilePath)) + { + apiKeyString = File.ReadAllText(apiKeyFilePath); + } + else + { + throw new Exception("Provided API key file path does not exists."); + } + return apiKeyString; + } #endregion } } From 811a62a0510ce9fc7390cb81b1d6ca63bd39132b Mon Sep 17 00:00:00 2001 From: Ghufran Zahidi <18732053+Ghufz@users.noreply.github.com> Date: Tue, 13 Jun 2023 14:33:02 +0530 Subject: [PATCH 4/7] Revert "updated the sample code." This reverts commit 5b945c7a59ef03782dc5f48d97784894d6b41614. --- .../Client/HttpSigningConfiguration.cs | 81 ++++++++----------- .../Client/HttpSigningConfiguration.cs | 81 ++++++++----------- .../Client/HttpSigningConfiguration.cs | 81 ++++++++----------- .../Client/HttpSigningConfiguration.cs | 81 ++++++++----------- .../Client/HttpSigningConfiguration.cs | 81 ++++++++----------- .../Client/HttpSigningConfiguration.cs | 81 ++++++++----------- .../Client/HttpSigningConfiguration.cs | 81 ++++++++----------- .../Client/HttpSigningConfiguration.cs | 81 ++++++++----------- 8 files changed, 256 insertions(+), 392 deletions(-) diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient-ConditionalSerialization/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient-ConditionalSerialization/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 265b513e84c2..0b3e867d0f42 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient-ConditionalSerialization/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient-ConditionalSerialization/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -48,11 +48,6 @@ public HttpSigningConfiguration() /// public string KeyFilePath { get; set; } - /// - /// Specify the API key in the form of a string, either configure the KeyString property or configure the KeyFilePath property. - /// - public string KeyString { get; set; } - /// /// Gets the key pass phrase for password protected key /// @@ -117,17 +112,6 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m //the list of signed headers and a base64-encoded signature. const string HEADER_AUTHORIZATION = "Authorization"; - //Read the api key from the file - if(!string.IsNullOrEmpty(this.KeyFilePath)) - { - this.KeyString = ReadApiKeyFromFile(KeyFilePath); - } - - if(string.IsNullOrEmpty(KeyString)) - { - throw new Exception("No API key has been provided."); - } - //Hash table to store singed headers var HttpSignedRequestHeader = new Dictionary(); var HttpSignatureHeader = new Dictionary(); @@ -266,7 +250,7 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m var headerValuesString = string.Join("\n", headerValuesList); var signatureStringHash = GetStringHash(HashAlgorithm.ToString(), headerValuesString); string headerSignatureStr = null; - var keyType = GetKeyType(KeyString); + var keyType = GetKeyType(KeyFilePath); if (keyType == PrivateKeyType.RSA) { @@ -317,7 +301,7 @@ private int GetUnixTime(DateTime date2) private string GetRSASignature(byte[] stringToSign) { - RSA rsa = GetRSAProviderFromPemFile(KeyString, KeyPassPhrase); + RSA rsa = GetRSAProviderFromPemFile(KeyFilePath, KeyPassPhrase); if (SigningAlgorithm == "RSASSA-PSS") { var signedbytes = rsa.SignHash(stringToSign, HashAlgorithm, RSASignaturePadding.Pss); @@ -341,7 +325,16 @@ private string GetRSASignature(byte[] stringToSign) /// ECDSA signature private string GetECDSASignature(byte[] dataToSign) { - var keyStr = KeyString; + string keyStr = string.Empty; + if (File.Exists(KeyFilePath)) + { + keyStr = File.ReadAllText(KeyFilePath); + } + else + { + keyStr = KeyFilePath; + } + const string ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----"; const string ecKeyFooter = "-----END EC PRIVATE KEY-----"; var ecKeyBase64String = keyStr.Replace(ecKeyHeader, "").Replace(ecKeyFooter, "").Trim(); @@ -434,13 +427,22 @@ private byte[] ConvertToECDSAANS1Format(byte[] signedBytes) return derBytes.ToArray(); } - private RSACryptoServiceProvider GetRSAProviderFromPemFile(string keyString, SecureString keyPassPhrase = null) + private RSACryptoServiceProvider GetRSAProviderFromPemFile(string pemfile, SecureString keyPassPhrase = null) { const string pempubheader = "-----BEGIN PUBLIC KEY-----"; const string pempubfooter = "-----END PUBLIC KEY-----"; bool isPrivateKeyFile = true; byte[] pemkey = null; - string pemstr = keyString; + + string pemstr = string.Empty; + if (File.Exists(pemfile)) + { + pemstr = File.ReadAllText(pemfile).Trim(); + } + else + { + pemstr = pemfile; + } if (pemstr.StartsWith(pempubheader) && pemstr.EndsWith(pempubfooter)) { @@ -727,15 +729,20 @@ private byte[] DecryptKey(byte[] cipherData, byte[] desKey, byte[] IV) /// /// Detect the key type from the pem file. /// - /// api key in string format + /// key file path in pem format /// Private Key Type - private PrivateKeyType GetKeyType(string keyString) + private PrivateKeyType GetKeyType(string keyFilePath) { string[] key = null; - if (string.IsNullOrEmpty(keyString)) + if (File.Exists(keyFilePath)) + { + key = File.ReadAllLines(keyFilePath); + } + else { - throw new Exception("No API key has been provided."); + // The ApiKeyFilePath is passed as string + key = new string[] { keyFilePath }; } const string ecPrivateKeyHeader = "BEGIN EC PRIVATE KEY"; @@ -745,7 +752,6 @@ private PrivateKeyType GetKeyType(string keyString) //var pkcs8Header = "BEGIN PRIVATE KEY"; //var pkcs8Footer = "END PRIVATE KEY"; PrivateKeyType keyType; - key = KeyString.TrimEnd().Split("\n"); if (key[0].Contains(rsaPrivateKeyHeader) && key[key.Length - 1].ToString().Contains(rsaPrivateFooter)) @@ -763,29 +769,6 @@ private PrivateKeyType GetKeyType(string keyString) } return keyType; } - - /// - /// Read the api key form the api key file path and stored it in KeyString property. - /// - /// api key file path - private string ReadApiKeyFromFile(string apiKeyFilePath) - { - string apiKeyString = null; - if(!string.IsNullOrEmpty(apiKeyFilePath) && !string.IsNullOrEmpty(this.KeyString)) - { - throw new Exception("Configure either the KeyFilePath or configure the KeyString property."); - } - - if(File.Exists(apiKeyFilePath)) - { - apiKeyString = File.ReadAllText(apiKeyFilePath); - } - else - { - throw new Exception("Provided API key file path does not exists."); - } - return apiKeyString; - } #endregion } } diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 265b513e84c2..0b3e867d0f42 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -48,11 +48,6 @@ public HttpSigningConfiguration() /// public string KeyFilePath { get; set; } - /// - /// Specify the API key in the form of a string, either configure the KeyString property or configure the KeyFilePath property. - /// - public string KeyString { get; set; } - /// /// Gets the key pass phrase for password protected key /// @@ -117,17 +112,6 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m //the list of signed headers and a base64-encoded signature. const string HEADER_AUTHORIZATION = "Authorization"; - //Read the api key from the file - if(!string.IsNullOrEmpty(this.KeyFilePath)) - { - this.KeyString = ReadApiKeyFromFile(KeyFilePath); - } - - if(string.IsNullOrEmpty(KeyString)) - { - throw new Exception("No API key has been provided."); - } - //Hash table to store singed headers var HttpSignedRequestHeader = new Dictionary(); var HttpSignatureHeader = new Dictionary(); @@ -266,7 +250,7 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m var headerValuesString = string.Join("\n", headerValuesList); var signatureStringHash = GetStringHash(HashAlgorithm.ToString(), headerValuesString); string headerSignatureStr = null; - var keyType = GetKeyType(KeyString); + var keyType = GetKeyType(KeyFilePath); if (keyType == PrivateKeyType.RSA) { @@ -317,7 +301,7 @@ private int GetUnixTime(DateTime date2) private string GetRSASignature(byte[] stringToSign) { - RSA rsa = GetRSAProviderFromPemFile(KeyString, KeyPassPhrase); + RSA rsa = GetRSAProviderFromPemFile(KeyFilePath, KeyPassPhrase); if (SigningAlgorithm == "RSASSA-PSS") { var signedbytes = rsa.SignHash(stringToSign, HashAlgorithm, RSASignaturePadding.Pss); @@ -341,7 +325,16 @@ private string GetRSASignature(byte[] stringToSign) /// ECDSA signature private string GetECDSASignature(byte[] dataToSign) { - var keyStr = KeyString; + string keyStr = string.Empty; + if (File.Exists(KeyFilePath)) + { + keyStr = File.ReadAllText(KeyFilePath); + } + else + { + keyStr = KeyFilePath; + } + const string ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----"; const string ecKeyFooter = "-----END EC PRIVATE KEY-----"; var ecKeyBase64String = keyStr.Replace(ecKeyHeader, "").Replace(ecKeyFooter, "").Trim(); @@ -434,13 +427,22 @@ private byte[] ConvertToECDSAANS1Format(byte[] signedBytes) return derBytes.ToArray(); } - private RSACryptoServiceProvider GetRSAProviderFromPemFile(string keyString, SecureString keyPassPhrase = null) + private RSACryptoServiceProvider GetRSAProviderFromPemFile(string pemfile, SecureString keyPassPhrase = null) { const string pempubheader = "-----BEGIN PUBLIC KEY-----"; const string pempubfooter = "-----END PUBLIC KEY-----"; bool isPrivateKeyFile = true; byte[] pemkey = null; - string pemstr = keyString; + + string pemstr = string.Empty; + if (File.Exists(pemfile)) + { + pemstr = File.ReadAllText(pemfile).Trim(); + } + else + { + pemstr = pemfile; + } if (pemstr.StartsWith(pempubheader) && pemstr.EndsWith(pempubfooter)) { @@ -727,15 +729,20 @@ private byte[] DecryptKey(byte[] cipherData, byte[] desKey, byte[] IV) /// /// Detect the key type from the pem file. /// - /// api key in string format + /// key file path in pem format /// Private Key Type - private PrivateKeyType GetKeyType(string keyString) + private PrivateKeyType GetKeyType(string keyFilePath) { string[] key = null; - if (string.IsNullOrEmpty(keyString)) + if (File.Exists(keyFilePath)) + { + key = File.ReadAllLines(keyFilePath); + } + else { - throw new Exception("No API key has been provided."); + // The ApiKeyFilePath is passed as string + key = new string[] { keyFilePath }; } const string ecPrivateKeyHeader = "BEGIN EC PRIVATE KEY"; @@ -745,7 +752,6 @@ private PrivateKeyType GetKeyType(string keyString) //var pkcs8Header = "BEGIN PRIVATE KEY"; //var pkcs8Footer = "END PRIVATE KEY"; PrivateKeyType keyType; - key = KeyString.TrimEnd().Split("\n"); if (key[0].Contains(rsaPrivateKeyHeader) && key[key.Length - 1].ToString().Contains(rsaPrivateFooter)) @@ -763,29 +769,6 @@ private PrivateKeyType GetKeyType(string keyString) } return keyType; } - - /// - /// Read the api key form the api key file path and stored it in KeyString property. - /// - /// api key file path - private string ReadApiKeyFromFile(string apiKeyFilePath) - { - string apiKeyString = null; - if(!string.IsNullOrEmpty(apiKeyFilePath) && !string.IsNullOrEmpty(this.KeyString)) - { - throw new Exception("Configure either the KeyFilePath or configure the KeyString property."); - } - - if(File.Exists(apiKeyFilePath)) - { - apiKeyString = File.ReadAllText(apiKeyFilePath); - } - else - { - throw new Exception("Provided API key file path does not exists."); - } - return apiKeyString; - } #endregion } } diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient-net47/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient-net47/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 265b513e84c2..0b3e867d0f42 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient-net47/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient-net47/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -48,11 +48,6 @@ public HttpSigningConfiguration() /// public string KeyFilePath { get; set; } - /// - /// Specify the API key in the form of a string, either configure the KeyString property or configure the KeyFilePath property. - /// - public string KeyString { get; set; } - /// /// Gets the key pass phrase for password protected key /// @@ -117,17 +112,6 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m //the list of signed headers and a base64-encoded signature. const string HEADER_AUTHORIZATION = "Authorization"; - //Read the api key from the file - if(!string.IsNullOrEmpty(this.KeyFilePath)) - { - this.KeyString = ReadApiKeyFromFile(KeyFilePath); - } - - if(string.IsNullOrEmpty(KeyString)) - { - throw new Exception("No API key has been provided."); - } - //Hash table to store singed headers var HttpSignedRequestHeader = new Dictionary(); var HttpSignatureHeader = new Dictionary(); @@ -266,7 +250,7 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m var headerValuesString = string.Join("\n", headerValuesList); var signatureStringHash = GetStringHash(HashAlgorithm.ToString(), headerValuesString); string headerSignatureStr = null; - var keyType = GetKeyType(KeyString); + var keyType = GetKeyType(KeyFilePath); if (keyType == PrivateKeyType.RSA) { @@ -317,7 +301,7 @@ private int GetUnixTime(DateTime date2) private string GetRSASignature(byte[] stringToSign) { - RSA rsa = GetRSAProviderFromPemFile(KeyString, KeyPassPhrase); + RSA rsa = GetRSAProviderFromPemFile(KeyFilePath, KeyPassPhrase); if (SigningAlgorithm == "RSASSA-PSS") { var signedbytes = rsa.SignHash(stringToSign, HashAlgorithm, RSASignaturePadding.Pss); @@ -341,7 +325,16 @@ private string GetRSASignature(byte[] stringToSign) /// ECDSA signature private string GetECDSASignature(byte[] dataToSign) { - var keyStr = KeyString; + string keyStr = string.Empty; + if (File.Exists(KeyFilePath)) + { + keyStr = File.ReadAllText(KeyFilePath); + } + else + { + keyStr = KeyFilePath; + } + const string ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----"; const string ecKeyFooter = "-----END EC PRIVATE KEY-----"; var ecKeyBase64String = keyStr.Replace(ecKeyHeader, "").Replace(ecKeyFooter, "").Trim(); @@ -434,13 +427,22 @@ private byte[] ConvertToECDSAANS1Format(byte[] signedBytes) return derBytes.ToArray(); } - private RSACryptoServiceProvider GetRSAProviderFromPemFile(string keyString, SecureString keyPassPhrase = null) + private RSACryptoServiceProvider GetRSAProviderFromPemFile(string pemfile, SecureString keyPassPhrase = null) { const string pempubheader = "-----BEGIN PUBLIC KEY-----"; const string pempubfooter = "-----END PUBLIC KEY-----"; bool isPrivateKeyFile = true; byte[] pemkey = null; - string pemstr = keyString; + + string pemstr = string.Empty; + if (File.Exists(pemfile)) + { + pemstr = File.ReadAllText(pemfile).Trim(); + } + else + { + pemstr = pemfile; + } if (pemstr.StartsWith(pempubheader) && pemstr.EndsWith(pempubfooter)) { @@ -727,15 +729,20 @@ private byte[] DecryptKey(byte[] cipherData, byte[] desKey, byte[] IV) /// /// Detect the key type from the pem file. /// - /// api key in string format + /// key file path in pem format /// Private Key Type - private PrivateKeyType GetKeyType(string keyString) + private PrivateKeyType GetKeyType(string keyFilePath) { string[] key = null; - if (string.IsNullOrEmpty(keyString)) + if (File.Exists(keyFilePath)) + { + key = File.ReadAllLines(keyFilePath); + } + else { - throw new Exception("No API key has been provided."); + // The ApiKeyFilePath is passed as string + key = new string[] { keyFilePath }; } const string ecPrivateKeyHeader = "BEGIN EC PRIVATE KEY"; @@ -745,7 +752,6 @@ private PrivateKeyType GetKeyType(string keyString) //var pkcs8Header = "BEGIN PRIVATE KEY"; //var pkcs8Footer = "END PRIVATE KEY"; PrivateKeyType keyType; - key = KeyString.TrimEnd().Split("\n"); if (key[0].Contains(rsaPrivateKeyHeader) && key[key.Length - 1].ToString().Contains(rsaPrivateFooter)) @@ -763,29 +769,6 @@ private PrivateKeyType GetKeyType(string keyString) } return keyType; } - - /// - /// Read the api key form the api key file path and stored it in KeyString property. - /// - /// api key file path - private string ReadApiKeyFromFile(string apiKeyFilePath) - { - string apiKeyString = null; - if(!string.IsNullOrEmpty(apiKeyFilePath) && !string.IsNullOrEmpty(this.KeyString)) - { - throw new Exception("Configure either the KeyFilePath or configure the KeyString property."); - } - - if(File.Exists(apiKeyFilePath)) - { - apiKeyString = File.ReadAllText(apiKeyFilePath); - } - else - { - throw new Exception("Provided API key file path does not exists."); - } - return apiKeyString; - } #endregion } } diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient-net48/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient-net48/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 265b513e84c2..0b3e867d0f42 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient-net48/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient-net48/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -48,11 +48,6 @@ public HttpSigningConfiguration() /// public string KeyFilePath { get; set; } - /// - /// Specify the API key in the form of a string, either configure the KeyString property or configure the KeyFilePath property. - /// - public string KeyString { get; set; } - /// /// Gets the key pass phrase for password protected key /// @@ -117,17 +112,6 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m //the list of signed headers and a base64-encoded signature. const string HEADER_AUTHORIZATION = "Authorization"; - //Read the api key from the file - if(!string.IsNullOrEmpty(this.KeyFilePath)) - { - this.KeyString = ReadApiKeyFromFile(KeyFilePath); - } - - if(string.IsNullOrEmpty(KeyString)) - { - throw new Exception("No API key has been provided."); - } - //Hash table to store singed headers var HttpSignedRequestHeader = new Dictionary(); var HttpSignatureHeader = new Dictionary(); @@ -266,7 +250,7 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m var headerValuesString = string.Join("\n", headerValuesList); var signatureStringHash = GetStringHash(HashAlgorithm.ToString(), headerValuesString); string headerSignatureStr = null; - var keyType = GetKeyType(KeyString); + var keyType = GetKeyType(KeyFilePath); if (keyType == PrivateKeyType.RSA) { @@ -317,7 +301,7 @@ private int GetUnixTime(DateTime date2) private string GetRSASignature(byte[] stringToSign) { - RSA rsa = GetRSAProviderFromPemFile(KeyString, KeyPassPhrase); + RSA rsa = GetRSAProviderFromPemFile(KeyFilePath, KeyPassPhrase); if (SigningAlgorithm == "RSASSA-PSS") { var signedbytes = rsa.SignHash(stringToSign, HashAlgorithm, RSASignaturePadding.Pss); @@ -341,7 +325,16 @@ private string GetRSASignature(byte[] stringToSign) /// ECDSA signature private string GetECDSASignature(byte[] dataToSign) { - var keyStr = KeyString; + string keyStr = string.Empty; + if (File.Exists(KeyFilePath)) + { + keyStr = File.ReadAllText(KeyFilePath); + } + else + { + keyStr = KeyFilePath; + } + const string ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----"; const string ecKeyFooter = "-----END EC PRIVATE KEY-----"; var ecKeyBase64String = keyStr.Replace(ecKeyHeader, "").Replace(ecKeyFooter, "").Trim(); @@ -434,13 +427,22 @@ private byte[] ConvertToECDSAANS1Format(byte[] signedBytes) return derBytes.ToArray(); } - private RSACryptoServiceProvider GetRSAProviderFromPemFile(string keyString, SecureString keyPassPhrase = null) + private RSACryptoServiceProvider GetRSAProviderFromPemFile(string pemfile, SecureString keyPassPhrase = null) { const string pempubheader = "-----BEGIN PUBLIC KEY-----"; const string pempubfooter = "-----END PUBLIC KEY-----"; bool isPrivateKeyFile = true; byte[] pemkey = null; - string pemstr = keyString; + + string pemstr = string.Empty; + if (File.Exists(pemfile)) + { + pemstr = File.ReadAllText(pemfile).Trim(); + } + else + { + pemstr = pemfile; + } if (pemstr.StartsWith(pempubheader) && pemstr.EndsWith(pempubfooter)) { @@ -727,15 +729,20 @@ private byte[] DecryptKey(byte[] cipherData, byte[] desKey, byte[] IV) /// /// Detect the key type from the pem file. /// - /// api key in string format + /// key file path in pem format /// Private Key Type - private PrivateKeyType GetKeyType(string keyString) + private PrivateKeyType GetKeyType(string keyFilePath) { string[] key = null; - if (string.IsNullOrEmpty(keyString)) + if (File.Exists(keyFilePath)) + { + key = File.ReadAllLines(keyFilePath); + } + else { - throw new Exception("No API key has been provided."); + // The ApiKeyFilePath is passed as string + key = new string[] { keyFilePath }; } const string ecPrivateKeyHeader = "BEGIN EC PRIVATE KEY"; @@ -745,7 +752,6 @@ private PrivateKeyType GetKeyType(string keyString) //var pkcs8Header = "BEGIN PRIVATE KEY"; //var pkcs8Footer = "END PRIVATE KEY"; PrivateKeyType keyType; - key = KeyString.TrimEnd().Split("\n"); if (key[0].Contains(rsaPrivateKeyHeader) && key[key.Length - 1].ToString().Contains(rsaPrivateFooter)) @@ -763,29 +769,6 @@ private PrivateKeyType GetKeyType(string keyString) } return keyType; } - - /// - /// Read the api key form the api key file path and stored it in KeyString property. - /// - /// api key file path - private string ReadApiKeyFromFile(string apiKeyFilePath) - { - string apiKeyString = null; - if(!string.IsNullOrEmpty(apiKeyFilePath) && !string.IsNullOrEmpty(this.KeyString)) - { - throw new Exception("Configure either the KeyFilePath or configure the KeyString property."); - } - - if(File.Exists(apiKeyFilePath)) - { - apiKeyString = File.ReadAllText(apiKeyFilePath); - } - else - { - throw new Exception("Provided API key file path does not exists."); - } - return apiKeyString; - } #endregion } } diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient-net5.0/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient-net5.0/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 265b513e84c2..0b3e867d0f42 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient-net5.0/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient-net5.0/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -48,11 +48,6 @@ public HttpSigningConfiguration() /// public string KeyFilePath { get; set; } - /// - /// Specify the API key in the form of a string, either configure the KeyString property or configure the KeyFilePath property. - /// - public string KeyString { get; set; } - /// /// Gets the key pass phrase for password protected key /// @@ -117,17 +112,6 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m //the list of signed headers and a base64-encoded signature. const string HEADER_AUTHORIZATION = "Authorization"; - //Read the api key from the file - if(!string.IsNullOrEmpty(this.KeyFilePath)) - { - this.KeyString = ReadApiKeyFromFile(KeyFilePath); - } - - if(string.IsNullOrEmpty(KeyString)) - { - throw new Exception("No API key has been provided."); - } - //Hash table to store singed headers var HttpSignedRequestHeader = new Dictionary(); var HttpSignatureHeader = new Dictionary(); @@ -266,7 +250,7 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m var headerValuesString = string.Join("\n", headerValuesList); var signatureStringHash = GetStringHash(HashAlgorithm.ToString(), headerValuesString); string headerSignatureStr = null; - var keyType = GetKeyType(KeyString); + var keyType = GetKeyType(KeyFilePath); if (keyType == PrivateKeyType.RSA) { @@ -317,7 +301,7 @@ private int GetUnixTime(DateTime date2) private string GetRSASignature(byte[] stringToSign) { - RSA rsa = GetRSAProviderFromPemFile(KeyString, KeyPassPhrase); + RSA rsa = GetRSAProviderFromPemFile(KeyFilePath, KeyPassPhrase); if (SigningAlgorithm == "RSASSA-PSS") { var signedbytes = rsa.SignHash(stringToSign, HashAlgorithm, RSASignaturePadding.Pss); @@ -341,7 +325,16 @@ private string GetRSASignature(byte[] stringToSign) /// ECDSA signature private string GetECDSASignature(byte[] dataToSign) { - var keyStr = KeyString; + string keyStr = string.Empty; + if (File.Exists(KeyFilePath)) + { + keyStr = File.ReadAllText(KeyFilePath); + } + else + { + keyStr = KeyFilePath; + } + const string ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----"; const string ecKeyFooter = "-----END EC PRIVATE KEY-----"; var ecKeyBase64String = keyStr.Replace(ecKeyHeader, "").Replace(ecKeyFooter, "").Trim(); @@ -434,13 +427,22 @@ private byte[] ConvertToECDSAANS1Format(byte[] signedBytes) return derBytes.ToArray(); } - private RSACryptoServiceProvider GetRSAProviderFromPemFile(string keyString, SecureString keyPassPhrase = null) + private RSACryptoServiceProvider GetRSAProviderFromPemFile(string pemfile, SecureString keyPassPhrase = null) { const string pempubheader = "-----BEGIN PUBLIC KEY-----"; const string pempubfooter = "-----END PUBLIC KEY-----"; bool isPrivateKeyFile = true; byte[] pemkey = null; - string pemstr = keyString; + + string pemstr = string.Empty; + if (File.Exists(pemfile)) + { + pemstr = File.ReadAllText(pemfile).Trim(); + } + else + { + pemstr = pemfile; + } if (pemstr.StartsWith(pempubheader) && pemstr.EndsWith(pempubfooter)) { @@ -727,15 +729,20 @@ private byte[] DecryptKey(byte[] cipherData, byte[] desKey, byte[] IV) /// /// Detect the key type from the pem file. /// - /// api key in string format + /// key file path in pem format /// Private Key Type - private PrivateKeyType GetKeyType(string keyString) + private PrivateKeyType GetKeyType(string keyFilePath) { string[] key = null; - if (string.IsNullOrEmpty(keyString)) + if (File.Exists(keyFilePath)) + { + key = File.ReadAllLines(keyFilePath); + } + else { - throw new Exception("No API key has been provided."); + // The ApiKeyFilePath is passed as string + key = new string[] { keyFilePath }; } const string ecPrivateKeyHeader = "BEGIN EC PRIVATE KEY"; @@ -745,7 +752,6 @@ private PrivateKeyType GetKeyType(string keyString) //var pkcs8Header = "BEGIN PRIVATE KEY"; //var pkcs8Footer = "END PRIVATE KEY"; PrivateKeyType keyType; - key = KeyString.TrimEnd().Split("\n"); if (key[0].Contains(rsaPrivateKeyHeader) && key[key.Length - 1].ToString().Contains(rsaPrivateFooter)) @@ -763,29 +769,6 @@ private PrivateKeyType GetKeyType(string keyString) } return keyType; } - - /// - /// Read the api key form the api key file path and stored it in KeyString property. - /// - /// api key file path - private string ReadApiKeyFromFile(string apiKeyFilePath) - { - string apiKeyString = null; - if(!string.IsNullOrEmpty(apiKeyFilePath) && !string.IsNullOrEmpty(this.KeyString)) - { - throw new Exception("Configure either the KeyFilePath or configure the KeyString property."); - } - - if(File.Exists(apiKeyFilePath)) - { - apiKeyString = File.ReadAllText(apiKeyFilePath); - } - else - { - throw new Exception("Provided API key file path does not exists."); - } - return apiKeyString; - } #endregion } } diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient-unityWebRequest/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient-unityWebRequest/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 265b513e84c2..0b3e867d0f42 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient-unityWebRequest/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient-unityWebRequest/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -48,11 +48,6 @@ public HttpSigningConfiguration() /// public string KeyFilePath { get; set; } - /// - /// Specify the API key in the form of a string, either configure the KeyString property or configure the KeyFilePath property. - /// - public string KeyString { get; set; } - /// /// Gets the key pass phrase for password protected key /// @@ -117,17 +112,6 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m //the list of signed headers and a base64-encoded signature. const string HEADER_AUTHORIZATION = "Authorization"; - //Read the api key from the file - if(!string.IsNullOrEmpty(this.KeyFilePath)) - { - this.KeyString = ReadApiKeyFromFile(KeyFilePath); - } - - if(string.IsNullOrEmpty(KeyString)) - { - throw new Exception("No API key has been provided."); - } - //Hash table to store singed headers var HttpSignedRequestHeader = new Dictionary(); var HttpSignatureHeader = new Dictionary(); @@ -266,7 +250,7 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m var headerValuesString = string.Join("\n", headerValuesList); var signatureStringHash = GetStringHash(HashAlgorithm.ToString(), headerValuesString); string headerSignatureStr = null; - var keyType = GetKeyType(KeyString); + var keyType = GetKeyType(KeyFilePath); if (keyType == PrivateKeyType.RSA) { @@ -317,7 +301,7 @@ private int GetUnixTime(DateTime date2) private string GetRSASignature(byte[] stringToSign) { - RSA rsa = GetRSAProviderFromPemFile(KeyString, KeyPassPhrase); + RSA rsa = GetRSAProviderFromPemFile(KeyFilePath, KeyPassPhrase); if (SigningAlgorithm == "RSASSA-PSS") { var signedbytes = rsa.SignHash(stringToSign, HashAlgorithm, RSASignaturePadding.Pss); @@ -341,7 +325,16 @@ private string GetRSASignature(byte[] stringToSign) /// ECDSA signature private string GetECDSASignature(byte[] dataToSign) { - var keyStr = KeyString; + string keyStr = string.Empty; + if (File.Exists(KeyFilePath)) + { + keyStr = File.ReadAllText(KeyFilePath); + } + else + { + keyStr = KeyFilePath; + } + const string ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----"; const string ecKeyFooter = "-----END EC PRIVATE KEY-----"; var ecKeyBase64String = keyStr.Replace(ecKeyHeader, "").Replace(ecKeyFooter, "").Trim(); @@ -434,13 +427,22 @@ private byte[] ConvertToECDSAANS1Format(byte[] signedBytes) return derBytes.ToArray(); } - private RSACryptoServiceProvider GetRSAProviderFromPemFile(string keyString, SecureString keyPassPhrase = null) + private RSACryptoServiceProvider GetRSAProviderFromPemFile(string pemfile, SecureString keyPassPhrase = null) { const string pempubheader = "-----BEGIN PUBLIC KEY-----"; const string pempubfooter = "-----END PUBLIC KEY-----"; bool isPrivateKeyFile = true; byte[] pemkey = null; - string pemstr = keyString; + + string pemstr = string.Empty; + if (File.Exists(pemfile)) + { + pemstr = File.ReadAllText(pemfile).Trim(); + } + else + { + pemstr = pemfile; + } if (pemstr.StartsWith(pempubheader) && pemstr.EndsWith(pempubfooter)) { @@ -727,15 +729,20 @@ private byte[] DecryptKey(byte[] cipherData, byte[] desKey, byte[] IV) /// /// Detect the key type from the pem file. /// - /// api key in string format + /// key file path in pem format /// Private Key Type - private PrivateKeyType GetKeyType(string keyString) + private PrivateKeyType GetKeyType(string keyFilePath) { string[] key = null; - if (string.IsNullOrEmpty(keyString)) + if (File.Exists(keyFilePath)) + { + key = File.ReadAllLines(keyFilePath); + } + else { - throw new Exception("No API key has been provided."); + // The ApiKeyFilePath is passed as string + key = new string[] { keyFilePath }; } const string ecPrivateKeyHeader = "BEGIN EC PRIVATE KEY"; @@ -745,7 +752,6 @@ private PrivateKeyType GetKeyType(string keyString) //var pkcs8Header = "BEGIN PRIVATE KEY"; //var pkcs8Footer = "END PRIVATE KEY"; PrivateKeyType keyType; - key = KeyString.TrimEnd().Split("\n"); if (key[0].Contains(rsaPrivateKeyHeader) && key[key.Length - 1].ToString().Contains(rsaPrivateFooter)) @@ -763,29 +769,6 @@ private PrivateKeyType GetKeyType(string keyString) } return keyType; } - - /// - /// Read the api key form the api key file path and stored it in KeyString property. - /// - /// api key file path - private string ReadApiKeyFromFile(string apiKeyFilePath) - { - string apiKeyString = null; - if(!string.IsNullOrEmpty(apiKeyFilePath) && !string.IsNullOrEmpty(this.KeyString)) - { - throw new Exception("Configure either the KeyFilePath or configure the KeyString property."); - } - - if(File.Exists(apiKeyFilePath)) - { - apiKeyString = File.ReadAllText(apiKeyFilePath); - } - else - { - throw new Exception("Provided API key file path does not exists."); - } - return apiKeyString; - } #endregion } } diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 265b513e84c2..0b3e867d0f42 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -48,11 +48,6 @@ public HttpSigningConfiguration() /// public string KeyFilePath { get; set; } - /// - /// Specify the API key in the form of a string, either configure the KeyString property or configure the KeyFilePath property. - /// - public string KeyString { get; set; } - /// /// Gets the key pass phrase for password protected key /// @@ -117,17 +112,6 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m //the list of signed headers and a base64-encoded signature. const string HEADER_AUTHORIZATION = "Authorization"; - //Read the api key from the file - if(!string.IsNullOrEmpty(this.KeyFilePath)) - { - this.KeyString = ReadApiKeyFromFile(KeyFilePath); - } - - if(string.IsNullOrEmpty(KeyString)) - { - throw new Exception("No API key has been provided."); - } - //Hash table to store singed headers var HttpSignedRequestHeader = new Dictionary(); var HttpSignatureHeader = new Dictionary(); @@ -266,7 +250,7 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m var headerValuesString = string.Join("\n", headerValuesList); var signatureStringHash = GetStringHash(HashAlgorithm.ToString(), headerValuesString); string headerSignatureStr = null; - var keyType = GetKeyType(KeyString); + var keyType = GetKeyType(KeyFilePath); if (keyType == PrivateKeyType.RSA) { @@ -317,7 +301,7 @@ private int GetUnixTime(DateTime date2) private string GetRSASignature(byte[] stringToSign) { - RSA rsa = GetRSAProviderFromPemFile(KeyString, KeyPassPhrase); + RSA rsa = GetRSAProviderFromPemFile(KeyFilePath, KeyPassPhrase); if (SigningAlgorithm == "RSASSA-PSS") { var signedbytes = rsa.SignHash(stringToSign, HashAlgorithm, RSASignaturePadding.Pss); @@ -341,7 +325,16 @@ private string GetRSASignature(byte[] stringToSign) /// ECDSA signature private string GetECDSASignature(byte[] dataToSign) { - var keyStr = KeyString; + string keyStr = string.Empty; + if (File.Exists(KeyFilePath)) + { + keyStr = File.ReadAllText(KeyFilePath); + } + else + { + keyStr = KeyFilePath; + } + const string ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----"; const string ecKeyFooter = "-----END EC PRIVATE KEY-----"; var ecKeyBase64String = keyStr.Replace(ecKeyHeader, "").Replace(ecKeyFooter, "").Trim(); @@ -434,13 +427,22 @@ private byte[] ConvertToECDSAANS1Format(byte[] signedBytes) return derBytes.ToArray(); } - private RSACryptoServiceProvider GetRSAProviderFromPemFile(string keyString, SecureString keyPassPhrase = null) + private RSACryptoServiceProvider GetRSAProviderFromPemFile(string pemfile, SecureString keyPassPhrase = null) { const string pempubheader = "-----BEGIN PUBLIC KEY-----"; const string pempubfooter = "-----END PUBLIC KEY-----"; bool isPrivateKeyFile = true; byte[] pemkey = null; - string pemstr = keyString; + + string pemstr = string.Empty; + if (File.Exists(pemfile)) + { + pemstr = File.ReadAllText(pemfile).Trim(); + } + else + { + pemstr = pemfile; + } if (pemstr.StartsWith(pempubheader) && pemstr.EndsWith(pempubfooter)) { @@ -727,15 +729,20 @@ private byte[] DecryptKey(byte[] cipherData, byte[] desKey, byte[] IV) /// /// Detect the key type from the pem file. /// - /// api key in string format + /// key file path in pem format /// Private Key Type - private PrivateKeyType GetKeyType(string keyString) + private PrivateKeyType GetKeyType(string keyFilePath) { string[] key = null; - if (string.IsNullOrEmpty(keyString)) + if (File.Exists(keyFilePath)) + { + key = File.ReadAllLines(keyFilePath); + } + else { - throw new Exception("No API key has been provided."); + // The ApiKeyFilePath is passed as string + key = new string[] { keyFilePath }; } const string ecPrivateKeyHeader = "BEGIN EC PRIVATE KEY"; @@ -745,7 +752,6 @@ private PrivateKeyType GetKeyType(string keyString) //var pkcs8Header = "BEGIN PRIVATE KEY"; //var pkcs8Footer = "END PRIVATE KEY"; PrivateKeyType keyType; - key = KeyString.TrimEnd().Split("\n"); if (key[0].Contains(rsaPrivateKeyHeader) && key[key.Length - 1].ToString().Contains(rsaPrivateFooter)) @@ -763,29 +769,6 @@ private PrivateKeyType GetKeyType(string keyString) } return keyType; } - - /// - /// Read the api key form the api key file path and stored it in KeyString property. - /// - /// api key file path - private string ReadApiKeyFromFile(string apiKeyFilePath) - { - string apiKeyString = null; - if(!string.IsNullOrEmpty(apiKeyFilePath) && !string.IsNullOrEmpty(this.KeyString)) - { - throw new Exception("Configure either the KeyFilePath or configure the KeyString property."); - } - - if(File.Exists(apiKeyFilePath)) - { - apiKeyString = File.ReadAllText(apiKeyFilePath); - } - else - { - throw new Exception("Provided API key file path does not exists."); - } - return apiKeyString; - } #endregion } } diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 265b513e84c2..0b3e867d0f42 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -48,11 +48,6 @@ public HttpSigningConfiguration() /// public string KeyFilePath { get; set; } - /// - /// Specify the API key in the form of a string, either configure the KeyString property or configure the KeyFilePath property. - /// - public string KeyString { get; set; } - /// /// Gets the key pass phrase for password protected key /// @@ -117,17 +112,6 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m //the list of signed headers and a base64-encoded signature. const string HEADER_AUTHORIZATION = "Authorization"; - //Read the api key from the file - if(!string.IsNullOrEmpty(this.KeyFilePath)) - { - this.KeyString = ReadApiKeyFromFile(KeyFilePath); - } - - if(string.IsNullOrEmpty(KeyString)) - { - throw new Exception("No API key has been provided."); - } - //Hash table to store singed headers var HttpSignedRequestHeader = new Dictionary(); var HttpSignatureHeader = new Dictionary(); @@ -266,7 +250,7 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m var headerValuesString = string.Join("\n", headerValuesList); var signatureStringHash = GetStringHash(HashAlgorithm.ToString(), headerValuesString); string headerSignatureStr = null; - var keyType = GetKeyType(KeyString); + var keyType = GetKeyType(KeyFilePath); if (keyType == PrivateKeyType.RSA) { @@ -317,7 +301,7 @@ private int GetUnixTime(DateTime date2) private string GetRSASignature(byte[] stringToSign) { - RSA rsa = GetRSAProviderFromPemFile(KeyString, KeyPassPhrase); + RSA rsa = GetRSAProviderFromPemFile(KeyFilePath, KeyPassPhrase); if (SigningAlgorithm == "RSASSA-PSS") { var signedbytes = rsa.SignHash(stringToSign, HashAlgorithm, RSASignaturePadding.Pss); @@ -341,7 +325,16 @@ private string GetRSASignature(byte[] stringToSign) /// ECDSA signature private string GetECDSASignature(byte[] dataToSign) { - var keyStr = KeyString; + string keyStr = string.Empty; + if (File.Exists(KeyFilePath)) + { + keyStr = File.ReadAllText(KeyFilePath); + } + else + { + keyStr = KeyFilePath; + } + const string ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----"; const string ecKeyFooter = "-----END EC PRIVATE KEY-----"; var ecKeyBase64String = keyStr.Replace(ecKeyHeader, "").Replace(ecKeyFooter, "").Trim(); @@ -434,13 +427,22 @@ private byte[] ConvertToECDSAANS1Format(byte[] signedBytes) return derBytes.ToArray(); } - private RSACryptoServiceProvider GetRSAProviderFromPemFile(string keyString, SecureString keyPassPhrase = null) + private RSACryptoServiceProvider GetRSAProviderFromPemFile(string pemfile, SecureString keyPassPhrase = null) { const string pempubheader = "-----BEGIN PUBLIC KEY-----"; const string pempubfooter = "-----END PUBLIC KEY-----"; bool isPrivateKeyFile = true; byte[] pemkey = null; - string pemstr = keyString; + + string pemstr = string.Empty; + if (File.Exists(pemfile)) + { + pemstr = File.ReadAllText(pemfile).Trim(); + } + else + { + pemstr = pemfile; + } if (pemstr.StartsWith(pempubheader) && pemstr.EndsWith(pempubfooter)) { @@ -727,15 +729,20 @@ private byte[] DecryptKey(byte[] cipherData, byte[] desKey, byte[] IV) /// /// Detect the key type from the pem file. /// - /// api key in string format + /// key file path in pem format /// Private Key Type - private PrivateKeyType GetKeyType(string keyString) + private PrivateKeyType GetKeyType(string keyFilePath) { string[] key = null; - if (string.IsNullOrEmpty(keyString)) + if (File.Exists(keyFilePath)) + { + key = File.ReadAllLines(keyFilePath); + } + else { - throw new Exception("No API key has been provided."); + // The ApiKeyFilePath is passed as string + key = new string[] { keyFilePath }; } const string ecPrivateKeyHeader = "BEGIN EC PRIVATE KEY"; @@ -745,7 +752,6 @@ private PrivateKeyType GetKeyType(string keyString) //var pkcs8Header = "BEGIN PRIVATE KEY"; //var pkcs8Footer = "END PRIVATE KEY"; PrivateKeyType keyType; - key = KeyString.TrimEnd().Split("\n"); if (key[0].Contains(rsaPrivateKeyHeader) && key[key.Length - 1].ToString().Contains(rsaPrivateFooter)) @@ -763,29 +769,6 @@ private PrivateKeyType GetKeyType(string keyString) } return keyType; } - - /// - /// Read the api key form the api key file path and stored it in KeyString property. - /// - /// api key file path - private string ReadApiKeyFromFile(string apiKeyFilePath) - { - string apiKeyString = null; - if(!string.IsNullOrEmpty(apiKeyFilePath) && !string.IsNullOrEmpty(this.KeyString)) - { - throw new Exception("Configure either the KeyFilePath or configure the KeyString property."); - } - - if(File.Exists(apiKeyFilePath)) - { - apiKeyString = File.ReadAllText(apiKeyFilePath); - } - else - { - throw new Exception("Provided API key file path does not exists."); - } - return apiKeyString; - } #endregion } } From ec6404d511d20b2847dfd974232c2c68eab24daa Mon Sep 17 00:00:00 2001 From: Ghufran Zahidi <18732053+Ghufz@users.noreply.github.com> Date: Tue, 13 Jun 2023 14:36:25 +0530 Subject: [PATCH 5/7] fix the sample code compilation error for split function. --- .../resources/csharp-netcore/HttpSigningConfiguration.mustache | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/openapi-generator/src/main/resources/csharp-netcore/HttpSigningConfiguration.mustache b/modules/openapi-generator/src/main/resources/csharp-netcore/HttpSigningConfiguration.mustache index 50d7bf35ab53..983ab4883404 100644 --- a/modules/openapi-generator/src/main/resources/csharp-netcore/HttpSigningConfiguration.mustache +++ b/modules/openapi-generator/src/main/resources/csharp-netcore/HttpSigningConfiguration.mustache @@ -737,7 +737,7 @@ namespace {{packageName}}.Client //var pkcs8Header = "BEGIN PRIVATE KEY"; //var pkcs8Footer = "END PRIVATE KEY"; PrivateKeyType keyType; - key = KeyString.TrimEnd().Split("\n"); + key = KeyString.TrimEnd().Split('\n'); if (key[0].Contains(rsaPrivateKeyHeader) && key[key.Length - 1].ToString().Contains(rsaPrivateFooter)) From 368a2e4ba3a8fe634254ee63f8f368d3b71da713 Mon Sep 17 00:00:00 2001 From: Ghufran Zahidi <18732053+Ghufz@users.noreply.github.com> Date: Tue, 13 Jun 2023 14:41:05 +0530 Subject: [PATCH 6/7] updated the sample code after the split function fix. --- .../src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs | 2 +- .../src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs | 2 +- .../src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs | 2 +- .../src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs | 2 +- .../src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs | 2 +- .../src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs | 2 +- .../src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs | 2 +- .../src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient-ConditionalSerialization/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient-ConditionalSerialization/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 116c890aee58..790be8d020f0 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient-ConditionalSerialization/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient-ConditionalSerialization/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -745,7 +745,7 @@ private PrivateKeyType GetKeyType(string keyString) //var pkcs8Header = "BEGIN PRIVATE KEY"; //var pkcs8Footer = "END PRIVATE KEY"; PrivateKeyType keyType; - key = KeyString.TrimEnd().Split("\n"); + key = KeyString.TrimEnd().Split('\n'); if (key[0].Contains(rsaPrivateKeyHeader) && key[key.Length - 1].ToString().Contains(rsaPrivateFooter)) diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 116c890aee58..790be8d020f0 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -745,7 +745,7 @@ private PrivateKeyType GetKeyType(string keyString) //var pkcs8Header = "BEGIN PRIVATE KEY"; //var pkcs8Footer = "END PRIVATE KEY"; PrivateKeyType keyType; - key = KeyString.TrimEnd().Split("\n"); + key = KeyString.TrimEnd().Split('\n'); if (key[0].Contains(rsaPrivateKeyHeader) && key[key.Length - 1].ToString().Contains(rsaPrivateFooter)) diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient-net47/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient-net47/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 116c890aee58..790be8d020f0 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient-net47/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient-net47/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -745,7 +745,7 @@ private PrivateKeyType GetKeyType(string keyString) //var pkcs8Header = "BEGIN PRIVATE KEY"; //var pkcs8Footer = "END PRIVATE KEY"; PrivateKeyType keyType; - key = KeyString.TrimEnd().Split("\n"); + key = KeyString.TrimEnd().Split('\n'); if (key[0].Contains(rsaPrivateKeyHeader) && key[key.Length - 1].ToString().Contains(rsaPrivateFooter)) diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient-net48/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient-net48/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 116c890aee58..790be8d020f0 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient-net48/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient-net48/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -745,7 +745,7 @@ private PrivateKeyType GetKeyType(string keyString) //var pkcs8Header = "BEGIN PRIVATE KEY"; //var pkcs8Footer = "END PRIVATE KEY"; PrivateKeyType keyType; - key = KeyString.TrimEnd().Split("\n"); + key = KeyString.TrimEnd().Split('\n'); if (key[0].Contains(rsaPrivateKeyHeader) && key[key.Length - 1].ToString().Contains(rsaPrivateFooter)) diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient-net5.0/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient-net5.0/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 116c890aee58..790be8d020f0 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient-net5.0/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient-net5.0/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -745,7 +745,7 @@ private PrivateKeyType GetKeyType(string keyString) //var pkcs8Header = "BEGIN PRIVATE KEY"; //var pkcs8Footer = "END PRIVATE KEY"; PrivateKeyType keyType; - key = KeyString.TrimEnd().Split("\n"); + key = KeyString.TrimEnd().Split('\n'); if (key[0].Contains(rsaPrivateKeyHeader) && key[key.Length - 1].ToString().Contains(rsaPrivateFooter)) diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient-unityWebRequest/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient-unityWebRequest/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 116c890aee58..790be8d020f0 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient-unityWebRequest/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient-unityWebRequest/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -745,7 +745,7 @@ private PrivateKeyType GetKeyType(string keyString) //var pkcs8Header = "BEGIN PRIVATE KEY"; //var pkcs8Footer = "END PRIVATE KEY"; PrivateKeyType keyType; - key = KeyString.TrimEnd().Split("\n"); + key = KeyString.TrimEnd().Split('\n'); if (key[0].Contains(rsaPrivateKeyHeader) && key[key.Length - 1].ToString().Contains(rsaPrivateFooter)) diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 116c890aee58..790be8d020f0 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -745,7 +745,7 @@ private PrivateKeyType GetKeyType(string keyString) //var pkcs8Header = "BEGIN PRIVATE KEY"; //var pkcs8Footer = "END PRIVATE KEY"; PrivateKeyType keyType; - key = KeyString.TrimEnd().Split("\n"); + key = KeyString.TrimEnd().Split('\n'); if (key[0].Contains(rsaPrivateKeyHeader) && key[key.Length - 1].ToString().Contains(rsaPrivateFooter)) diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 116c890aee58..790be8d020f0 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -745,7 +745,7 @@ private PrivateKeyType GetKeyType(string keyString) //var pkcs8Header = "BEGIN PRIVATE KEY"; //var pkcs8Footer = "END PRIVATE KEY"; PrivateKeyType keyType; - key = KeyString.TrimEnd().Split("\n"); + key = KeyString.TrimEnd().Split('\n'); if (key[0].Contains(rsaPrivateKeyHeader) && key[key.Length - 1].ToString().Contains(rsaPrivateFooter)) From a9a585649722b444baabf3e59985639939a8080f Mon Sep 17 00:00:00 2001 From: Ghufran Zahidi <18732053+Ghufz@users.noreply.github.com> Date: Tue, 13 Jun 2023 17:56:26 +0530 Subject: [PATCH 7/7] Removed the either or check for filePath or KeyString. --- .../csharp-netcore/HttpSigningConfiguration.mustache | 8 ++------ .../Org.OpenAPITools/Client/HttpSigningConfiguration.cs | 8 ++------ .../Org.OpenAPITools/Client/HttpSigningConfiguration.cs | 8 ++------ .../Org.OpenAPITools/Client/HttpSigningConfiguration.cs | 8 ++------ .../Org.OpenAPITools/Client/HttpSigningConfiguration.cs | 8 ++------ .../Org.OpenAPITools/Client/HttpSigningConfiguration.cs | 8 ++------ .../Org.OpenAPITools/Client/HttpSigningConfiguration.cs | 8 ++------ .../Org.OpenAPITools/Client/HttpSigningConfiguration.cs | 8 ++------ .../Org.OpenAPITools/Client/HttpSigningConfiguration.cs | 8 ++------ 9 files changed, 18 insertions(+), 54 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/csharp-netcore/HttpSigningConfiguration.mustache b/modules/openapi-generator/src/main/resources/csharp-netcore/HttpSigningConfiguration.mustache index 983ab4883404..a1169150ea22 100644 --- a/modules/openapi-generator/src/main/resources/csharp-netcore/HttpSigningConfiguration.mustache +++ b/modules/openapi-generator/src/main/resources/csharp-netcore/HttpSigningConfiguration.mustache @@ -110,7 +110,7 @@ namespace {{packageName}}.Client const string HEADER_AUTHORIZATION = "Authorization"; //Read the api key from the file - if(!string.IsNullOrEmpty(this.KeyFilePath)) + if(string.IsNullOrEmpty(this.KeyString)) { this.KeyString = ReadApiKeyFromFile(KeyFilePath); } @@ -763,11 +763,7 @@ namespace {{packageName}}.Client private string ReadApiKeyFromFile(string apiKeyFilePath) { string apiKeyString = null; - if(!string.IsNullOrEmpty(apiKeyFilePath) && !string.IsNullOrEmpty(this.KeyString)) - { - throw new Exception("Configure either the KeyFilePath or configure the KeyString property."); - } - + if(File.Exists(apiKeyFilePath)) { apiKeyString = File.ReadAllText(apiKeyFilePath); diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient-ConditionalSerialization/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient-ConditionalSerialization/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 790be8d020f0..05442e501a96 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient-ConditionalSerialization/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient-ConditionalSerialization/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -118,7 +118,7 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m const string HEADER_AUTHORIZATION = "Authorization"; //Read the api key from the file - if(!string.IsNullOrEmpty(this.KeyFilePath)) + if(string.IsNullOrEmpty(this.KeyString)) { this.KeyString = ReadApiKeyFromFile(KeyFilePath); } @@ -771,11 +771,7 @@ private PrivateKeyType GetKeyType(string keyString) private string ReadApiKeyFromFile(string apiKeyFilePath) { string apiKeyString = null; - if(!string.IsNullOrEmpty(apiKeyFilePath) && !string.IsNullOrEmpty(this.KeyString)) - { - throw new Exception("Configure either the KeyFilePath or configure the KeyString property."); - } - + if(File.Exists(apiKeyFilePath)) { apiKeyString = File.ReadAllText(apiKeyFilePath); diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 790be8d020f0..05442e501a96 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -118,7 +118,7 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m const string HEADER_AUTHORIZATION = "Authorization"; //Read the api key from the file - if(!string.IsNullOrEmpty(this.KeyFilePath)) + if(string.IsNullOrEmpty(this.KeyString)) { this.KeyString = ReadApiKeyFromFile(KeyFilePath); } @@ -771,11 +771,7 @@ private PrivateKeyType GetKeyType(string keyString) private string ReadApiKeyFromFile(string apiKeyFilePath) { string apiKeyString = null; - if(!string.IsNullOrEmpty(apiKeyFilePath) && !string.IsNullOrEmpty(this.KeyString)) - { - throw new Exception("Configure either the KeyFilePath or configure the KeyString property."); - } - + if(File.Exists(apiKeyFilePath)) { apiKeyString = File.ReadAllText(apiKeyFilePath); diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient-net47/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient-net47/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 790be8d020f0..05442e501a96 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient-net47/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient-net47/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -118,7 +118,7 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m const string HEADER_AUTHORIZATION = "Authorization"; //Read the api key from the file - if(!string.IsNullOrEmpty(this.KeyFilePath)) + if(string.IsNullOrEmpty(this.KeyString)) { this.KeyString = ReadApiKeyFromFile(KeyFilePath); } @@ -771,11 +771,7 @@ private PrivateKeyType GetKeyType(string keyString) private string ReadApiKeyFromFile(string apiKeyFilePath) { string apiKeyString = null; - if(!string.IsNullOrEmpty(apiKeyFilePath) && !string.IsNullOrEmpty(this.KeyString)) - { - throw new Exception("Configure either the KeyFilePath or configure the KeyString property."); - } - + if(File.Exists(apiKeyFilePath)) { apiKeyString = File.ReadAllText(apiKeyFilePath); diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient-net48/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient-net48/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 790be8d020f0..05442e501a96 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient-net48/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient-net48/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -118,7 +118,7 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m const string HEADER_AUTHORIZATION = "Authorization"; //Read the api key from the file - if(!string.IsNullOrEmpty(this.KeyFilePath)) + if(string.IsNullOrEmpty(this.KeyString)) { this.KeyString = ReadApiKeyFromFile(KeyFilePath); } @@ -771,11 +771,7 @@ private PrivateKeyType GetKeyType(string keyString) private string ReadApiKeyFromFile(string apiKeyFilePath) { string apiKeyString = null; - if(!string.IsNullOrEmpty(apiKeyFilePath) && !string.IsNullOrEmpty(this.KeyString)) - { - throw new Exception("Configure either the KeyFilePath or configure the KeyString property."); - } - + if(File.Exists(apiKeyFilePath)) { apiKeyString = File.ReadAllText(apiKeyFilePath); diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient-net5.0/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient-net5.0/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 790be8d020f0..05442e501a96 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient-net5.0/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient-net5.0/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -118,7 +118,7 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m const string HEADER_AUTHORIZATION = "Authorization"; //Read the api key from the file - if(!string.IsNullOrEmpty(this.KeyFilePath)) + if(string.IsNullOrEmpty(this.KeyString)) { this.KeyString = ReadApiKeyFromFile(KeyFilePath); } @@ -771,11 +771,7 @@ private PrivateKeyType GetKeyType(string keyString) private string ReadApiKeyFromFile(string apiKeyFilePath) { string apiKeyString = null; - if(!string.IsNullOrEmpty(apiKeyFilePath) && !string.IsNullOrEmpty(this.KeyString)) - { - throw new Exception("Configure either the KeyFilePath or configure the KeyString property."); - } - + if(File.Exists(apiKeyFilePath)) { apiKeyString = File.ReadAllText(apiKeyFilePath); diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient-unityWebRequest/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient-unityWebRequest/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 790be8d020f0..05442e501a96 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient-unityWebRequest/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient-unityWebRequest/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -118,7 +118,7 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m const string HEADER_AUTHORIZATION = "Authorization"; //Read the api key from the file - if(!string.IsNullOrEmpty(this.KeyFilePath)) + if(string.IsNullOrEmpty(this.KeyString)) { this.KeyString = ReadApiKeyFromFile(KeyFilePath); } @@ -771,11 +771,7 @@ private PrivateKeyType GetKeyType(string keyString) private string ReadApiKeyFromFile(string apiKeyFilePath) { string apiKeyString = null; - if(!string.IsNullOrEmpty(apiKeyFilePath) && !string.IsNullOrEmpty(this.KeyString)) - { - throw new Exception("Configure either the KeyFilePath or configure the KeyString property."); - } - + if(File.Exists(apiKeyFilePath)) { apiKeyString = File.ReadAllText(apiKeyFilePath); diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 790be8d020f0..05442e501a96 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -118,7 +118,7 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m const string HEADER_AUTHORIZATION = "Authorization"; //Read the api key from the file - if(!string.IsNullOrEmpty(this.KeyFilePath)) + if(string.IsNullOrEmpty(this.KeyString)) { this.KeyString = ReadApiKeyFromFile(KeyFilePath); } @@ -771,11 +771,7 @@ private PrivateKeyType GetKeyType(string keyString) private string ReadApiKeyFromFile(string apiKeyFilePath) { string apiKeyString = null; - if(!string.IsNullOrEmpty(apiKeyFilePath) && !string.IsNullOrEmpty(this.KeyString)) - { - throw new Exception("Configure either the KeyFilePath or configure the KeyString property."); - } - + if(File.Exists(apiKeyFilePath)) { apiKeyString = File.ReadAllText(apiKeyFilePath); diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 790be8d020f0..05442e501a96 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -118,7 +118,7 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m const string HEADER_AUTHORIZATION = "Authorization"; //Read the api key from the file - if(!string.IsNullOrEmpty(this.KeyFilePath)) + if(string.IsNullOrEmpty(this.KeyString)) { this.KeyString = ReadApiKeyFromFile(KeyFilePath); } @@ -771,11 +771,7 @@ private PrivateKeyType GetKeyType(string keyString) private string ReadApiKeyFromFile(string apiKeyFilePath) { string apiKeyString = null; - if(!string.IsNullOrEmpty(apiKeyFilePath) && !string.IsNullOrEmpty(this.KeyString)) - { - throw new Exception("Configure either the KeyFilePath or configure the KeyString property."); - } - + if(File.Exists(apiKeyFilePath)) { apiKeyString = File.ReadAllText(apiKeyFilePath);