2022-03-14 09:33:31 +01:00
|
|
|
from dataclasses import dataclass
|
2022-06-09 10:19:49 +02:00
|
|
|
from enum import Enum
|
2022-03-14 09:33:31 +01:00
|
|
|
from flask import url_for
|
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
from app.db import Session
|
2022-06-09 10:19:49 +02:00
|
|
|
from app.errors import LinkException, ProtonPartnerNotSetUp
|
|
|
|
from app.models import User, Partner
|
2022-03-14 09:33:31 +01:00
|
|
|
from app.proton.proton_client import ProtonClient, ProtonUser
|
2022-06-09 10:19:49 +02:00
|
|
|
from app.account_linking import (
|
|
|
|
process_login_case,
|
|
|
|
process_link_case,
|
|
|
|
PartnerLinkRequest,
|
|
|
|
)
|
2022-03-14 09:33:31 +01:00
|
|
|
|
|
|
|
PROTON_PARTNER_NAME = "Proton"
|
2022-05-23 16:10:24 +02:00
|
|
|
_PROTON_PARTNER: Optional[Partner] = None
|
2022-03-14 09:33:31 +01:00
|
|
|
|
|
|
|
|
2022-05-23 16:10:24 +02:00
|
|
|
def get_proton_partner() -> Partner:
|
|
|
|
global _PROTON_PARTNER
|
|
|
|
if _PROTON_PARTNER is None:
|
2022-03-14 09:33:31 +01:00
|
|
|
partner = Partner.get_by(name=PROTON_PARTNER_NAME)
|
|
|
|
if partner is None:
|
2022-05-05 15:05:39 +02:00
|
|
|
raise ProtonPartnerNotSetUp
|
2022-05-23 16:36:13 +02:00
|
|
|
Session.expunge(partner)
|
|
|
|
_PROTON_PARTNER = partner
|
2022-05-23 16:10:24 +02:00
|
|
|
return _PROTON_PARTNER
|
|
|
|
|
|
|
|
|
2022-06-09 10:19:49 +02:00
|
|
|
class Action(Enum):
|
2022-03-14 09:33:31 +01:00
|
|
|
Login = 1
|
|
|
|
Link = 2
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class ProtonCallbackResult:
|
|
|
|
redirect_to_login: bool
|
|
|
|
flash_message: Optional[str]
|
|
|
|
flash_category: Optional[str]
|
|
|
|
redirect: Optional[str]
|
|
|
|
user: Optional[User]
|
|
|
|
|
|
|
|
|
2022-06-09 10:19:49 +02:00
|
|
|
def generate_account_not_allowed_to_log_in() -> ProtonCallbackResult:
|
2022-03-14 09:33:31 +01:00
|
|
|
return ProtonCallbackResult(
|
2022-06-09 10:19:49 +02:00
|
|
|
redirect_to_login=True,
|
|
|
|
flash_message="This account is not allowed to log in with Proton. Please convert your account to a full Proton account",
|
|
|
|
flash_category="error",
|
|
|
|
redirect=None,
|
|
|
|
user=None,
|
2022-03-14 09:33:31 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class ProtonCallbackHandler:
|
|
|
|
def __init__(self, proton_client: ProtonClient):
|
|
|
|
self.proton_client = proton_client
|
|
|
|
|
2022-05-23 16:10:24 +02:00
|
|
|
def handle_login(self, partner: Partner) -> ProtonCallbackResult:
|
2022-06-09 10:19:49 +02:00
|
|
|
try:
|
|
|
|
user = self.__get_partner_user()
|
|
|
|
if user is None:
|
|
|
|
return generate_account_not_allowed_to_log_in()
|
|
|
|
res = process_login_case(user, partner)
|
|
|
|
return ProtonCallbackResult(
|
|
|
|
redirect_to_login=False,
|
|
|
|
flash_message=None,
|
|
|
|
flash_category=None,
|
|
|
|
redirect=None,
|
|
|
|
user=res.user,
|
|
|
|
)
|
|
|
|
except LinkException as e:
|
|
|
|
return ProtonCallbackResult(
|
|
|
|
redirect_to_login=True,
|
|
|
|
flash_message=e.message,
|
|
|
|
flash_category="error",
|
|
|
|
redirect=None,
|
|
|
|
user=None,
|
|
|
|
)
|
2022-03-14 09:33:31 +01:00
|
|
|
|
2022-05-23 16:10:24 +02:00
|
|
|
def handle_link(
|
|
|
|
self, current_user: Optional[User], partner: Partner
|
|
|
|
) -> ProtonCallbackResult:
|
2022-03-14 09:33:31 +01:00
|
|
|
if current_user is None:
|
|
|
|
raise Exception("Cannot link account with current_user being None")
|
2022-06-09 10:19:49 +02:00
|
|
|
try:
|
|
|
|
user = self.__get_partner_user()
|
|
|
|
if user is None:
|
|
|
|
return generate_account_not_allowed_to_log_in()
|
|
|
|
res = process_link_case(user, current_user, partner)
|
|
|
|
return ProtonCallbackResult(
|
|
|
|
redirect_to_login=False,
|
|
|
|
flash_message="Account successfully linked",
|
|
|
|
flash_category="success",
|
|
|
|
redirect=url_for("dashboard.setting"),
|
|
|
|
user=res.user,
|
|
|
|
)
|
|
|
|
except LinkException as e:
|
|
|
|
return ProtonCallbackResult(
|
|
|
|
redirect_to_login=False,
|
|
|
|
flash_message=e.message,
|
|
|
|
flash_category="error",
|
|
|
|
redirect=None,
|
|
|
|
user=None,
|
|
|
|
)
|
|
|
|
|
|
|
|
def __get_partner_user(self) -> Optional[PartnerLinkRequest]:
|
|
|
|
proton_user = self.__get_proton_user()
|
|
|
|
if proton_user is None:
|
|
|
|
return None
|
|
|
|
return PartnerLinkRequest(
|
|
|
|
email=proton_user.email,
|
|
|
|
external_user_id=proton_user.id,
|
|
|
|
name=proton_user.name,
|
|
|
|
plan=proton_user.plan,
|
2022-06-16 10:25:50 +02:00
|
|
|
from_partner=False, # The user has started this flow, so we don't mark it as created by a partner
|
2022-06-09 10:19:49 +02:00
|
|
|
)
|
2022-03-14 09:33:31 +01:00
|
|
|
|
2022-06-09 10:19:49 +02:00
|
|
|
def __get_proton_user(self) -> Optional[ProtonUser]:
|
2022-03-14 09:33:31 +01:00
|
|
|
user = self.proton_client.get_user()
|
2022-06-09 10:19:49 +02:00
|
|
|
if user is None:
|
|
|
|
return None
|
|
|
|
return ProtonUser(email=user.email, plan=user.plan, name=user.name, id=user.id)
|