-
Notifications
You must be signed in to change notification settings - Fork 0
/
usbtan-viaclipboard
executable file
·65 lines (60 loc) · 2.62 KB
/
usbtan-viaclipboard
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
#!/usr/bin/env python3
# encoding: utf-8
import re
import sys
import pyperclip
import subprocess
import gi
gi.require_version('Notify', '0.7')
from gi.repository import Notify
Notify.init("chipTAN USB")
summary = 'Anfrage für TAN'
# from https://www.devdungeon.com/content/desktop-notifications-linux-python
TIMEOUT_SECONDS = 30
if __name__ == '__main__':
clipboard = pyperclip.paste()
iban = re.search(r'[A-Z]{2}[0-9]{20}', clipboard.replace(' ',''))
startcode = re.search(r'\b[0-9]{8}\b', clipboard)
betrag = re.search(r'\b[0-9.]+,[0-9]{2}\b', clipboard)
sys.stderr.write(f'clibpoard: {clipboard}\n')
sys.stderr.write(f'startcode: {startcode}\n')
sys.stderr.write(f'IBAN: {iban}\n')
sys.stderr.write(f'Betrag: {betrag}\n')
if (iban):
iban = iban.group(0)[-10:]
if (betrag):
betrag = betrag.group(0).replace('.','') # tausender Trennzeichen entfernen
if (startcode and startcode.group(0)):
startcode = startcode.group(0)
body = f'Startcode: {startcode}\nIBAN: {iban}\nBetrag: {betrag}\n\nBitte am SmartCard Lesegerät innerhalb von {TIMEOUT_SECONDS} Sekunden bestätigen.'
icon = 'dialog-password'
notification = Notify.Notification.new(summary, body, icon)
notification.show()
arguments = [startcode, iban, betrag]
arguments = [a for a in arguments if a]
try:
sub = subprocess.run(['usbtan-cli']+arguments, stdout=subprocess.PIPE, timeout= 30, check=True, encoding='ascii')
tokens = [line.split(' = ') for line in sub.stdout.split('\n')]
tantoken = [token for token in tokens if len(token) == 2 and token[0] == 'TAN']
if tantoken:
tan = tantoken[0][1]
pyperclip.copy(tan)
body = f'TAN {tan} wurde in Zwischenablage kopiert.'
icon = 'dialog-information'
notification.update(summary, body, icon)
notification.show()
except subprocess.CalledProcessError:
body = 'SmartCard Lesegerät konnte keine TAN liefern.'
icon = 'dialog-error'
notification.update(summary, body, icon)
notification.show()
except subprocess.TimeoutExpired:
body = 'Keine rechtzeitige Antwort vom SmartCard Lesegerät.'
icon = 'dialog-error'
notification.update(summary, body, icon)
notification.show()
else:
body = 'Nicht möglich: Kein Startcode in Zwischenablage erkannt.'
icon = 'dialog-error'
notification = Notify.Notification.new(summary, body, icon)
notification.show()