authentik.sources.scim.views.v2.auth

SCIM Token auth

 1"""SCIM Token auth"""
 2
 3from base64 import b64decode
 4from typing import Any
 5
 6from django.conf import settings
 7from rest_framework.authentication import BaseAuthentication, get_authorization_header
 8from rest_framework.request import Request
 9from rest_framework.views import APIView
10
11from authentik.core.middleware import CTX_AUTH_VIA
12from authentik.core.models import Token, TokenIntents, User
13from authentik.lib.tracing import active_tracer
14from authentik.sources.scim.models import SCIMSource
15
16
17class SCIMTokenAuth(BaseAuthentication):
18    """SCIM Token auth"""
19
20    def __init__(self, view: APIView) -> None:
21        super().__init__()
22        self.view = view
23
24    def legacy(self, key: str, source_slug: str) -> Token | None:  # pragma: no cover
25        """Legacy HTTP-Basic auth for testing"""
26        if not settings.TEST and not settings.DEBUG:
27            return None
28        _username, _, password = b64decode(key.encode()).decode().partition(":")
29        token = self.check_token(password, source_slug)
30        if token:
31            CTX_AUTH_VIA.set("scim_basic")
32            return (token.user, token)
33        return None
34
35    def check_token(self, key: str, source_slug: str) -> Token | None:
36        """Check that a token exists, is not expired, and is assigned to the correct source"""
37        token = Token.objects.filter(key=key, intent=TokenIntents.INTENT_API).first()
38        if not token:
39            return None
40        source: SCIMSource = token.scimsource_set.first()
41        if not source:
42            return None
43        if source.slug != source_slug:
44            return None
45        self.view.source = source
46        return token
47
48    @active_tracer().instrument()
49    def authenticate(self, request: Request) -> tuple[User, Any] | None:
50        kwargs = request._request.resolver_match.kwargs
51        source_slug = kwargs.get("source_slug", None)
52        auth = get_authorization_header(request).decode()
53        auth_type, _, key = auth.partition(" ")
54        if auth_type != "Bearer":
55            return self.legacy(key, source_slug)
56        token = self.check_token(key, source_slug)
57        if not token:
58            return None
59        CTX_AUTH_VIA.set("scim_token")
60        return (token.user, token)
class SCIMTokenAuth(rest_framework.authentication.BaseAuthentication):
18class SCIMTokenAuth(BaseAuthentication):
19    """SCIM Token auth"""
20
21    def __init__(self, view: APIView) -> None:
22        super().__init__()
23        self.view = view
24
25    def legacy(self, key: str, source_slug: str) -> Token | None:  # pragma: no cover
26        """Legacy HTTP-Basic auth for testing"""
27        if not settings.TEST and not settings.DEBUG:
28            return None
29        _username, _, password = b64decode(key.encode()).decode().partition(":")
30        token = self.check_token(password, source_slug)
31        if token:
32            CTX_AUTH_VIA.set("scim_basic")
33            return (token.user, token)
34        return None
35
36    def check_token(self, key: str, source_slug: str) -> Token | None:
37        """Check that a token exists, is not expired, and is assigned to the correct source"""
38        token = Token.objects.filter(key=key, intent=TokenIntents.INTENT_API).first()
39        if not token:
40            return None
41        source: SCIMSource = token.scimsource_set.first()
42        if not source:
43            return None
44        if source.slug != source_slug:
45            return None
46        self.view.source = source
47        return token
48
49    @active_tracer().instrument()
50    def authenticate(self, request: Request) -> tuple[User, Any] | None:
51        kwargs = request._request.resolver_match.kwargs
52        source_slug = kwargs.get("source_slug", None)
53        auth = get_authorization_header(request).decode()
54        auth_type, _, key = auth.partition(" ")
55        if auth_type != "Bearer":
56            return self.legacy(key, source_slug)
57        token = self.check_token(key, source_slug)
58        if not token:
59            return None
60        CTX_AUTH_VIA.set("scim_token")
61        return (token.user, token)

SCIM Token auth

SCIMTokenAuth(view: rest_framework.views.APIView)
21    def __init__(self, view: APIView) -> None:
22        super().__init__()
23        self.view = view
view
def legacy(self, key: str, source_slug: str) -> authentik.core.models.Token | None:
25    def legacy(self, key: str, source_slug: str) -> Token | None:  # pragma: no cover
26        """Legacy HTTP-Basic auth for testing"""
27        if not settings.TEST and not settings.DEBUG:
28            return None
29        _username, _, password = b64decode(key.encode()).decode().partition(":")
30        token = self.check_token(password, source_slug)
31        if token:
32            CTX_AUTH_VIA.set("scim_basic")
33            return (token.user, token)
34        return None

Legacy HTTP-Basic auth for testing

def check_token(self, key: str, source_slug: str) -> authentik.core.models.Token | None:
36    def check_token(self, key: str, source_slug: str) -> Token | None:
37        """Check that a token exists, is not expired, and is assigned to the correct source"""
38        token = Token.objects.filter(key=key, intent=TokenIntents.INTENT_API).first()
39        if not token:
40            return None
41        source: SCIMSource = token.scimsource_set.first()
42        if not source:
43            return None
44        if source.slug != source_slug:
45            return None
46        self.view.source = source
47        return token

Check that a token exists, is not expired, and is assigned to the correct source

@active_tracer().instrument()
def authenticate( self, request: rest_framework.request.Request) -> tuple[authentik.core.models.User, Any] | None:
49    @active_tracer().instrument()
50    def authenticate(self, request: Request) -> tuple[User, Any] | None:
51        kwargs = request._request.resolver_match.kwargs
52        source_slug = kwargs.get("source_slug", None)
53        auth = get_authorization_header(request).decode()
54        auth_type, _, key = auth.partition(" ")
55        if auth_type != "Bearer":
56            return self.legacy(key, source_slug)
57        token = self.check_token(key, source_slug)
58        if not token:
59            return None
60        CTX_AUTH_VIA.set("scim_token")
61        return (token.user, token)

Authenticate the request and return a two-tuple of (user, token).