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 58a961b5da3c..32dbb195b865 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 /// @@ -108,6 +113,17 @@ namespace {{packageName}}.Client var HttpSignedRequestHeader = new Dictionary(); var HttpSignatureHeader = new Dictionary(); + //Read the api key from the file + if(string.IsNullOrEmpty(this.KeyString)) + { + this.KeyString = ReadApiKeyFromFile(KeyFilePath); + } + + if(string.IsNullOrEmpty(KeyString)) + { + throw new Exception("No API key has been provided."); + } + if (HttpSigningHeader.Count == 0) { HttpSigningHeader.Add("(created)"); @@ -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,14 +333,9 @@ namespace {{packageName}}.Client /// ECDSA signature private string GetECDSASignature(byte[] dataToSign) { - if (!File.Exists(KeyFilePath)) - { - throw new Exception("key file path does not exist."); - } - const string ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----"; const string ecKeyFooter = "-----END EC PRIVATE KEY-----"; - var keyStr = File.ReadAllText(KeyFilePath); + var keyStr = KeyString; var ecKeyBase64String = keyStr.Replace(ecKeyHeader, "").Replace(ecKeyFooter, "").Trim(); var keyBytes = System.Convert.FromBase64String(ecKeyBase64String); var ecdsa = ECDsa.Create(); @@ -415,18 +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; - - if (!File.Exists(pemfile)) - { - throw new Exception("private key file does not exist."); - } - string pemstr = File.ReadAllText(pemfile).Trim(); + string pemstr = keyString; if (pemstr.StartsWith(pempubheader) && pemstr.EndsWith(pempubfooter)) { @@ -713,13 +719,13 @@ 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) { - if (!File.Exists(keyFilePath)) + if (string.IsNullOrEmpty(keyString)) { - throw new Exception("Key file path does not exist."); + throw new Exception("No API key has been provided."); } const string ecPrivateKeyHeader = "BEGIN EC PRIVATE KEY"; @@ -729,7 +735,7 @@ namespace {{packageName}}.Client //var pkcs8Header = "BEGIN PRIVATE KEY"; //var pkcs8Footer = "END PRIVATE KEY"; PrivateKeyType keyType; - var key = File.ReadAllLines(keyFilePath); + var key = KeyString.TrimEnd().Split('\n'); if (key[0].Contains(rsaPrivateKeyHeader) && key[key.Length - 1].ToString().Contains(rsaPrivateFooter)) @@ -747,6 +753,25 @@ 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(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-ConditionalSerialization/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient-ConditionalSerialization/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 6a8f9c8dc016..d9566545dcea 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 /// @@ -116,6 +121,17 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m var HttpSignedRequestHeader = new Dictionary(); var HttpSignatureHeader = new Dictionary(); + //Read the api key from the file + if(string.IsNullOrEmpty(this.KeyString)) + { + this.KeyString = ReadApiKeyFromFile(KeyFilePath); + } + + if(string.IsNullOrEmpty(KeyString)) + { + throw new Exception("No API key has been provided."); + } + if (HttpSigningHeader.Count == 0) { HttpSigningHeader.Add("(created)"); @@ -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,14 +341,9 @@ private string GetRSASignature(byte[] stringToSign) /// ECDSA signature private string GetECDSASignature(byte[] dataToSign) { - if (!File.Exists(KeyFilePath)) - { - throw new Exception("key file path does not exist."); - } - const string ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----"; const string ecKeyFooter = "-----END EC PRIVATE KEY-----"; - var keyStr = File.ReadAllText(KeyFilePath); + var keyStr = KeyString; var ecKeyBase64String = keyStr.Replace(ecKeyHeader, "").Replace(ecKeyFooter, "").Trim(); var keyBytes = System.Convert.FromBase64String(ecKeyBase64String); var ecdsa = ECDsa.Create(); @@ -423,18 +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; - - if (!File.Exists(pemfile)) - { - throw new Exception("private key file does not exist."); - } - string pemstr = File.ReadAllText(pemfile).Trim(); + string pemstr = keyString; if (pemstr.StartsWith(pempubheader) && pemstr.EndsWith(pempubfooter)) { @@ -721,13 +727,13 @@ 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) { - if (!File.Exists(keyFilePath)) + if (string.IsNullOrEmpty(keyString)) { - throw new Exception("Key file path does not exist."); + throw new Exception("No API key has been provided."); } const string ecPrivateKeyHeader = "BEGIN EC PRIVATE KEY"; @@ -737,7 +743,7 @@ private PrivateKeyType GetKeyType(string keyFilePath) //var pkcs8Header = "BEGIN PRIVATE KEY"; //var pkcs8Footer = "END PRIVATE KEY"; PrivateKeyType keyType; - var key = File.ReadAllLines(keyFilePath); + var key = KeyString.TrimEnd().Split('\n'); if (key[0].Contains(rsaPrivateKeyHeader) && key[key.Length - 1].ToString().Contains(rsaPrivateFooter)) @@ -755,6 +761,25 @@ 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(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 6a8f9c8dc016..d9566545dcea 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 /// @@ -116,6 +121,17 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m var HttpSignedRequestHeader = new Dictionary(); var HttpSignatureHeader = new Dictionary(); + //Read the api key from the file + if(string.IsNullOrEmpty(this.KeyString)) + { + this.KeyString = ReadApiKeyFromFile(KeyFilePath); + } + + if(string.IsNullOrEmpty(KeyString)) + { + throw new Exception("No API key has been provided."); + } + if (HttpSigningHeader.Count == 0) { HttpSigningHeader.Add("(created)"); @@ -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,14 +341,9 @@ private string GetRSASignature(byte[] stringToSign) /// ECDSA signature private string GetECDSASignature(byte[] dataToSign) { - if (!File.Exists(KeyFilePath)) - { - throw new Exception("key file path does not exist."); - } - const string ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----"; const string ecKeyFooter = "-----END EC PRIVATE KEY-----"; - var keyStr = File.ReadAllText(KeyFilePath); + var keyStr = KeyString; var ecKeyBase64String = keyStr.Replace(ecKeyHeader, "").Replace(ecKeyFooter, "").Trim(); var keyBytes = System.Convert.FromBase64String(ecKeyBase64String); var ecdsa = ECDsa.Create(); @@ -423,18 +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; - - if (!File.Exists(pemfile)) - { - throw new Exception("private key file does not exist."); - } - string pemstr = File.ReadAllText(pemfile).Trim(); + string pemstr = keyString; if (pemstr.StartsWith(pempubheader) && pemstr.EndsWith(pempubfooter)) { @@ -721,13 +727,13 @@ 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) { - if (!File.Exists(keyFilePath)) + if (string.IsNullOrEmpty(keyString)) { - throw new Exception("Key file path does not exist."); + throw new Exception("No API key has been provided."); } const string ecPrivateKeyHeader = "BEGIN EC PRIVATE KEY"; @@ -737,7 +743,7 @@ private PrivateKeyType GetKeyType(string keyFilePath) //var pkcs8Header = "BEGIN PRIVATE KEY"; //var pkcs8Footer = "END PRIVATE KEY"; PrivateKeyType keyType; - var key = File.ReadAllLines(keyFilePath); + var key = KeyString.TrimEnd().Split('\n'); if (key[0].Contains(rsaPrivateKeyHeader) && key[key.Length - 1].ToString().Contains(rsaPrivateFooter)) @@ -755,6 +761,25 @@ 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(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 6a8f9c8dc016..d9566545dcea 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 /// @@ -116,6 +121,17 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m var HttpSignedRequestHeader = new Dictionary(); var HttpSignatureHeader = new Dictionary(); + //Read the api key from the file + if(string.IsNullOrEmpty(this.KeyString)) + { + this.KeyString = ReadApiKeyFromFile(KeyFilePath); + } + + if(string.IsNullOrEmpty(KeyString)) + { + throw new Exception("No API key has been provided."); + } + if (HttpSigningHeader.Count == 0) { HttpSigningHeader.Add("(created)"); @@ -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,14 +341,9 @@ private string GetRSASignature(byte[] stringToSign) /// ECDSA signature private string GetECDSASignature(byte[] dataToSign) { - if (!File.Exists(KeyFilePath)) - { - throw new Exception("key file path does not exist."); - } - const string ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----"; const string ecKeyFooter = "-----END EC PRIVATE KEY-----"; - var keyStr = File.ReadAllText(KeyFilePath); + var keyStr = KeyString; var ecKeyBase64String = keyStr.Replace(ecKeyHeader, "").Replace(ecKeyFooter, "").Trim(); var keyBytes = System.Convert.FromBase64String(ecKeyBase64String); var ecdsa = ECDsa.Create(); @@ -423,18 +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; - - if (!File.Exists(pemfile)) - { - throw new Exception("private key file does not exist."); - } - string pemstr = File.ReadAllText(pemfile).Trim(); + string pemstr = keyString; if (pemstr.StartsWith(pempubheader) && pemstr.EndsWith(pempubfooter)) { @@ -721,13 +727,13 @@ 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) { - if (!File.Exists(keyFilePath)) + if (string.IsNullOrEmpty(keyString)) { - throw new Exception("Key file path does not exist."); + throw new Exception("No API key has been provided."); } const string ecPrivateKeyHeader = "BEGIN EC PRIVATE KEY"; @@ -737,7 +743,7 @@ private PrivateKeyType GetKeyType(string keyFilePath) //var pkcs8Header = "BEGIN PRIVATE KEY"; //var pkcs8Footer = "END PRIVATE KEY"; PrivateKeyType keyType; - var key = File.ReadAllLines(keyFilePath); + var key = KeyString.TrimEnd().Split('\n'); if (key[0].Contains(rsaPrivateKeyHeader) && key[key.Length - 1].ToString().Contains(rsaPrivateFooter)) @@ -755,6 +761,25 @@ 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(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 6a8f9c8dc016..d9566545dcea 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 /// @@ -116,6 +121,17 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m var HttpSignedRequestHeader = new Dictionary(); var HttpSignatureHeader = new Dictionary(); + //Read the api key from the file + if(string.IsNullOrEmpty(this.KeyString)) + { + this.KeyString = ReadApiKeyFromFile(KeyFilePath); + } + + if(string.IsNullOrEmpty(KeyString)) + { + throw new Exception("No API key has been provided."); + } + if (HttpSigningHeader.Count == 0) { HttpSigningHeader.Add("(created)"); @@ -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,14 +341,9 @@ private string GetRSASignature(byte[] stringToSign) /// ECDSA signature private string GetECDSASignature(byte[] dataToSign) { - if (!File.Exists(KeyFilePath)) - { - throw new Exception("key file path does not exist."); - } - const string ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----"; const string ecKeyFooter = "-----END EC PRIVATE KEY-----"; - var keyStr = File.ReadAllText(KeyFilePath); + var keyStr = KeyString; var ecKeyBase64String = keyStr.Replace(ecKeyHeader, "").Replace(ecKeyFooter, "").Trim(); var keyBytes = System.Convert.FromBase64String(ecKeyBase64String); var ecdsa = ECDsa.Create(); @@ -423,18 +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; - - if (!File.Exists(pemfile)) - { - throw new Exception("private key file does not exist."); - } - string pemstr = File.ReadAllText(pemfile).Trim(); + string pemstr = keyString; if (pemstr.StartsWith(pempubheader) && pemstr.EndsWith(pempubfooter)) { @@ -721,13 +727,13 @@ 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) { - if (!File.Exists(keyFilePath)) + if (string.IsNullOrEmpty(keyString)) { - throw new Exception("Key file path does not exist."); + throw new Exception("No API key has been provided."); } const string ecPrivateKeyHeader = "BEGIN EC PRIVATE KEY"; @@ -737,7 +743,7 @@ private PrivateKeyType GetKeyType(string keyFilePath) //var pkcs8Header = "BEGIN PRIVATE KEY"; //var pkcs8Footer = "END PRIVATE KEY"; PrivateKeyType keyType; - var key = File.ReadAllLines(keyFilePath); + var key = KeyString.TrimEnd().Split('\n'); if (key[0].Contains(rsaPrivateKeyHeader) && key[key.Length - 1].ToString().Contains(rsaPrivateFooter)) @@ -755,6 +761,25 @@ 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(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 6a8f9c8dc016..d9566545dcea 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 /// @@ -116,6 +121,17 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m var HttpSignedRequestHeader = new Dictionary(); var HttpSignatureHeader = new Dictionary(); + //Read the api key from the file + if(string.IsNullOrEmpty(this.KeyString)) + { + this.KeyString = ReadApiKeyFromFile(KeyFilePath); + } + + if(string.IsNullOrEmpty(KeyString)) + { + throw new Exception("No API key has been provided."); + } + if (HttpSigningHeader.Count == 0) { HttpSigningHeader.Add("(created)"); @@ -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,14 +341,9 @@ private string GetRSASignature(byte[] stringToSign) /// ECDSA signature private string GetECDSASignature(byte[] dataToSign) { - if (!File.Exists(KeyFilePath)) - { - throw new Exception("key file path does not exist."); - } - const string ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----"; const string ecKeyFooter = "-----END EC PRIVATE KEY-----"; - var keyStr = File.ReadAllText(KeyFilePath); + var keyStr = KeyString; var ecKeyBase64String = keyStr.Replace(ecKeyHeader, "").Replace(ecKeyFooter, "").Trim(); var keyBytes = System.Convert.FromBase64String(ecKeyBase64String); var ecdsa = ECDsa.Create(); @@ -423,18 +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; - - if (!File.Exists(pemfile)) - { - throw new Exception("private key file does not exist."); - } - string pemstr = File.ReadAllText(pemfile).Trim(); + string pemstr = keyString; if (pemstr.StartsWith(pempubheader) && pemstr.EndsWith(pempubfooter)) { @@ -721,13 +727,13 @@ 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) { - if (!File.Exists(keyFilePath)) + if (string.IsNullOrEmpty(keyString)) { - throw new Exception("Key file path does not exist."); + throw new Exception("No API key has been provided."); } const string ecPrivateKeyHeader = "BEGIN EC PRIVATE KEY"; @@ -737,7 +743,7 @@ private PrivateKeyType GetKeyType(string keyFilePath) //var pkcs8Header = "BEGIN PRIVATE KEY"; //var pkcs8Footer = "END PRIVATE KEY"; PrivateKeyType keyType; - var key = File.ReadAllLines(keyFilePath); + var key = KeyString.TrimEnd().Split('\n'); if (key[0].Contains(rsaPrivateKeyHeader) && key[key.Length - 1].ToString().Contains(rsaPrivateFooter)) @@ -755,6 +761,25 @@ 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(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 6a8f9c8dc016..d9566545dcea 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 /// @@ -116,6 +121,17 @@ internal Dictionary GetHttpSignedHeader(string basePath,string m var HttpSignedRequestHeader = new Dictionary(); var HttpSignatureHeader = new Dictionary(); + //Read the api key from the file + if(string.IsNullOrEmpty(this.KeyString)) + { + this.KeyString = ReadApiKeyFromFile(KeyFilePath); + } + + if(string.IsNullOrEmpty(KeyString)) + { + throw new Exception("No API key has been provided."); + } + if (HttpSigningHeader.Count == 0) { HttpSigningHeader.Add("(created)"); @@ -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,14 +341,9 @@ private string GetRSASignature(byte[] stringToSign) /// ECDSA signature private string GetECDSASignature(byte[] dataToSign) { - if (!File.Exists(KeyFilePath)) - { - throw new Exception("key file path does not exist."); - } - const string ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----"; const string ecKeyFooter = "-----END EC PRIVATE KEY-----"; - var keyStr = File.ReadAllText(KeyFilePath); + var keyStr = KeyString; var ecKeyBase64String = keyStr.Replace(ecKeyHeader, "").Replace(ecKeyFooter, "").Trim(); var keyBytes = System.Convert.FromBase64String(ecKeyBase64String); var ecdsa = ECDsa.Create(); @@ -423,18 +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; - - if (!File.Exists(pemfile)) - { - throw new Exception("private key file does not exist."); - } - string pemstr = File.ReadAllText(pemfile).Trim(); + string pemstr = keyString; if (pemstr.StartsWith(pempubheader) && pemstr.EndsWith(pempubfooter)) { @@ -721,13 +727,13 @@ 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) { - if (!File.Exists(keyFilePath)) + if (string.IsNullOrEmpty(keyString)) { - throw new Exception("Key file path does not exist."); + throw new Exception("No API key has been provided."); } const string ecPrivateKeyHeader = "BEGIN EC PRIVATE KEY"; @@ -737,7 +743,7 @@ private PrivateKeyType GetKeyType(string keyFilePath) //var pkcs8Header = "BEGIN PRIVATE KEY"; //var pkcs8Footer = "END PRIVATE KEY"; PrivateKeyType keyType; - var key = File.ReadAllLines(keyFilePath); + var key = KeyString.TrimEnd().Split('\n'); if (key[0].Contains(rsaPrivateKeyHeader) && key[key.Length - 1].ToString().Contains(rsaPrivateFooter)) @@ -755,6 +761,25 @@ 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(File.Exists(apiKeyFilePath)) + { + apiKeyString = File.ReadAllText(apiKeyFilePath); + } + else + { + throw new Exception("Provided API key file path does not exists."); + } + return apiKeyString; + } + #endregion } }