Skip to content

Instantly share code, notes, and snippets.

@kirk-marple
Created February 13, 2020 04:09
Show Gist options
  • Save kirk-marple/9429bb53a969fd2b5f2c0c6fc6d61da6 to your computer and use it in GitHub Desktop.
Save kirk-marple/9429bb53a969fd2b5f2c0c6fc6d61da6 to your computer and use it in GitHub Desktop.
Verify Authy one-time token
private async Task<bool> VerifyAuthyTokenAsync(ILogger logger, IConfiguration configuration, string authyId, string token)
{
var apiKey = configuration.GetSection("AuthySettings")?["ApiKey"];
if (String.IsNullOrEmpty(apiKey))
throw new InvalidOperationException("Invalid Authy API key.");
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("X-Authy-API-Key", apiKey);
HttpResponseMessage response = await client.GetAsync($"https://api.authy.com/protected/json/verify/{token}/{authyId}");
var unauthorized = (response.StatusCode == HttpStatusCode.Unauthorized);
var body = await response.Content.ReadAsStringAsync();
logger?.LogInformation($"Recognized {(unauthorized ? "unauthorized" : "authorized")} token.");
logger?.LogDebug(body);
return !unauthorized;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment