2024-01-24 12:29:35 +01:00
|
|
|
import os
|
2024-01-30 07:02:03 +01:00
|
|
|
from flask import Flask, render_template, request, redirect, abort, url_for, session
|
2024-01-24 12:29:35 +01:00
|
|
|
import requests
|
2024-01-24 14:55:17 +01:00
|
|
|
from datetime import datetime
|
2024-02-15 03:45:03 +01:00
|
|
|
from urllib.parse import urlencode
|
2024-01-24 12:29:35 +01:00
|
|
|
|
|
|
|
def get_env_variable(var_name):
|
|
|
|
value = os.environ.get(var_name)
|
|
|
|
if not value:
|
|
|
|
raise ValueError(f"Missing required environment variable: {var_name}")
|
|
|
|
return value
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
|
|
|
# Read environment variables outside the route function
|
|
|
|
client_id = get_env_variable('CLIENT_ID')
|
2024-01-31 07:31:28 +01:00
|
|
|
client_secret = get_env_variable('CLIENT_SECRET')
|
2024-01-24 12:29:35 +01:00
|
|
|
redirect_uri = get_env_variable('REDIRECT_URI')
|
|
|
|
optional_scopes = get_env_variable('OPTIONAL_SCOPES')
|
|
|
|
database_url = get_env_variable('DATABASE_URL')
|
2024-01-30 07:08:58 +01:00
|
|
|
secret_key = get_env_variable('APP_SECRET_KEY')
|
|
|
|
|
|
|
|
# Set secret key to enable sessions
|
|
|
|
app.secret_key = secret_key
|
2024-01-24 12:29:35 +01:00
|
|
|
|
2024-02-07 08:13:26 +01:00
|
|
|
# https://www.inoreader.com/oauth2/auth
|
2024-03-01 10:53:15 +01:00
|
|
|
# Corrected URL for Inoreader OAuth
|
|
|
|
# AUTH_URL = 'https://www.inoreader.com/oauth2/auth'
|
2024-02-07 08:13:26 +01:00
|
|
|
AUTH_URL = 'https://github.com/login/oauth/authorize'
|
2024-03-01 10:53:15 +01:00
|
|
|
#defining constant
|
|
|
|
# TOKEN_URL = 'https://www.inoreader.com/oauth2/token'
|
|
|
|
# USER_INFO_URL = 'https://www.inoreader.com/reader/api/0/user-info'
|
2024-01-24 12:29:35 +01:00
|
|
|
|
|
|
|
@app.route('/')
|
|
|
|
def home():
|
2024-02-15 03:45:03 +01:00
|
|
|
if is_logged_in():
|
2024-03-01 10:53:15 +01:00
|
|
|
return main_menu()
|
|
|
|
else:
|
|
|
|
return generate_login_page()
|
|
|
|
|
|
|
|
def main_menu():
|
|
|
|
token_id = session.get('token_id')
|
|
|
|
token = get_token_from_database(token_id)
|
|
|
|
|
|
|
|
user_info = get_user_info(token['access_token'])
|
|
|
|
last_synced, next_sync = format_sync_times(token)
|
|
|
|
|
|
|
|
return render_template('home.html', user_login=user_info.get('userName'),
|
|
|
|
user_email=user_info.get('userEmail'),
|
|
|
|
readwise_api_key=token.get('readwise_api_key', ''),
|
|
|
|
last_synced=last_synced, next_sync=next_sync)
|
|
|
|
|
|
|
|
def generate_login_page():
|
2024-02-15 03:45:03 +01:00
|
|
|
session['csrf_protection_string'] = os.urandom(16).hex()
|
2024-03-01 10:53:15 +01:00
|
|
|
|
2024-02-15 03:45:03 +01:00
|
|
|
oauth_params = {
|
2024-03-01 10:53:15 +01:00
|
|
|
'client_id': client_id,
|
|
|
|
'redirect_uri': redirect_uri,
|
|
|
|
'response_type': 'code',
|
|
|
|
'scope': optional_scopes,
|
|
|
|
'state': session['csrf_protection_string']
|
|
|
|
}
|
2024-02-15 03:45:03 +01:00
|
|
|
oauth_url = f'{AUTH_URL}?{urlencode(oauth_params)}'
|
2024-02-15 03:56:15 +01:00
|
|
|
return render_template('login.html', oauth_url=oauth_url)
|
|
|
|
|
2024-02-15 03:45:03 +01:00
|
|
|
|
2024-01-24 12:29:35 +01:00
|
|
|
@app.route('/oauth-redirect')
|
|
|
|
def oauth_redirect():
|
|
|
|
auth_code = request.args.get('code')
|
|
|
|
csrf_token = request.args.get('state')
|
|
|
|
|
|
|
|
# Verify the CSRF protection string
|
2024-02-07 08:13:26 +01:00
|
|
|
if csrf_token != session.get('csrf_protection_string'):
|
|
|
|
abort(403, 'Invalid CSRF token. Please try again.')
|
2024-01-24 12:29:35 +01:00
|
|
|
|
2024-03-01 10:53:15 +01:00
|
|
|
# Exchange authorization code for access and refresh tokens using the Inoreader API
|
2024-01-24 12:29:35 +01:00
|
|
|
response = requests.post(
|
2024-03-01 10:53:15 +01:00
|
|
|
https://github.com/login/oauth/access_token',
|
|
|
|
headers={'Accept': 'application/json'},
|
2024-01-24 12:29:35 +01:00
|
|
|
data={
|
|
|
|
'code': auth_code,
|
2024-01-31 07:31:28 +01:00
|
|
|
'redirect_uri': redirect_uri,
|
|
|
|
'client_id': client_id,
|
|
|
|
'client_secret': client_secret,
|
2024-03-01 10:53:15 +01:00
|
|
|
|
2024-01-24 12:29:35 +01:00
|
|
|
}
|
|
|
|
)
|
2024-01-30 16:40:04 +01:00
|
|
|
raise_for_status(response)
|
2024-01-30 16:05:39 +01:00
|
|
|
token = response.json()
|
2024-01-24 12:29:35 +01:00
|
|
|
|
2024-03-01 10:53:15 +01:00
|
|
|
# Fetch user information from Inoreader
|
|
|
|
user_info = requests.get(https://api.github.com/user', headers={
|
2024-01-30 16:40:04 +01:00
|
|
|
'Authorization': f'Bearer {token.get("access_token")}'
|
2024-03-01 10:53:15 +01:00
|
|
|
}).json()
|
2024-02-15 03:45:03 +01:00
|
|
|
|
2024-01-24 12:29:35 +01:00
|
|
|
# Save tokens for later use
|
2024-03-01 10:53:15 +01:00
|
|
|
token_id = save_or_update_token(user_info.get('userEmail'), token)
|
2024-01-24 12:29:35 +01:00
|
|
|
|
2024-01-30 16:05:39 +01:00
|
|
|
set_session_token_id(token_id)
|
2024-01-30 07:02:03 +01:00
|
|
|
return redirect(url_for('home'))
|
|
|
|
|
|
|
|
# logout
|
2024-01-30 07:11:58 +01:00
|
|
|
@app.route('/logout', methods=['POST'])
|
2024-01-30 07:02:03 +01:00
|
|
|
def logout():
|
|
|
|
token_id = session.get('token_id')
|
|
|
|
|
|
|
|
if not token_id:
|
|
|
|
return redirect(url_for('home'))
|
|
|
|
|
|
|
|
# remove token_id from session
|
|
|
|
session.pop('token_id', None)
|
|
|
|
|
2024-01-30 16:05:39 +01:00
|
|
|
# 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'))
|
|
|
|
|
2024-01-30 07:02:03 +01:00
|
|
|
response = requests.put(f'{database_url}/token/{token_id}', headers={
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
}, json={
|
2024-01-30 16:05:39 +01:00
|
|
|
'readwise_api_key': request.form.get('readwise_api_key')
|
2024-01-30 07:02:03 +01:00
|
|
|
})
|
2024-01-30 16:40:04 +01:00
|
|
|
raise_for_status(response)
|
2024-01-30 07:02:03 +01:00
|
|
|
|
|
|
|
return redirect(url_for('home'))
|
2024-01-24 12:29:35 +01:00
|
|
|
|
|
|
|
def is_logged_in():
|
2024-01-30 16:05:39 +01:00
|
|
|
token_id = session.get('token_id')
|
|
|
|
if not token_id:
|
2024-01-24 12:29:35 +01:00
|
|
|
return False
|
|
|
|
|
2024-01-30 16:05:39 +01:00
|
|
|
response = requests.get(f'{database_url}/token/{token_id}')
|
2024-01-30 16:40:04 +01:00
|
|
|
raise_for_status(response)
|
2024-01-30 16:05:39 +01:00
|
|
|
resp_json = response.json()
|
|
|
|
token = resp_json['token']
|
|
|
|
|
|
|
|
return token.get('active', False)
|
|
|
|
|
2024-03-01 10:53:15 +01:00
|
|
|
def save_or_update_token(email, access_token, refresh_token, expiration_seconds):
|
|
|
|
response = requests.get(f'{database_url}/token?email={email}')
|
|
|
|
raise_for_status(response)
|
|
|
|
|
|
|
|
if response.status_code == 200:
|
|
|
|
update_login(response.json()['token']['id'], access_token, refresh_token, expiration_seconds)
|
2024-01-30 17:47:08 +01:00
|
|
|
else:
|
2024-03-01 10:53:15 +01:00
|
|
|
add_login(email, access_token, refresh_token, expiration_seconds)
|
|
|
|
|
|
|
|
def add_login(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
|
|
|
|
}
|
|
|
|
)
|
|
|
|
raise_for_status(response)
|
|
|
|
return response.json().get('id')
|
|
|
|
|
|
|
|
def update_login(token_id, access_token, refresh_token, expiration_seconds):
|
|
|
|
response = requests.put(
|
|
|
|
f'{database_url}/token/{token_id}',
|
|
|
|
headers={'Content-Type': 'application/json'},
|
|
|
|
json={
|
|
|
|
'access_token': access_token,
|
|
|
|
'refresh_token': refresh_token,
|
|
|
|
'expiration_seconds': expiration_seconds
|
|
|
|
}
|
|
|
|
)
|
|
|
|
raise_for_status(response)
|
|
|
|
return token_id
|
2024-01-30 16:05:39 +01:00
|
|
|
|
|
|
|
def set_session_token_id(token_id):
|
|
|
|
session['token_id'] = token_id
|
|
|
|
|
2024-01-30 16:40:04 +01:00
|
|
|
def raise_for_status(response):
|
|
|
|
if response.status_code not in range(200, 300):
|
2024-01-30 17:15:08 +01:00
|
|
|
try:
|
2024-03-01 10:53:15 +01:00
|
|
|
msg = response.json().get('error', 'No error message provided')
|
|
|
|
except Exception:
|
2024-01-30 17:16:20 +01:00
|
|
|
msg = response.text
|
2024-03-01 10:53:15 +01:00
|
|
|
raise Exception(f'HTTPError: {response.status_code} - Message: {msg}')
|
2024-01-30 16:40:04 +01:00
|
|
|
|
2024-01-24 12:29:35 +01:00
|
|
|
if __name__ == '__main__':
|
2024-01-24 14:41:56 +01:00
|
|
|
app.run(host='0.0.0.0', debug=True, port=5000)
|