Coverage for app/venv/lib/python3.14/site-packages/weblate/api/authentication.py: 100%
29 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 07:15 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 07:15 +0000
1# Copyright © Michal Čihař <michal@weblate.org>
2# SPDX-FileCopyrightText: 2025 Javier Pérez <jdbp@protonmail.com>
3#
4# SPDX-License-Identifier: GPL-3.0-or-later
6from __future__ import annotations
8from django.utils.translation import gettext
9from drf_spectacular.authentication import SessionScheme
10from drf_spectacular.extensions import OpenApiAuthenticationExtension
11from rest_framework.authentication import TokenAuthentication
14class BearerAuthentication(TokenAuthentication):
15 """RFC 6750 compatible Bearer authentication."""
17 keyword = "Bearer"
20class WeblateSessionScheme(SessionScheme):
21 target_class = "rest_framework.authentication.SessionAuthentication"
22 priority = 1
24 def get_security_definition(self, auto_schema):
25 result = super().get_security_definition(auto_schema)
26 result["description"] = gettext(
27 "Session-based authentication used when user is signed in."
28 )
29 return result
32class BearerScheme(OpenApiAuthenticationExtension):
33 target_class = "weblate.api.authentication.BearerAuthentication"
34 name = "bearerAuth"
35 priority = 1
37 def get_description(self, keyword: str) -> str:
38 intro = (
39 gettext("Token-based authentication with required prefix `%s`.") % keyword
40 )
41 user_token = gettext(
42 "Each user has a personal access token which they can get from their respective user profile. These tokens have the `wlu_` prefix."
43 )
44 project_token = gettext(
45 "It is possible to create project tokens whose access to the API is limited to operations to their associated project. These tokens have the `wlp_` prefix."
46 )
47 return f"""{intro}
49- {user_token}
50- {project_token}
51 """
53 def get_security_definition(self, auto_schema):
54 keyword = self.target.keyword
55 return {
56 "type": "apiKey",
57 "in": "header",
58 "name": keyword,
59 "description": self.get_description(keyword),
60 }
63class TokenScheme(BearerScheme):
64 target_class = "rest_framework.authentication.TokenAuthentication"
65 name = "tokenAuth"