Skip to content

Commit

Permalink
Merge pull request #58 from BrandonPotter/AddCheckForDependencyOnLinux
Browse files Browse the repository at this point in the history
Add check for dependency on linux
  • Loading branch information
ahwm authored Oct 14, 2020
2 parents 589f980 + 48726b7 commit 9c2980d
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 8 deletions.
17 changes: 17 additions & 0 deletions Google.Authenticator.Tests/QRCodeTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using Xunit;
using Shouldly;

namespace Google.Authenticator.Tests
{
public class QRCodeTest
{
[Fact]
public void CanGenerateQRCode()
{
var subject = new TwoFactorAuthenticator();
var setupCodeInfo = subject.GenerateSetupCode("issuer","[email protected]","secret", false, 2);
setupCodeInfo.QrCodeSetupImageUrl.ShouldNotBeNull();
}
}
}
2 changes: 1 addition & 1 deletion Google.Authenticator/Google.Authenticator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<Description>Google Authenticator Two-Factor Authentication Library (Not officially affiliated with Google.)</Description>
<Authors>Brandon Potter</Authors>
<Company>Brandon Potter</Company>
<Version>2.0.2</Version>
<Version>2.1.0</Version>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/BrandonPotter/GoogleAuthenticator</PackageProjectUrl>
<PackageId>GoogleAuthenticator</PackageId>
Expand Down
15 changes: 15 additions & 0 deletions Google.Authenticator/MissingDependencyException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;

namespace Google.Authenticator
{
public class MissingDependencyException : Exception
{
public MissingDependencyException(string message) : base(message)
{
}

public MissingDependencyException(string message, Exception innerException) : base(message, innerException)
{
}
}
}
27 changes: 20 additions & 7 deletions Google.Authenticator/TwoFactorAuthenticator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,28 @@ public SetupCode GenerateSetupCode(string issuer, string accountTitleNoSpaces, b
string qrCodeUrl = string.Empty;
if (generateQrCode)
{
using (QRCodeGenerator qrGenerator = new QRCodeGenerator())
using (QRCodeData qrCodeData = qrGenerator.CreateQrCode(provisionUrl, QRCodeGenerator.ECCLevel.Q))
using (QRCode qrCode = new QRCode(qrCodeData))
using (Bitmap qrCodeImage = qrCode.GetGraphic(QRPixelsPerModule))
using (MemoryStream ms = new MemoryStream())
try
{
qrCodeImage.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
using (QRCodeGenerator qrGenerator = new QRCodeGenerator())
using (QRCodeData qrCodeData = qrGenerator.CreateQrCode(provisionUrl, QRCodeGenerator.ECCLevel.Q))
using (QRCode qrCode = new QRCode(qrCodeData))
using (Bitmap qrCodeImage = qrCode.GetGraphic(QRPixelsPerModule))
using (MemoryStream ms = new MemoryStream())
{
qrCodeImage.Save(ms, System.Drawing.Imaging.ImageFormat.Png);

qrCodeUrl = String.Format("data:image/png;base64,{0}", Convert.ToBase64String(ms.ToArray()));
}
}
catch (System.TypeInitializationException e)
{
if (e.InnerException != null
&& e.InnerException.GetType() == typeof(System.DllNotFoundException)
&& e.InnerException.Message.Contains("libgdiplus"))
{
throw new MissingDependencyException("It looks like libgdiplus has not been installed - see https://github.com/codebude/QRCoder/issues/227", e);
}

qrCodeUrl = String.Format("data:image/png;base64,{0}", Convert.ToBase64String(ms.ToArray()));
}
}

Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ Simple, easy to use server-side two-factor authentication library for .NET that
See blog post for usage instructions:

https://csharprookie.wordpress.com/2015/03/17/implementing-free-two-factor-authentication-in-net-using-google-authenticator/

# Notes
On linux, you need to ensure `libgdiplus` is installed if you want to generate QR Codes. See [https://github.com/codebude/QRCoder/issues/227](https://github.com/codebude/QRCoder/issues/227).

0 comments on commit 9c2980d

Please sign in to comment.