-
-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
43c6487
commit 3228d9d
Showing
5 changed files
with
134 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using Midjourney.Infrastructure.Services; | ||
|
||
namespace Midjourney.Captcha.API.Controllers | ||
{ | ||
/// <summary> | ||
/// 2FA | ||
/// </summary> | ||
[ApiController] | ||
[Route("/")] | ||
public class TwoFAController : Controller | ||
{ | ||
[HttpGet("{secret}")] | ||
public IActionResult GetOtp(string secret) | ||
{ | ||
if (string.IsNullOrEmpty(secret)) | ||
{ | ||
return BadRequest("Missing secret parameter"); | ||
} | ||
|
||
var loadTime = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); | ||
var otp = TwoFAHelper.GenerateOtp(secret, loadTime); | ||
|
||
var remainingTime = TwoFAHelper.CalculateRemainingTime(loadTime); | ||
|
||
var htmlContent = $@" | ||
<html> | ||
<head> | ||
<title>2FA Auth</title> | ||
<script> | ||
const loadTime = {loadTime}; | ||
const remainingTime = {remainingTime}; | ||
if (remainingTime <= 0) {{ | ||
location.reload(); | ||
}} else {{ | ||
setTimeout(() => {{ | ||
location.reload(); | ||
}}, remainingTime * 1000); | ||
}} | ||
</script> | ||
</head> | ||
<body> | ||
<pre>{{ | ||
""token"": ""{otp}"" | ||
}}</pre> | ||
</body> | ||
</html> | ||
"; | ||
return Content(htmlContent, "text/html"); | ||
} | ||
} | ||
} |
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
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using System.Globalization; | ||
using System.Security.Cryptography; | ||
using System.Text; | ||
|
||
namespace Midjourney.Infrastructure.Services | ||
{ | ||
/// <summary> | ||
/// 2fa 验证帮助类 | ||
/// https://github.com/wuzf/2fa/blob/main/worker.js | ||
/// </summary> | ||
public class TwoFAHelper | ||
{ | ||
public static string GenerateOtp(string secret) | ||
{ | ||
var loadTime = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); | ||
var otp = GenerateOtp(secret, loadTime); | ||
return otp; | ||
} | ||
|
||
public static string GenerateOtp(string secret, long loadTime) | ||
{ | ||
const int timeStep = 30; | ||
|
||
var counter = loadTime / timeStep; | ||
var counterBytes = BitConverter.GetBytes(counter); | ||
if (BitConverter.IsLittleEndian) | ||
{ | ||
Array.Reverse(counterBytes); | ||
} | ||
|
||
var key = Base32Decode(secret); | ||
|
||
using var hmac = new HMACSHA1(key); | ||
var hash = hmac.ComputeHash(counterBytes); | ||
|
||
var offset = hash[^1] & 0xF; | ||
var binaryCode = ((hash[offset] & 0x7F) << 24) | | ||
((hash[offset + 1] & 0xFF) << 16) | | ||
((hash[offset + 2] & 0xFF) << 8) | | ||
(hash[offset + 3] & 0xFF); | ||
|
||
var otp = (binaryCode % 1_000_000).ToString("D6", CultureInfo.InvariantCulture); | ||
return otp; | ||
} | ||
|
||
private static byte[] Base32Decode(string base32) | ||
{ | ||
const string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; | ||
base32 = base32.ToUpperInvariant(); | ||
|
||
var bits = new StringBuilder(); | ||
foreach (var c in base32) | ||
{ | ||
var index = alphabet.IndexOf(c); | ||
if (index < 0) throw new ArgumentException("Invalid Base32 character."); | ||
bits.Append(Convert.ToString(index, 2).PadLeft(5, '0')); | ||
} | ||
|
||
var byteList = new List<byte>(); | ||
for (int i = 0; i + 8 <= bits.Length; i += 8) | ||
{ | ||
byteList.Add(Convert.ToByte(bits.ToString(i, 8), 2)); | ||
} | ||
|
||
return byteList.ToArray(); | ||
} | ||
|
||
public static int CalculateRemainingTime(long loadTime) | ||
{ | ||
const int timeStep = 30; | ||
var epochTime = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); | ||
var currentCounter = epochTime / timeStep; | ||
var expirationTime = (currentCounter + 1) * timeStep; | ||
return (int)(expirationTime - loadTime); | ||
} | ||
} | ||
} |