Files
Link-Log/backend/app/api/mastodon.py
T
Olaf 57e9775882
Build LinkLog Development Image / development-image (push) Successful in 10s
Mastodon Oauth
2026-08-25 23:31:49 +02:00

33 lines
1.3 KiB
Python

## Copyright © 2026 Olaf Kolkman
## SPDX-License-Identifier: GPL-3.0-or-later
from urllib.parse import quote
from fastapi import APIRouter, Depends, HTTPException
from fastapi.responses import RedirectResponse
from starlette.requests import Request
from backend.app.api.dependencies import get_current_user
from backend.app.services.mastodon_oauth import finish_authorization, start_authorization
router = APIRouter()
@router.get('/oauth/start')
def oauth_start(instance: str = 'mastodon.social', user: dict = Depends(get_current_user)):
try:
authorization_url = start_authorization(user['id'], instance)
except Exception as error:
raise HTTPException(status_code=502, detail=f'Could not register with Mastodon: {error}') from error
return {'authorization_url': authorization_url}
@router.get('/oauth/callback')
def oauth_callback(request: Request, code: str | None = None, state: str | None = None, error: str | None = None):
if error or not code or not state:
return RedirectResponse(f'/profile?mastodon_error={quote(error or "Authorization was cancelled")}')
try:
finish_authorization(code, state)
except Exception as callback_error:
return RedirectResponse(f'/profile?mastodon_error={quote(str(callback_error))}')
return RedirectResponse('/profile?mastodon=connected')