Skip to content

Commit

Permalink
Added check for libgdiplus
Browse files Browse the repository at this point in the history
  • Loading branch information
flytzen committed Oct 14, 2020
1 parent 589f980 commit 4d69e32
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 7 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();
}
}
}
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

0 comments on commit 4d69e32

Please sign in to comment.