Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make better registration and add logs #4

Merged
merged 4 commits into from
Jan 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions vulcan/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
from OpenSSL import crypto
import json
import base64
import logging
import requests


log = logging.getLogger('client')
log.setLevel(logging.INFO)

handler = logging.StreamHandler()
log.addHandler(handler)

class VulcanAPIException(Exception):
pass
Expand All @@ -23,3 +32,13 @@ def signature(cert, passphrase, data):
p12 = crypto.load_pkcs12(base64.b64decode(cert), passphrase.encode('utf-8'))
sign = crypto.sign(p12.get_privatekey(), json.dumps(data).encode('utf-8'), 'RSA-SHA1')
return base64.b64encode(sign).decode('utf-8')

def get_components():
r = requests.get('http://komponenty.vulcan.net.pl/UonetPlusMobile/RoutingRules.txt')
components = (c.split(',') for c in r.text.split())
return {a[0]: a[1] for a in components}

def get_base_url(token):
code = token[0:3]
components = get_components()
return components[code]
29 changes: 25 additions & 4 deletions vulcan/vulcan.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import requests
from datetime import datetime
from operator import itemgetter
import logging


class Vulcan(object):
"""
Expand All @@ -19,7 +21,7 @@ class Vulcan(object):
app_version = '18.10.1.433'
cert_passphrase = 'CE75EA598C7743AD9B0B7328DED85B06'

def __init__(self, certyfikat):
def __init__(self, certyfikat, logging_level=None):
self._cert = certyfikat
self._session = requests.session()
self._headers = {
Expand All @@ -30,10 +32,17 @@ def __init__(self, certyfikat):
self._url = certyfikat['AdresBazowyRestApi']
self._base_url = self._url + 'mobile-api/Uczen.v3.'
self._full_url = None

if logging_level: Vulcan.set_logging_level(logging_level)

self.uczen = None
uczniowie = self.uczniowie()
self.ustaw_ucznia(uczniowie[0])

@staticmethod
def set_logging_level(logging_level):
log.setLevel(logging_level)

@staticmethod
def zarejestruj(token, symbol, pin):
"""
Expand All @@ -48,8 +57,11 @@ def zarejestruj(token, symbol, pin):
:type pin: :class:`str`
:rtype: :class:`dict`
"""
token = str(token).upper()
symbol = str(symbol).lower()
pin = str(pin)
data = {
'PIN': str(pin),
'PIN': pin,
'TokenKey': token,
'AppVersion': Vulcan.app_version,
'DeviceId': uuid(),
Expand All @@ -68,11 +80,20 @@ def zarejestruj(token, symbol, pin):
'RequestMobileType': 'RegisterDevice',
'User-Agent': 'MobileUserAgent',
}
url = 'https://lekcjaplus.vulcan.net.pl/{}/mobile-api/Uczen.v3.UczenStart/Certyfikat'.format(symbol)
try:
base_url = get_base_url(token)
except KeyError:
raise VulcanAPIException('Niepoprawny token!')
url = '{}/{}/mobile-api/Uczen.v3.UczenStart/Certyfikat'.format(base_url, symbol)
log.info('Rejestrowanie...')
try:
r = requests.post(url, json=data, headers=headers)
j = r.json()
return j['TokenCert']
log.debug(j)
cert = j['TokenCert']
assert cert
log.info('Zarejestrowano pomyślnie!')
return cert
except:
raise VulcanAPIException('Nie można utworzyć certyfikatu!')

Expand Down