diff --git a/app/app.py b/app/app.py index 88a7a44..35848a3 100644 --- a/app/app.py +++ b/app/app.py @@ -1,6 +1,7 @@ -from flask import Flask, render_template, url_for, redirect, request -from authlib.integrations.flask_client import OAuth +from urllib.parse import urlparse from secrets import token_urlsafe +from flask import Flask, render_template, url_for, redirect, request, make_response, session +from authlib.integrations.flask_client import OAuth from requests import post app = Flask(__name__) @@ -8,6 +9,7 @@ app = Flask(__name__) SECRET_KEY = token_urlsafe(32) app.secret_key = SECRET_KEY +cache = OAuthCache() oauth = OAuth(app) gotosocial = oauth.register( @@ -31,40 +33,60 @@ def index(): @app.route('/set_domain', methods=['POST']) def set_domain(): if(request.method == 'POST'): - payload = { - 'client_name':'gotosocial-fe', - 'redirect_uris':url_for('index', _external=True) - } - response = post(f'https://{request.form["domain"]}/api/v1/apps', data=payload) - client_data = response.json() - gotosocial.client_id = client_data['client_id'] - gotosocial.client_secret = client_data['client_secret'] - gotosocial.access_token_params = { - 'response_type':'token', - 'grant_type':'authorization_code', - 'client_id':client_data['client_id'], - 'client_secret':client_data['client_secret'] - } - gotosocial.access_token_url = f'https://{request.form["domain"]}{gotosocial.access_token_url}' - gotosocial.authorize_url = f'https://{request.form["domain"]}{gotosocial.authorize_url}' - gotosocial.api_base_url = f'https://{request.form["domain"]}{gotosocial.api_base_url}' - return redirect('/login') + try: + domain_parse = urlparse(request.form["domain"]) + if(domain_parse): + if(domain_parse.scheme): + domain = domain.geturl() + else: + domain = f'https://{domain_parse.geturl()}' + payload = { + 'client_name':'gotosocial-fe', + 'redirect_uris':url_for('index', _external=True) + } + response = post(f'{domain}/api/v1/apps', data=payload) + client_data = response.json() + oauth.gotosocial.client_id = client_data['client_id'] + oauth.gotosocial.client_secret = client_data['client_secret'] + oauth.gotosocial.access_token_params = { + 'response_type':'token', + 'grant_type':'authorization_code', + 'client_id':client_data['client_id'], + 'client_secret':client_data['client_secret'] + } + oauth.gotosocial.access_token_url = f'{domain}{oauth.gotosocial.access_token_url}' + oauth.gotosocial.authorize_url = f'{domain}{oauth.gotosocial.authorize_url}' + oauth.gotosocial.api_base_url = f'{domain}{oauth.gotosocial.api_base_url}' + return redirect('/login') + else: + return "Did you even submit anything?" + except: + return "Are you sure you're putting in a GoToSocial instance url?" else: return "Sorry, but you can't get *GET* /set_domain, hun." @app.route('/login') def login(): redirect_uri = url_for('authorize', _external=True) - return gotosocial.authorize_redirect(redirect_uri) + return oauth.gotosocial.authorize_redirect(redirect_uri) @app.route('/authorize') def authorize(): - token = gotosocial.authorize_access_token() - print(token) - response = gotosocial.get( - 'api/v1/accounts/verify_credentials', token=token) + token = oauth.gotosocial.authorize_access_token() + session['oauth_token'] = token + response = oauth.gotosocial.get( + 'api/v1/accounts/verify_credentials') response.raise_for_status() - account_info = response.json() - return account_info + return redirect(url_for('home', _external=True)) +@app.route('/home') +def home(): + # If we're here, assume that we already authenticated for now. + token = session['oauth_token'] + # TODO: Long-term shoukd make sure we store the token in localStorage and try to retrieve it there first. + response = oauth.gotosocial.get( + 'api/v1/timelines/home', token=token) + response.raise_for_status() + home_timeline = response.json() + return home_timeline # render_template('index.html') diff --git a/app/templates/home/index.html b/app/templates/home/index.html new file mode 100755 index 0000000..ccb7218 --- /dev/null +++ b/app/templates/home/index.html @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index ea1878e..d1233e9 100755 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,8 +7,8 @@ services: build: . volumes: - ./app:/var/www/app - #environment: - # - + environment: + - PYTHONUNBUFFERED=1 ports: - 5000:5000 command: 'sh -c "cd app && flask run --host=0.0.0.0"'