aboutsummaryrefslogtreecommitdiff
path: root/jb/views/auth.py
blob: e6410a616ec3771c0422e9a25a837cfa0b7f8396 (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import secrets
from typing import Annotated
from urllib.parse import urlencode

from fastapi import (
    APIRouter,
    BackgroundTasks,
    Depends,
    Header,
    HTTPException,
    Request,
    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 (
    INVITE_AMT_LINK_TOKEN_TTL,
    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.managers.thl import create_paypal_cashout_method_if_not_exists
from jb.models.auth import (
    AccountLogin,
    AmtAccountLink,
    MagicLinkExchangeRequest,
    User,
)
from jb.settings import render_base_html
from jb.views.utils import get_client_ip

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


def try_create_paypal_cashout_method_if_not_exists(
    product_user_id: str, email: str, client_ip: str
) -> None:
    try:
        create_paypal_cashout_method_if_not_exists(
            product_user_id=product_user_id,
            email=email,
            client_ip=client_ip,
        )
    except Exception:
        LOG.exception("Failed to create PayPal cashout method for %s", product_user_id)


def authenticate_invite_amt_account_link(
    authorization: Annotated[str | None, Header()] = None,
) -> None:
    expected_token = settings.invite_amt_account_token
    if expected_token is None:
        raise HTTPException(
            status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
            detail="not configured",
        )

    scheme, _, supplied_token = (authorization or "").partition(" ")
    authenticated = scheme.lower() == "bearer" and secrets.compare_digest(
        supplied_token,
        expected_token.get_secret_value(),
    )
    if not authenticated:
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail="Invalid scheduler authentication token",
            headers={"WWW-Authenticate": "Bearer"},
        )


@auth_router.post("/magic-link/request/")
def request_magic_link(body: AccountLogin) -> dict[str, str]:
    return _request_magic_link(str(body.email))


def _request_magic_link(email: str) -> dict[str, str]:
    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(
    request: Request,
    background_tasks: BackgroundTasks,
    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)
        client_ip = get_client_ip(request)
        _exchange_magic_link(
            token,
            response,
            gr_api,
            client_ip=client_ip,
            background_tasks=background_tasks,
        )
        return response
    return HTMLResponse(
        render_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(
    request: Request,
    background_tasks: BackgroundTasks,
    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."""
    client_ip = get_client_ip(request)
    _exchange_magic_link(
        body.token,
        response,
        gr_api,
        client_ip=client_ip,
        background_tasks=background_tasks,
    )


def _exchange_magic_link(
    token: str,
    response: Response,
    gr_api: GRApiManager,
    client_ip: str,
    background_tasks: BackgroundTasks,
) -> None:
    user_email = consume_magic_token(token)
    user = gr_api.ensure_user_exists(User.model_validate({"email": user_email}))

    # Cashout setup is not required for login and should not delay the response.
    background_tasks.add_task(
        try_create_paypal_cashout_method_if_not_exists,
        product_user_id=user.product_user_id,
        email=str(user.email),
        client_ip=client_ip,
    )

    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,
    gr_api: Annotated[GRApiManager, Depends(get_gr_api_manager)],
) -> dict[str, str]:
    """Link an AMT account and login."""
    email = str(body.email)
    amt_worker_id = body.amt_worker_id

    # If the email already has transitioned, treat this as a login request
    user = User.model_validate({"email": email})
    user_exists = gr_api.get_user_if_exists(user.product_user_id)
    if user_exists:
        return _request_magic_link(email)

    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.post("/link-amt/invite/")
def invite_amt_account_link(
    body: AmtAccountLink,
    _authenticated: Annotated[None, Depends(authenticate_invite_amt_account_link)],
) -> dict[str, str]:
    """Create a long-lived AMT account-link URL without sending an email."""
    token = create_amt_account_link_token(
        email=str(body.email),
        amt_worker_id=body.amt_worker_id,
        ttl_seconds=INVITE_AMT_LINK_TOKEN_TTL,
    )
    query = urlencode({"token": token})
    return {"magic_link": f"{settings.base_url}auth/link-amt/?{query}"}


@auth_router.get("/debug/", response_class=HTMLResponse, include_in_schema=False)
def link_amt_account_landing_page(
    request: Request,
    background_tasks: BackgroundTasks,
    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",
            )
        client_ip = get_client_ip(request)
        _response = RedirectResponse(url="/", status_code=status.HTTP_303_SEE_OTHER)
        _exchange_amt_account_link(
            token=token,
            response=_response,
            gr_api=gr_api,
            client_ip=client_ip,
            background_tasks=background_tasks,
        )

    return HTMLResponse(
        render_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(
    request: Request,
    background_tasks: BackgroundTasks,
    body: MagicLinkExchangeRequest,
    response: Response,
    gr_api: Annotated[GRApiManager, Depends(get_gr_api_manager)],
) -> None:
    """Validate the email link, then transition the bound AMT account."""
    client_ip = get_client_ip(request)
    try:
        _exchange_amt_account_link(
            body.token,
            response,
            gr_api,
            client_ip=client_ip,
            background_tasks=background_tasks,
        )
    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,
    client_ip: str,
    background_tasks: BackgroundTasks,
):
    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)
    background_tasks.add_task(
        try_create_paypal_cashout_method_if_not_exists,
        product_user_id=user.product_user_id,
        email=str(user.email),
        client_ip=client_ip,
    )

    # 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="/")