'How authenticate against https://postman-echo.com/auth/hawk example?
I want to develop a simple application wich make some request to an API that uses Hawk Authentication. For make the Authentication easier I found that PostMan has an open API to try this. On PostMan app the request works good but when I tried to do it in .NET Core using HawkNet library I recieved a 401 UnAuthorize response.
I've tried almost anything and there is no documentation or examples but the ones in HawkNet's repository.
¿Can any one knows how to create correctly the mac?
Extra Info: PostMan echo api doc - https://docs.postman-echo.com/?version=latest#843acf02-a33c-c4bb-d742-c07b9212e4b0
Example code that I've been trying:
var httpClient = new HttpClient();
var requestUri = new Uri("https://postman-echo.com/auth/hawk");
var csharpCredential = new HawkNet.HawkCredential()
{
Id = "dh37fgj492je",
Key = "werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn",
Algorithm = "SHA256"
};
var header = HawkNet.Hawk.GetAuthorizationHeader("postman-echo.com", "GET", requestUri, csharpCredential);
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Hawk", header);
var response = await httpClient.GetAsync(requestUri);
Solution 1:[1]
Even your question is quite old now I stumbled on it while trying so solve the exact same Problem. The above approach didn't work for me either. What was working for me was calculating the mac myself. Hope this will save anybody some time in the future :)
var credential = new HawkCredential
{
Id = "dh37fgj492je",
Algorithm = "sha256",
Key = "werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn",
};
var date = DateTime.Now;
var ts = Hawk.ConvertToUnixTimestamp(date);
var mac = Hawk.CalculateMac("postman-echo.com", "get", new Uri("https://postman-echo.com/auth/hawk"),
"hello", ts.ToString(), "k3j4h2", credential, "header");
var authorization = string.Format(
"id=\"dh37fgj492je\", ts=\"{0}\", nonce=\"k3j4h2\", mac=\"{1}\", ext=\"hello\"",
ts, mac);
var request = new HttpRequestMessage(HttpMethod.Get, "https://postman-echo.com/auth/hawk");
request.Headers.Host = "postman-echo.com";
request.Headers.Authorization =
new System.Net.Http.Headers.AuthenticationHeaderValue("Hawk", authorization);
var client = _clientFactory.CreateClient();
var response = await client.SendAsync(request);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | vernou |
