aboutsummaryrefslogtreecommitdiff
path: root/jb/views/auth.py
blob: ce9c1e0bfe4648d695b0af845e0ce989e3f7b12f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
from typing import Annotated
from urllib.parse import urlencode

from fastapi import APIRouter, Depends, HTTPException, Response, status
from fastapi.responses import HTMLResponse, RedirectResponse

from jb.api.auth import (
    SESSION_COOKIE_NAME,
    create_session,
    get_authenticated_user,
)
from jb.api.magic_token import (
    consume_amt_account_link_token,
    consume_magic_token,
    create_amt_account_link_token,
    create_magic_token,
)
from jb.config import settings
from jb.decorators import LOG
from jb.dependencies import get_gr_api_manager
from jb.managers.email_manager import (
    get_or_create_contact,
    send_amt_link_email,
    send_login_email,
)
from jb.managers.gr_api import GRApiManager
from jb.models.auth import (
    AccountLogin,
    AmtAccountLink,
    MagicLinkExchangeRequest,
    User,
)
from jb.settings import BASE_HTML

auth_router = APIRouter(prefix="/auth", tags=["Auth"])


@auth_router.post("/magic-link/request")
def request_magic_link(body: AccountLogin) -> dict[str, str]:
    """Create a magic link."""
    email = str(body.email)
    token = create_magic_token(user_email=email)

    if settings.debug:
        query = urlencode({"token": token})
        return {"magic_link": f"{settings.base_url}auth/magic-link/?{query}"}

    send_login_email(email=email, magic_token=token)
    return {"detail": "Link sent. Check your inbox and follow the link to log in."}


@auth_router.get("/magic-link/", response_class=HTMLResponse, include_in_schema=False)
def magic_link_landing_page(
    gr_api: Annotated[GRApiManager, Depends(get_gr_api_manager)],
    token: str | None = None,
) -> Response:
    """Serve the SPA without redeeming the token; email prefetches are harmless."""
    if settings.debug:
        if token is None:
            raise HTTPException(
                status_code=status.HTTP_400_BAD_REQUEST,
                detail="token is required",
            )
        response = RedirectResponse(url="/", status_code=status.HTTP_303_SEE_OTHER)
        _exchange_magic_link(token, response, gr_api)
        return response
    return HTMLResponse(
        BASE_HTML,
        headers={
            "Cache-Control": "no-store",
            "Referrer-Policy": "no-referrer",
            "X-Robots-Tag": "noindex, nofollow",
        },
    )


@auth_router.post("/magic-link/exchange", status_code=status.HTTP_204_NO_CONTENT)
def exchange_magic_link(
    body: MagicLinkExchangeRequest,
    response: Response,
    gr_api: Annotated[GRApiManager, Depends(get_gr_api_manager)],
) -> None:
    """Exchange a magic link only after its landing page makes an explicit POST."""
    _exchange_magic_link(body.token, response, gr_api)


def _exchange_magic_link(token: str, response: Response, gr_api: GRApiManager) -> None:
    user_email = consume_magic_token(token)
    user = gr_api.ensure_user_exists(User.model_validate({"email": user_email}))
    response.set_cookie(
        key=SESSION_COOKIE_NAME,
        value=create_session(user.product_user_id),
        max_age=settings.session_token_ttl_seconds,
        httponly=True,
        secure=not settings.debug,
        samesite="lax",
        path="/",
    )


@auth_router.post("/link-amt/request")
def link_amt_account(body: AmtAccountLink) -> dict[str, str]:
    """Link an AMT account and login."""
    email = str(body.email)
    amt_worker_id = body.amt_worker_id

    # TODO! Prevent a user that's already transitioned their account, from being
    # TODO!  able to continuously create this special link token.
    # TODO! Max Notes: this seems to be handled within the gr-api, and that
    # TODO!  can raise, the following line would / should fail if needed.

    token = create_amt_account_link_token(email=email, amt_worker_id=amt_worker_id)

    if settings.debug:
        query = urlencode({"token": token})
        return {"magic_link": f"{settings.base_url}auth/link-amt/?{query}"}

    send_amt_link_email(email=email, magic_token=token)
    return {}


@auth_router.get("/debug/", response_class=HTMLResponse, include_in_schema=False)
def link_amt_account_landing_page(
    gr_api: Annotated[GRApiManager, Depends(get_gr_api_manager)],
    token: str | None = None,
) -> HTMLResponse:
    """Serve the account-link SPA without consuming the one-time token."""

    # TODO! Try catch any of this, and if it fails, show the user a
    # TODO! failed HTML page. As of now, it shows them a failed JSON response.

    if settings.debug:
        if token is None:
            raise HTTPException(
                status_code=status.HTTP_400_BAD_REQUEST,
                detail="token is required",
            )

        _response = RedirectResponse(url="/", status_code=status.HTTP_303_SEE_OTHER)
        _exchange_amt_account_link(token=token, response=_response, gr_api=gr_api)

    return HTMLResponse(
        BASE_HTML,
        headers={
            "Cache-Control": "no-store",
            "Referrer-Policy": "no-referrer",
            "X-Robots-Tag": "noindex, nofollow",
        },
    )


@auth_router.post("/link-amt/exchange", status_code=status.HTTP_204_NO_CONTENT)
def exchange_amt_account_link(
    body: MagicLinkExchangeRequest,
    response: Response,
    gr_api: Annotated[GRApiManager, Depends(get_gr_api_manager)],
) -> None:
    """Validate the email link, then transition the bound AMT account."""
    try:
        _exchange_amt_account_link(body.token, response, gr_api)
    except ValueError as e:
        LOG.error(f"Failed to exchange AMT account link: {e}")
        raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))


def _exchange_amt_account_link(token: str, response: Response, gr_api: GRApiManager):
    token_data = consume_amt_account_link_token(token)
    email = token_data.email
    amt_worker_id = token_data.amt_worker_id

    user = User(email=email)
    user = gr_api.transition_user_from_amt(user=user, amt_worker_id=amt_worker_id)

    # In Mautic, associate the email with the worker ID (AFTER the user has transitioned)
    get_or_create_contact(email=email, amt_worker_id=amt_worker_id)

    session_token = create_session(user.product_user_id)
    response.set_cookie(
        key=SESSION_COOKIE_NAME,
        value=session_token,
        max_age=settings.session_token_ttl_seconds,
        httponly=True,
        secure=not settings.debug,
        samesite="lax",
        path="/",
    )


@auth_router.get("/session", response_model=User)
def get_session(
    user: Annotated[User, Depends(get_authenticated_user)],
) -> User:
    return user


@auth_router.delete("/session", status_code=status.HTTP_204_NO_CONTENT)
def delete_session(
    response: Response,
) -> None:
    # Logout is idempotent so clients can always discard their local token.
    # JWT sessions are stateless, so logout discards the browser cookie. A token
    # copied elsewhere remains valid until its short, configured expiration.
    response.delete_cookie(key=SESSION_COOKIE_NAME, path="/")