authentik.enterprise.providers.ssf.views.auth

SSF Token auth

 1"""SSF Token auth"""
 2
 3from typing import TYPE_CHECKING, Any
 4
 5from django.db.models import Q
 6from rest_framework.authentication import BaseAuthentication, get_authorization_header
 7from rest_framework.request import Request
 8
 9from authentik.core.models import Token, TokenIntents, User
10from authentik.enterprise.providers.ssf.models import SSFProvider
11from authentik.lib.tracing import active_tracer
12from authentik.providers.oauth2.models import AccessToken
13
14if TYPE_CHECKING:
15    from authentik.enterprise.providers.ssf.views.base import SSFView
16
17
18class SSFTokenAuth(BaseAuthentication):
19    """SSF Token auth"""
20
21    view: SSFView
22
23    def __init__(self, view: SSFView) -> None:
24        super().__init__()
25        self.view = view
26
27    def check_token(self, key: str) -> Token | None:
28        """Check that a token exists, is not expired, and is assigned to the correct provider"""
29        token = Token.objects.filter(key=key, intent=TokenIntents.INTENT_API).first()
30        if not token:
31            return None
32        provider: SSFProvider = token.ssfprovider_set.first()
33        if not provider:
34            return None
35        self.view.application = provider.backchannel_application
36        self.view.provider = provider
37        return token
38
39    def check_jwt(self, jwt: str) -> AccessToken | None:
40        """Check JWT-based authentication, this supports tokens issued either by providers
41        configured directly in the provider, and by providers assigned to the application
42        that the SSF provider is a backchannel provider of."""
43        token = AccessToken.objects.filter(token=jwt, revoked=False).first()
44        if not token:
45            return None
46        ssf_provider = SSFProvider.objects.filter(
47            Q(oidc_auth_providers__in=[token.provider])
48            | Q(backchannel_application__provider__in=[token.provider]),
49        ).first()
50        if not ssf_provider:
51            return None
52        self.view.application = ssf_provider.backchannel_application
53        self.view.provider = ssf_provider
54        return token
55
56    @active_tracer().instrument()
57    def authenticate(self, request: Request) -> tuple[User, Any] | None:
58        auth = get_authorization_header(request).decode()
59        auth_type, _, key = auth.partition(" ")
60        if auth_type != "Bearer":
61            return None
62        token = self.check_token(key)
63        if token:
64            return (token.user, token)
65        jwt_token = self.check_jwt(key)
66        if jwt_token:
67            return (jwt_token.user, token)
68        return None
69
70    # Required to correctly propagate a 401 header which the SSF spec requires
71    def authenticate_header(self, request):
72        return "SSF"
class SSFTokenAuth(rest_framework.authentication.BaseAuthentication):
19class SSFTokenAuth(BaseAuthentication):
20    """SSF Token auth"""
21
22    view: SSFView
23
24    def __init__(self, view: SSFView) -> None:
25        super().__init__()
26        self.view = view
27
28    def check_token(self, key: str) -> Token | None:
29        """Check that a token exists, is not expired, and is assigned to the correct provider"""
30        token = Token.objects.filter(key=key, intent=TokenIntents.INTENT_API).first()
31        if not token:
32            return None
33        provider: SSFProvider = token.ssfprovider_set.first()
34        if not provider:
35            return None
36        self.view.application = provider.backchannel_application
37        self.view.provider = provider
38        return token
39
40    def check_jwt(self, jwt: str) -> AccessToken | None:
41        """Check JWT-based authentication, this supports tokens issued either by providers
42        configured directly in the provider, and by providers assigned to the application
43        that the SSF provider is a backchannel provider of."""
44        token = AccessToken.objects.filter(token=jwt, revoked=False).first()
45        if not token:
46            return None
47        ssf_provider = SSFProvider.objects.filter(
48            Q(oidc_auth_providers__in=[token.provider])
49            | Q(backchannel_application__provider__in=[token.provider]),
50        ).first()
51        if not ssf_provider:
52            return None
53        self.view.application = ssf_provider.backchannel_application
54        self.view.provider = ssf_provider
55        return token
56
57    @active_tracer().instrument()
58    def authenticate(self, request: Request) -> tuple[User, Any] | None:
59        auth = get_authorization_header(request).decode()
60        auth_type, _, key = auth.partition(" ")
61        if auth_type != "Bearer":
62            return None
63        token = self.check_token(key)
64        if token:
65            return (token.user, token)
66        jwt_token = self.check_jwt(key)
67        if jwt_token:
68            return (jwt_token.user, token)
69        return None
70
71    # Required to correctly propagate a 401 header which the SSF spec requires
72    def authenticate_header(self, request):
73        return "SSF"

SSF Token auth

24    def __init__(self, view: SSFView) -> None:
25        super().__init__()
26        self.view = view
def check_token(self, key: str) -> authentik.core.models.Token | None:
28    def check_token(self, key: str) -> Token | None:
29        """Check that a token exists, is not expired, and is assigned to the correct provider"""
30        token = Token.objects.filter(key=key, intent=TokenIntents.INTENT_API).first()
31        if not token:
32            return None
33        provider: SSFProvider = token.ssfprovider_set.first()
34        if not provider:
35            return None
36        self.view.application = provider.backchannel_application
37        self.view.provider = provider
38        return token

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

def check_jwt(self, jwt: str) -> authentik.providers.oauth2.models.AccessToken | None:
40    def check_jwt(self, jwt: str) -> AccessToken | None:
41        """Check JWT-based authentication, this supports tokens issued either by providers
42        configured directly in the provider, and by providers assigned to the application
43        that the SSF provider is a backchannel provider of."""
44        token = AccessToken.objects.filter(token=jwt, revoked=False).first()
45        if not token:
46            return None
47        ssf_provider = SSFProvider.objects.filter(
48            Q(oidc_auth_providers__in=[token.provider])
49            | Q(backchannel_application__provider__in=[token.provider]),
50        ).first()
51        if not ssf_provider:
52            return None
53        self.view.application = ssf_provider.backchannel_application
54        self.view.provider = ssf_provider
55        return token

Check JWT-based authentication, this supports tokens issued either by providers configured directly in the provider, and by providers assigned to the application that the SSF provider is a backchannel provider of.

@active_tracer().instrument()
def authenticate( self, request: rest_framework.request.Request) -> tuple[authentik.core.models.User, Any] | None:
57    @active_tracer().instrument()
58    def authenticate(self, request: Request) -> tuple[User, Any] | None:
59        auth = get_authorization_header(request).decode()
60        auth_type, _, key = auth.partition(" ")
61        if auth_type != "Bearer":
62            return None
63        token = self.check_token(key)
64        if token:
65            return (token.user, token)
66        jwt_token = self.check_jwt(key)
67        if jwt_token:
68            return (jwt_token.user, token)
69        return None

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

def authenticate_header(self, request):
72    def authenticate_header(self, request):
73        return "SSF"

Return a string to be used as the value of the WWW-Authenticate header in a 401 Unauthenticated response, or None if the authentication scheme should return 403 Permission Denied responses.