Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

_cred.py 2.7KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Credential managers for L{twisted.mail}.
  5. """
  6. import hashlib
  7. import hmac
  8. from zope.interface import implementer
  9. from twisted.cred import credentials
  10. from twisted.mail._except import IllegalClientResponse
  11. from twisted.mail.interfaces import IChallengeResponse, IClientAuthentication
  12. from twisted.python.compat import nativeString
  13. @implementer(IClientAuthentication)
  14. class CramMD5ClientAuthenticator:
  15. def __init__(self, user):
  16. self.user = user
  17. def getName(self):
  18. return b"CRAM-MD5"
  19. def challengeResponse(self, secret, chal):
  20. response = hmac.HMAC(secret, chal, digestmod=hashlib.md5).hexdigest()
  21. return self.user + b" " + response.encode("ascii")
  22. @implementer(IClientAuthentication)
  23. class LOGINAuthenticator:
  24. def __init__(self, user):
  25. self.user = user
  26. self.challengeResponse = self.challengeUsername
  27. def getName(self):
  28. return b"LOGIN"
  29. def challengeUsername(self, secret, chal):
  30. # Respond to something like "Username:"
  31. self.challengeResponse = self.challengeSecret
  32. return self.user
  33. def challengeSecret(self, secret, chal):
  34. # Respond to something like "Password:"
  35. return secret
  36. @implementer(IClientAuthentication)
  37. class PLAINAuthenticator:
  38. def __init__(self, user):
  39. self.user = user
  40. def getName(self):
  41. return b"PLAIN"
  42. def challengeResponse(self, secret, chal):
  43. return b"\0" + self.user + b"\0" + secret
  44. @implementer(IChallengeResponse)
  45. class LOGINCredentials(credentials.UsernamePassword):
  46. def __init__(self):
  47. self.challenges = [b"Password\0", b"User Name\0"]
  48. self.responses = [b"password", b"username"]
  49. credentials.UsernamePassword.__init__(self, None, None)
  50. def getChallenge(self):
  51. return self.challenges.pop()
  52. def setResponse(self, response):
  53. setattr(self, nativeString(self.responses.pop()), response)
  54. def moreChallenges(self):
  55. return bool(self.challenges)
  56. @implementer(IChallengeResponse)
  57. class PLAINCredentials(credentials.UsernamePassword):
  58. def __init__(self):
  59. credentials.UsernamePassword.__init__(self, None, None)
  60. def getChallenge(self):
  61. return b""
  62. def setResponse(self, response):
  63. parts = response.split(b"\0")
  64. if len(parts) != 3:
  65. raise IllegalClientResponse("Malformed Response - wrong number of parts")
  66. useless, self.username, self.password = parts
  67. def moreChallenges(self):
  68. return False
  69. __all__ = [
  70. "CramMD5ClientAuthenticator",
  71. "LOGINCredentials",
  72. "LOGINAuthenticator",
  73. "PLAINCredentials",
  74. "PLAINAuthenticator",
  75. ]