forked from BrandonPotter/GoogleAuthenticator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQRCodeAndSetupUrlTests.cs
More file actions
100 lines (89 loc) · 4.08 KB
/
QRCodeAndSetupUrlTests.cs
File metadata and controls
100 lines (89 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using Xunit;
using Shouldly;
using System.Diagnostics;
using System;
using ZXing;
using System.Collections.Generic;
using System.IO;
namespace Google.Authenticator.Tests
{
public class QRCodeAndSetupUrlTests
{
[Theory]
[InlineData("issuer", "otpauth://totp/issuer:a%40b.com?secret=ONSWG4TFOQ&issuer=issuer")]
[InlineData("Foo & Bar", "otpauth://totp/Foo%20%26%20Bar:a%40b.com?secret=ONSWG4TFOQ&issuer=Foo%20%26%20Bar")]
[InlineData("个", "otpauth://totp/%E4%B8%AA:a%40b.com?secret=ONSWG4TFOQ&issuer=%E4%B8%AA")]
public void CanGenerateQRCode(string issuer, string expectedUrl)
{
var subject = new TwoFactorAuthenticator();
var setupCodeInfo = subject.GenerateSetupCode(
issuer,
"a@b.com",
"secret",
false,
2);
var actualUrl = ExtractUrlFromQRImage(setupCodeInfo.QrCodeSetupImageUrl);
var rawUrl = setupCodeInfo.SetupUrl;
Assert.Multiple(() =>
{
actualUrl.ShouldBe(expectedUrl, "QR Code Url is not as expected");
rawUrl.ShouldBe(expectedUrl, "SetupUrl is not as expected");
});
}
[Theory]
[InlineData("issuer", "otpauth://totp/issuer:a%40b.com?secret=ONSWG4TFOQ&issuer=issuer&algorithm=SHA256")]
[InlineData("Foo & Bar", "otpauth://totp/Foo%20%26%20Bar:a%40b.com?secret=ONSWG4TFOQ&issuer=Foo%20%26%20Bar&algorithm=SHA256")]
[InlineData("个", "otpauth://totp/%E4%B8%AA:a%40b.com?secret=ONSWG4TFOQ&issuer=%E4%B8%AA&algorithm=SHA256")]
public void CanGenerateSHA256QRCode(string issuer, string expectedUrl)
{
var subject = new TwoFactorAuthenticator(HashType.SHA256);
var setupCodeInfo = subject.GenerateSetupCode(
issuer,
"a@b.com",
"secret",
false,
2);
var actualUrl = ExtractUrlFromQRImage(setupCodeInfo.QrCodeSetupImageUrl);
var rawUrl = setupCodeInfo.SetupUrl;
Assert.Multiple(() =>
{
actualUrl.ShouldBe(expectedUrl, "QR Code Url is not as expected");
rawUrl.ShouldBe(expectedUrl, "SetupUrl is not as expected");
});
}
[Theory]
[InlineData("issuer", "otpauth://totp/issuer:a%40b.com?secret=ONSWG4TFOQ&issuer=issuer&algorithm=SHA512")]
[InlineData("Foo & Bar", "otpauth://totp/Foo%20%26%20Bar:a%40b.com?secret=ONSWG4TFOQ&issuer=Foo%20%26%20Bar&algorithm=SHA512")]
[InlineData("个", "otpauth://totp/%E4%B8%AA:a%40b.com?secret=ONSWG4TFOQ&issuer=%E4%B8%AA&algorithm=SHA512")]
public void CanGenerateSHA512QRCode(string issuer, string expectedUrl)
{
var subject = new TwoFactorAuthenticator(HashType.SHA512);
var setupCodeInfo = subject.GenerateSetupCode(
issuer,
"a@b.com",
"secret",
false,
2);
var actualUrl = ExtractUrlFromQRImage(setupCodeInfo.QrCodeSetupImageUrl);
var rawUrl = setupCodeInfo.SetupUrl;
Assert.Multiple(() =>
{
actualUrl.ShouldBe(expectedUrl, "QR Code Url is not as expected");
rawUrl.ShouldBe(expectedUrl, "SetupUrl is not as expected");
});
}
private static string ExtractUrlFromQRImage(string qrCodeSetupImageUrl)
{
var headerLength = "data:image/png;base64,".Length;
var rawImageData = qrCodeSetupImageUrl.Substring(headerLength, qrCodeSetupImageUrl.Length - headerLength);
var imageData = Convert.FromBase64String(rawImageData);
var reader = new BarcodeReaderGeneric();
reader.Options.PossibleFormats = new List<BarcodeFormat> {
BarcodeFormat.QR_CODE
};
var image = new ImageMagick.MagickImage(imageData);
var wrappedImage = new ZXing.Magick.MagickImageLuminanceSource(image);
return reader.Decode(wrappedImage).Text;
}
}
}