This commit is contained in:
@@ -428,13 +428,13 @@ namespace AyaNova.Api.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Generate HOTP secret and return for use in auth app
|
/// Generate TOTP secret and return for use in auth app
|
||||||
///
|
///
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="apiVersion">From route path</param>
|
/// <param name="apiVersion">From route path</param>
|
||||||
/// <returns>New HOTP secret</returns>
|
/// <returns>New TOTP secret</returns>
|
||||||
[HttpGet("hotp")]
|
[HttpGet("totp")]
|
||||||
public async Task<IActionResult> GenerateAndSendHOTP(ApiVersion apiVersion)
|
public async Task<IActionResult> GenerateAndSendTOTP(ApiVersion apiVersion)
|
||||||
{
|
{
|
||||||
if (!serverState.IsOpen)
|
if (!serverState.IsOpen)
|
||||||
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
|
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
|
||||||
@@ -451,18 +451,33 @@ namespace AyaNova.Api.Controllers
|
|||||||
return StatusCode(403, new ApiNotAuthorizedResponse());
|
return StatusCode(403, new ApiNotAuthorizedResponse());
|
||||||
|
|
||||||
var tfa = new TwoFactorAuth("AyaNova");
|
var tfa = new TwoFactorAuth("AyaNova");
|
||||||
u.HotpSecret = tfa.CreateSecret(160);
|
u.TotpSecret = tfa.CreateSecret(160);
|
||||||
await ct.SaveChangesAsync();
|
await ct.SaveChangesAsync();
|
||||||
|
|
||||||
|
//https://github.com/google/google-authenticator/wiki/Key-Uri-Format
|
||||||
|
|
||||||
|
QRCoder.PayloadGenerator.OneTimePassword generator = new QRCoder.PayloadGenerator.OneTimePassword()
|
||||||
|
{
|
||||||
|
Secret = u.TotpSecret,
|
||||||
|
Issuer = "AyaNova",
|
||||||
|
//Label = $"AyaNova.{u.Id}",
|
||||||
|
Type = QRCoder.PayloadGenerator.OneTimePassword.OneTimePasswordAuthType.TOTP
|
||||||
|
};
|
||||||
|
string payload = generator.ToString();
|
||||||
|
|
||||||
|
// QRCodeGenerator qrGenerator = new QRCodeGenerator();
|
||||||
|
// QRCodeData qrCodeData = qrGenerator.CreateQrCode(payload, QRCodeGenerator.ECCLevel.Q);
|
||||||
|
// QRCode qrCode = new QRCode(qrCodeData);
|
||||||
|
// var qrCodeAsBitmap = qrCode.GetGraphic(20);
|
||||||
|
|
||||||
QRCodeGenerator qrGenerator = new QRCodeGenerator();
|
QRCodeGenerator qrGenerator = new QRCodeGenerator();
|
||||||
QRCodeData qrCodeData = qrGenerator.CreateQrCode(u.HotpSecret, QRCodeGenerator.ECCLevel.Q);
|
QRCodeData qrCodeData = qrGenerator.CreateQrCode(payload, QRCodeGenerator.ECCLevel.Q);
|
||||||
Base64QRCode qrCode = new Base64QRCode(qrCodeData);
|
Base64QRCode qrCode = new Base64QRCode(qrCodeData);
|
||||||
string qrCodeImageAsBase64 = qrCode.GetGraphic(20);
|
string qrCodeImageAsBase64 = qrCode.GetGraphic(4);
|
||||||
|
|
||||||
return Ok(ApiOkResponse.Response(new
|
return Ok(ApiOkResponse.Response(new
|
||||||
{
|
{
|
||||||
s = u.HotpSecret,
|
s = u.TotpSecret,
|
||||||
qr = qrCodeImageAsBase64
|
qr = qrCodeImageAsBase64
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -97,7 +97,7 @@ namespace AyaNova.Models
|
|||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
public DateTime? PasswordResetCodeExpire { get; set; }//---
|
public DateTime? PasswordResetCodeExpire { get; set; }//---
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
public string HotpSecret { get; set; }//---
|
public string TotpSecret { get; set; }//---
|
||||||
//==========================
|
//==========================
|
||||||
|
|
||||||
//relations
|
//relations
|
||||||
|
|||||||
@@ -441,7 +441,7 @@ $BODY$ LANGUAGE PLPGSQL STABLE");
|
|||||||
//Add user table
|
//Add user table
|
||||||
await ExecQueryAsync("CREATE TABLE auser (id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY, active BOOL NOT NULL, name TEXT NOT NULL UNIQUE, "
|
await ExecQueryAsync("CREATE TABLE auser (id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY, active BOOL NOT NULL, name TEXT NOT NULL UNIQUE, "
|
||||||
+ "lastlogin TIMESTAMP, login TEXT NOT NULL UNIQUE, password TEXT NOT NULL, salt TEXT NOT NULL, roles INTEGER NOT NULL, currentauthtoken TEXT, "
|
+ "lastlogin TIMESTAMP, login TEXT NOT NULL UNIQUE, password TEXT NOT NULL, salt TEXT NOT NULL, roles INTEGER NOT NULL, currentauthtoken TEXT, "
|
||||||
+ "dlkey TEXT, dlkeyexpire TIMESTAMP, hotpsecret TEXT, twofactorenabled BOOL, passwordresetcode TEXT, passwordresetcodeexpire TIMESTAMP, usertype INTEGER NOT NULL, "
|
+ "dlkey TEXT, dlkeyexpire TIMESTAMP, totpsecret TEXT, twofactorenabled BOOL, passwordresetcode TEXT, passwordresetcodeexpire TIMESTAMP, usertype INTEGER NOT NULL, "
|
||||||
+ "employeenumber TEXT, notes TEXT, customerid BIGINT, "
|
+ "employeenumber TEXT, notes TEXT, customerid BIGINT, "
|
||||||
+ "headofficeid BIGINT, vendorid BIGINT, wiki TEXT, customfields TEXT, tags VARCHAR(255) ARRAY)");
|
+ "headofficeid BIGINT, vendorid BIGINT, wiki TEXT, customfields TEXT, tags VARCHAR(255) ARRAY)");
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user