url encoding in python
This commit is contained in:
parent
bde73bf535
commit
b5373d144e
52
app/main.py
52
app/main.py
|
@ -2,6 +2,7 @@ import os
|
||||||
from flask import Flask, render_template, request, redirect, abort, url_for, session
|
from flask import Flask, render_template, request, redirect, abort, url_for, session
|
||||||
import requests
|
import requests
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from urllib.parse import quote_plus, urlencode
|
||||||
|
|
||||||
def get_env_variable(var_name):
|
def get_env_variable(var_name):
|
||||||
value = os.environ.get(var_name)
|
value = os.environ.get(var_name)
|
||||||
|
@ -27,30 +28,45 @@ AUTH_URL = 'https://github.com/login/oauth/authorize'
|
||||||
|
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
def home():
|
def home():
|
||||||
if is_logged_in():
|
"""Home route that either shows user info or initiates OAuth flow."""
|
||||||
token_id = session.get('token_id')
|
if 'token_id' in session:
|
||||||
|
try:
|
||||||
|
# Attempt to fetch user info using the stored access token
|
||||||
|
token_id = session['token_id']
|
||||||
resp = requests.get(f'{database_url}/token/{token_id}')
|
resp = requests.get(f'{database_url}/token/{token_id}')
|
||||||
raise_for_status(resp)
|
resp.raise_for_status() # This will raise an exception for HTTP errors
|
||||||
resp_json = resp.json()
|
token = resp.json()['token']
|
||||||
token = resp_json['token']
|
|
||||||
|
|
||||||
user_info = requests.get('https://api.github.com/user', headers={
|
# Use the access token to fetch user info from GitHub
|
||||||
'Authorization': f'Bearer {token.get("access_token")}'
|
user_info_resp = requests.get('https://api.github.com/user', headers={'Authorization': f'token {token["access_token"]}'})
|
||||||
}).json()
|
user_info_resp.raise_for_status()
|
||||||
|
user_info = user_info_resp.json()
|
||||||
|
|
||||||
last_synced = datetime.fromtimestamp(token.get('updated_at')).strftime('%Y-%m-%d %H:%M:%S')
|
# Prepare user info and token details for rendering
|
||||||
next_sync = datetime.fromtimestamp(token.get('updated_at') + token.get('expiration_seconds')).strftime('%Y-%m-%d %H:%M:%S')
|
last_synced = datetime.fromtimestamp(token['updated_at']).strftime('%Y-%m-%d %H:%M:%S')
|
||||||
return render_template('home.html', user_login=user_info.get('login'), user_email=user_info.get('email'), # for inoreader it's userName and userEmail
|
next_sync = datetime.fromtimestamp(token['updated_at'] + token['expires_in']).strftime('%Y-%m-%d %H:%M:%S')
|
||||||
readwise_api_key=token.get('readwise_api_key') or '',
|
|
||||||
last_synced=last_synced, next_sync=next_sync)
|
|
||||||
|
|
||||||
# Generate a CSRF protection string
|
return render_template('home.html', user_info=user_info, last_synced=last_synced, next_sync=next_sync)
|
||||||
session['csrf_protection_string'] = os.urandom(16).hex()
|
except Exception as e:
|
||||||
|
# Handle errors (e.g., token expired) by clearing the session and redirecting to login
|
||||||
|
session.pop('token_id', None)
|
||||||
|
return redirect(url_for('home'))
|
||||||
|
|
||||||
# Pass dynamic variables to the template
|
else:
|
||||||
return render_template('login.html', auth_url=AUTH_URL, client_id=client_id, redirect_uri=redirect_uri,
|
# User not logged in; start OAuth flow
|
||||||
optional_scopes=optional_scopes, csrf_protection_string=session.get('csrf_protection_string'))
|
session['csrf_protection_string'] = os.urandom(16).hex() # CSRF protection
|
||||||
|
|
||||||
|
# Construct OAuth URL
|
||||||
|
params = {
|
||||||
|
'client_id': client_id,
|
||||||
|
'redirect_uri': redirect_uri,
|
||||||
|
'response_type': 'code',
|
||||||
|
'scope': optional_scopes,
|
||||||
|
'state': session['csrf_protection_string']
|
||||||
|
}
|
||||||
|
oauth_url = f'{AUTH_URL}?{urlencode(params)}'
|
||||||
|
|
||||||
|
return redirect(oauth_url)
|
||||||
@app.route('/oauth-redirect')
|
@app.route('/oauth-redirect')
|
||||||
def oauth_redirect():
|
def oauth_redirect():
|
||||||
auth_code = request.args.get('code')
|
auth_code = request.args.get('code')
|
||||||
|
|
Loading…
Reference in New Issue
Block a user