2022-05-06 14:41:52 +02:00
|
|
|
import requests
|
2022-03-14 09:33:31 +01:00
|
|
|
from flask import request, session, redirect, flash, url_for
|
|
|
|
from flask_limiter.util import get_remote_address
|
|
|
|
from flask_login import current_user
|
|
|
|
from requests_oauthlib import OAuth2Session
|
2022-08-12 15:02:00 +02:00
|
|
|
from typing import Optional
|
2022-03-14 09:33:31 +01:00
|
|
|
|
|
|
|
from app.auth.base import auth_bp
|
|
|
|
from app.auth.views.login_utils import after_login
|
|
|
|
from app.config import (
|
|
|
|
PROTON_BASE_URL,
|
|
|
|
PROTON_CLIENT_ID,
|
|
|
|
PROTON_CLIENT_SECRET,
|
2022-06-14 10:29:18 +02:00
|
|
|
PROTON_EXTRA_HEADER_NAME,
|
|
|
|
PROTON_EXTRA_HEADER_VALUE,
|
2022-03-14 09:33:31 +01:00
|
|
|
PROTON_VALIDATE_CERTS,
|
|
|
|
URL,
|
|
|
|
)
|
2022-07-04 15:40:17 +02:00
|
|
|
from app.log import LOG
|
2022-07-26 09:55:24 +02:00
|
|
|
from app.models import ApiKey, User
|
2022-03-14 09:33:31 +01:00
|
|
|
from app.proton.proton_client import HttpProtonClient, convert_access_token
|
2022-05-23 16:10:24 +02:00
|
|
|
from app.proton.proton_callback_handler import (
|
|
|
|
ProtonCallbackHandler,
|
|
|
|
Action,
|
|
|
|
)
|
2022-06-20 14:34:20 +02:00
|
|
|
from app.proton.utils import get_proton_partner
|
2022-08-11 10:38:44 +02:00
|
|
|
from app.utils import sanitize_next_url, sanitize_scheme
|
2022-03-14 09:33:31 +01:00
|
|
|
|
|
|
|
_authorization_base_url = PROTON_BASE_URL + "/oauth/authorize"
|
|
|
|
_token_url = PROTON_BASE_URL + "/oauth/token"
|
|
|
|
|
|
|
|
# need to set explicitly redirect_uri instead of leaving the lib to pre-fill redirect_uri
|
|
|
|
# when served behind nginx, the redirect_uri is localhost... and not the real url
|
|
|
|
_redirect_uri = URL + "/auth/proton/callback"
|
|
|
|
|
2022-07-05 22:26:48 +02:00
|
|
|
SESSION_ACTION_KEY = "oauth_action"
|
|
|
|
SESSION_STATE_KEY = "oauth_state"
|
2022-08-11 10:38:44 +02:00
|
|
|
DEFAULT_SCHEME = "auth.simplelogin"
|
2022-07-05 22:26:48 +02:00
|
|
|
|
2022-03-14 09:33:31 +01:00
|
|
|
|
2022-07-26 09:55:24 +02:00
|
|
|
def get_api_key_for_user(user: User) -> str:
|
|
|
|
ak = ApiKey.create(
|
|
|
|
user_id=user.id,
|
|
|
|
name="Created via Login with Proton on mobile app",
|
|
|
|
commit=True,
|
|
|
|
)
|
|
|
|
return ak.code
|
|
|
|
|
|
|
|
|
2022-08-12 15:02:00 +02:00
|
|
|
def extract_action() -> Optional[Action]:
|
2022-03-14 09:33:31 +01:00
|
|
|
action = request.args.get("action")
|
|
|
|
if action is not None:
|
|
|
|
if action == "link":
|
|
|
|
return Action.Link
|
2022-08-12 13:17:21 +02:00
|
|
|
elif action == "login":
|
|
|
|
return Action.Login
|
2022-03-14 09:33:31 +01:00
|
|
|
else:
|
2022-08-12 15:02:00 +02:00
|
|
|
LOG.w(f"Unknown action received: {action}")
|
|
|
|
return None
|
2022-03-14 09:33:31 +01:00
|
|
|
return Action.Login
|
|
|
|
|
|
|
|
|
|
|
|
def get_action_from_state() -> Action:
|
2022-07-05 22:26:48 +02:00
|
|
|
oauth_action = session[SESSION_ACTION_KEY]
|
2022-03-14 09:33:31 +01:00
|
|
|
if oauth_action == Action.Login.value:
|
|
|
|
return Action.Login
|
|
|
|
elif oauth_action == Action.Link.value:
|
|
|
|
return Action.Link
|
|
|
|
raise Exception(f"Unknown action in state: {oauth_action}")
|
|
|
|
|
|
|
|
|
|
|
|
@auth_bp.route("/proton/login")
|
|
|
|
def proton_login():
|
|
|
|
if PROTON_CLIENT_ID is None or PROTON_CLIENT_SECRET is None:
|
|
|
|
return redirect(url_for("auth.login"))
|
|
|
|
|
2022-08-12 13:17:21 +02:00
|
|
|
action = extract_action()
|
2022-08-12 15:02:00 +02:00
|
|
|
if action is None:
|
|
|
|
return redirect(url_for("auth.login"))
|
2022-08-12 13:17:21 +02:00
|
|
|
if action == Action.Link and not current_user.is_authenticated:
|
|
|
|
return redirect(url_for("auth.login"))
|
|
|
|
|
2022-03-14 09:33:31 +01:00
|
|
|
next_url = sanitize_next_url(request.args.get("next"))
|
|
|
|
if next_url:
|
2022-05-09 12:10:36 +02:00
|
|
|
session["oauth_next"] = next_url
|
2022-06-30 15:41:50 +02:00
|
|
|
elif "oauth_next" in session:
|
|
|
|
del session["oauth_next"]
|
2022-07-26 09:55:24 +02:00
|
|
|
|
2022-08-11 10:38:44 +02:00
|
|
|
scheme = sanitize_scheme(request.args.get("scheme"))
|
|
|
|
if scheme:
|
|
|
|
session["oauth_scheme"] = scheme
|
|
|
|
elif "oauth_scheme" in session:
|
|
|
|
del session["oauth_scheme"]
|
|
|
|
|
2022-07-26 09:55:24 +02:00
|
|
|
mode = request.args.get("mode", "session")
|
|
|
|
if mode == "apikey":
|
|
|
|
session["oauth_mode"] = "apikey"
|
|
|
|
else:
|
|
|
|
session["oauth_mode"] = "session"
|
|
|
|
|
2022-05-09 12:10:36 +02:00
|
|
|
proton = OAuth2Session(PROTON_CLIENT_ID, redirect_uri=_redirect_uri)
|
2022-03-14 09:33:31 +01:00
|
|
|
authorization_url, state = proton.authorization_url(_authorization_base_url)
|
|
|
|
|
|
|
|
# State is used to prevent CSRF, keep this for later.
|
2022-07-05 22:26:48 +02:00
|
|
|
session[SESSION_STATE_KEY] = state
|
2022-08-12 13:17:21 +02:00
|
|
|
session[SESSION_ACTION_KEY] = action.value
|
2022-03-14 09:33:31 +01:00
|
|
|
return redirect(authorization_url)
|
|
|
|
|
|
|
|
|
|
|
|
@auth_bp.route("/proton/callback")
|
|
|
|
def proton_callback():
|
2022-07-05 22:26:48 +02:00
|
|
|
if SESSION_STATE_KEY not in session or SESSION_STATE_KEY not in session:
|
|
|
|
flash("Invalid state, please retry", "error")
|
|
|
|
return redirect(url_for("auth.login"))
|
2022-03-14 09:33:31 +01:00
|
|
|
if PROTON_CLIENT_ID is None or PROTON_CLIENT_SECRET is None:
|
|
|
|
return redirect(url_for("auth.login"))
|
|
|
|
|
|
|
|
# user clicks on cancel
|
|
|
|
if "error" in request.args:
|
|
|
|
flash("Please use another sign in method then", "warning")
|
|
|
|
return redirect("/")
|
|
|
|
|
|
|
|
proton = OAuth2Session(
|
|
|
|
PROTON_CLIENT_ID,
|
2022-07-05 22:26:48 +02:00
|
|
|
state=session[SESSION_STATE_KEY],
|
2022-03-14 09:33:31 +01:00
|
|
|
redirect_uri=_redirect_uri,
|
|
|
|
)
|
2022-05-06 14:41:52 +02:00
|
|
|
|
|
|
|
def check_status_code(response: requests.Response) -> requests.Response:
|
|
|
|
if response.status_code != 200:
|
|
|
|
raise Exception(
|
|
|
|
f"Bad Proton API response [status={response.status_code}]: {response.json()}"
|
|
|
|
)
|
|
|
|
return response
|
|
|
|
|
|
|
|
proton.register_compliance_hook("access_token_response", check_status_code)
|
2022-06-14 10:29:18 +02:00
|
|
|
|
|
|
|
headers = None
|
|
|
|
if PROTON_EXTRA_HEADER_NAME and PROTON_EXTRA_HEADER_VALUE:
|
|
|
|
headers = {PROTON_EXTRA_HEADER_NAME: PROTON_EXTRA_HEADER_VALUE}
|
|
|
|
|
2022-07-04 15:40:17 +02:00
|
|
|
try:
|
|
|
|
token = proton.fetch_token(
|
|
|
|
_token_url,
|
|
|
|
client_secret=PROTON_CLIENT_SECRET,
|
|
|
|
authorization_response=request.url,
|
|
|
|
verify=PROTON_VALIDATE_CERTS,
|
|
|
|
method="GET",
|
|
|
|
include_client_id=True,
|
|
|
|
headers=headers,
|
|
|
|
)
|
|
|
|
except Exception as e:
|
|
|
|
LOG.warning(f"Error fetching Proton token: {e}")
|
|
|
|
flash("There was an error in the login process", "error")
|
|
|
|
return redirect(url_for("auth.login"))
|
|
|
|
|
2022-03-14 09:33:31 +01:00
|
|
|
credentials = convert_access_token(token["access_token"])
|
|
|
|
action = get_action_from_state()
|
|
|
|
|
|
|
|
proton_client = HttpProtonClient(
|
|
|
|
PROTON_BASE_URL, credentials, get_remote_address(), verify=PROTON_VALIDATE_CERTS
|
|
|
|
)
|
|
|
|
handler = ProtonCallbackHandler(proton_client)
|
2022-05-23 16:10:24 +02:00
|
|
|
proton_partner = get_proton_partner()
|
2022-03-14 09:33:31 +01:00
|
|
|
|
2022-08-11 10:38:44 +02:00
|
|
|
next_url = session.get("oauth_next")
|
2022-03-14 09:33:31 +01:00
|
|
|
if action == Action.Login:
|
2022-05-23 16:10:24 +02:00
|
|
|
res = handler.handle_login(proton_partner)
|
2022-03-14 09:33:31 +01:00
|
|
|
elif action == Action.Link:
|
2022-05-23 16:10:24 +02:00
|
|
|
res = handler.handle_link(current_user, proton_partner)
|
2022-03-14 09:33:31 +01:00
|
|
|
else:
|
|
|
|
raise Exception(f"Unknown Action: {action.name}")
|
|
|
|
|
|
|
|
if res.flash_message is not None:
|
|
|
|
flash(res.flash_message, res.flash_category)
|
|
|
|
|
2022-08-11 10:38:44 +02:00
|
|
|
oauth_scheme = session.get("oauth_scheme")
|
2022-07-26 09:55:24 +02:00
|
|
|
if session.get("oauth_mode", "session") == "apikey":
|
|
|
|
apikey = get_api_key_for_user(res.user)
|
2022-08-11 10:38:44 +02:00
|
|
|
scheme = oauth_scheme or DEFAULT_SCHEME
|
2022-08-12 13:17:21 +02:00
|
|
|
return redirect(f"{scheme}:///login?apikey={apikey}")
|
2022-07-26 09:55:24 +02:00
|
|
|
|
2022-03-14 09:33:31 +01:00
|
|
|
if res.redirect_to_login:
|
|
|
|
return redirect(url_for("auth.login"))
|
|
|
|
|
2022-08-11 10:38:44 +02:00
|
|
|
if next_url and next_url[0] == "/" and oauth_scheme:
|
|
|
|
next_url = f"{oauth_scheme}://{next_url}"
|
2022-03-14 09:33:31 +01:00
|
|
|
|
2022-08-11 10:38:44 +02:00
|
|
|
redirect_url = next_url or res.redirect
|
|
|
|
return after_login(res.user, redirect_url, login_from_proton=True)
|