From 10e6415b72579175370a137f0493042c478b07bd Mon Sep 17 00:00:00 2001 From: dabruh <11458706-dabruh@users.noreply.gitlab.com> Date: Sun, 5 Jun 2022 02:48:59 +0200 Subject: [PATCH] First POC. --- powershell/BankIDClipboardWatcher.ps1 | 39 --------------------------- powershell/BankIDTokenFileWatcher.ps1 | 37 +++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 39 deletions(-) delete mode 100644 powershell/BankIDClipboardWatcher.ps1 create mode 100644 powershell/BankIDTokenFileWatcher.ps1 diff --git a/powershell/BankIDClipboardWatcher.ps1 b/powershell/BankIDClipboardWatcher.ps1 deleted file mode 100644 index 4be48e8..0000000 --- a/powershell/BankIDClipboardWatcher.ps1 +++ /dev/null @@ -1,39 +0,0 @@ -function Get-Hash([string]$textToHash) { - $hasher = new-object System.Security.Cryptography.MD5CryptoServiceProvider - $toHash = [System.Text.Encoding]::UTF8.GetBytes($textToHash) - $hashByteArray = $hasher.ComputeHash($toHash) - - foreach($byte in $hashByteArray){ - $result += "{0:X2}" -f $byte - } - - return $result; -} - -$ClearClipboard = $true -$PreviousHash = Get-Hash(Get-Clipboard) - -while ($true) { - $Clipboard = Get-Clipboard - $ClipboardHash = Get-Hash($Clipboard) - - if ($ClipboardHash -eq $PreviousHash) { - Write-Host "NOCHG" - Start-Sleep -Milliseconds 250 - Continue - } - elseif ($Clipboard -match '^bankid:\/\/\/') { - Write-Host $Clipboard - start "$Clipboard" - } - else { - Write-Host "CHG" - } - - if ($ClearClipboard) { - Set-Clipboard -Value $null - } - - $PreviousHash = Get-Hash($Clipboard) - Start-Sleep -Milliseconds 250 -} diff --git a/powershell/BankIDTokenFileWatcher.ps1 b/powershell/BankIDTokenFileWatcher.ps1 new file mode 100644 index 0000000..d84a664 --- /dev/null +++ b/powershell/BankIDTokenFileWatcher.ps1 @@ -0,0 +1,37 @@ +# +# BankIDTokenFileWatcher +# +# Starts BankID when a token is written to $TokenFile. +# +# Run this script on logon together with BankID. This script is intended to be +# used with an SSH server installed on the host so that a client can write this +# token to disk. +# + +$Token = $null +$LastToken = $null +$TokenFile = "C:\bid_autostarttoken" + +Write-Host "Starting file watcher for '$TokenFile'" + +while ($true) { + if (Test-Path $TokenFile) { + $TokenFileData = Get-Item "$TokenFile" + + if (((Get-Date) - $TokenFileData.LastWriteTime).totalseconds -le 1){ + Write-Host "File modified: $TokenFile" + $Token = Get-Content "$TokenFile" + + if ($Token -ne $LastToken) { + Write-Host "New token: $Token" + Start-Process "bankid:///?autostarttoken=$Token&redirect=null" + Start-Sleep -Milliseconds 500 + $LastToken = $Token + } + else { + Write-Host "Token unchanged: $Token" + } + } + } + Start-Sleep -Milliseconds 500 +}