-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathAPI.asmx.cs
More file actions
54 lines (45 loc) · 1.91 KB
/
API.asmx.cs
File metadata and controls
54 lines (45 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System.Web.Services;
using Google.Authenticator;
namespace AuthenticatorAPI
{
/// <summary>
/// API to generate and validate google authenticator
/// </summary>
[WebService(Namespace = "http://authenticatorapi.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class AuthenticatorAPI : WebService
{
[WebMethod]
public PairingInfo Pair(string appName, string appInfo, string secretCode)
{
var tfa = new TwoFactorAuthenticator();
var setupInfo = tfa.GenerateSetupCode(appName, appInfo, secretCode, 300, 300);
var newQrUrl = ConvertToNewUrl(setupInfo.QrCodeSetupImageUrl);
var html = "<a title='Manually pair with " + setupInfo.ManualEntryKey +
"' href='https://www.authenticatorapi.com'><img src='" + newQrUrl.Replace("http://","https://") +
"' border=0></a>";
return new PairingInfo { ManualSetupCode = setupInfo.ManualEntryKey, Html = html };
}
static string ConvertToNewUrl(string originalUrl)
{
// Replace the necessary parts of the original URL
var convertedUrl = originalUrl.Replace("chart.googleapis.com/chart?cht=qr&chs=", "www.AuthenticatorAPI.com/qr.aspx?size=");
convertedUrl = convertedUrl.Replace("&chl=", "&data=");
return convertedUrl;
}
[WebMethod]
public bool ValidatePin(string pin, string secretCode)
{
var tfa = new TwoFactorAuthenticator();
var isCorrectPin = tfa.ValidateTwoFactorPIN(secretCode, pin);
return isCorrectPin;
}
}
public class PairingInfo
{
public string ManualSetupCode { get; set; }
public string Html { get; set; }
}
}