cred_sshkeys.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # -*- test-case-name: twisted.test.test_strcred -*-
  2. #
  3. # Copyright (c) Twisted Matrix Laboratories.
  4. # See LICENSE for details.
  5. """
  6. Cred plugin for ssh key login.
  7. """
  8. from __future__ import absolute_import, division
  9. from zope.interface import implementer
  10. from twisted import plugin
  11. from twisted.cred.strcred import ICheckerFactory
  12. sshKeyCheckerFactoryHelp = """
  13. This allows SSH public key authentication, based on public keys listed in
  14. authorized_keys and authorized_keys2 files in user .ssh/ directories.
  15. """
  16. try:
  17. from twisted.conch.checkers import (
  18. SSHPublicKeyChecker, UNIXAuthorizedKeysFiles)
  19. @implementer(ICheckerFactory, plugin.IPlugin)
  20. class SSHKeyCheckerFactory(object):
  21. """
  22. Generates checkers that will authenticate a SSH public key
  23. """
  24. authType = 'sshkey'
  25. authHelp = sshKeyCheckerFactoryHelp
  26. argStringFormat = 'No argstring required.'
  27. credentialInterfaces = SSHPublicKeyChecker.credentialInterfaces
  28. def generateChecker(self, argstring=''):
  29. """
  30. This checker factory ignores the argument string. Everything
  31. needed to authenticate users is pulled out of the public keys
  32. listed in user .ssh/ directories.
  33. """
  34. return SSHPublicKeyChecker(UNIXAuthorizedKeysFiles())
  35. theSSHKeyCheckerFactory = SSHKeyCheckerFactory()
  36. except ImportError:
  37. # if checkers can't be imported, then there should be no SSH cred plugin
  38. pass