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 6b773b8d596..a1169150ea2 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.KeyString))
+ {
+ 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))
+ if (string.IsNullOrEmpty(keyString))
{
- key = File.ReadAllLines(keyFilePath);
- }
- else
- {
- // 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,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 0b3e867d0f4..05442e501a9 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 @@ namespace Org.OpenAPITools.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
///
@@ -112,6 +117,17 @@ namespace Org.OpenAPITools.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.KeyString))
+ {
+ 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 @@ namespace Org.OpenAPITools.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)
{
@@ -301,7 +317,7 @@ namespace Org.OpenAPITools.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);
@@ -325,16 +341,7 @@ namespace Org.OpenAPITools.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();
@@ -427,22 +434,13 @@ namespace Org.OpenAPITools.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))
{
@@ -729,20 +727,15 @@ namespace Org.OpenAPITools.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))
+ if (string.IsNullOrEmpty(keyString))
{
- key = File.ReadAllLines(keyFilePath);
- }
- else
- {
- // 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 @@ namespace Org.OpenAPITools.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))
@@ -769,6 +763,25 @@ namespace Org.OpenAPITools.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-httpclient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs
index 0b3e867d0f4..05442e501a9 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 @@ namespace Org.OpenAPITools.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
///
@@ -112,6 +117,17 @@ namespace Org.OpenAPITools.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.KeyString))
+ {
+ 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 @@ namespace Org.OpenAPITools.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)
{
@@ -301,7 +317,7 @@ namespace Org.OpenAPITools.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);
@@ -325,16 +341,7 @@ namespace Org.OpenAPITools.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();
@@ -427,22 +434,13 @@ namespace Org.OpenAPITools.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))
{
@@ -729,20 +727,15 @@ namespace Org.OpenAPITools.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))
+ if (string.IsNullOrEmpty(keyString))
{
- key = File.ReadAllLines(keyFilePath);
- }
- else
- {
- // 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 @@ namespace Org.OpenAPITools.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))
@@ -769,6 +763,25 @@ namespace Org.OpenAPITools.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-net47/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient-net47/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs
index 0b3e867d0f4..05442e501a9 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 @@ namespace Org.OpenAPITools.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
///
@@ -112,6 +117,17 @@ namespace Org.OpenAPITools.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.KeyString))
+ {
+ 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 @@ namespace Org.OpenAPITools.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)
{
@@ -301,7 +317,7 @@ namespace Org.OpenAPITools.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);
@@ -325,16 +341,7 @@ namespace Org.OpenAPITools.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();
@@ -427,22 +434,13 @@ namespace Org.OpenAPITools.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))
{
@@ -729,20 +727,15 @@ namespace Org.OpenAPITools.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))
+ if (string.IsNullOrEmpty(keyString))
{
- key = File.ReadAllLines(keyFilePath);
- }
- else
- {
- // 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 @@ namespace Org.OpenAPITools.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))
@@ -769,6 +763,25 @@ namespace Org.OpenAPITools.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-net48/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient-net48/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs
index 0b3e867d0f4..05442e501a9 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 @@ namespace Org.OpenAPITools.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
///
@@ -112,6 +117,17 @@ namespace Org.OpenAPITools.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.KeyString))
+ {
+ 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 @@ namespace Org.OpenAPITools.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)
{
@@ -301,7 +317,7 @@ namespace Org.OpenAPITools.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);
@@ -325,16 +341,7 @@ namespace Org.OpenAPITools.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();
@@ -427,22 +434,13 @@ namespace Org.OpenAPITools.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))
{
@@ -729,20 +727,15 @@ namespace Org.OpenAPITools.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))
+ if (string.IsNullOrEmpty(keyString))
{
- key = File.ReadAllLines(keyFilePath);
- }
- else
- {
- // 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 @@ namespace Org.OpenAPITools.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))
@@ -769,6 +763,25 @@ namespace Org.OpenAPITools.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-net5.0/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient-net5.0/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs
index 0b3e867d0f4..05442e501a9 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 @@ namespace Org.OpenAPITools.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
///
@@ -112,6 +117,17 @@ namespace Org.OpenAPITools.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.KeyString))
+ {
+ 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 @@ namespace Org.OpenAPITools.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)
{
@@ -301,7 +317,7 @@ namespace Org.OpenAPITools.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);
@@ -325,16 +341,7 @@ namespace Org.OpenAPITools.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();
@@ -427,22 +434,13 @@ namespace Org.OpenAPITools.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))
{
@@ -729,20 +727,15 @@ namespace Org.OpenAPITools.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))
+ if (string.IsNullOrEmpty(keyString))
{
- key = File.ReadAllLines(keyFilePath);
- }
- else
- {
- // 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 @@ namespace Org.OpenAPITools.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))
@@ -769,6 +763,25 @@ namespace Org.OpenAPITools.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-unityWebRequest/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient-unityWebRequest/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs
index 0b3e867d0f4..05442e501a9 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 @@ namespace Org.OpenAPITools.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
///
@@ -112,6 +117,17 @@ namespace Org.OpenAPITools.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.KeyString))
+ {
+ 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 @@ namespace Org.OpenAPITools.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)
{
@@ -301,7 +317,7 @@ namespace Org.OpenAPITools.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);
@@ -325,16 +341,7 @@ namespace Org.OpenAPITools.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();
@@ -427,22 +434,13 @@ namespace Org.OpenAPITools.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))
{
@@ -729,20 +727,15 @@ namespace Org.OpenAPITools.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))
+ if (string.IsNullOrEmpty(keyString))
{
- key = File.ReadAllLines(keyFilePath);
- }
- else
- {
- // 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 @@ namespace Org.OpenAPITools.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))
@@ -769,6 +763,25 @@ namespace Org.OpenAPITools.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/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs
index 0b3e867d0f4..05442e501a9 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 @@ namespace Org.OpenAPITools.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
///
@@ -112,6 +117,17 @@ namespace Org.OpenAPITools.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.KeyString))
+ {
+ 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 @@ namespace Org.OpenAPITools.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)
{
@@ -301,7 +317,7 @@ namespace Org.OpenAPITools.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);
@@ -325,16 +341,7 @@ namespace Org.OpenAPITools.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();
@@ -427,22 +434,13 @@ namespace Org.OpenAPITools.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))
{
@@ -729,20 +727,15 @@ namespace Org.OpenAPITools.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))
+ if (string.IsNullOrEmpty(keyString))
{
- key = File.ReadAllLines(keyFilePath);
- }
- else
- {
- // 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 @@ namespace Org.OpenAPITools.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))
@@ -769,6 +763,25 @@ namespace Org.OpenAPITools.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/OpenAPIClientCore/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs
index 0b3e867d0f4..05442e501a9 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 @@ namespace Org.OpenAPITools.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
///
@@ -112,6 +117,17 @@ namespace Org.OpenAPITools.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.KeyString))
+ {
+ 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 @@ namespace Org.OpenAPITools.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)
{
@@ -301,7 +317,7 @@ namespace Org.OpenAPITools.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);
@@ -325,16 +341,7 @@ namespace Org.OpenAPITools.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();
@@ -427,22 +434,13 @@ namespace Org.OpenAPITools.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))
{
@@ -729,20 +727,15 @@ namespace Org.OpenAPITools.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))
+ if (string.IsNullOrEmpty(keyString))
{
- key = File.ReadAllLines(keyFilePath);
- }
- else
- {
- // 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 @@ namespace Org.OpenAPITools.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))
@@ -769,6 +763,25 @@ namespace Org.OpenAPITools.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
}
}