Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
5c28f79
Merge pull request #26 from OpenAPITools/master
code-lucidal58 Nov 2, 2020
f4028ff
Merge pull request #27 from OpenAPITools/master
code-lucidal58 Dec 17, 2020
5d1956b
Merge branch 'OpenAPITools:master' into master
code-lucidal58 May 11, 2021
2efa03e
Merge branch 'master' of ssh://github.com/OpenAPITools/openapi-genera…
vvb Aug 13, 2021
232cc89
Merge pull request #28 from CiscoM31/feature/refresh_master
vvb Aug 13, 2021
d3e3725
Merge branch 'master' of ssh://github.com/OpenAPITools/openapi-genera…
vvb Aug 18, 2021
91716ea
Merge pull request #42 from CiscoM31/refresh/master
vvb Aug 18, 2021
76f570e
Merge branch 'master' of ssh://github.com/OpenAPITools/openapi-genera…
vvb Aug 23, 2021
3ba948b
Merge pull request #43 from CiscoM31/refresh/master
vvb Aug 23, 2021
56f32d5
Merge branch 'master' of ssh://github.com/OpenAPITools/openapi-genera…
vvb Aug 25, 2021
058f491
Merge pull request #44 from CiscoM31/refresh/master
vvb Aug 25, 2021
b19bb30
Merge branch 'OpenAPITools:master' into master
vvb Sep 3, 2021
d58e4a2
Merge branch 'OpenAPITools:master' into master
sebastien-rosset Oct 11, 2021
7cd305c
Merge branch 'OpenAPITools:master' into master
sebastien-rosset Oct 12, 2021
6debc0b
Merge branch 'master' of ssh://github.com/OpenAPITools/openapi-genera…
vvb Mar 31, 2022
dfb40dc
Merge pull request #62 from CiscoM31/refresh/upstream
vvb Mar 31, 2022
a0e05b8
Merge pull request #78 from CiscoM31/feature/refresh_13_Jul_2022
vvb Jul 13, 2022
9264d06
Merge pull request #2 from CiscoM31/master
Ghufz Jun 8, 2023
d4de734
Merge branch 'OpenAPITools:master' into master
Ghufz Jun 12, 2023
d692ea4
added property in HttpSIgningConfiguration to accept API key in strin…
Ghufz Jun 12, 2023
a899c8d
Merge branch 'OpenAPITools:master' into apikey_string
Ghufz Jun 12, 2023
c384fe3
remove trailing space, update samples
wing328 Jun 12, 2023
5b945c7
updated the sample code.
Ghufz Jun 12, 2023
811a62a
Revert "updated the sample code."
Ghufz Jun 13, 2023
8f2c9f6
Merge branch 'apikey_string' of github.com:Ghufz/openapi-generator in…
Ghufz Jun 13, 2023
ec6404d
fix the sample code compilation error for split function.
Ghufz Jun 13, 2023
368a2e4
updated the sample code after the split function fix.
Ghufz Jun 13, 2023
a9a5856
Removed the either or check for filePath or KeyString.
Ghufz Jun 13, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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>();
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -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))
{
Expand Down Expand Up @@ -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";
Expand All @@ -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))
Expand All @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ public HttpSigningConfiguration()
/// </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>
Expand Down Expand Up @@ -112,6 +117,17 @@ internal Dictionary<string, string> GetHttpSignedHeader(string basePath,string m
//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>();
Expand Down Expand Up @@ -250,7 +266,7 @@ internal Dictionary<string, string> GetHttpSignedHeader(string basePath,string m
var headerValuesString = string.Join("\n", headerValuesList);
var signatureStringHash = GetStringHash(HashAlgorithm.ToString(), headerValuesString);
string headerSignatureStr = null;
var keyType = GetKeyType(KeyFilePath);
var keyType = GetKeyType(KeyString);

if (keyType == PrivateKeyType.RSA)
{
Expand Down Expand Up @@ -301,7 +317,7 @@ private int GetUnixTime(DateTime date2)

private string GetRSASignature(byte[] stringToSign)
{
RSA rsa = GetRSAProviderFromPemFile(KeyFilePath, KeyPassPhrase);
RSA rsa = GetRSAProviderFromPemFile(KeyString, KeyPassPhrase);
if (SigningAlgorithm == "RSASSA-PSS")
{
var signedbytes = rsa.SignHash(stringToSign, HashAlgorithm, RSASignaturePadding.Pss);
Expand All @@ -325,16 +341,7 @@ private string GetRSASignature(byte[] stringToSign)
/// <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();
Expand Down Expand Up @@ -427,22 +434,13 @@ private byte[] ConvertToECDSAANS1Format(byte[] signedBytes)
return derBytes.ToArray();
}

private RSACryptoServiceProvider GetRSAProviderFromPemFile(string pemfile, SecureString keyPassPhrase = null)
private RSACryptoServiceProvider GetRSAProviderFromPemFile(string keyString, SecureString keyPassPhrase = null)
{
const string pempubheader = "-----BEGIN PUBLIC KEY-----";
const string pempubfooter = "-----END PUBLIC KEY-----";
bool isPrivateKeyFile = true;
byte[] pemkey = null;

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))
{
Expand Down Expand Up @@ -729,20 +727,15 @@ private byte[] DecryptKey(byte[] cipherData, byte[] desKey, byte[] IV)
/// <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";
Expand All @@ -752,6 +745,7 @@ private PrivateKeyType GetKeyType(string keyFilePath)
//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))
Expand All @@ -769,6 +763,25 @@ private PrivateKeyType GetKeyType(string keyFilePath)
}
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