authentik.enterprise.providers.scim.api
1from datetime import datetime 2 3from django.urls import reverse 4from django.utils.translation import gettext as _ 5from rest_framework.exceptions import ValidationError 6 7from authentik.enterprise.license import LicenseKey 8from authentik.providers.scim.models import SCIMAuthenticationMode, SCIMProvider 9from authentik.sources.oauth.models import UserOAuthSourceConnection 10 11 12class SCIMProviderSerializerMixin: 13 14 def _get_token(self, instance: SCIMProvider) -> UserOAuthSourceConnection | None: 15 user = instance.auth_oauth_user 16 conn = UserOAuthSourceConnection.objects.filter( 17 user=user, source=instance.auth_oauth 18 ).first() 19 return conn 20 21 def get_auth_oauth_token_last_updated(self, instance: SCIMProvider) -> datetime | None: 22 conn = self._get_token(instance) 23 return conn.last_updated if conn else None 24 25 def get_auth_oauth_token_expires(self, instance: SCIMProvider) -> datetime | None: 26 conn = self._get_token(instance) 27 return conn.expires if conn else None 28 29 def get_auth_oauth_url_callback(self, instance: SCIMProvider) -> str | None: 30 if ( 31 instance.auth_mode != SCIMAuthenticationMode.OAUTH_INTERACTIVE 32 or not instance.backchannel_application 33 ): 34 return None 35 relative_url = reverse( 36 "authentik_enterprise_providers_scim:callback", 37 kwargs={"application_slug": instance.backchannel_application.slug}, 38 ) 39 if "request" not in self.context: 40 return relative_url 41 return self.context["request"].build_absolute_uri(relative_url) 42 43 def get_auth_oauth_url_start(self, instance: SCIMProvider) -> str | None: 44 if ( 45 instance.auth_mode != SCIMAuthenticationMode.OAUTH_INTERACTIVE 46 or not instance.backchannel_application 47 ): 48 return None 49 relative_url = reverse( 50 "authentik_enterprise_providers_scim:start", 51 kwargs={"application_slug": instance.backchannel_application.slug}, 52 ) 53 if "request" not in self.context: 54 return relative_url 55 return self.context["request"].build_absolute_uri(relative_url) 56 57 def validate_auth_mode(self, auth_mode: SCIMAuthenticationMode) -> SCIMAuthenticationMode: 58 if auth_mode in [ 59 SCIMAuthenticationMode.OAUTH_SILENT, 60 SCIMAuthenticationMode.OAUTH_INTERACTIVE, 61 ]: 62 if not LicenseKey.cached_summary().status.is_valid: 63 raise ValidationError(_("Enterprise is required to use the OAuth mode.")) 64 return auth_mode
class
SCIMProviderSerializerMixin:
13class SCIMProviderSerializerMixin: 14 15 def _get_token(self, instance: SCIMProvider) -> UserOAuthSourceConnection | None: 16 user = instance.auth_oauth_user 17 conn = UserOAuthSourceConnection.objects.filter( 18 user=user, source=instance.auth_oauth 19 ).first() 20 return conn 21 22 def get_auth_oauth_token_last_updated(self, instance: SCIMProvider) -> datetime | None: 23 conn = self._get_token(instance) 24 return conn.last_updated if conn else None 25 26 def get_auth_oauth_token_expires(self, instance: SCIMProvider) -> datetime | None: 27 conn = self._get_token(instance) 28 return conn.expires if conn else None 29 30 def get_auth_oauth_url_callback(self, instance: SCIMProvider) -> str | None: 31 if ( 32 instance.auth_mode != SCIMAuthenticationMode.OAUTH_INTERACTIVE 33 or not instance.backchannel_application 34 ): 35 return None 36 relative_url = reverse( 37 "authentik_enterprise_providers_scim:callback", 38 kwargs={"application_slug": instance.backchannel_application.slug}, 39 ) 40 if "request" not in self.context: 41 return relative_url 42 return self.context["request"].build_absolute_uri(relative_url) 43 44 def get_auth_oauth_url_start(self, instance: SCIMProvider) -> str | None: 45 if ( 46 instance.auth_mode != SCIMAuthenticationMode.OAUTH_INTERACTIVE 47 or not instance.backchannel_application 48 ): 49 return None 50 relative_url = reverse( 51 "authentik_enterprise_providers_scim:start", 52 kwargs={"application_slug": instance.backchannel_application.slug}, 53 ) 54 if "request" not in self.context: 55 return relative_url 56 return self.context["request"].build_absolute_uri(relative_url) 57 58 def validate_auth_mode(self, auth_mode: SCIMAuthenticationMode) -> SCIMAuthenticationMode: 59 if auth_mode in [ 60 SCIMAuthenticationMode.OAUTH_SILENT, 61 SCIMAuthenticationMode.OAUTH_INTERACTIVE, 62 ]: 63 if not LicenseKey.cached_summary().status.is_valid: 64 raise ValidationError(_("Enterprise is required to use the OAuth mode.")) 65 return auth_mode
def
get_auth_oauth_token_last_updated( self, instance: authentik.providers.scim.models.SCIMProvider) -> datetime.datetime | None:
def
get_auth_oauth_token_expires( self, instance: authentik.providers.scim.models.SCIMProvider) -> datetime.datetime | None:
def
get_auth_oauth_url_callback( self, instance: authentik.providers.scim.models.SCIMProvider) -> str | None:
30 def get_auth_oauth_url_callback(self, instance: SCIMProvider) -> str | None: 31 if ( 32 instance.auth_mode != SCIMAuthenticationMode.OAUTH_INTERACTIVE 33 or not instance.backchannel_application 34 ): 35 return None 36 relative_url = reverse( 37 "authentik_enterprise_providers_scim:callback", 38 kwargs={"application_slug": instance.backchannel_application.slug}, 39 ) 40 if "request" not in self.context: 41 return relative_url 42 return self.context["request"].build_absolute_uri(relative_url)
def
get_auth_oauth_url_start( self, instance: authentik.providers.scim.models.SCIMProvider) -> str | None:
44 def get_auth_oauth_url_start(self, instance: SCIMProvider) -> str | None: 45 if ( 46 instance.auth_mode != SCIMAuthenticationMode.OAUTH_INTERACTIVE 47 or not instance.backchannel_application 48 ): 49 return None 50 relative_url = reverse( 51 "authentik_enterprise_providers_scim:start", 52 kwargs={"application_slug": instance.backchannel_application.slug}, 53 ) 54 if "request" not in self.context: 55 return relative_url 56 return self.context["request"].build_absolute_uri(relative_url)
def
validate_auth_mode( self, auth_mode: authentik.providers.scim.models.SCIMAuthenticationMode) -> authentik.providers.scim.models.SCIMAuthenticationMode:
58 def validate_auth_mode(self, auth_mode: SCIMAuthenticationMode) -> SCIMAuthenticationMode: 59 if auth_mode in [ 60 SCIMAuthenticationMode.OAUTH_SILENT, 61 SCIMAuthenticationMode.OAUTH_INTERACTIVE, 62 ]: 63 if not LicenseKey.cached_summary().status.is_valid: 64 raise ValidationError(_("Enterprise is required to use the OAuth mode.")) 65 return auth_mode