cred_file.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 a file of the format 'username:password'.
  7. """
  8. from __future__ import absolute_import, division
  9. import sys
  10. from zope.interface import implementer
  11. from twisted import plugin
  12. from twisted.cred.checkers import FilePasswordDB
  13. from twisted.cred.strcred import ICheckerFactory
  14. from twisted.cred.credentials import IUsernamePassword, IUsernameHashedPassword
  15. fileCheckerFactoryHelp = """
  16. This checker expects to receive the location of a file that
  17. conforms to the FilePasswordDB format. Each line in the file
  18. should be of the format 'username:password', in plain text.
  19. """
  20. invalidFileWarning = 'Warning: not a valid file'
  21. @implementer(ICheckerFactory, plugin.IPlugin)
  22. class FileCheckerFactory(object):
  23. """
  24. A factory for instances of L{FilePasswordDB}.
  25. """
  26. authType = 'file'
  27. authHelp = fileCheckerFactoryHelp
  28. argStringFormat = 'Location of a FilePasswordDB-formatted file.'
  29. # Explicitly defined here because FilePasswordDB doesn't do it for us
  30. credentialInterfaces = (IUsernamePassword, IUsernameHashedPassword)
  31. errorOutput = sys.stderr
  32. def generateChecker(self, argstring):
  33. """
  34. This checker factory expects to get the location of a file.
  35. The file should conform to the format required by
  36. L{FilePasswordDB} (using defaults for all
  37. initialization parameters).
  38. """
  39. from twisted.python.filepath import FilePath
  40. if not argstring.strip():
  41. raise ValueError('%r requires a filename' % self.authType)
  42. elif not FilePath(argstring).isfile():
  43. self.errorOutput.write('%s: %s\n' % (invalidFileWarning, argstring))
  44. return FilePasswordDB(argstring)
  45. theFileCheckerFactory = FileCheckerFactory()