14
14
from db_create_user_database import check_user_db_exists ,check_host_record_exists ,backup_user_db ,create_sample_db ,check_db_server_connectivity
15
15
from db_user_insert import gc_user_update
16
16
from db_dropbox import check_user_token_exists
17
+ from db_oura_auth import check_oura_token_exists
17
18
from diasend_data_download_db_insert import diasend_data_export_insert
18
19
from glimp_data_download_db_insert import glimp_data_insert
19
20
from mind_monitor_data_download_db_insert import mm_data_insert
29
30
import Crypto .Random
30
31
from Crypto .Cipher import AES
31
32
import base64
32
-
33
-
33
+ from requests_oauthlib import OAuth2Session
34
+
34
35
#----Crypto Variables----
35
36
# salt size in bytes
36
37
SALT_SIZE = 16
@@ -55,7 +56,6 @@ def create_app(encr_pass_input,debug=False):
55
56
DOWNLOAD_DIR = str (path_params .get ("download_dir" ))
56
57
TEMP_FILE_PATH = str (path_params .get ("temp_file_path" ))
57
58
58
-
59
59
encr_pass = encr_pass_input
60
60
61
61
dbx_params = config (filename = "encrypted_settings.ini" , section = "dropbox" ,encr_pass = encr_pass )
@@ -64,6 +64,12 @@ def create_app(encr_pass_input,debug=False):
64
64
REDIRECT_URI = str (dbx_params .get ("redirect_uri" ))
65
65
integrated_with_dropbox = str (dbx_params .get ("integrated_with_dropbox" ))
66
66
67
+ oura_params = config (filename = "encrypted_settings.ini" , section = "oura" ,encr_pass = encr_pass )
68
+ OURA_CLIENT_ID = str (oura_params .get ("oura_client_id" ))
69
+ OURA_CLIENT_SECRET = str (oura_params .get ("oura_client_secret" ))
70
+ OURA_AUTH_URL = str (oura_params .get ("oura_auth_url" ))
71
+ OURA_TOKEN_URL = str (oura_params .get ("oura_token_url" ))
72
+
67
73
anticaptcha_params = config (filename = "encrypted_settings.ini" , section = "anticaptcha" ,encr_pass = encr_pass )
68
74
anticaptcha_api_key = str (anticaptcha_params .get ("api_key" ))
69
75
if anticaptcha_api_key == "" :
@@ -123,6 +129,7 @@ def index():
123
129
archive_to_dropbox = False
124
130
archive_radio = None
125
131
dbx_auth_token = session .get ('dbx_auth_token' ,None )
132
+ oura_refresh_token = session .get ('oura_refresh_token' ,None )
126
133
auto_synch = False
127
134
128
135
output = DOWNLOAD_DIR
@@ -249,7 +256,21 @@ def index():
249
256
return redirect (url_for ('dropbox_auth_request' ))
250
257
else :
251
258
archive_to_dropbox = True
252
-
259
+
260
+ #----Check for and Retrieve Oura token----
261
+ if request .form .get ('ouraCheckbox' ) is not None :
262
+ if oura_refresh_token is None :
263
+ db_exists = check_user_db_exists (gc_username ,gc_password ,db_host ,superuser_un ,superuser_pw ,encr_pass )
264
+ if db_exists == True :
265
+ oura_token_exists , oura_token_from_db = check_oura_token_exists (gc_username ,db_host ,db_name ,superuser_un ,superuser_pw ,encr_pass )
266
+ if oura_token_exists == True : # token exists in database and it is not None
267
+ oura_refresh_token = oura_token_from_db
268
+ print ('Refresh Token: {}' .format (oura_refresh_token ))
269
+ else :
270
+ return redirect (url_for ('oura_auth_request' ))
271
+ else :
272
+ return redirect (url_for ('oura_auth_request' ))
273
+
253
274
# CLEANUP BEFORE DOWNLOAD -----------------
254
275
255
276
#----Delete Files and DB Data variables----
@@ -384,7 +405,8 @@ def index():
384
405
#PG:Call to execute "GC login" script
385
406
if gc_username is not None :
386
407
try :
387
- gc_agent = gc .login (gc_username , gc_password , mfp_username ,db_host ,superuser_un ,superuser_pw ,dbx_auth_token ,encr_pass ,save_pwd )
408
+ gc_agent = gc .login (gc_username , gc_password , mfp_username ,db_host ,superuser_un ,superuser_pw ,dbx_auth_token ,oura_refresh_token ,encr_pass ,save_pwd )
409
+ session ['oura_refresh_token' ] = None
388
410
gc_user_update (gc_username ,db_host ,db_name ,superuser_un ,superuser_pw ,encrypted_superuser_pw ,auto_synch_checkbox ,encr_pass ) #----Set Auto_Synch switch----
389
411
gc_login_progress = 'Login to GC successfull'
390
412
with StdoutRedirection (gc_username ):
@@ -455,6 +477,11 @@ def index():
455
477
time .sleep (1 )
456
478
with ErrorStdoutRedirection (gc_username ):
457
479
print ((str (datetime .datetime .now ()) + ' ' + gc_fit_well_progress ))
480
+
481
+ #TODO:
482
+ #PG:Call to execute "Parse and insert Oura wellness data" script
483
+ #TODO
484
+
458
485
#PG:Call to execute "Parse and insert JSON wellness data" script
459
486
try :
460
487
gc_json_well_progress = 'GC JSON wellness download started'
@@ -764,12 +791,48 @@ def dropbox_auth_request():
764
791
pass
765
792
766
793
@app .route ("/dropbox_confirm" )
767
- def dropbox_confirm ():
768
- token = dropbox_auth_finish (session ,request )
769
- session ['dbx_auth_token' ] = token
770
- continue_btn = 'delete'
771
- flash (' You have successfuly authenticated with Dropbox. Click "Continue" to proceed with download.' ,'success' )
772
- return redirect (url_for ('index' ,continue_btn = continue_btn ))
794
+ def dropbox_confirm ():
795
+ try :
796
+ token = dropbox_auth_finish (session ,request )
797
+ session ['dbx_auth_token' ] = token
798
+ continue_btn = 'delete'
799
+ flash (' You have successfuly authenticated with Dropbox. Click "Continue" to proceed with download.' ,'success' )
800
+ return redirect (url_for ('index' ,continue_btn = continue_btn ))
801
+ except Exception as e :
802
+ session ['dbx_auth_token' ] = None
803
+ flash (' There was a problem authenticating with Dropbox.' ,'warning' )
804
+ return redirect (url_for ('index' ))
805
+
806
+
807
+ @app .route ('/oura_auth_request' )
808
+ def oura_auth_request ():
809
+ try :
810
+ oura_session = OAuth2Session (OURA_CLIENT_ID )
811
+ authorization_url , state = oura_session .authorization_url (OURA_AUTH_URL )
812
+ session ['oura_oauth_state' ] = state
813
+ return redirect (authorization_url , 301 )
814
+ except Exception as e :
815
+ pass
816
+
817
+ @app .route ('/oura_confirm' )# Must match Redirect URI specified in https://cloud.ouraring.com/oauth/applications.
818
+ def oura_confirm ():
819
+ oura_session = OAuth2Session (OURA_CLIENT_ID , state = session ['oura_oauth_state' ])
820
+ try :
821
+ response = oura_session .fetch_token (
822
+ OURA_TOKEN_URL ,
823
+ client_secret = OURA_CLIENT_SECRET ,
824
+ authorization_response = request .url )
825
+
826
+ oura_refresh_token = response ['refresh_token' ]
827
+
828
+ session ['oura_refresh_token' ] = oura_refresh_token
829
+ continue_btn = 'delete'
830
+ flash (' You have successfuly authenticated with Oura. Click "Continue" to proceed with download.' ,'success' )
831
+ return redirect (url_for ('index' ,continue_btn = continue_btn ))
832
+ except Exception as e :
833
+ session ['oura_refresh_token' ] = None
834
+ flash (' There was a problem authenticating with Oura.' ,'warning' )
835
+ return redirect (url_for ('index' ))
773
836
774
837
@app .route ("/process_running" , methods = ['GET' ,'POST' ])
775
838
def process_running ():
0 commit comments