misc/powershell/BankIDTokenFileWatcher.ps1

39 lines
1.3 KiB
PowerShell
Raw Normal View History

2022-06-05 02:48:59 +02:00
#
# 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
2022-06-05 02:55:22 +02:00
# token to disk. You probably want to combine this script with the following:
# - https://gitlab.com/dabruh/dotfiles/-/blob/master/.local/share/applications/bankid.desktop
# - https://gitlab.com/dabruh/dotfiles/-/blob/master/.local/bin/send-bankid-token
2022-06-05 02:48:59 +02:00
$Token = $null
$LastToken = $null
2022-06-05 02:50:48 +02:00
$TokenFile = "C:\.bidtoken"
2022-06-05 02:48:59 +02:00
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
}