Skip to content

Commit

Permalink
Merge pull request #65 from SEL-Columbia/0.2.1-dev
Browse files Browse the repository at this point in the history
0.2.1 dev
  • Loading branch information
jmbott authored Aug 16, 2017
2 parents ac77558 + fdb4750 commit 9816019
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
2 changes: 1 addition & 1 deletion minigrid/device_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def write_credit_card(
b'C', # C for credit
b'\x08', # Offset
b'\x00\xf4', # Length
card_produce_time, # card produced time
b'\x00\x00\x00\x00', # old card produce time section
bytes(4), # card read time TODO
uuid.UUID(payment_id).bytes,
))
Expand Down
24 changes: 21 additions & 3 deletions minigrid/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from urllib.parse import urlencode
from uuid import uuid4, UUID

import logging

from asyncio_portier import get_verified_email

from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
Expand Down Expand Up @@ -649,12 +651,16 @@ def _user_or_maintenance_card(binary):

def _credit_card(session, cipher, binary, credit_card_id):
result = OrderedDict()
raw_sector_3 = unhexlify(binary[183:273])
# logging.info(f'Sector 3: {raw_sector_3}')
# result[3] contains tariff information
# result[3] = _decrypt(cipher, unhexlify(binary[183:273])).hex()
raw_sector_4 = unhexlify(binary[274:])
# logging.info(f'Raw Sector 4: {raw_sector_4}')
if not any(raw_sector_4):
return result
sector_4 = raw_sector_4.split(b'###')[0][:-2]
logging.info(f'Sector 4: {sector_4}')
# If card has been used...
record_timestamp = datetime.fromtimestamp(
int(sector_4[4:14].decode('ascii'))).isoformat()
Expand Down Expand Up @@ -691,25 +697,31 @@ def _credit_card(session, cipher, binary, credit_card_id):

def _pack_into_dict(session, binary):
# TODO: here there be dragons...
import logging
logging.info(f'Card Contents: {binary}')
try:
# Checks device address in first 12 characters
device_address = unhexlify(binary[:12])
logging.info(f'Device Address: {device_address}')
device_exists = session.query(
exists().where(models.Device.address == device_address)).scalar()
except Exception as error:
import logging
import logging # remove
logging.error(str(error))
device_exists = False
if not device_exists: # TODO: new error class
raise tornado.web.HTTPError(
400, 'bad device id {}'.format(binary[:12]))
binary = binary[12:]
binary = binary[12:] # Remove device address form binary
result = OrderedDict()
# Is it safe to assume that sector 1 is always first? I hope so
sector_1 = unhexlify(binary[1:91])
sector_1 = unhexlify(binary[1:91]) # Sector label is one character, ignore it, take 90 after as sector 1
# logging.info(f'Sector 1: {sector_1}')
# Use this for the future... displaying in the UI
# system_id = sector_1[:2]
# application_id = sector_1[2:4]
card_type = sector_1[4:5].decode('ascii')
logging.info(f'Card Type: {card_type}')
try:
result['Card Type'] = _card_type_dict[card_type]
except KeyError:
Expand All @@ -720,9 +732,11 @@ def _pack_into_dict(session, binary):
# offset = sector_1[5:6]
# length = sector_1[6:8]
card_produced_time = sector_1[8:12]
logging.info(f'Card Produce Time Unencrypted: {card_produced_time}')
result['Card Creation Time'] = datetime.fromtimestamp(
int.from_bytes(card_produced_time, 'big')).isoformat()
card_last_read_time = sector_1[12:16]
logging.info(f'Card Last Read Time: {card_last_read_time}')
result['Card Last Read Time'] = datetime.fromtimestamp(
int.from_bytes(card_last_read_time, 'big')).isoformat()
payment_id = sector_1[16:32].hex()
Expand All @@ -734,12 +748,15 @@ def _pack_into_dict(session, binary):
key = payment_system.aes_key
cipher = Cipher(AES(key), modes.ECB(), backend=default_backend())
sector_2_enc = unhexlify(binary[92:156])
# logging.info(f'Sector 2 Encrypted: {sector_2_enc}')
sector_2 = _decrypt(cipher, sector_2_enc)
# logging.info(f'Sector 2: {sector_2}')
raw_secret_value = sector_2[:4]
if card_type == 'C':
secret_value = int.from_bytes(raw_secret_value, 'big')
card_produce_time = datetime.fromtimestamp(
int.from_bytes(sector_2[20:24], 'big'))
logging.info(f'Card Produce Time Encrypted: {card_produce_time}')
result['Card Creation Time'] = card_produce_time.isoformat()
current_timestamp = datetime.now()
delta = current_timestamp - card_produce_time
Expand Down Expand Up @@ -802,6 +819,7 @@ def post(self):
# TODO: clean this up
payload = _pack_into_dict(self.session, body)
except Exception as error:
logging.info(f'Error: {error}')
cache.set('card_read_error', str(error), 5)
else:
cache.set('received_info', payload, 5)
Expand Down
2 changes: 1 addition & 1 deletion prod/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: '2'
services:
minigrid:
image: selcolumbia/minigrid-server:0.2.0
image: selcolumbia/minigrid-server:0.2.1
command: ./prod/run.sh --db_host=db --redis_url=redis://redis:6379/0 --minigrid-website-url=https://www.example.com
depends_on:
- redis
Expand Down
6 changes: 3 additions & 3 deletions prod/install.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env sh
# Minigrid Server installer for version 0.2.0
# Minigrid Server installer for version 0.2.1
set -e

# Do you have docker installed?
Expand Down Expand Up @@ -108,8 +108,8 @@ $SUDO openssl dhparam -out /etc/letsencrypt/live/$LETSENCRYPT_DIR/dhparam.pem 20
printf "========================================\n"
printf " Generating configuration \n"
printf "========================================\n"
$CURL -L https://raw.githubusercontent.com/SEL-Columbia/minigrid-server/0.2.0/prod/docker-compose.yml > docker-compose.yml
$CURL -L https://raw.githubusercontent.com/SEL-Columbia/minigrid-server/0.2.0/prod/nginx.conf > nginx.conf
$CURL -L https://raw.githubusercontent.com/SEL-Columbia/minigrid-server/0.2.1/prod/docker-compose.yml > docker-compose.yml
$CURL -L https://raw.githubusercontent.com/SEL-Columbia/minigrid-server/0.2.1/prod/nginx.conf > nginx.conf

sed -i s/www.example.com/$LETSENCRYPT_DIR/g docker-compose.yml
sed -i s/www.example.com/$LETSENCRYPT_DIR/g nginx.conf
Expand Down

0 comments on commit 9816019

Please sign in to comment.