-
Notifications
You must be signed in to change notification settings - Fork 3
/
hype.py
168 lines (159 loc) · 6.18 KB
/
hype.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from banking import Banking
from uuid import uuid4
import json
import requests
from utils import loginrequired
from datetime import datetime, date
class Hype(Banking):
ENROLL_URL = "https://api.hype.it/v2/auth/hypeconnector.aspx"
PROFILE_URL = "https://api.hype.it/v1/rest/u/profile"
BALANCE_URL = "https://api.hype.it/v1/rest/u/balance"
CARD_URL = "https://api.hype.it/v1/rest/your/card"
MOVEMENTS_URL = "https://api.hype.it/v1/rest/m/last/{}"
APP_VERSION = "5.1.6"
DEVICE_ID = str(uuid4()).replace("-", "") + "hype"
DEVICE_INFO = json.dumps({
"jailbreak": "false",
"osversion": "13.3.1",
"model": "iPhone11,2"
})
def __init__(self):
self._username = None
self.newids = None
self.bin = None
self.checksum = None
super().__init__()
def login(self, username, password, birthdate):
if isinstance(birthdate, datetime) or isinstance(birthdate, date):
dob = birthdate.strftime("%d/%m/%Y")
elif isinstance(birthdate, str):
dob = datetime.fromisoformat(birthdate).strftime("%d/%m/%Y")
elif birthdate is None:
dob = None
else:
raise ValueError("Invalid birth date")
enroll1 = self._session.post(
self.ENROLL_URL,
data={
"additionalinfo": self.DEVICE_INFO,
"codiceinternet": username,
"datanascita": dob,
"deviceid": self.DEVICE_ID,
"function": "FREE/LOGINFIRSTSTEP.SPR",
"pin": password,
"platform": "IPHONE"
},
timeout=10
)
try:
if enroll1.json()["Check"] != "OK":
raise self.AuthenticationError("Login failed")
except json.decoder.JSONDecodeError:
raise self.AuthenticationError("Failed to parse response for login request")
except KeyError:
raise self.AuthenticationError("Login failed")
enroll2 = self._session.post(
self.ENROLL_URL,
data={
"additionalinfo": self.DEVICE_INFO,
"deviceid": self.DEVICE_ID,
"function": "INFO/ENROLLBIO.SPR",
"platform": "IPHONE"
},
timeout=10
)
try:
if enroll2.json()["ErrorMessage"] != "":
raise self.AuthenticationError("Server returned error: " + enroll2.json()["ErrorMessage"])
except json.decoder.JSONDecodeError:
raise self.RequestException("Failed to parse response for bioToken request")
except KeyError:
raise self.AuthenticationError("Missing data in response for bioToken request")
self.bin = enroll2.json()["Bin"]
self._username = username
def otp2fa(self, code):
if self._username is None:
raise Exception("Please login() before verifying OTP code")
otp = self._session.post(
self.ENROLL_URL,
data={
"additionalinfo": self.DEVICE_INFO,
"codiceinternet": self._username,
"deviceid": self.DEVICE_ID,
"function": "FREE/LOGINSECONDSTEP.SPR",
"pwd": str(code),
"platform": "IPHONE"
},
timeout=10
)
try:
if otp.json()["Check"] != "OK":
raise self.AuthenticationError("OTP verification failed. Please login() again")
except json.decoder.JSONDecodeError:
raise self.RequestException("Failed to parse response for OTP verification request")
except KeyError:
raise self.AuthenticationError("OTP verification failed. Please login() again")
self.checksum = otp.json()["Checksum"]
self.token = self._session.cookies.get_dict()["token"]
self.newids = self._session.cookies.get_dict()["newids"]
self._session = requests.Session()
self._session.headers.update({
"hype_token": self.token,
"newids": self.newids,
"App-Version": self.APP_VERSION
})
@loginrequired
def renew(self):
"""
Token renewal
"""
renewal = self._session.post(
self.ENROLL_URL,
data={
"additionalinfo": self.DEVICE_INFO,
"bin": self.bin,
"checksum": self.checksum,
"deviceid": self.DEVICE_ID,
"function": "FREE/LOGINFIRSTSTEPFA.SPR",
"platform": "IPHONE"
},
timeout=10
)
try:
if renewal.json()["Check"] != "OK":
raise self.AuthenticationError("Renewal failed")
except json.decoder.JSONDecodeError:
raise self.AuthenticationError("Failed to parse response for renewal request")
except KeyError:
raise self.AuthenticationError("Renewal failed")
reenroll = self._session.post(
self.ENROLL_URL,
data={
"additionalinfo": self.DEVICE_INFO,
"deviceid": self.DEVICE_ID,
"function": "INFO/ENROLLBIO.SPR",
"platform": "IPHONE"
},
timeout=10
)
try:
if reenroll.json()["ErrorMessage"] != "":
raise self.AuthenticationError("Server returned error: " + reenroll.json()["ErrorMessage"])
except json.decoder.JSONDecodeError:
raise self.RequestException("Failed to parse response for bioToken request")
except KeyError:
raise self.AuthenticationError("Missing data in response for bioToken request")
self.token = self._session.cookies.get_dict()["token"]
self.newids = self._session.cookies.get_dict()["newids"]
self._session = requests.Session()
self._session.headers.update({
"hype_token": self.token,
"newids": self.newids,
"App-Version": self.APP_VERSION
})
self.bin = reenroll.json()["Bin"]
@loginrequired
def get_movements(self, limit=5):
return self._api_request(method="GET", url=self.MOVEMENTS_URL.format(limit))