11"""authentik Kerberos Source Models"""
22
33import os
4+ from base64 import b64decode
45from pathlib import Path
56from tempfile import gettempdir
67from typing import Any
78
89import gssapi
910import pglock
1011from django .db import connection , models
11- from django .db .models .fields import b64decode
1212from django .http import HttpRequest
1313from django .shortcuts import reverse
1414from django .templatetags .static import static
1515from django .utils .timezone import now
1616from django .utils .translation import gettext_lazy as _
17- from kadmin import KAdmin , KAdminApiVersion
18- from kadmin . exceptions import PyKAdminException
17+ from kadmin import KAdm5Variant , KAdmin , KAdminApiVersion
18+ from kadmin import exceptions as kadmin_exceptions
1919from rest_framework .serializers import Serializer
2020from structlog .stdlib import get_logger
2121
4242class KAdminType (models .TextChoices ):
4343 MIT = "MIT"
4444 HEIMDAL = "Heimdal"
45- OTHER = "other"
4645
4746
4847class KerberosSource (IncomingSyncSource ):
@@ -54,7 +53,7 @@ class KerberosSource(IncomingSyncSource):
5453 help_text = _ ("Custom krb5.conf to use. Uses the system one by default" ),
5554 )
5655 kadmin_type = models .TextField (
57- choices = KAdminType .choices , default = KAdminType .OTHER , help_text = _ ("KAdmin server type" )
56+ choices = KAdminType .choices , default = KAdminType .MIT , help_text = _ ("KAdmin server type" )
5857 )
5958
6059 sync_users = models .BooleanField (
@@ -239,20 +238,22 @@ def krb5_conf_path(self) -> str | None:
239238 return str (conf_path )
240239
241240 def _kadmin_init (self ) -> KAdmin | None :
242- api_version = None
241+ variant = KAdm5Variant .MitClient
242+ api_version = KAdminApiVersion .Version2
243243 match self .kadmin_type :
244244 case KAdminType .MIT :
245+ variant = KAdm5Variant .MitClient
245246 api_version = KAdminApiVersion .Version4
246247 case KAdminType .HEIMDAL :
247- api_version = KAdminApiVersion .Version2
248- case KAdminType .OTHER :
248+ variant = KAdm5Variant .HeimdalClient
249249 api_version = KAdminApiVersion .Version2
250250 # kadmin doesn't use a ccache for its connection
251251 # as such, we don't need to create a separate ccache for each source
252252 if not self .sync_principal :
253253 return None
254254 if self .sync_password :
255255 return KAdmin .with_password (
256+ variant ,
256257 self .sync_principal ,
257258 self .sync_password ,
258259 api_version = api_version ,
@@ -265,12 +266,14 @@ def _kadmin_init(self) -> KAdmin | None:
265266 keytab_path .write_bytes (b64decode (self .sync_keytab ))
266267 keytab = f"FILE:{ keytab_path } "
267268 return KAdmin .with_keytab (
269+ variant ,
268270 self .sync_principal ,
269271 keytab ,
270272 api_version = api_version ,
271273 )
272274 if self .sync_ccache :
273275 return KAdmin .with_ccache (
276+ variant ,
274277 self .sync_principal ,
275278 self .sync_ccache ,
276279 api_version = api_version ,
@@ -285,9 +288,9 @@ def connection(self) -> KAdmin | None:
285288 _kadmin_connections [str (self .pk )] = self ._kadmin_init ()
286289 return _kadmin_connections .get (str (self .pk ), None )
287290
288- def check_connection (self ) -> dict [str , str ]:
291+ def check_connection (self ) -> dict [str , str | bool ]:
289292 """Check Kerberos Connection"""
290- status = {"status" : "ok" }
293+ status : dict [ str , str | bool ] = {"status" : "ok" }
291294 if not self .sync_users :
292295 return status
293296 with Krb5ConfContext (self ):
@@ -297,7 +300,7 @@ def check_connection(self) -> dict[str, str]:
297300 status ["status" ] = "no connection"
298301 return status
299302 status ["principal_exists" ] = kadm .principal_exists (self .sync_principal )
300- except PyKAdminException as exc :
303+ except kadmin_exceptions . PyKAdminException as exc :
301304 status ["status" ] = str (exc )
302305 return status
303306
0 commit comments