-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement QR code scanning from screen
Closes #20
- Loading branch information
1 parent
fcc39ff
commit ac1b392
Showing
10 changed files
with
164 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using System.Drawing; | ||
using System.Windows; | ||
using Guard.Core.Models; | ||
|
||
namespace Guard.Core.Import.Importer | ||
{ | ||
internal class QRScreenCaptureImporter : IImporter | ||
{ | ||
public string Name => "QRScreenCapture"; | ||
public IImporter.ImportType Type => IImporter.ImportType.ScreenCapture; | ||
public string SupportedFileExtensions => ""; | ||
|
||
public bool RequiresPassword(string? path) => false; | ||
|
||
public (int total, int duplicate, int tokenID) Parse(string? path, byte[]? password) | ||
{ | ||
MainWindow mainWindow = (MainWindow)Application.Current.MainWindow; | ||
|
||
mainWindow.MinimizeWindow(); | ||
|
||
Bitmap capture = ScreenCapture.CaptureAllScreens(); | ||
|
||
mainWindow.RestoreWindow(); | ||
|
||
string? text = | ||
QRCode.ParseQRBitmap(capture) | ||
?? throw new Exception(I18n.GetString("import.noqrfound.screen")); | ||
|
||
if (!text.StartsWith("otpauth")) | ||
{ | ||
throw new Exception(I18n.GetString("import.invalidqr.screen")); | ||
} | ||
|
||
// Google Authenticator | ||
if (text.StartsWith("otpauth-migration:")) | ||
{ | ||
int duplicateTokens = 0; | ||
List<OTPUri> otpUris = GoogleAuthenticator.Parse(text); | ||
if (otpUris.Count == 0) | ||
{ | ||
throw new Exception( | ||
"Google Authenticator migration failed because no tokens were found." | ||
); | ||
} | ||
foreach (OTPUri gOTPUri in otpUris) | ||
{ | ||
DBTOTPToken gDBToken = OTPUriParser.ConvertToDBToken(gOTPUri); | ||
if (!TokenManager.AddToken(gDBToken)) | ||
{ | ||
duplicateTokens++; | ||
} | ||
} | ||
return (otpUris.Count, duplicateTokens, 0); | ||
} | ||
|
||
OTPUri otpUri = OTPUriParser.Parse(text); | ||
DBTOTPToken dbToken = OTPUriParser.ConvertToDBToken(otpUri); | ||
|
||
if (!TokenManager.AddToken(dbToken)) | ||
{ | ||
return (1, 1, 0); | ||
} | ||
|
||
return (1, 0, dbToken.Id); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System.Drawing; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace Guard.Core.Import | ||
{ | ||
internal class ScreenCapture | ||
{ | ||
private enum SystemMetric | ||
{ | ||
SM_XVIRTUALSCREEN = 76, // 0x4C | ||
SM_YVIRTUALSCREEN = 77, // 0x4D | ||
SM_CXVIRTUALSCREEN = 78, // 0x4E | ||
SM_CYVIRTUALSCREEN = 79, // 0x4F | ||
} | ||
|
||
[DllImport("user32.dll")] | ||
static extern int GetSystemMetrics(SystemMetric smIndex); | ||
|
||
public static Bitmap CaptureAllScreens() | ||
{ | ||
int screenLeft = GetSystemMetrics(SystemMetric.SM_XVIRTUALSCREEN); | ||
int screenTop = GetSystemMetrics(SystemMetric.SM_YVIRTUALSCREEN); | ||
int screenWidth = GetSystemMetrics(SystemMetric.SM_CXVIRTUALSCREEN); | ||
int screenHeight = GetSystemMetrics(SystemMetric.SM_CYVIRTUALSCREEN); | ||
|
||
Bitmap bmp = new(screenWidth, screenHeight); | ||
using (Graphics g = Graphics.FromImage(bmp)) | ||
{ | ||
g.CopyFromScreen(screenLeft, screenTop, 0, 0, bmp.Size); | ||
} | ||
|
||
return bmp; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters