Skip to content

Commit

Permalink
Add QR code generator.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbound committed Feb 22, 2024
1 parent 07905ee commit 91daac9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 60 deletions.
34 changes: 31 additions & 3 deletions Server/Components/Account/Pages/Manage/EnableAuthenticator.razor
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
@using System.Text.Encodings.Web
@using Microsoft.AspNetCore.Identity
@using Remotely.Server.Data
@using QRCoder;
@using QRCoder.Extensions;

@inject UserManager<RemotelyUser> UserManager
@inject IdentityUserAccessor UserAccessor
Expand Down Expand Up @@ -38,8 +40,16 @@ else
</li>
<li>
<p>Scan the QR Code or enter this key <kbd>@sharedKey</kbd> into your two factor authenticator app. Spaces and casing do not matter.</p>
<div class="alert alert-info">Learn how to <a href="https://go.microsoft.com/fwlink/?Linkid=852423">enable QR code generation</a>.</div>
<div></div>
<div>
@if (!string.IsNullOrWhiteSpace(_qrCodeDataUri))
{
<img src="@_qrCodeDataUri" alt="QR code" class="img-fluid" />
}
else
{
<div class="alert alert-warning">Failed to generate QR code.</div>
}
</div>
<div data-url="@authenticatorUri"></div>
</li>
<li>
Expand Down Expand Up @@ -73,6 +83,7 @@ else
private RemotelyUser user = default!;
private string? sharedKey;
private string? authenticatorUri;
private string? _qrCodeDataUri;
private IEnumerable<string>? recoveryCodes;

[CascadingParameter]
Expand Down Expand Up @@ -132,6 +143,23 @@ else

var email = await UserManager.GetEmailAsync(user);
authenticatorUri = GenerateQrCodeUri(email!, unformattedKey!);
_qrCodeDataUri = GetQrCodeDataUri(authenticatorUri);
}

private string? GetQrCodeDataUri(string authenticatorUri)
{
try
{
var qrCodeBytes = PngByteQRCodeHelper.GetQRCode(authenticatorUri, QRCodeGenerator.ECCLevel.Q, 5);
var base64QrCode = Convert.ToBase64String(qrCodeBytes);
return $"data:image/png;base64,{base64QrCode}";
}
catch (Exception ex)
{
Logger.LogError(ex, "Error generating QR code");
return null;
}

}

private string FormatKey(string unformattedKey)
Expand All @@ -156,7 +184,7 @@ else
return string.Format(
CultureInfo.InvariantCulture,
AuthenticatorUriFormat,
UrlEncoder.Encode("Microsoft.AspNetCore.Identity.UI"),
UrlEncoder.Encode("Remotely"),
UrlEncoder.Encode(email),
unformattedKey);
}
Expand Down
64 changes: 7 additions & 57 deletions Server/Components/Pages/Credits.razor
Original file line number Diff line number Diff line change
Expand Up @@ -11,70 +11,13 @@
</div>
</div>
<div class="row">
<div class="col-sm-6 col-md-4">
<h5>.NET Core</h5>
<ul>
<li>License: <a href="https://github.com/dotnet/core/blob/master/LICENSE.TXT">https://github.com/dotnet/core/blob/master/LICENSE.TXT</a></li>
<li>Project: <a href="https://github.com/dotnet/core">https://github.com/dotnet/core</a></li>
</ul>
</div>
<div class="col-sm-6 col-md-4">
<h5>ASP.NET Core</h5>
<ul>
<li>License: <a href="https://github.com/aspnet/AspNetCore/blob/master/LICENSE.txt">https://github.com/aspnet/AspNetCore/blob/master/LICENSE.txt</a></li>
<li>Project: <a href="https://github.com/aspnet/AspNetCore">https://github.com/aspnet/AspNetCore</a></li>
</ul>
</div>

