-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added HMAC signature from core assembly and added matching rest clien…
…t overloads. (#443)
- Loading branch information
1 parent
5f1ab99
commit 7ad7637
Showing
5 changed files
with
254 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/NoFrixion.MoneyMoov/HmacSignature/HmacAuthenticationConstants.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// ----------------------------------------------------------------------------- | ||
// Filename: HmacAuthenticationConstants.cs | ||
// | ||
// Description: Constants for HMAC authentication: | ||
// | ||
// Author(s): | ||
// Donal O'Connor ([email protected]) | ||
// | ||
// History: | ||
// 22 04 2024 Donal O'Connor Created, Harcourt St, Dublin, Ireland. | ||
// | ||
// License: | ||
// MIT. | ||
// ----------------------------------------------------------------------------- | ||
|
||
namespace NoFrixion.MoneyMoov; | ||
|
||
public static class HmacAuthenticationConstants | ||
{ | ||
public const string SIGNATURE_SCHEME_NAME = "Signature"; | ||
public const string APP_ID_HEADER_NAME = "appId"; | ||
public const string AUTHORIZATION_HEADER_NAME = "Authorization"; | ||
public const string DATE_HEADER_NAME = "Date"; | ||
public const string NONCE_HEADER_NAME = "x-mod-nonce"; | ||
public const string MERCHANT_ID_HEADER_NAME = "x-nfx-merchantid"; | ||
public const string NOFRIXION_SIGNATURE_HEADER_NAME = "x-nfx-signature"; | ||
public const string HTTP_RETRY_HEADER_NAME = "x-mod-retry"; | ||
public const string IDEMPOTENT_HEADER_NAME = "idempotency-key"; | ||
} |
106 changes: 106 additions & 0 deletions
106
src/NoFrixion.MoneyMoov/HmacSignature/HmacSignatureAuthHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// ----------------------------------------------------------------------------- | ||
// Filename: HmacSignatureAuthHelper.cs | ||
// | ||
// Description: Used for generating HMAC signatures and verifying them. | ||
// | ||
// Author(s): | ||
// Donal O'Connor ([email protected]) | ||
// | ||
// History: | ||
// 08 02 2022 Donal O'Connor Created, Carmichael House, | ||
// Dublin, Ireland. | ||
// | ||
// License: | ||
// MIT. | ||
// ----------------------------------------------------------------------------- | ||
|
||
using System.Net; | ||
using System.Security.Cryptography; | ||
using System.Text; | ||
|
||
namespace NoFrixion.MoneyMoov; | ||
|
||
public static class HmacSignatureAuthHelper | ||
{ | ||
public static Dictionary<string, string> GetAppHeaders(string appId, | ||
string idempotencyKey, | ||
string secret, | ||
DateTime date, | ||
Guid merchantId) | ||
{ | ||
var signature = GenerateSignature(idempotencyKey, date, secret, true); | ||
|
||
var headers = new Dictionary<string, string> | ||
{ | ||
{HmacAuthenticationConstants.AUTHORIZATION_HEADER_NAME, GenerateAppAuthHeaderContent(appId, signature)}, | ||
{HmacAuthenticationConstants.DATE_HEADER_NAME, date.ToString("R")}, | ||
{HmacAuthenticationConstants.IDEMPOTENT_HEADER_NAME, idempotencyKey}, | ||
{HmacAuthenticationConstants.MERCHANT_ID_HEADER_NAME, merchantId.ToString()}, | ||
}; | ||
|
||
return headers; | ||
} | ||
|
||
public static Dictionary<string, string> GetHeaders(string keyId, | ||
string nonce, | ||
string secret, | ||
DateTime date, | ||
bool asRetry = false) | ||
{ | ||
var signature = GenerateSignature(nonce, date, secret); | ||
|
||
var headers = new Dictionary<string, string> | ||
{ | ||
{HmacAuthenticationConstants.AUTHORIZATION_HEADER_NAME, GenerateAuthHeaderContent(keyId, signature)}, | ||
{HmacAuthenticationConstants.DATE_HEADER_NAME, date.ToString("R")}, | ||
{HmacAuthenticationConstants.NONCE_HEADER_NAME, nonce}, | ||
{HmacAuthenticationConstants.HTTP_RETRY_HEADER_NAME, asRetry.ToString().ToLower()}, | ||
{HmacAuthenticationConstants.NOFRIXION_SIGNATURE_HEADER_NAME, signature}, | ||
}; | ||
|
||
return headers; | ||
} | ||
|
||
public static string GenerateSignature(string nonce, DateTime date, string secret, bool hmac256 = false) | ||
{ | ||
return hmac256 ? | ||
HashAndEncode256($"date: {date:R}\n{HmacAuthenticationConstants.IDEMPOTENT_HEADER_NAME}: {nonce}", secret) : | ||
HashAndEncode($"date: {date:R}\n{HmacAuthenticationConstants.NONCE_HEADER_NAME}: {nonce}", secret); | ||
} | ||
|
||
private static string GenerateAppAuthHeaderContent(string apiKey, string signature) | ||
{ | ||
return $"Signature appId=\"{apiKey}\",headers=\"date {HmacAuthenticationConstants.IDEMPOTENT_HEADER_NAME}\",signature=\"{signature}\""; | ||
} | ||
|
||
private static string GenerateAuthHeaderContent(string apiKey, string signature) | ||
{ | ||
return $"Signature keyId=\"{apiKey}\",headers=\"date x-mod-nonce\",signature=\"{signature}\""; | ||
} | ||
|
||
private static string HashAndEncode(string message, string secret) | ||
{ | ||
var ascii = Encoding.ASCII; | ||
|
||
HMACSHA1 hmac = new HMACSHA1(ascii.GetBytes(secret)); | ||
hmac.Initialize(); | ||
|
||
byte[] messageBuffer = ascii.GetBytes(message); | ||
byte[] hash = hmac.ComputeHash(messageBuffer); | ||
|
||
return WebUtility.UrlEncode(Convert.ToBase64String(hash)); | ||
} | ||
|
||
private static string HashAndEncode256(string message, string secret) | ||
{ | ||
var ascii = Encoding.ASCII; | ||
|
||
var hmac = new HMACSHA256(ascii.GetBytes(secret)); | ||
hmac.Initialize(); | ||
|
||
var messageBuffer = ascii.GetBytes(message); | ||
var hash = hmac.ComputeHash(messageBuffer); | ||
|
||
return WebUtility.UrlEncode(Convert.ToBase64String(hash)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters