-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcardWebconnector.py
124 lines (104 loc) · 4.36 KB
/
cardWebconnector.py
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import urllib
import urllib2
import sys
import json
import hashlib
import feedbackHandler
from smartcard.util import *
# import RPi.GPIO as GPIO
# define the apdus used in this script
GET_RESPONSE = [0XA0, 0XC0, 0x00, 0x00]
SELECT = [0xA0, 0xA4, 0x00, 0x00, 0x02]
GET_UID = [0xFF, 0xCA, 0x00, 0x00, 0x00]
class cardWebconnector(object):
lastId = None
url = None
eventKey = None
def __init__(self, args):
self.url = args['--url']
self.eventKey = args['--event-key']
def update(self, observable, (addedcards, removedcards)):
for card in addedcards:
# Create connection
connection = card.createConnection()
connection.connect()
# Get Reader-ID as Resource
resource = self.getReaderID(connection)
# Get Card-UID
cardUID = self.getUID(connection)
print "Inserted Card with UID: " + str(cardUID)
if self.checkNewUID(cardUID):
# here we go
self.webConnect(cardUID, resource)
else:
print "#### Same UID ( " + str(cardUID) + " ) as Before... Skipping... ####"
feedbackHandler.setFeedback(feedbackHandler.ERROR, feedbackHandler.ACTIVE)
for card in removedcards:
feedbackHandler.setFeedback(feedbackHandler.SUCCESS, feedbackHandler.INACTIVE)
feedbackHandler.setFeedback(feedbackHandler.ERROR, feedbackHandler.INACTIVE)
def webConnect(self, uid, resource):
request_data = {}
request_data['card_uid'] = "".join(map(str, uid))
request_data['event_key'] = self.eventKey
request_data['resource'] = resource
try:
print "Calling " + str(self.url) + " with Options: " + str(request_data)
request = urllib2.Request(self.url, data=json.dumps(request_data))
request.add_header('Content-Type', 'application/json')
response = urllib2.urlopen(request, timeout=1)
if response.code is 200:
self.saveCurrUID(uid)
self.processWebResponse(response)
# Yay, everything good -> Success-Feedback
feedbackHandler.setFeedback(feedbackHandler.SUCCESS, feedbackHandler.ACTIVE)
else:
# Not 200er Code? Error-Feedback
feedbackHandler.setFeedback(feedbackHandler.ERROR, feedbackHandler.ACTIVE)
print "Error from Webservice: " + str(response.code)
except urllib2.URLError as e:
# Error while POSTing Data -> Error-Feedback
feedbackHandler.setFeedback(feedbackHandler.ERROR, feedbackHandler.ACTIVE)
except Exception as e:
print e
def checkNewUID(self, uid):
if uid == self.lastId or uid is False:
return False
else:
return True
def saveCurrUID(self, uid):
self.lastId = uid
def getReaderID(self, connection):
# Read SHA1-Hash of Reader-Name (as Readers are addressed by Names not IDs)
m = hashlib.sha1()
m.update(str(connection.getReader()))
return m.hexdigest()
def getUID(self, connection):
# Read UID from Card
return self.executeCardCmd(connection, GET_UID)
def executeCardCmd(self, connection, cmd):
response, sw1, sw2 = connection.transmit(cmd)
if not self.validateCardResponse(sw1, sw2):
print "CMD: " + str(cmd)
sys.exit(1)
else:
return response
def validateCardResponse(self, sw1, sw2):
response = False
if sw1 is 0x90 and sw2 is 0x00:
response = True
elif sw1 is 0x67 and sw2 is 0x00:
print "Wrong length (Lc incoherent with Data In)"
elif sw1 is 0x68 and sw2 is 0x00:
print "CLA byte is not correct"
elif sw1 is 0x6a and sw2 is 0x81:
print "Function not supported (INS byte is not correct), or not available for the selected card"
elif sw1 is 0x6B and sw2 is 0x00:
print "Wrong parameter P1-P2"
elif sw1 is 0x6F and sw2 is 0x01:
print "Card mute (or removed)"
return response
def processWebResponse(self, response):
if 'application/json' in response.info().getheader('Content-Type') :
print json.loads(response.read())
else:
print response.info().getheader('Content-Type')