-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass_token.py
240 lines (204 loc) · 8.98 KB
/
class_token.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import json
import os
import requests
from requests.auth import HTTPBasicAuth
from datetime import datetime, timedelta
from pprint import pprint
time_format = '%Y-%m-%d %H:%M:%S.%f' # used for json Imports
# :todo :branch add .request() method to parent class
# --> pass in with **kwargs
# --> decorate **kwargs in child classes with additional arguments
class Token:
"""Parent class of all token Types used in the rational ConnectedCooking API
Attributes:
:param scope (str): token scope - sent to REST API in headers (default: retrieved from os.environ[stage_scope])
:param created (datetime.datetime): automatically set - when the token was initialized (default: now())
:param valid (bool): if token is valid (default: None)
:param site (str): indicating if production/staging is called (default: 'live')
:param token (str): token as string (default: None)
"""
def __init__(self,
scope=os.environ.get('stage_scope'),
created=datetime.now(),
valid=None,
details=None,
site='live',
token=None):
self.scope = scope
self.created = created
self.valid = valid
self.details = details
self.site = site
self.token = token
def export_local(self, path=None):
"""exports the current parameters of the token Object to a local JSON file
Parameters:
:param path: the file path to be saved (default: 'token.json')
:type path: str
:returns True/False
"""
if path is None:
path = 'token.json' # default variable does not work in class method, workaround
data = {'token': self.token,
'created': str(self.created),
'details': self.details}
with open(path, mode='w') as fh:
json.dump(data, fh)
return True
def import_local(self, path=None):
"""imports token from a local JSON file and sets all object parameters if unsuccessful, sets token.valid=False
Parameters:
:param path: the file path to be imported from (default: 'token.json')
:returns: True/False
"""
if path is None:
path = 'token.json'
try:
with open(path) as fh:
local = json.load(fh)
self.token = local['token']
self.details = local['details']
self.created = datetime.strptime(local['created'], time_format)
self.valid = True
return True
except FileNotFoundError:
self.valid = False
return False
class JWEToken(Token):
"""subclass - used for authorisation
"""
def request(self, *,
username: str = os.environ.get('live_user'),
password: str = os.environ.get('live_password'),
scope=None,
url: str = 'https://www.club-rational.com/member/loginCC'):
"""retrieves a JWE token from the API based on user/password
Parameters:
:param username: user in auth (default: retrieved from os.environ['live_user'])
:param password: password in auth (default: retrieved from os.environ['live_password'])
:param scope: scope parameter to be sent with request (default: self.scope/'HOFER_pleitner')
:param url: request url (default: 'https://www.club-rational.com/member/loginCC')
:returns True/False """
if scope is None:
scope = self.scope
payload = {'username': username,
'password': password}
with requests.Session() as sess:
sess.headers.update({'scope': scope})
r = requests.Request('POST',
url=url,
data=payload)
prepped = sess.prepare_request(r)
response = sess.send(prepped)
r_json = response.json()
self.token = r_json.get('data')
self.details = {'request_headers': dict(prepped.headers),
'request_body': prepped.body,
'request_url': prepped.url,
'response': response.status_code}
result = True if self.details['response'] == 200 else False
return result
class BearerToken(Token):
"""subclass used for Bearer Token - additional parameters
Parameters:
:param refresh_token (str): refresh token from latest request (default: None)
:param valid_to (datetime.datetime): expiration datetime - set at request with sec offset (default: None)
"""
def __init__(self,
refresh_token=None,
valid_to=None):
super().__init__() # inherits all parameters from parent class + additional parameters
self.refresh_token = refresh_token
self.valid_to = valid_to
def import_local(self,
path=None):
"""imports token from a local JSON file and sets all object parameters if unsuccessful, sets token.valid=False
Parameters:
:param path: the file path to be imported from (default: 'token.json')
:returns: True/False
"""
if path is None:
path = self.site + '_bearer_token.json'
if super().import_local(path=path): # inherits all parameters from super().import_local()
with open(path) as fh:
local = json.load(fh)
self.refresh_token = local['refresh_token']
self.valid_to = datetime.strptime(local['valid_to'], time_format)
return True
else: # if super().import_local with path fails, set valid to False
self.valid = False
return False
def export_local(self,
path=None):
"""exports the current parameters of the token Object to a local JSON file
Parameters:
:param path: the file path to be saved (default: 'token.json')
:type path: str
:returns True/False
"""
if path is None:
path = self.site + '_bearer_joken.json'
data = {'token': self.token,
'details': self.details,
'created': str(self.created),
'valid_to': str(self.valid_to),
'refresh_token': self.refresh_token}
try:
with open(path, mode='w') as fh:
json.dump(data, fh)
return True
except FileExistsError:
return False
def request(self,
jwe: str,
client_id: str = os.environ.get('live_client_id'),
client_secret: str = os.environ.get('live_client_secret'),
scope: str = os.environ.get('stage_scope'),
url: str = 'https://www.connectedcooking.com/oauth/token'):
"""
creates a bearer token based on jwe token, client secret/id
Parameters:
:param jwe: valid jwe token, get from JWEToken.token
:param client_id: client id for API (default: retrieved from os.environ['live_client_id'])
:param client_secret: client secret for API (default: retrieved from os.environ['live_client_secret'])
:param scope: scope parameter to be sent with request (default: self.scope/'HOFER_pleitner')
:param url: request url (default: 'https://www.connectedcooking.com/oauth/token')
:returns: True/False
"""
payload = {'jwe': jwe,
'grant_type': 'jwe_token'}
with requests.Session() as sess:
sess.headers.update({'scope': scope})
r = requests.Request(method='POST',
url=url,
data=payload,
auth=HTTPBasicAuth(client_id, client_secret))
prepped = sess.prepare_request(r)
response = sess.send(prepped)
r_json = response.json()
self.token = r_json.get('access_token')
self.refresh_token = r_json.get('refresh_token')
self.valid_to = datetime.now() + timedelta(seconds=int(r_json.get('expires_in')))
self.valid = False if r_json.get('access_token') is None else True
self.site = url
self.details = {'request_headers': dict(prepped.headers),
'request_url': prepped.url,
'request_body': prepped.body,
'response': response.status_code}
return True if self.details['response'] == 200 else False
def check(self):
"""checks if valid_to date is in the past, sets self.valid True/False
:returns: bool
"""
if self.valid_to > datetime.now():
self.valid = True
return True
else:
self.valid = False
return False
if __name__ == '__main__':
jwe_test = JWEToken()
print(jwe_test.valid)
print(jwe_test.scope)
print(jwe_test.site)
pprint(jwe_test.import_local())