Skip to content

Commit 5b945c7

Browse files
committed
updated the sample code.
1 parent d692ea4 commit 5b945c7

8 files changed

Lines changed: 392 additions & 256 deletions

File tree

samples/client/petstore/csharp-netcore/OpenAPIClient-ConditionalSerialization/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ public HttpSigningConfiguration()
4848
/// </summary>
4949
public string KeyFilePath { get; set; }
5050

51+
/// <summary>
52+
/// Specify the API key in the form of a string, either configure the KeyString property or configure the KeyFilePath property.
53+
/// </summary>
54+
public string KeyString { get; set; }
55+
5156
/// <summary>
5257
/// Gets the key pass phrase for password protected key
5358
/// </summary>
@@ -112,6 +117,17 @@ internal Dictionary<string, string> GetHttpSignedHeader(string basePath,string m
112117
//the list of signed headers and a base64-encoded signature.
113118
const string HEADER_AUTHORIZATION = "Authorization";
114119

120+
//Read the api key from the file
121+
if(!string.IsNullOrEmpty(this.KeyFilePath))
122+
{
123+
this.KeyString = ReadApiKeyFromFile(KeyFilePath);
124+
}
125+
126+
if(string.IsNullOrEmpty(KeyString))
127+
{
128+
throw new Exception("No API key has been provided.");
129+
}
130+
115131
//Hash table to store singed headers
116132
var HttpSignedRequestHeader = new Dictionary<string, string>();
117133
var HttpSignatureHeader = new Dictionary<string, string>();
@@ -250,7 +266,7 @@ internal Dictionary<string, string> GetHttpSignedHeader(string basePath,string m
250266
var headerValuesString = string.Join("\n", headerValuesList);
251267
var signatureStringHash = GetStringHash(HashAlgorithm.ToString(), headerValuesString);
252268
string headerSignatureStr = null;
253-
var keyType = GetKeyType(KeyFilePath);
269+
var keyType = GetKeyType(KeyString);
254270

255271
if (keyType == PrivateKeyType.RSA)
256272
{
@@ -301,7 +317,7 @@ private int GetUnixTime(DateTime date2)
301317

302318
private string GetRSASignature(byte[] stringToSign)
303319
{
304-
RSA rsa = GetRSAProviderFromPemFile(KeyFilePath, KeyPassPhrase);
320+
RSA rsa = GetRSAProviderFromPemFile(KeyString, KeyPassPhrase);
305321
if (SigningAlgorithm == "RSASSA-PSS")
306322
{
307323
var signedbytes = rsa.SignHash(stringToSign, HashAlgorithm, RSASignaturePadding.Pss);
@@ -325,16 +341,7 @@ private string GetRSASignature(byte[] stringToSign)
325341
/// <returns>ECDSA signature</returns>
326342
private string GetECDSASignature(byte[] dataToSign)
327343
{
328-
string keyStr = string.Empty;
329-
if (File.Exists(KeyFilePath))
330-
{
331-
keyStr = File.ReadAllText(KeyFilePath);
332-
}
333-
else
334-
{
335-
keyStr = KeyFilePath;
336-
}
337-
344+
var keyStr = KeyString;
338345
const string ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----";
339346
const string ecKeyFooter = "-----END EC PRIVATE KEY-----";
340347
var ecKeyBase64String = keyStr.Replace(ecKeyHeader, "").Replace(ecKeyFooter, "").Trim();
@@ -427,22 +434,13 @@ private byte[] ConvertToECDSAANS1Format(byte[] signedBytes)
427434
return derBytes.ToArray();
428435
}
429436

430-
private RSACryptoServiceProvider GetRSAProviderFromPemFile(string pemfile, SecureString keyPassPhrase = null)
437+
private RSACryptoServiceProvider GetRSAProviderFromPemFile(string keyString, SecureString keyPassPhrase = null)
431438
{
432439
const string pempubheader = "-----BEGIN PUBLIC KEY-----";
433440
const string pempubfooter = "-----END PUBLIC KEY-----";
434441
bool isPrivateKeyFile = true;
435442
byte[] pemkey = null;
436-
437-
string pemstr = string.Empty;
438-
if (File.Exists(pemfile))
439-
{
440-
pemstr = File.ReadAllText(pemfile).Trim();
441-
}
442-
else
443-
{
444-
pemstr = pemfile;
445-
}
443+
string pemstr = keyString;
446444

447445
if (pemstr.StartsWith(pempubheader) && pemstr.EndsWith(pempubfooter))
448446
{
@@ -729,20 +727,15 @@ private byte[] DecryptKey(byte[] cipherData, byte[] desKey, byte[] IV)
729727
/// <summary>
730728
/// Detect the key type from the pem file.
731729
/// </summary>
732-
/// <param name="keyFilePath">key file path in pem format</param>
730+
/// <param name="keyString">api key in string format</param>
733731
/// <returns>Private Key Type</returns>
734-
private PrivateKeyType GetKeyType(string keyFilePath)
732+
private PrivateKeyType GetKeyType(string keyString)
735733
{
736734
string[] key = null;
737735

738-
if (File.Exists(keyFilePath))
739-
{
740-
key = File.ReadAllLines(keyFilePath);
741-
}
742-
else
736+
if (string.IsNullOrEmpty(keyString))
743737
{
744-
// The ApiKeyFilePath is passed as string
745-
key = new string[] { keyFilePath };
738+
throw new Exception("No API key has been provided.");
746739
}
747740

748741
const string ecPrivateKeyHeader = "BEGIN EC PRIVATE KEY";
@@ -752,6 +745,7 @@ private PrivateKeyType GetKeyType(string keyFilePath)
752745
//var pkcs8Header = "BEGIN PRIVATE KEY";
753746
//var pkcs8Footer = "END PRIVATE KEY";
754747
PrivateKeyType keyType;
748+
key = KeyString.TrimEnd().Split("\n");
755749

756750
if (key[0].Contains(rsaPrivateKeyHeader) &&
757751
key[key.Length - 1].ToString().Contains(rsaPrivateFooter))
@@ -769,6 +763,29 @@ private PrivateKeyType GetKeyType(string keyFilePath)
769763
}
770764
return keyType;
771765
}
766+
767+
/// <summary>
768+
/// Read the api key form the api key file path and stored it in KeyString property.
769+
/// </summary>
770+
/// <param name="apiKeyFilePath">api key file path</param>
771+
private string ReadApiKeyFromFile(string apiKeyFilePath)
772+
{
773+
string apiKeyString = null;
774+
if(!string.IsNullOrEmpty(apiKeyFilePath) && !string.IsNullOrEmpty(this.KeyString))
775+
{
776+
throw new Exception("Configure either the KeyFilePath or configure the KeyString property.");
777+
}
778+
779+
if(File.Exists(apiKeyFilePath))
780+
{
781+
apiKeyString = File.ReadAllText(apiKeyFilePath);
782+
}
783+
else
784+
{
785+
throw new Exception("Provided API key file path does not exists.");
786+
}
787+
return apiKeyString;
788+
}
772789
#endregion
773790
}
774791
}

samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ public HttpSigningConfiguration()
4848
/// </summary>
4949
public string KeyFilePath { get; set; }
5050

51+
/// <summary>
52+
/// Specify the API key in the form of a string, either configure the KeyString property or configure the KeyFilePath property.
53+
/// </summary>
54+
public string KeyString { get; set; }
55+
5156
/// <summary>
5257
/// Gets the key pass phrase for password protected key
5358
/// </summary>
@@ -112,6 +117,17 @@ internal Dictionary<string, string> GetHttpSignedHeader(string basePath,string m
112117
//the list of signed headers and a base64-encoded signature.
113118
const string HEADER_AUTHORIZATION = "Authorization";
114119

120+
//Read the api key from the file
121+
if(!string.IsNullOrEmpty(this.KeyFilePath))
122+
{
123+
this.KeyString = ReadApiKeyFromFile(KeyFilePath);
124+
}
125+
126+
if(string.IsNullOrEmpty(KeyString))
127+
{
128+
throw new Exception("No API key has been provided.");
129+
}
130+
115131
//Hash table to store singed headers
116132
var HttpSignedRequestHeader = new Dictionary<string, string>();
117133
var HttpSignatureHeader = new Dictionary<string, string>();
@@ -250,7 +266,7 @@ internal Dictionary<string, string> GetHttpSignedHeader(string basePath,string m
250266
var headerValuesString = string.Join("\n", headerValuesList);
251267
var signatureStringHash = GetStringHash(HashAlgorithm.ToString(), headerValuesString);
252268
string headerSignatureStr = null;
253-
var keyType = GetKeyType(KeyFilePath);
269+
var keyType = GetKeyType(KeyString);
254270

255271
if (keyType == PrivateKeyType.RSA)
256272
{
@@ -301,7 +317,7 @@ private int GetUnixTime(DateTime date2)
301317

302318
private string GetRSASignature(byte[] stringToSign)
303319
{
304-
RSA rsa = GetRSAProviderFromPemFile(KeyFilePath, KeyPassPhrase);
320+
RSA rsa = GetRSAProviderFromPemFile(KeyString, KeyPassPhrase);
305321
if (SigningAlgorithm == "RSASSA-PSS")
306322
{
307323
var signedbytes = rsa.SignHash(stringToSign, HashAlgorithm, RSASignaturePadding.Pss);
@@ -325,16 +341,7 @@ private string GetRSASignature(byte[] stringToSign)
325341
/// <returns>ECDSA signature</returns>
326342
private string GetECDSASignature(byte[] dataToSign)
327343
{
328-
string keyStr = string.Empty;
329-
if (File.Exists(KeyFilePath))
330-
{
331-
keyStr = File.ReadAllText(KeyFilePath);
332-
}
333-
else
334-
{
335-
keyStr = KeyFilePath;
336-
}
337-
344+
var keyStr = KeyString;
338345
const string ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----";
339346
const string ecKeyFooter = "-----END EC PRIVATE KEY-----";
340347
var ecKeyBase64String = keyStr.Replace(ecKeyHeader, "").Replace(ecKeyFooter, "").Trim();
@@ -427,22 +434,13 @@ private byte[] ConvertToECDSAANS1Format(byte[] signedBytes)
427434
return derBytes.ToArray();
428435
}
429436

430-
private RSACryptoServiceProvider GetRSAProviderFromPemFile(string pemfile, SecureString keyPassPhrase = null)
437+
private RSACryptoServiceProvider GetRSAProviderFromPemFile(string keyString, SecureString keyPassPhrase = null)
431438
{
432439
const string pempubheader = "-----BEGIN PUBLIC KEY-----";
433440
const string pempubfooter = "-----END PUBLIC KEY-----";
434441
bool isPrivateKeyFile = true;
435442
byte[] pemkey = null;
436-
437-
string pemstr = string.Empty;
438-
if (File.Exists(pemfile))
439-
{
440-
pemstr = File.ReadAllText(pemfile).Trim();
441-
}
442-
else
443-
{
444-
pemstr = pemfile;
445-
}
443+
string pemstr = keyString;
446444

447445
if (pemstr.StartsWith(pempubheader) && pemstr.EndsWith(pempubfooter))
448446
{
@@ -729,20 +727,15 @@ private byte[] DecryptKey(byte[] cipherData, byte[] desKey, byte[] IV)
729727
/// <summary>
730728
/// Detect the key type from the pem file.
731729
/// </summary>
732-
/// <param name="keyFilePath">key file path in pem format</param>
730+
/// <param name="keyString">api key in string format</param>
733731
/// <returns>Private Key Type</returns>
734-
private PrivateKeyType GetKeyType(string keyFilePath)
732+
private PrivateKeyType GetKeyType(string keyString)
735733
{
736734
string[] key = null;
737735

738-
if (File.Exists(keyFilePath))
739-
{
740-
key = File.ReadAllLines(keyFilePath);
741-
}
742-
else
736+
if (string.IsNullOrEmpty(keyString))
743737
{
744-
// The ApiKeyFilePath is passed as string
745-
key = new string[] { keyFilePath };
738+
throw new Exception("No API key has been provided.");
746739
}
747740

748741
const string ecPrivateKeyHeader = "BEGIN EC PRIVATE KEY";
@@ -752,6 +745,7 @@ private PrivateKeyType GetKeyType(string keyFilePath)
752745
//var pkcs8Header = "BEGIN PRIVATE KEY";
753746
//var pkcs8Footer = "END PRIVATE KEY";
754747
PrivateKeyType keyType;
748+
key = KeyString.TrimEnd().Split("\n");
755749

756750
if (key[0].Contains(rsaPrivateKeyHeader) &&
757751
key[key.Length - 1].ToString().Contains(rsaPrivateFooter))
@@ -769,6 +763,29 @@ private PrivateKeyType GetKeyType(string keyFilePath)
769763
}
770764
return keyType;
771765
}
766+
767+
/// <summary>
768+
/// Read the api key form the api key file path and stored it in KeyString property.
769+
/// </summary>
770+
/// <param name="apiKeyFilePath">api key file path</param>
771+
private string ReadApiKeyFromFile(string apiKeyFilePath)
772+
{
773+
string apiKeyString = null;
774+
if(!string.IsNullOrEmpty(apiKeyFilePath) && !string.IsNullOrEmpty(this.KeyString))
775+
{
776+
throw new Exception("Configure either the KeyFilePath or configure the KeyString property.");
777+
}
778+
779+
if(File.Exists(apiKeyFilePath))
780+
{
781+
apiKeyString = File.ReadAllText(apiKeyFilePath);
782+
}
783+
else
784+
{
785+
throw new Exception("Provided API key file path does not exists.");
786+
}
787+
return apiKeyString;
788+
}
772789
#endregion
773790
}
774791
}

0 commit comments

Comments
 (0)