From 3ec293e1faeea72641d568ce68416528c3525d99 Mon Sep 17 00:00:00 2001 From: Swapnil Date: Tue, 30 Jan 2024 20:35:39 +0530 Subject: [PATCH] app-service: Updated templates and login logic --- app/main.py | 77 +++++++++++++++++------- app/templates/home.html | 28 +++++++++ app/templates/{index.html => login.html} | 4 +- app/templates/success.html | 15 ----- 4 files changed, 86 insertions(+), 38 deletions(-) create mode 100644 app/templates/home.html rename app/templates/{index.html => login.html} (90%) delete mode 100644 app/templates/success.html diff --git a/app/main.py b/app/main.py index fbed310..d2e153c 100644 --- a/app/main.py +++ b/app/main.py @@ -26,23 +26,28 @@ csrf_protection_string = None @app.route('/') def home(): if is_logged_in(): - resp_json = requests.get(f'{database_url}/token/latest').json() - access_token = resp_json['token']['access_token'] - - # set session token id - session['token_id'] = resp_json['token']['id'] + token_id = session.get('token_id') + resp = requests.get(f'{database_url}/token/{token_id}') + resp.raise_for_status() + resp_json = resp.json() + token = resp_json['token'] user_info = requests.get('https://api.github.com/user', headers={ - 'Authorization': f'Bearer {access_token}' + 'Authorization': f'Bearer {token.get("access_token")}' }).json() - return render_template('success.html', user_info=user_info) + + last_synced = datetime.fromtimestamp(token.get('timestamp')).strftime('%Y-%m-%d %H:%M:%S') + next_sync = datetime.fromtimestamp(token.get('timestamp') + token.get('expiration_seconds')).strftime('%Y-%m-%d %H:%M:%S') + return render_template('home.html', user_info=user_info, + readwise_api_key=token.get('readwise_api_key', None), + last_synced=last_synced, next_sync=next_sync) # Generate a CSRF protection string global csrf_protection_string csrf_protection_string = os.urandom(16).hex() # Pass dynamic variables to the template - return render_template('index.html', client_id=client_id, redirect_uri=redirect_uri, + return render_template('login.html', client_id=client_id, redirect_uri=redirect_uri, optional_scopes=optional_scopes, csrf_protection_string=csrf_protection_string) @app.route('/oauth-redirect') @@ -86,15 +91,21 @@ def oauth_redirect(): response.raise_for_status() - tokens = response.json() + token = response.json() # TEST: Github OAuth - REMOVE - tokens['refresh_token'] = 'N/A' - tokens['expires_in'] = 36000 + token['refresh_token'] = 'N/A' + token['expires_in'] = 3600 # Save tokens for later use - save_tokens(tokens['access_token'], tokens['refresh_token'], tokens['expires_in']) + token_id = save_token( + token.get('email'), # for inoreader it's userEmail + token.get('access_token'), + token.get('refresh_token'), + token.get('expires_in') + ) + set_session_token_id(token_id) return redirect(url_for('home')) # logout @@ -108,32 +119,51 @@ def logout(): # remove token_id from session session.pop('token_id', None) + # response = requests.put(f'{database_url}/token/{token_id}', headers={ + # 'Content-Type': 'application/json' + # }, json={ + # 'is_logged_in': False + # }) + # response.raise_for_status() + + return redirect(url_for('home')) + +@app.route('/readwise', methods=['POST']) +def submit_readwise_api(): + token_id = session.get('token_id') + + if not token_id: + return redirect(url_for('home')) + response = requests.put(f'{database_url}/token/{token_id}', headers={ 'Content-Type': 'application/json' }, json={ - 'is_logged_in': False + 'readwise_api_key': request.form.get('readwise_api_key') }) response.raise_for_status() return redirect(url_for('home')) def is_logged_in(): - response = requests.get(f'{database_url}/token/latest') - response.raise_for_status() - if response.status_code == 204: + token_id = session.get('token_id') + if not token_id: return False - elif response.status_code == 200: - resp_json = response.json() - return resp_json['token']['is_logged_in'] or False - return False -def save_tokens(access_token, refresh_token, expiration_seconds): + response = requests.get(f'{database_url}/token/{token_id}') + response.raise_for_status() + resp_json = response.json() + token = resp_json['token'] + + return token.get('active', False) + +def save_token(email, access_token, refresh_token, expiration_seconds): response = requests.post( f'{database_url}/token', headers={ 'Content-Type': 'application/json' }, json={ + 'email': email, 'access_token': access_token, 'refresh_token': refresh_token, 'expiration_seconds': expiration_seconds @@ -141,5 +171,10 @@ def save_tokens(access_token, refresh_token, expiration_seconds): ) response.raise_for_status() + return response.json().get('id') + +def set_session_token_id(token_id): + session['token_id'] = token_id + if __name__ == '__main__': app.run(host='0.0.0.0', debug=True, port=5000) \ No newline at end of file diff --git a/app/templates/home.html b/app/templates/home.html new file mode 100644 index 0000000..dc1dcbe --- /dev/null +++ b/app/templates/home.html @@ -0,0 +1,28 @@ + + + + + + Inoreader To Readwise + + +

Logged In as {{ user_info.login }}({{user_info.name}})

+ + +

Last Synced: {{ last_synced }}

+

Next Synced: {{ next_synced }}

+
+ + +
+ + + +
+ + +
+ +
+ + diff --git a/app/templates/index.html b/app/templates/login.html similarity index 90% rename from app/templates/index.html rename to app/templates/login.html index 637b6ec..5ed3901 100644 --- a/app/templates/index.html +++ b/app/templates/login.html @@ -3,10 +3,10 @@ - Simple Frontend + Inoreader To Readwise - +