forked from loafle/openapi-generator-original
[csharp-netcore] Configure the API key in string format for HttpSigningConfiguration (#15818)
* added property in HttpSIgningConfiguration to accept API key in string format. * remove trailing space, update samples * updated the sample code. * Revert "updated the sample code." This reverts commit 5b945c7a59ef03782dc5f48d97784894d6b41614. * fix the sample code compilation error for split function. * updated the sample code after the split function fix. * Removed the either or check for filePath or KeyString. --------- Co-authored-by: Aanisha Mishra <aanisha.mishra05@gmail.com> Co-authored-by: Vikrant Balyan (vvb) <vvb@cisco.com> Co-authored-by: Vikrant Balyan <vvb@users.noreply.github.com> Co-authored-by: Sebastien Rosset <serosset@cisco.com> Co-authored-by: William Cheng <wing328hk@gmail.com>
This commit is contained in:
parent
37d8a0d3ff
commit
9fe5cc0b01
@ -40,6 +40,11 @@ namespace {{packageName}}.Client
|
||||
/// </summary>
|
||||
public string KeyFilePath { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Specify the API key in the form of a string, either configure the KeyString property or configure the KeyFilePath property.
|
||||
/// </summary>
|
||||
public string KeyString { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the key pass phrase for password protected key
|
||||
/// </summary>
|
||||
@ -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<string, string>();
|
||||
var HttpSignatureHeader = new Dictionary<string, string>();
|
||||
@ -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
|
||||
/// <returns>ECDSA signature</returns>
|
||||
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
|
||||
/// <summary>
|
||||
/// Detect the key type from the pem file.
|
||||
/// </summary>
|
||||
/// <param name="keyFilePath">key file path in pem format</param>
|
||||
/// <param name="keyString">api key in string format</param>
|
||||
/// <returns>Private Key Type</returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read the api key form the api key file path and stored it in KeyString property.
|
||||
/// </summary>
|
||||
/// <param name="apiKeyFilePath">api key file path</param>
|
||||
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
|
||||
}
|
||||
}
|
||||
|
@ -48,6 +48,11 @@ namespace Org.OpenAPITools.Client
|
||||
/// </summary>
|
||||
public string KeyFilePath { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Specify the API key in the form of a string, either configure the KeyString property or configure the KeyFilePath property.
|
||||
/// </summary>
|
||||
public string KeyString { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the key pass phrase for password protected key
|
||||
/// </summary>
|
||||
@ -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<string, string>();
|
||||
var HttpSignatureHeader = new Dictionary<string, string>();
|
||||
@ -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
|
||||
/// <returns>ECDSA signature</returns>
|
||||
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
|
||||
/// <summary>
|
||||
/// Detect the key type from the pem file.
|
||||
/// </summary>
|
||||
/// <param name="keyFilePath">key file path in pem format</param>
|
||||
/// <param name="keyString">api key in string format</param>
|
||||
/// <returns>Private Key Type</returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read the api key form the api key file path and stored it in KeyString property.
|
||||
/// </summary>
|
||||
/// <param name="apiKeyFilePath">api key file path</param>
|
||||
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
|
||||
}
|
||||
}
|
||||
|
@ -48,6 +48,11 @@ namespace Org.OpenAPITools.Client
|
||||
/// </summary>
|
||||
public string KeyFilePath { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Specify the API key in the form of a string, either configure the KeyString property or configure the KeyFilePath property.
|
||||
/// </summary>
|
||||
public string KeyString { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the key pass phrase for password protected key
|
||||
/// </summary>
|
||||
@ -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<string, string>();
|
||||
var HttpSignatureHeader = new Dictionary<string, string>();
|
||||
@ -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
|
||||
/// <returns>ECDSA signature</returns>
|
||||
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
|
||||
/// <summary>
|
||||
/// Detect the key type from the pem file.
|
||||
/// </summary>
|
||||
/// <param name="keyFilePath">key file path in pem format</param>
|
||||
/// <param name="keyString">api key in string format</param>
|
||||
/// <returns>Private Key Type</returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read the api key form the api key file path and stored it in KeyString property.
|
||||
/// </summary>
|
||||
/// <param name="apiKeyFilePath">api key file path</param>
|
||||
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
|
||||
}
|
||||
}
|
||||
|
@ -48,6 +48,11 @@ namespace Org.OpenAPITools.Client
|
||||
/// </summary>
|
||||
public string KeyFilePath { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Specify the API key in the form of a string, either configure the KeyString property or configure the KeyFilePath property.
|
||||
/// </summary>
|
||||
public string KeyString { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the key pass phrase for password protected key
|
||||
/// </summary>
|
||||
@ -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<string, string>();
|
||||
var HttpSignatureHeader = new Dictionary<string, string>();
|
||||
@ -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
|
||||
/// <returns>ECDSA signature</returns>
|
||||
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
|
||||
/// <summary>
|
||||
/// Detect the key type from the pem file.
|
||||
/// </summary>
|
||||
/// <param name="keyFilePath">key file path in pem format</param>
|
||||
/// <param name="keyString">api key in string format</param>
|
||||
/// <returns>Private Key Type</returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read the api key form the api key file path and stored it in KeyString property.
|
||||
/// </summary>
|
||||
/// <param name="apiKeyFilePath">api key file path</param>
|
||||
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
|
||||
}
|
||||
}
|
||||
|
@ -48,6 +48,11 @@ namespace Org.OpenAPITools.Client
|
||||
/// </summary>
|
||||
public string KeyFilePath { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Specify the API key in the form of a string, either configure the KeyString property or configure the KeyFilePath property.
|
||||
/// </summary>
|
||||
public string KeyString { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the key pass phrase for password protected key
|
||||
/// </summary>
|
||||
@ -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<string, string>();
|
||||
var HttpSignatureHeader = new Dictionary<string, string>();
|
||||
@ -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
|
||||
/// <returns>ECDSA signature</returns>
|
||||
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
|
||||
/// <summary>
|
||||
/// Detect the key type from the pem file.
|
||||
/// </summary>
|
||||
/// <param name="keyFilePath">key file path in pem format</param>
|
||||
/// <param name="keyString">api key in string format</param>
|
||||
/// <returns>Private Key Type</returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read the api key form the api key file path and stored it in KeyString property.
|
||||
/// </summary>
|
||||
/// <param name="apiKeyFilePath">api key file path</param>
|
||||
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
|
||||
}
|
||||
}
|
||||
|
@ -48,6 +48,11 @@ namespace Org.OpenAPITools.Client
|
||||
/// </summary>
|
||||
public string KeyFilePath { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Specify the API key in the form of a string, either configure the KeyString property or configure the KeyFilePath property.
|
||||
/// </summary>
|
||||
public string KeyString { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the key pass phrase for password protected key
|
||||
/// </summary>
|
||||
@ -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<string, string>();
|
||||
var HttpSignatureHeader = new Dictionary<string, string>();
|
||||
@ -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
|
||||
/// <returns>ECDSA signature</returns>
|
||||
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
|
||||
/// <summary>
|
||||
/// Detect the key type from the pem file.
|
||||
/// </summary>
|
||||
/// <param name="keyFilePath">key file path in pem format</param>
|
||||
/// <param name="keyString">api key in string format</param>
|
||||
/// <returns>Private Key Type</returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read the api key form the api key file path and stored it in KeyString property.
|
||||
/// </summary>
|
||||
/// <param name="apiKeyFilePath">api key file path</param>
|
||||
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
|
||||
}
|
||||
}
|
||||
|
@ -48,6 +48,11 @@ namespace Org.OpenAPITools.Client
|
||||
/// </summary>
|
||||
public string KeyFilePath { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Specify the API key in the form of a string, either configure the KeyString property or configure the KeyFilePath property.
|
||||
/// </summary>
|
||||
public string KeyString { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the key pass phrase for password protected key
|
||||
/// </summary>
|
||||
@ -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<string, string>();
|
||||
var HttpSignatureHeader = new Dictionary<string, string>();
|
||||
@ -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
|
||||
/// <returns>ECDSA signature</returns>
|
||||
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
|
||||
/// <summary>
|
||||
/// Detect the key type from the pem file.
|
||||
/// </summary>
|
||||
/// <param name="keyFilePath">key file path in pem format</param>
|
||||
/// <param name="keyString">api key in string format</param>
|
||||
/// <returns>Private Key Type</returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read the api key form the api key file path and stored it in KeyString property.
|
||||
/// </summary>
|
||||
/// <param name="apiKeyFilePath">api key file path</param>
|
||||
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
|
||||
}
|
||||
}
|
||||
|
@ -48,6 +48,11 @@ namespace Org.OpenAPITools.Client
|
||||
/// </summary>
|
||||
public string KeyFilePath { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Specify the API key in the form of a string, either configure the KeyString property or configure the KeyFilePath property.
|
||||
/// </summary>
|
||||
public string KeyString { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the key pass phrase for password protected key
|
||||
/// </summary>
|
||||
@ -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<string, string>();
|
||||
var HttpSignatureHeader = new Dictionary<string, string>();
|
||||
@ -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
|
||||
/// <returns>ECDSA signature</returns>
|
||||
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
|
||||
/// <summary>
|
||||
/// Detect the key type from the pem file.
|
||||
/// </summary>
|
||||
/// <param name="keyFilePath">key file path in pem format</param>
|
||||
/// <param name="keyString">api key in string format</param>
|
||||
/// <returns>Private Key Type</returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read the api key form the api key file path and stored it in KeyString property.
|
||||
/// </summary>
|
||||
/// <param name="apiKeyFilePath">api key file path</param>
|
||||
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
|
||||
}
|
||||
}
|
||||
|
@ -48,6 +48,11 @@ namespace Org.OpenAPITools.Client
|
||||
/// </summary>
|
||||
public string KeyFilePath { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Specify the API key in the form of a string, either configure the KeyString property or configure the KeyFilePath property.
|
||||
/// </summary>
|
||||
public string KeyString { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the key pass phrase for password protected key
|
||||
/// </summary>
|
||||
@ -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<string, string>();
|
||||
var HttpSignatureHeader = new Dictionary<string, string>();
|
||||
@ -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
|
||||
/// <returns>ECDSA signature</returns>
|
||||
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
|
||||
/// <summary>
|
||||
/// Detect the key type from the pem file.
|
||||
/// </summary>
|
||||
/// <param name="keyFilePath">key file path in pem format</param>
|
||||
/// <param name="keyString">api key in string format</param>
|
||||
/// <returns>Private Key Type</returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read the api key form the api key file path and stored it in KeyString property.
|
||||
/// </summary>
|
||||
/// <param name="apiKeyFilePath">api key file path</param>
|
||||
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
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user