# Path: aurweb/l10n.py
# def get_translator_for_request(request):
#     """
#     Determine the preferred language from a FastAPI request object and build a
#     translator function for it.
# 
#     Example:
#     ```python
#     _ = get_translator_for_request(request)
#     print(_("Hello"))
#     ```
#     """
#     lang = request.cookies.get("AURLANG")
#     if lang is None:
#         lang = aurweb.config.get("options", "default_lang")
#     translator = Translator()
# 
#     def translate(message):
#         return translator.translate(message, lang)
# 
#     return translate
# 
# Path: aurweb/schema.py
# def compile_tinyint_sqlite(type_, compiler, **kw):
# def compile_bigint_sqlite(type_, compiler, **kw):


import time
import uuid
import fastapi
import aurweb.config
import aurweb.db
from urllib.parse import urlencode
from authlib.integrations.starlette_client import OAuth, OAuthError
from fastapi import Depends, HTTPException
from fastapi.responses import RedirectResponse
from sqlalchemy.sql import select
from starlette.requests import Request
from aurweb.l10n import get_translator_for_request
from aurweb.schema import Bans, Sessions, Users

    We specify prompt=login to force the user to input their credentials even
    if they’re already logged on the SSO. This is less practical, but given AUR
    has the potential to impact many users, better safe than sorry.

    The `redirect` argument is a query parameter specifying the post-login
    redirect URL.
    """
    authenticate_url = aurweb.config.get("options", "aur_location") + "/sso/authenticate"
    if redirect:
        authenticate_url = authenticate_url + "?" + urlencode([("redirect", redirect)])
    return await oauth.sso.authorize_redirect(request, authenticate_url, prompt="login")


def is_account_suspended(conn, user_id):
    row = conn.execute(select([Users.c.Suspended]).where(Users.c.ID == user_id)).fetchone()
    return row is not None and bool(row[0])


def open_session(request, conn, user_id):
    """
    Create a new user session into the database. Return its SID.
    """
    if is_account_suspended(conn, user_id):
        _ = get_translator_for_request(request)
        raise HTTPException(status_code=403, detail=_('Account suspended'))
        # TODO This is a terrible message because it could imply the attempt at
        #      logging in just caused the suspension.

    sid = uuid.uuid4().hex

# Path: aurweb/l10n.py
# def get_translator_for_request(request):
#     """
#     Determine the preferred language from a FastAPI request object and build a
#     translator function for it.
# 
#     Example:
#     ```python
#     _ = get_translator_for_request(request)
#     print(_("Hello"))
#     ```
#     """
#     lang = request.cookies.get("AURLANG")
#     if lang is None:
#         lang = aurweb.config.get("options", "default_lang")
#     translator = Translator()
# 
#     def translate(message):
#         return translator.translate(message, lang)
# 
#     return translate
# 
# Path: aurweb/schema.py
# def compile_tinyint_sqlite(type_, compiler, **kw):
# def compile_bigint_sqlite(type_, compiler, **kw):


# Path: aurweb/routers/sso.py
import time
import uuid
import fastapi
import aurweb.config
import aurweb.db
from urllib.parse import urlencode
from authlib.integrations.starlette_client import OAuth, OAuthError
from fastapi import Depends, HTTPException
from fastapi.responses import RedirectResponse
from sqlalchemy.sql import select
from starlette.requests import Request
from aurweb.l10n import get_translator_for_request
from aurweb.schema import Bans, Sessions, Users


    We specify prompt=login to force the user to input their credentials even
    if they’re already logged on the SSO. This is less practical, but given AUR
    has the potential to impact many users, better safe than sorry.

    The `redirect` argument is a query parameter specifying the post-login
    redirect URL.
    """
    authenticate_url = aurweb.config.get("options", "aur_location") + "/sso/authenticate"
    if redirect:
        authenticate_url = authenticate_url + "?" + urlencode([("redirect", redirect)])
    return await oauth.sso.authorize_redirect(request, authenticate_url, prompt="login")


def is_account_suspended(conn, user_id):
    row = conn.execute(select([Users.c.Suspended]).where(Users.c.ID == user_id)).fetchone()
    return row is not None and bool(row[0])


def open_session(request, conn, user_id):
    """
    Create a new user session into the database. Return its SID.
    """
    if is_account_suspended(conn, user_id):
        _ = get_translator_for_request(request)
        raise HTTPException(status_code=403, detail=_('Account suspended'))
        # TODO This is a terrible message because it could imply the attempt at
        #      logging in just caused the suspension.

    sid = uuid.uuid4().hex

    conn.execute(Sessions.insert().values(