<div class="col-sm-6 col-md-4">
<h5>Bootstrap</h5>
<ul>
<li>License: <a href="https://github.com/twbs/bootstrap/blob/v4-dev/LICENSE">https://github.com/twbs/bootstrap/blob/v4-dev/LICENSE</a></li>
<li>Project: <a href="https://github.com/twbs/bootstrap">https://github.com/twbs/bootstrap</a></li>
</ul>
</div>
<div class="col-sm-6 col-md-4">
<h5>Font Awesome</h5>
<ul>
<li>License: <a href="https://fontawesome.com/license">https://fontawesome.com/license</a></li>
<li>Project: <a href="https://github.com/FortAwesome/Font-Awesome">https://github.com/FortAwesome/Font-Awesome</a></li>
</ul>
</div>
<div class="col-sm-6 col-md-4">
<h5>jQuery</h5>
<ul>
<li>License: <a href="https://github.com/jquery/jquery/blob/master/LICENSE.txt">https://github.com/jquery/jquery/blob/master/LICENSE.txt</a></li>
<li>Project: <a href="https://github.com/jquery/jquery">https://github.com/jquery/jquery</a></li>
</ul>
</div>
<div class="col-sm-6 col-md-4">
<h5>Entity Framework Core</h5>
<ul>
<li>License: <a href="https://github.com/aspnet/EntityFrameworkCore/blob/release/2.2/LICENSE.txt">https://github.com/aspnet/EntityFrameworkCore/blob/release/2.2/LICENSE.txt</a></li>
<li>Project: <a href="https://github.com/aspnet/EntityFrameworkCore">https://github.com/aspnet/EntityFrameworkCore</a></li>
</ul>
</div>
<div class="col-sm-6 col-md-4">
<h5>Bootswatch (Bootstrap Themes)</h5>
<ul>
<li>License: <a href="https://github.com/thomaspark/bootswatch/blob/master/LICENSE">https://github.com/thomaspark/bootswatch/blob/master/LICENSE</a></li>
<li>Project: <a href="https://github.com/thomaspark/bootswatch">https://github.com/thomaspark/bootswatch</a></li>
</ul>
</div>
<div class="col-sm-6 col-md-4">
<h5>PowerShell</h5>
<ul>
<li>License: <a href="https://github.com/PowerShell/PowerShell/blob/master/LICENSE.txt">https://github.com/PowerShell/PowerShell/blob/master/LICENSE.txt</a></li>
<li>Project: <a href="https://github.com/PowerShell/PowerShell">https://github.com/PowerShell/PowerShell</a></li>
</ul>
</div>
<div class="col-sm-6 col-md-4">
<h5>Windows Command Reference</h5>
<ul>
<li>License: <a href="https://github.com/MicrosoftDocs/windowsserverdocs/blob/master/LICENSE">https://github.com/MicrosoftDocs/windowsserverdocs/blob/master/LICENSE</a></li>
<li>Project: <a href="https://github.com/MicrosoftDocs/windowsserverdocs/tree/master/WindowsServerDocs/administration/windows-commands">https://github.com/MicrosoftDocs/windowsserverdocs/tree/master/WindowsServerDocs/administration/windows-commands</a></li>
</ul>
</div>
<div class="col-sm-6 col-md-4">
<h5>NAudio</h5>
<ul>
Expand All @@ -89,4 +32,11 @@
<li>Project: <a href="https://github.com/sharpdx/SharpDX">https://github.com/sharpdx/SharpDX</a></li>
</ul>
</div>
<div class="col-sm-6 col-md-4">
<h5>QRCoder</h5>
<ul>
<li>License: <a href="https://github.com/codebude/QRCoder/blob/master/LICENSE.txt">https://github.com/codebude/QRCoder/blob/master/LICENSE.txt</a></li>
<li>Project: <a href="https://github.com/codebude/QRCoder/">https://github.com/codebude/QRCoder/</a></li>
</ul>
</div>
</div>

0 comments on commit 91daac9

Please sign in to comment.