[csharp-netcore] To support Secret API key input as string (#13276)

* To support api secret key to take input as string.

* To support Secret API key input as string

Co-authored-by: Savitha M R <savitha.m@maplelabs.com>
This commit is contained in:
Savitha M R
2022-08-29 15:47:30 +05:30
committed by GitHub
parent d97081e7dc
commit 522a5df24a
7 changed files with 168 additions and 70 deletions

View File

@@ -325,14 +325,18 @@ namespace Org.OpenAPITools.Client
/// <returns>ECDSA signature</returns>
private string GetECDSASignature(byte[] dataToSign)
{
if (!File.Exists(KeyFilePath))
string keyStr = string.Empty;
if (File.Exists(KeyFilePath))
{
throw new Exception("key file path does not exist.");
keyStr = File.ReadAllText(KeyFilePath);
}
else
{
keyStr = KeyFilePath;
}
const string ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----";
const string ecKeyFooter = "-----END EC PRIVATE KEY-----";
var keyStr = File.ReadAllText(KeyFilePath);
var ecKeyBase64String = keyStr.Replace(ecKeyHeader, "").Replace(ecKeyFooter, "").Trim();
var keyBytes = System.Convert.FromBase64String(ecKeyBase64String);
var ecdsa = ECDsa.Create();
@@ -430,11 +434,15 @@ namespace Org.OpenAPITools.Client
bool isPrivateKeyFile = true;
byte[] pemkey = null;
if (!File.Exists(pemfile))
string pemstr = string.Empty;
if (File.Exists(pemfile))
{
throw new Exception("private key file does not exist.");
pemstr = File.ReadAllText(pemfile).Trim();
}
else
{
pemstr = pemfile;
}
string pemstr = File.ReadAllText(pemfile).Trim();
if (pemstr.StartsWith(pempubheader) && pemstr.EndsWith(pempubfooter))
{
@@ -725,9 +733,16 @@ namespace Org.OpenAPITools.Client
/// <returns>Private Key Type</returns>
private PrivateKeyType GetKeyType(string keyFilePath)
{
if (!File.Exists(keyFilePath))
string[] key = null;
if (File.Exists(keyFilePath))
{
throw new Exception("Key file path does not exist.");
key = File.ReadAllLines(keyFilePath);
}
else
{
// The ApiKeyFilePath is passed as string
key = new string[] { keyFilePath };
}
const string ecPrivateKeyHeader = "BEGIN EC PRIVATE KEY";
@@ -737,7 +752,6 @@ namespace Org.OpenAPITools.Client
//var pkcs8Header = "BEGIN PRIVATE KEY";
//var pkcs8Footer = "END PRIVATE KEY";
PrivateKeyType keyType;
var key = File.ReadAllLines(keyFilePath);
if (key[0].Contains(rsaPrivateKeyHeader) &&
key[key.Length - 1].ToString().Contains(rsaPrivateFooter))
@@ -751,7 +765,7 @@ namespace Org.OpenAPITools.Client
}
else
{
throw new Exception("Either the key is invalid or key is not supported");
throw new Exception("The key file path does not exist or key is invalid or key is not supported");
}
return keyType;
}

View File

@@ -325,14 +325,18 @@ namespace Org.OpenAPITools.Client
/// <returns>ECDSA signature</returns>
private string GetECDSASignature(byte[] dataToSign)
{
if (!File.Exists(KeyFilePath))
string keyStr = string.Empty;
if (File.Exists(KeyFilePath))
{
throw new Exception("key file path does not exist.");
keyStr = File.ReadAllText(KeyFilePath);
}
else
{
keyStr = KeyFilePath;
}
const string ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----";
const string ecKeyFooter = "-----END EC PRIVATE KEY-----";
var keyStr = File.ReadAllText(KeyFilePath);
var ecKeyBase64String = keyStr.Replace(ecKeyHeader, "").Replace(ecKeyFooter, "").Trim();
var keyBytes = System.Convert.FromBase64String(ecKeyBase64String);
var ecdsa = ECDsa.Create();
@@ -430,11 +434,15 @@ namespace Org.OpenAPITools.Client
bool isPrivateKeyFile = true;
byte[] pemkey = null;
if (!File.Exists(pemfile))
string pemstr = string.Empty;
if (File.Exists(pemfile))
{
throw new Exception("private key file does not exist.");
pemstr = File.ReadAllText(pemfile).Trim();
}
else
{
pemstr = pemfile;
}
string pemstr = File.ReadAllText(pemfile).Trim();
if (pemstr.StartsWith(pempubheader) && pemstr.EndsWith(pempubfooter))
{
@@ -725,9 +733,16 @@ namespace Org.OpenAPITools.Client
/// <returns>Private Key Type</returns>
private PrivateKeyType GetKeyType(string keyFilePath)
{
if (!File.Exists(keyFilePath))
string[] key = null;
if (File.Exists(keyFilePath))
{
throw new Exception("Key file path does not exist.");
key = File.ReadAllLines(keyFilePath);
}
else
{
// The ApiKeyFilePath is passed as string
key = new string[] { keyFilePath };
}
const string ecPrivateKeyHeader = "BEGIN EC PRIVATE KEY";
@@ -737,7 +752,6 @@ namespace Org.OpenAPITools.Client
//var pkcs8Header = "BEGIN PRIVATE KEY";
//var pkcs8Footer = "END PRIVATE KEY";
PrivateKeyType keyType;
var key = File.ReadAllLines(keyFilePath);
if (key[0].Contains(rsaPrivateKeyHeader) &&
key[key.Length - 1].ToString().Contains(rsaPrivateFooter))
@@ -751,7 +765,7 @@ namespace Org.OpenAPITools.Client
}
else
{
throw new Exception("Either the key is invalid or key is not supported");
throw new Exception("The key file path does not exist or key is invalid or key is not supported");
}
return keyType;
}

View File

@@ -325,14 +325,18 @@ namespace Org.OpenAPITools.Client
/// <returns>ECDSA signature</returns>
private string GetECDSASignature(byte[] dataToSign)
{
if (!File.Exists(KeyFilePath))
string keyStr = string.Empty;
if (File.Exists(KeyFilePath))
{
throw new Exception("key file path does not exist.");
keyStr = File.ReadAllText(KeyFilePath);
}
else
{
keyStr = KeyFilePath;
}
const string ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----";
const string ecKeyFooter = "-----END EC PRIVATE KEY-----";
var keyStr = File.ReadAllText(KeyFilePath);
var ecKeyBase64String = keyStr.Replace(ecKeyHeader, "").Replace(ecKeyFooter, "").Trim();
var keyBytes = System.Convert.FromBase64String(ecKeyBase64String);
var ecdsa = ECDsa.Create();
@@ -430,11 +434,15 @@ namespace Org.OpenAPITools.Client
bool isPrivateKeyFile = true;
byte[] pemkey = null;
if (!File.Exists(pemfile))
string pemstr = string.Empty;
if (File.Exists(pemfile))
{
throw new Exception("private key file does not exist.");
pemstr = File.ReadAllText(pemfile).Trim();
}
else
{
pemstr = pemfile;
}
string pemstr = File.ReadAllText(pemfile).Trim();
if (pemstr.StartsWith(pempubheader) && pemstr.EndsWith(pempubfooter))
{
@@ -725,9 +733,16 @@ namespace Org.OpenAPITools.Client
/// <returns>Private Key Type</returns>
private PrivateKeyType GetKeyType(string keyFilePath)
{
if (!File.Exists(keyFilePath))
string[] key = null;
if (File.Exists(keyFilePath))
{
throw new Exception("Key file path does not exist.");
key = File.ReadAllLines(keyFilePath);
}
else
{
// The ApiKeyFilePath is passed as string
key = new string[] { keyFilePath };
}
const string ecPrivateKeyHeader = "BEGIN EC PRIVATE KEY";
@@ -737,7 +752,6 @@ namespace Org.OpenAPITools.Client
//var pkcs8Header = "BEGIN PRIVATE KEY";
//var pkcs8Footer = "END PRIVATE KEY";
PrivateKeyType keyType;
var key = File.ReadAllLines(keyFilePath);
if (key[0].Contains(rsaPrivateKeyHeader) &&
key[key.Length - 1].ToString().Contains(rsaPrivateFooter))
@@ -751,7 +765,7 @@ namespace Org.OpenAPITools.Client
}
else
{
throw new Exception("Either the key is invalid or key is not supported");
throw new Exception("The key file path does not exist or key is invalid or key is not supported");
}
return keyType;
}

View File

@@ -325,14 +325,18 @@ namespace Org.OpenAPITools.Client
/// <returns>ECDSA signature</returns>
private string GetECDSASignature(byte[] dataToSign)
{
if (!File.Exists(KeyFilePath))
string keyStr = string.Empty;
if (File.Exists(KeyFilePath))
{
throw new Exception("key file path does not exist.");
keyStr = File.ReadAllText(KeyFilePath);
}
else
{
keyStr = KeyFilePath;
}
const string ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----";
const string ecKeyFooter = "-----END EC PRIVATE KEY-----";
var keyStr = File.ReadAllText(KeyFilePath);
var ecKeyBase64String = keyStr.Replace(ecKeyHeader, "").Replace(ecKeyFooter, "").Trim();
var keyBytes = System.Convert.FromBase64String(ecKeyBase64String);
var ecdsa = ECDsa.Create();
@@ -430,11 +434,15 @@ namespace Org.OpenAPITools.Client
bool isPrivateKeyFile = true;
byte[] pemkey = null;
if (!File.Exists(pemfile))
string pemstr = string.Empty;
if (File.Exists(pemfile))
{
throw new Exception("private key file does not exist.");
pemstr = File.ReadAllText(pemfile).Trim();
}
else
{
pemstr = pemfile;
}
string pemstr = File.ReadAllText(pemfile).Trim();
if (pemstr.StartsWith(pempubheader) && pemstr.EndsWith(pempubfooter))
{
@@ -725,9 +733,16 @@ namespace Org.OpenAPITools.Client
/// <returns>Private Key Type</returns>
private PrivateKeyType GetKeyType(string keyFilePath)
{
if (!File.Exists(keyFilePath))
string[] key = null;
if (File.Exists(keyFilePath))
{
throw new Exception("Key file path does not exist.");
key = File.ReadAllLines(keyFilePath);
}
else
{
// The ApiKeyFilePath is passed as string
key = new string[] { keyFilePath };
}
const string ecPrivateKeyHeader = "BEGIN EC PRIVATE KEY";
@@ -737,7 +752,6 @@ namespace Org.OpenAPITools.Client
//var pkcs8Header = "BEGIN PRIVATE KEY";
//var pkcs8Footer = "END PRIVATE KEY";
PrivateKeyType keyType;
var key = File.ReadAllLines(keyFilePath);
if (key[0].Contains(rsaPrivateKeyHeader) &&
key[key.Length - 1].ToString().Contains(rsaPrivateFooter))
@@ -751,7 +765,7 @@ namespace Org.OpenAPITools.Client
}
else
{
throw new Exception("Either the key is invalid or key is not supported");
throw new Exception("The key file path does not exist or key is invalid or key is not supported");
}
return keyType;
}

View File

@@ -325,14 +325,18 @@ namespace Org.OpenAPITools.Client
/// <returns>ECDSA signature</returns>
private string GetECDSASignature(byte[] dataToSign)
{
if (!File.Exists(KeyFilePath))
string keyStr = string.Empty;
if (File.Exists(KeyFilePath))
{
throw new Exception("key file path does not exist.");
keyStr = File.ReadAllText(KeyFilePath);
}
else
{
keyStr = KeyFilePath;
}
const string ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----";
const string ecKeyFooter = "-----END EC PRIVATE KEY-----";
var keyStr = File.ReadAllText(KeyFilePath);
var ecKeyBase64String = keyStr.Replace(ecKeyHeader, "").Replace(ecKeyFooter, "").Trim();
var keyBytes = System.Convert.FromBase64String(ecKeyBase64String);
var ecdsa = ECDsa.Create();
@@ -430,11 +434,15 @@ namespace Org.OpenAPITools.Client
bool isPrivateKeyFile = true;
byte[] pemkey = null;
if (!File.Exists(pemfile))
string pemstr = string.Empty;
if (File.Exists(pemfile))
{
throw new Exception("private key file does not exist.");
pemstr = File.ReadAllText(pemfile).Trim();
}
else
{
pemstr = pemfile;
}
string pemstr = File.ReadAllText(pemfile).Trim();
if (pemstr.StartsWith(pempubheader) && pemstr.EndsWith(pempubfooter))
{
@@ -725,9 +733,16 @@ namespace Org.OpenAPITools.Client
/// <returns>Private Key Type</returns>
private PrivateKeyType GetKeyType(string keyFilePath)
{
if (!File.Exists(keyFilePath))
string[] key = null;
if (File.Exists(keyFilePath))
{
throw new Exception("Key file path does not exist.");
key = File.ReadAllLines(keyFilePath);
}
else
{
// The ApiKeyFilePath is passed as string
key = new string[] { keyFilePath };
}
const string ecPrivateKeyHeader = "BEGIN EC PRIVATE KEY";
@@ -737,7 +752,6 @@ namespace Org.OpenAPITools.Client
//var pkcs8Header = "BEGIN PRIVATE KEY";
//var pkcs8Footer = "END PRIVATE KEY";
PrivateKeyType keyType;
var key = File.ReadAllLines(keyFilePath);
if (key[0].Contains(rsaPrivateKeyHeader) &&
key[key.Length - 1].ToString().Contains(rsaPrivateFooter))
@@ -751,7 +765,7 @@ namespace Org.OpenAPITools.Client
}
else
{
throw new Exception("Either the key is invalid or key is not supported");
throw new Exception("The key file path does not exist or key is invalid or key is not supported");
}
return keyType;
}

View File

@@ -325,14 +325,18 @@ namespace Org.OpenAPITools.Client
/// <returns>ECDSA signature</returns>
private string GetECDSASignature(byte[] dataToSign)
{
if (!File.Exists(KeyFilePath))
string keyStr = string.Empty;
if (File.Exists(KeyFilePath))
{
throw new Exception("key file path does not exist.");
keyStr = File.ReadAllText(KeyFilePath);
}
else
{
keyStr = KeyFilePath;
}
const string ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----";
const string ecKeyFooter = "-----END EC PRIVATE KEY-----";
var keyStr = File.ReadAllText(KeyFilePath);
var ecKeyBase64String = keyStr.Replace(ecKeyHeader, "").Replace(ecKeyFooter, "").Trim();
var keyBytes = System.Convert.FromBase64String(ecKeyBase64String);
var ecdsa = ECDsa.Create();
@@ -430,11 +434,15 @@ namespace Org.OpenAPITools.Client
bool isPrivateKeyFile = true;
byte[] pemkey = null;
if (!File.Exists(pemfile))
string pemstr = string.Empty;
if (File.Exists(pemfile))
{
throw new Exception("private key file does not exist.");
pemstr = File.ReadAllText(pemfile).Trim();
}
else
{
pemstr = pemfile;
}
string pemstr = File.ReadAllText(pemfile).Trim();
if (pemstr.StartsWith(pempubheader) && pemstr.EndsWith(pempubfooter))
{
@@ -725,9 +733,16 @@ namespace Org.OpenAPITools.Client
/// <returns>Private Key Type</returns>
private PrivateKeyType GetKeyType(string keyFilePath)
{
if (!File.Exists(keyFilePath))
string[] key = null;
if (File.Exists(keyFilePath))
{
throw new Exception("Key file path does not exist.");
key = File.ReadAllLines(keyFilePath);
}
else
{
// The ApiKeyFilePath is passed as string
key = new string[] { keyFilePath };
}
const string ecPrivateKeyHeader = "BEGIN EC PRIVATE KEY";
@@ -737,7 +752,6 @@ namespace Org.OpenAPITools.Client
//var pkcs8Header = "BEGIN PRIVATE KEY";
//var pkcs8Footer = "END PRIVATE KEY";
PrivateKeyType keyType;
var key = File.ReadAllLines(keyFilePath);
if (key[0].Contains(rsaPrivateKeyHeader) &&
key[key.Length - 1].ToString().Contains(rsaPrivateFooter))
@@ -751,7 +765,7 @@ namespace Org.OpenAPITools.Client
}
else
{
throw new Exception("Either the key is invalid or key is not supported");
throw new Exception("The key file path does not exist or key is invalid or key is not supported");
}
return keyType;
}