authentik.enterprise.endpoints.connectors.agent.views.apple_register
1from django.db import transaction 2from django.urls import reverse 3from drf_spectacular.utils import extend_schema 4from rest_framework.exceptions import ValidationError 5from rest_framework.fields import BooleanField, CharField 6from rest_framework.permissions import IsAuthenticated 7from rest_framework.request import Request 8from rest_framework.response import Response 9from rest_framework.views import APIView 10from structlog.stdlib import get_logger 11 12from authentik.api.validation import validate 13from authentik.core.api.users import UserSelfSerializer 14from authentik.core.api.utils import PassiveSerializer 15from authentik.endpoints.connectors.agent.auth import AgentAuth 16from authentik.endpoints.connectors.agent.models import ( 17 AgentDeviceConnection, 18 AgentDeviceUserBinding, 19 AppleUnlockKey, 20 DeviceAuthenticationToken, 21 DeviceToken, 22) 23from authentik.enterprise.api import EnterpriseRequiredMixin 24from authentik.events.models import Event, EventAction 25from authentik.events.utils import model_to_dict 26 27LOGGER = get_logger() 28 29 30class AgentPSSODeviceStateUser(PassiveSerializer): 31 """A user currently registered for Platform SSO on this device""" 32 33 username = CharField() 34 enclave_key_id = CharField() 35 36 37class RegisterDeviceView(APIView): 38 39 class AgentPSSODeviceRegistration(EnterpriseRequiredMixin, PassiveSerializer): 40 """Register Apple device via Platform SSO""" 41 42 device_signing_key = CharField() 43 device_encryption_key = CharField() 44 sign_key_id = CharField() 45 enc_key_id = CharField() 46 47 class AgentPSSODeviceRegistrationResponse(PassiveSerializer): 48 """authentik settings for Platform SSO tokens""" 49 50 client_id = CharField() 51 issuer = CharField() 52 token_endpoint = CharField() 53 jwks_endpoint = CharField() 54 audience = CharField() 55 nonce_endpoint = CharField() 56 authorization_endpoint = CharField() 57 58 class AgentPSSODeviceState(PassiveSerializer): 59 """What authentik currently has stored for this device's Platform SSO 60 registration, so the client can detect drift and repair it""" 61 62 device_registered = BooleanField() 63 sign_key_id = CharField(allow_blank=True) 64 enc_key_id = CharField(allow_blank=True) 65 users = AgentPSSODeviceStateUser(many=True) 66 67 permission_classes = [IsAuthenticated] 68 pagination_class = None 69 filter_backends = [] 70 serializer_class = AgentPSSODeviceRegistration 71 authentication_classes = [AgentAuth] 72 73 @extend_schema( 74 responses={ 75 200: AgentPSSODeviceState(), 76 } 77 ) 78 def get(self, request: Request) -> Response: 79 device_token: DeviceToken = request.auth 80 conn: AgentDeviceConnection = device_token.device 81 users = AgentDeviceUserBinding.objects.filter( 82 target=conn.device, connector=conn.connector 83 ).select_related("user") 84 return Response( 85 data={ 86 "device_registered": bool(conn.apple_signing_key), 87 "sign_key_id": conn.apple_sign_key_id, 88 "enc_key_id": conn.apple_enc_key_id, 89 "users": [ 90 { 91 "username": binding.user.username, 92 "enclave_key_id": binding.apple_enclave_key_id, 93 } 94 for binding in users 95 ], 96 } 97 ) 98 99 @extend_schema(responses={204: None}) 100 @transaction.atomic() 101 def delete(self, request: Request) -> Response: 102 """Clear this device's Platform SSO registration, used when the configuration 103 profile is removed from the device. The device stays enrolled otherwise.""" 104 device_token: DeviceToken = request.auth 105 conn: AgentDeviceConnection = device_token.device 106 conn.apple_signing_key = "" 107 conn.apple_encryption_key = "" 108 conn.apple_key_exchange_key = "" 109 conn.apple_sign_key_id = "" 110 conn.apple_enc_key_id = "" 111 conn.save() 112 bindings = AgentDeviceUserBinding.objects.filter( 113 target=conn.device, connector=conn.connector 114 ) 115 # Unlock keys hang off the bindings and are useless without the enclave key 116 AppleUnlockKey.objects.filter(device_user__in=bindings).delete() 117 bindings.update(apple_secure_enclave_key="", apple_enclave_key_id="") 118 DeviceAuthenticationToken.objects.filter(device=conn.device).delete() 119 LOGGER.info("Cleared Platform SSO registration", device=conn.device.name) 120 Event.new( 121 EventAction.MODEL_UPDATED, 122 model=model_to_dict(conn), 123 message="Platform SSO registration removed", 124 ).from_http(request) 125 return Response(status=204) 126 127 @extend_schema( 128 responses={ 129 200: AgentPSSODeviceRegistrationResponse(), 130 } 131 ) 132 @validate(AgentPSSODeviceRegistration) 133 def post(self, request: Request, body: AgentPSSODeviceRegistration) -> Response: 134 device_token: DeviceToken = request.auth 135 conn: AgentDeviceConnection = device_token.device 136 conn.apple_signing_key = body.validated_data["device_signing_key"] 137 conn.apple_encryption_key = body.validated_data["device_encryption_key"] 138 conn.apple_key_exchange_key = body.validated_data["device_encryption_key"] 139 conn.apple_sign_key_id = body.validated_data["sign_key_id"] 140 conn.apple_enc_key_id = body.validated_data["enc_key_id"] 141 conn.save() 142 return Response( 143 data={ 144 "client_id": str(conn.connector.pk), 145 "issuer": self.request.build_absolute_uri( 146 reverse("authentik_enterprise_endpoints_connectors_agent:psso-token") 147 ), 148 "audience": str(conn.device.pk), 149 "token_endpoint": request.build_absolute_uri( 150 reverse("authentik_enterprise_endpoints_connectors_agent:psso-token") 151 ), 152 "jwks_endpoint": request.build_absolute_uri( 153 reverse("authentik_enterprise_endpoints_connectors_agent:psso-jwks") 154 ), 155 "nonce_endpoint": request.build_absolute_uri( 156 reverse("authentik_enterprise_endpoints_connectors_agent:psso-nonce") 157 ), 158 "authorization_endpoint": request.build_absolute_uri( 159 reverse( 160 "authentik_enterprise_endpoints_connectors_agent:psso-preauthenticate", 161 kwargs={"connector_uuid": str(conn.connector.pk)}, 162 ) 163 ), 164 } 165 ) 166 167 168class RegisterUserView(APIView): 169 170 class AgentPSSOUserRegistration(EnterpriseRequiredMixin, PassiveSerializer): 171 """Register Apple device user via Platform SSO""" 172 173 user_auth = CharField() 174 user_secure_enclave_key = CharField() 175 enclave_key_id = CharField() 176 177 permission_classes = [IsAuthenticated] 178 pagination_class = None 179 filter_backends = [] 180 serializer_class = AgentPSSOUserRegistration 181 authentication_classes = [AgentAuth] 182 183 @extend_schema( 184 responses={ 185 200: UserSelfSerializer(), 186 } 187 ) 188 @validate(AgentPSSOUserRegistration) 189 def post(self, request: Request, body: AgentPSSOUserRegistration) -> Response: 190 device_token: DeviceToken = request.auth 191 conn: AgentDeviceConnection = device_token.device 192 user_token = DeviceAuthenticationToken.objects.filter( 193 device=conn.device, 194 token=body.validated_data["user_auth"], 195 device_token=device_token, 196 ).first() 197 if not user_token: 198 raise ValidationError("Invalid user authentication") 199 # These fields must be set on create as well as update; update_or_create() returns 200 # immediately when it creates, so anything only in `defaults` is never applied. 201 enclave_keys = { 202 "apple_secure_enclave_key": body.validated_data["user_secure_enclave_key"], 203 "apple_enclave_key_id": body.validated_data["enclave_key_id"], 204 } 205 AgentDeviceUserBinding.objects.update_or_create( 206 target=conn.device, 207 user=user_token.user, 208 connector=conn.connector, 209 create_defaults={ 210 "is_primary": True, 211 "order": 0, 212 **enclave_keys, 213 }, 214 defaults=enclave_keys, 215 ) 216 return Response( 217 UserSelfSerializer(instance=user_token.user, context={"request": request}).data 218 )
LOGGER =
<BoundLoggerLazyProxy(logger=None, wrapper_class=None, processors=None, context_class=None, initial_values={}, logger_factory_args=())>
31class AgentPSSODeviceStateUser(PassiveSerializer): 32 """A user currently registered for Platform SSO on this device""" 33 34 username = CharField() 35 enclave_key_id = CharField()
A user currently registered for Platform SSO on this device
Inherited Members
class
RegisterDeviceView(rest_framework.views.APIView):
38class RegisterDeviceView(APIView): 39 40 class AgentPSSODeviceRegistration(EnterpriseRequiredMixin, PassiveSerializer): 41 """Register Apple device via Platform SSO""" 42 43 device_signing_key = CharField() 44 device_encryption_key = CharField() 45 sign_key_id = CharField() 46 enc_key_id = CharField() 47 48 class AgentPSSODeviceRegistrationResponse(PassiveSerializer): 49 """authentik settings for Platform SSO tokens""" 50 51 client_id = CharField() 52 issuer = CharField() 53 token_endpoint = CharField() 54 jwks_endpoint = CharField() 55 audience = CharField() 56 nonce_endpoint = CharField() 57 authorization_endpoint = CharField() 58 59 class AgentPSSODeviceState(PassiveSerializer): 60 """What authentik currently has stored for this device's Platform SSO 61 registration, so the client can detect drift and repair it""" 62 63 device_registered = BooleanField() 64 sign_key_id = CharField(allow_blank=True) 65 enc_key_id = CharField(allow_blank=True) 66 users = AgentPSSODeviceStateUser(many=True) 67 68 permission_classes = [IsAuthenticated] 69 pagination_class = None 70 filter_backends = [] 71 serializer_class = AgentPSSODeviceRegistration 72 authentication_classes = [AgentAuth] 73 74 @extend_schema( 75 responses={ 76 200: AgentPSSODeviceState(), 77 } 78 ) 79 def get(self, request: Request) -> Response: 80 device_token: DeviceToken = request.auth 81 conn: AgentDeviceConnection = device_token.device 82 users = AgentDeviceUserBinding.objects.filter( 83 target=conn.device, connector=conn.connector 84 ).select_related("user") 85 return Response( 86 data={ 87 "device_registered": bool(conn.apple_signing_key), 88 "sign_key_id": conn.apple_sign_key_id, 89 "enc_key_id": conn.apple_enc_key_id, 90 "users": [ 91 { 92 "username": binding.user.username, 93 "enclave_key_id": binding.apple_enclave_key_id, 94 } 95 for binding in users 96 ], 97 } 98 ) 99 100 @extend_schema(responses={204: None}) 101 @transaction.atomic() 102 def delete(self, request: Request) -> Response: 103 """Clear this device's Platform SSO registration, used when the configuration 104 profile is removed from the device. The device stays enrolled otherwise.""" 105 device_token: DeviceToken = request.auth 106 conn: AgentDeviceConnection = device_token.device 107 conn.apple_signing_key = "" 108 conn.apple_encryption_key = "" 109 conn.apple_key_exchange_key = "" 110 conn.apple_sign_key_id = "" 111 conn.apple_enc_key_id = "" 112 conn.save() 113 bindings = AgentDeviceUserBinding.objects.filter( 114 target=conn.device, connector=conn.connector 115 ) 116 # Unlock keys hang off the bindings and are useless without the enclave key 117 AppleUnlockKey.objects.filter(device_user__in=bindings).delete() 118 bindings.update(apple_secure_enclave_key="", apple_enclave_key_id="") 119 DeviceAuthenticationToken.objects.filter(device=conn.device).delete() 120 LOGGER.info("Cleared Platform SSO registration", device=conn.device.name) 121 Event.new( 122 EventAction.MODEL_UPDATED, 123 model=model_to_dict(conn), 124 message="Platform SSO registration removed", 125 ).from_http(request) 126 return Response(status=204) 127 128 @extend_schema( 129 responses={ 130 200: AgentPSSODeviceRegistrationResponse(), 131 } 132 ) 133 @validate(AgentPSSODeviceRegistration) 134 def post(self, request: Request, body: AgentPSSODeviceRegistration) -> Response: 135 device_token: DeviceToken = request.auth 136 conn: AgentDeviceConnection = device_token.device 137 conn.apple_signing_key = body.validated_data["device_signing_key"] 138 conn.apple_encryption_key = body.validated_data["device_encryption_key"] 139 conn.apple_key_exchange_key = body.validated_data["device_encryption_key"] 140 conn.apple_sign_key_id = body.validated_data["sign_key_id"] 141 conn.apple_enc_key_id = body.validated_data["enc_key_id"] 142 conn.save() 143 return Response( 144 data={ 145 "client_id": str(conn.connector.pk), 146 "issuer": self.request.build_absolute_uri( 147 reverse("authentik_enterprise_endpoints_connectors_agent:psso-token") 148 ), 149 "audience": str(conn.device.pk), 150 "token_endpoint": request.build_absolute_uri( 151 reverse("authentik_enterprise_endpoints_connectors_agent:psso-token") 152 ), 153 "jwks_endpoint": request.build_absolute_uri( 154 reverse("authentik_enterprise_endpoints_connectors_agent:psso-jwks") 155 ), 156 "nonce_endpoint": request.build_absolute_uri( 157 reverse("authentik_enterprise_endpoints_connectors_agent:psso-nonce") 158 ), 159 "authorization_endpoint": request.build_absolute_uri( 160 reverse( 161 "authentik_enterprise_endpoints_connectors_agent:psso-preauthenticate", 162 kwargs={"connector_uuid": str(conn.connector.pk)}, 163 ) 164 ), 165 } 166 )
Intentionally simple parent class for all views. Only implements dispatch-by-method and simple sanity checking.
serializer_class =
<class 'RegisterDeviceView.AgentPSSODeviceRegistration'>
authentication_classes =
[<class 'authentik.endpoints.connectors.agent.auth.AgentAuth'>]
@extend_schema(responses={200: AgentPSSODeviceState()})
def
get( self, request: rest_framework.request.Request) -> rest_framework.response.Response:
74 @extend_schema( 75 responses={ 76 200: AgentPSSODeviceState(), 77 } 78 ) 79 def get(self, request: Request) -> Response: 80 device_token: DeviceToken = request.auth 81 conn: AgentDeviceConnection = device_token.device 82 users = AgentDeviceUserBinding.objects.filter( 83 target=conn.device, connector=conn.connector 84 ).select_related("user") 85 return Response( 86 data={ 87 "device_registered": bool(conn.apple_signing_key), 88 "sign_key_id": conn.apple_sign_key_id, 89 "enc_key_id": conn.apple_enc_key_id, 90 "users": [ 91 { 92 "username": binding.user.username, 93 "enclave_key_id": binding.apple_enclave_key_id, 94 } 95 for binding in users 96 ], 97 } 98 )
@extend_schema(responses={204: None})
@transaction.atomic()
def
delete( self, request: rest_framework.request.Request) -> rest_framework.response.Response:
100 @extend_schema(responses={204: None}) 101 @transaction.atomic() 102 def delete(self, request: Request) -> Response: 103 """Clear this device's Platform SSO registration, used when the configuration 104 profile is removed from the device. The device stays enrolled otherwise.""" 105 device_token: DeviceToken = request.auth 106 conn: AgentDeviceConnection = device_token.device 107 conn.apple_signing_key = "" 108 conn.apple_encryption_key = "" 109 conn.apple_key_exchange_key = "" 110 conn.apple_sign_key_id = "" 111 conn.apple_enc_key_id = "" 112 conn.save() 113 bindings = AgentDeviceUserBinding.objects.filter( 114 target=conn.device, connector=conn.connector 115 ) 116 # Unlock keys hang off the bindings and are useless without the enclave key 117 AppleUnlockKey.objects.filter(device_user__in=bindings).delete() 118 bindings.update(apple_secure_enclave_key="", apple_enclave_key_id="") 119 DeviceAuthenticationToken.objects.filter(device=conn.device).delete() 120 LOGGER.info("Cleared Platform SSO registration", device=conn.device.name) 121 Event.new( 122 EventAction.MODEL_UPDATED, 123 model=model_to_dict(conn), 124 message="Platform SSO registration removed", 125 ).from_http(request) 126 return Response(status=204)
Clear this device's Platform SSO registration, used when the configuration profile is removed from the device. The device stays enrolled otherwise.
@extend_schema(responses={200: AgentPSSODeviceRegistrationResponse()})
@validate(AgentPSSODeviceRegistration)
def
post( self, request: rest_framework.request.Request, body: RegisterDeviceView.AgentPSSODeviceRegistration) -> rest_framework.response.Response:
128 @extend_schema( 129 responses={ 130 200: AgentPSSODeviceRegistrationResponse(), 131 } 132 ) 133 @validate(AgentPSSODeviceRegistration) 134 def post(self, request: Request, body: AgentPSSODeviceRegistration) -> Response: 135 device_token: DeviceToken = request.auth 136 conn: AgentDeviceConnection = device_token.device 137 conn.apple_signing_key = body.validated_data["device_signing_key"] 138 conn.apple_encryption_key = body.validated_data["device_encryption_key"] 139 conn.apple_key_exchange_key = body.validated_data["device_encryption_key"] 140 conn.apple_sign_key_id = body.validated_data["sign_key_id"] 141 conn.apple_enc_key_id = body.validated_data["enc_key_id"] 142 conn.save() 143 return Response( 144 data={ 145 "client_id": str(conn.connector.pk), 146 "issuer": self.request.build_absolute_uri( 147 reverse("authentik_enterprise_endpoints_connectors_agent:psso-token") 148 ), 149 "audience": str(conn.device.pk), 150 "token_endpoint": request.build_absolute_uri( 151 reverse("authentik_enterprise_endpoints_connectors_agent:psso-token") 152 ), 153 "jwks_endpoint": request.build_absolute_uri( 154 reverse("authentik_enterprise_endpoints_connectors_agent:psso-jwks") 155 ), 156 "nonce_endpoint": request.build_absolute_uri( 157 reverse("authentik_enterprise_endpoints_connectors_agent:psso-nonce") 158 ), 159 "authorization_endpoint": request.build_absolute_uri( 160 reverse( 161 "authentik_enterprise_endpoints_connectors_agent:psso-preauthenticate", 162 kwargs={"connector_uuid": str(conn.connector.pk)}, 163 ) 164 ), 165 } 166 )
class
RegisterDeviceView.AgentPSSODeviceRegistration(authentik.enterprise.api.EnterpriseRequiredMixin, authentik.core.api.utils.PassiveSerializer):
40 class AgentPSSODeviceRegistration(EnterpriseRequiredMixin, PassiveSerializer): 41 """Register Apple device via Platform SSO""" 42 43 device_signing_key = CharField() 44 device_encryption_key = CharField() 45 sign_key_id = CharField() 46 enc_key_id = CharField()
Register Apple device via Platform SSO
class
RegisterDeviceView.AgentPSSODeviceRegistrationResponse(authentik.core.api.utils.PassiveSerializer):
48 class AgentPSSODeviceRegistrationResponse(PassiveSerializer): 49 """authentik settings for Platform SSO tokens""" 50 51 client_id = CharField() 52 issuer = CharField() 53 token_endpoint = CharField() 54 jwks_endpoint = CharField() 55 audience = CharField() 56 nonce_endpoint = CharField() 57 authorization_endpoint = CharField()
authentik settings for Platform SSO tokens
Inherited Members
59 class AgentPSSODeviceState(PassiveSerializer): 60 """What authentik currently has stored for this device's Platform SSO 61 registration, so the client can detect drift and repair it""" 62 63 device_registered = BooleanField() 64 sign_key_id = CharField(allow_blank=True) 65 enc_key_id = CharField(allow_blank=True) 66 users = AgentPSSODeviceStateUser(many=True)
What authentik currently has stored for this device's Platform SSO registration, so the client can detect drift and repair it
Inherited Members
class
RegisterUserView(rest_framework.views.APIView):
169class RegisterUserView(APIView): 170 171 class AgentPSSOUserRegistration(EnterpriseRequiredMixin, PassiveSerializer): 172 """Register Apple device user via Platform SSO""" 173 174 user_auth = CharField() 175 user_secure_enclave_key = CharField() 176 enclave_key_id = CharField() 177 178 permission_classes = [IsAuthenticated] 179 pagination_class = None 180 filter_backends = [] 181 serializer_class = AgentPSSOUserRegistration 182 authentication_classes = [AgentAuth] 183 184 @extend_schema( 185 responses={ 186 200: UserSelfSerializer(), 187 } 188 ) 189 @validate(AgentPSSOUserRegistration) 190 def post(self, request: Request, body: AgentPSSOUserRegistration) -> Response: 191 device_token: DeviceToken = request.auth 192 conn: AgentDeviceConnection = device_token.device 193 user_token = DeviceAuthenticationToken.objects.filter( 194 device=conn.device, 195 token=body.validated_data["user_auth"], 196 device_token=device_token, 197 ).first() 198 if not user_token: 199 raise ValidationError("Invalid user authentication") 200 # These fields must be set on create as well as update; update_or_create() returns 201 # immediately when it creates, so anything only in `defaults` is never applied. 202 enclave_keys = { 203 "apple_secure_enclave_key": body.validated_data["user_secure_enclave_key"], 204 "apple_enclave_key_id": body.validated_data["enclave_key_id"], 205 } 206 AgentDeviceUserBinding.objects.update_or_create( 207 target=conn.device, 208 user=user_token.user, 209 connector=conn.connector, 210 create_defaults={ 211 "is_primary": True, 212 "order": 0, 213 **enclave_keys, 214 }, 215 defaults=enclave_keys, 216 ) 217 return Response( 218 UserSelfSerializer(instance=user_token.user, context={"request": request}).data 219 )
Intentionally simple parent class for all views. Only implements dispatch-by-method and simple sanity checking.
serializer_class =
<class 'RegisterUserView.AgentPSSOUserRegistration'>
authentication_classes =
[<class 'authentik.endpoints.connectors.agent.auth.AgentAuth'>]
@extend_schema(responses={200: UserSelfSerializer()})
@validate(AgentPSSOUserRegistration)
def
post( self, request: rest_framework.request.Request, body: RegisterUserView.AgentPSSOUserRegistration) -> rest_framework.response.Response:
184 @extend_schema( 185 responses={ 186 200: UserSelfSerializer(), 187 } 188 ) 189 @validate(AgentPSSOUserRegistration) 190 def post(self, request: Request, body: AgentPSSOUserRegistration) -> Response: 191 device_token: DeviceToken = request.auth 192 conn: AgentDeviceConnection = device_token.device 193 user_token = DeviceAuthenticationToken.objects.filter( 194 device=conn.device, 195 token=body.validated_data["user_auth"], 196 device_token=device_token, 197 ).first() 198 if not user_token: 199 raise ValidationError("Invalid user authentication") 200 # These fields must be set on create as well as update; update_or_create() returns 201 # immediately when it creates, so anything only in `defaults` is never applied. 202 enclave_keys = { 203 "apple_secure_enclave_key": body.validated_data["user_secure_enclave_key"], 204 "apple_enclave_key_id": body.validated_data["enclave_key_id"], 205 } 206 AgentDeviceUserBinding.objects.update_or_create( 207 target=conn.device, 208 user=user_token.user, 209 connector=conn.connector, 210 create_defaults={ 211 "is_primary": True, 212 "order": 0, 213 **enclave_keys, 214 }, 215 defaults=enclave_keys, 216 ) 217 return Response( 218 UserSelfSerializer(instance=user_token.user, context={"request": request}).data 219 )
class
RegisterUserView.AgentPSSOUserRegistration(authentik.enterprise.api.EnterpriseRequiredMixin, authentik.core.api.utils.PassiveSerializer):
171 class AgentPSSOUserRegistration(EnterpriseRequiredMixin, PassiveSerializer): 172 """Register Apple device user via Platform SSO""" 173 174 user_auth = CharField() 175 user_secure_enclave_key = CharField() 176 enclave_key_id = CharField()
Register Apple device user via Platform SSO