From 4d69e32338a23f93f41843eebc3672bc7ddfe82e Mon Sep 17 00:00:00 2001 From: Frans Lytzen Date: Wed, 14 Oct 2020 18:20:55 +0100 Subject: [PATCH 1/3] Added check for libgdiplus --- Google.Authenticator.Tests/QRCodeTest.cs | 17 ++++++++++++ .../MissingDependencyException.cs | 15 +++++++++++ .../TwoFactorAuthenticator.cs | 27 ++++++++++++++----- 3 files changed, 52 insertions(+), 7 deletions(-) create mode 100644 Google.Authenticator.Tests/QRCodeTest.cs create mode 100644 Google.Authenticator/MissingDependencyException.cs diff --git a/Google.Authenticator.Tests/QRCodeTest.cs b/Google.Authenticator.Tests/QRCodeTest.cs new file mode 100644 index 0000000..c3bb3ae --- /dev/null +++ b/Google.Authenticator.Tests/QRCodeTest.cs @@ -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","a@b.com","secret", false, 2); + setupCodeInfo.QrCodeSetupImageUrl.ShouldNotBeNull(); + } + } +} \ No newline at end of file diff --git a/Google.Authenticator/MissingDependencyException.cs b/Google.Authenticator/MissingDependencyException.cs new file mode 100644 index 0000000..de4d02a --- /dev/null +++ b/Google.Authenticator/MissingDependencyException.cs @@ -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) + { + } + } +} \ No newline at end of file diff --git a/Google.Authenticator/TwoFactorAuthenticator.cs b/Google.Authenticator/TwoFactorAuthenticator.cs index 608eadd..d048bc2 100644 --- a/Google.Authenticator/TwoFactorAuthenticator.cs +++ b/Google.Authenticator/TwoFactorAuthenticator.cs @@ -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())); } } From aba52c409cfa1dc27189dd5dfbe580496cfc7803 Mon Sep 17 00:00:00 2001 From: Frans Lytzen Date: Wed, 14 Oct 2020 18:22:17 +0100 Subject: [PATCH 2/3] Updated readme --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index d15d3fc..c426750 100644 --- a/README.md +++ b/README.md @@ -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). \ No newline at end of file From 48726b71f24d9de5b5e51f0cafc0a54edfbb21cd Mon Sep 17 00:00:00 2001 From: Frans Lytzen Date: Wed, 14 Oct 2020 18:22:50 +0100 Subject: [PATCH 3/3] Updated to v 2.1 --- Google.Authenticator/Google.Authenticator.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Google.Authenticator/Google.Authenticator.csproj b/Google.Authenticator/Google.Authenticator.csproj index db869ef..4c02410 100644 --- a/Google.Authenticator/Google.Authenticator.csproj +++ b/Google.Authenticator/Google.Authenticator.csproj @@ -7,7 +7,7 @@ Google Authenticator Two-Factor Authentication Library (Not officially affiliated with Google.) Brandon Potter Brandon Potter - 2.0.2 + 2.1.0 Apache-2.0 https://github.com/BrandonPotter/GoogleAuthenticator GoogleAuthenticator