diff --git a/app/main.py b/app/main.py index 7545bd0..b552e2b 100644 --- a/app/main.py +++ b/app/main.py @@ -22,7 +22,12 @@ csrf_protection_string = None @app.route('/') def home(): if is_logged_in(): - return render_template('success.html') + resp_json = requests.get(f'{database_url}/token/latest').json() + access_token = resp_json['token']['access_token'] + user_info = requests.get('https://api.github.com/user', headers={ + 'Authorization': f'Bearer {access_token}' + }).json() + return render_template('success.html', user_info=user_info) # Generate a CSRF protection string global csrf_protection_string @@ -42,19 +47,32 @@ def oauth_redirect(): abort(400, 'Invalid CSRF token. Please try again.') # Exchange authorization code for access and refresh tokens + # response = requests.post( + # 'https://www.inoreader.com/oauth2/token', + # headers={ + # 'Content-Type': 'application/x-www-form-urlencoded', + # }, + # data={ + # 'code': auth_code, + # 'redirect_uri': get_env_variable('REDIRECT_URI'), + # 'client_id': get_env_variable('CLIENT_ID'), + # 'client_secret': get_env_variable('CLIENT_SECRET'), + # 'scope': '', + # 'grant_type': 'authorization_code' + # } + # ) + + # TEST: Github OAuth - REMOVE response = requests.post( - 'https://www.inoreader.com/oauth2/token', + 'https://github.com/login/oauth/access_token', headers={ - 'Content-Type': 'application/x-www-form-urlencoded', - 'User-agent': 'your-user-agent' + 'Accept': 'application/json' }, data={ 'code': auth_code, 'redirect_uri': get_env_variable('REDIRECT_URI'), 'client_id': get_env_variable('CLIENT_ID'), - 'client_secret': get_env_variable('CLIENT_SECRET'), - 'scope': '', - 'grant_type': 'authorization_code' + 'client_secret': get_env_variable('CLIENT_SECRET') } ) @@ -62,6 +80,10 @@ def oauth_redirect(): tokens = response.json() + # TEST: Github OAuth - REMOVE + tokens['refresh_token'] = 'N/A' + tokens['expires_in'] = 36000 + # Save tokens for later use save_tokens(tokens['access_token'], tokens['refresh_token'], tokens['expires_in']) diff --git a/app/templates/index.html b/app/templates/index.html index c0b183c..637b6ec 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -15,7 +15,8 @@ var encodedOptionalScopes = encodeURIComponent('{{ optional_scopes }}'); // Construct the URL using Jinja variables - var oauthUrl = `https://www.inoreader.com/oauth2/auth?client_id={{ client_id }}&redirect_uri=${encodedRedirectUri}&response_type=code&scope=${encodedOptionalScopes}&state={{ csrf_protection_string }}`; + // var oauthUrl = `https://www.inoreader.com/oauth2/auth?client_id={{ client_id }}&redirect_uri=${encodedRedirectUri}&response_type=code&scope=${encodedOptionalScopes}&state={{ csrf_protection_string }}`; + var oauthUrl = `https://github.com/login/oauth/authorize?client_id={{ client_id }}&redirect_uri=${encodedRedirectUri}&response_type=code&scope=${encodedOptionalScopes}&state={{ csrf_protection_string }}`; // Redirect to the constructed URL window.location.href = oauthUrl; diff --git a/app/templates/success.html b/app/templates/success.html index 8e2a59c..16207a4 100644 --- a/app/templates/success.html +++ b/app/templates/success.html @@ -6,6 +6,6 @@ Simple Frontend -

Logged In!

+

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