Skip to content

Instantly share code, notes, and snippets.

@yvesf
Created February 25, 2015 19:56
Show Gist options
  • Save yvesf/7cc8300176df8322c913 to your computer and use it in GitHub Desktop.
Save yvesf/7cc8300176df8322c913 to your computer and use it in GitHub Desktop.
Python imaplib kerberos mixin
import base64
import kerberos
import imaplib
class IMAPGSSMixin(object):
def gss_cb(self,challenge):
input_token = base64.b64encode(challenge).decode('utf-8')
if self.step == 0:
ret = kerberos.authGSSClientStep(self._gss_context, input_token)
if ret != kerberos.AUTH_GSS_CONTINUE:
self.step = 1
elif self.step == 1:
ret = kerberos.authGSSClientUnwrap(self._gss_context, input_token)
response = kerberos.authGSSClientResponse(self._gss_context)
ret = kerberos.authGSSClientWrap(self._gss_context, response, self.user)
response = kerberos.authGSSClientResponse(self._gss_context)
if response is None:
return ""
else:
return base64.b64decode(response)
def login_gssapi(self, user):
self.user = user
self.step = 0
_unused, self._gss_context = kerberos.authGSSClientInit("imap@{}".format(self.host))
return self.authenticate(b"GSSAPI", self.gss_cb)
class IMAP4(imaplib.IMAP4_SSL, IMAPGSSMixin):
pass
# M.login_gssapi("USERNAME")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment