interface.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. """
  2. This module contains the definition of the C{L{OpenIDStore}}
  3. interface.
  4. """
  5. class OpenIDStore(object):
  6. """
  7. This is the interface for the store objects the OpenID library
  8. uses. It is a single class that provides all of the persistence
  9. mechanisms that the OpenID library needs, for both servers and
  10. consumers.
  11. @change: Version 2.0 removed the C{storeNonce}, C{getAuthKey}, and C{isDumb}
  12. methods, and changed the behavior of the C{L{useNonce}} method
  13. to support one-way nonces. It added C{L{cleanupNonces}},
  14. C{L{cleanupAssociations}}, and C{L{cleanup}}.
  15. @sort: storeAssociation, getAssociation, removeAssociation,
  16. useNonce
  17. """
  18. def storeAssociation(self, server_url, association):
  19. """
  20. This method puts a C{L{Association
  21. <openid.association.Association>}} object into storage,
  22. retrievable by server URL and handle.
  23. @param server_url: The URL of the identity server that this
  24. association is with. Because of the way the server
  25. portion of the library uses this interface, don't assume
  26. there are any limitations on the character set of the
  27. input string. In particular, expect to see unescaped
  28. non-url-safe characters in the server_url field.
  29. @type server_url: C{str}
  30. @param association: The C{L{Association
  31. <openid.association.Association>}} to store.
  32. @type association: C{L{Association
  33. <openid.association.Association>}}
  34. @return: C{None}
  35. @rtype: C{NoneType}
  36. """
  37. raise NotImplementedError
  38. def getAssociation(self, server_url, handle=None):
  39. """
  40. This method returns an C{L{Association
  41. <openid.association.Association>}} object from storage that
  42. matches the server URL and, if specified, handle. It returns
  43. C{None} if no such association is found or if the matching
  44. association is expired.
  45. If no handle is specified, the store may return any
  46. association which matches the server URL. If multiple
  47. associations are valid, the recommended return value for this
  48. method is the one most recently issued.
  49. This method is allowed (and encouraged) to garbage collect
  50. expired associations when found. This method must not return
  51. expired associations.
  52. @param server_url: The URL of the identity server to get the
  53. association for. Because of the way the server portion of
  54. the library uses this interface, don't assume there are
  55. any limitations on the character set of the input string.
  56. In particular, expect to see unescaped non-url-safe
  57. characters in the server_url field.
  58. @type server_url: C{str}
  59. @param handle: This optional parameter is the handle of the
  60. specific association to get. If no specific handle is
  61. provided, any valid association matching the server URL is
  62. returned.
  63. @type handle: C{str} or C{NoneType}
  64. @return: The C{L{Association
  65. <openid.association.Association>}} for the given identity
  66. server.
  67. @rtype: C{L{Association <openid.association.Association>}} or
  68. C{NoneType}
  69. """
  70. raise NotImplementedError
  71. def removeAssociation(self, server_url, handle):
  72. """
  73. This method removes the matching association if it's found,
  74. and returns whether the association was removed or not.
  75. @param server_url: The URL of the identity server the
  76. association to remove belongs to. Because of the way the
  77. server portion of the library uses this interface, don't
  78. assume there are any limitations on the character set of
  79. the input string. In particular, expect to see unescaped
  80. non-url-safe characters in the server_url field.
  81. @type server_url: C{str}
  82. @param handle: This is the handle of the association to
  83. remove. If there isn't an association found that matches
  84. both the given URL and handle, then there was no matching
  85. handle found.
  86. @type handle: C{str}
  87. @return: Returns whether or not the given association existed.
  88. @rtype: C{bool} or C{int}
  89. """
  90. raise NotImplementedError
  91. def useNonce(self, server_url, timestamp, salt):
  92. """Called when using a nonce.
  93. This method should return C{True} if the nonce has not been
  94. used before, and store it for a while to make sure nobody
  95. tries to use the same value again. If the nonce has already
  96. been used or the timestamp is not current, return C{False}.
  97. You may use L{openid.store.nonce.SKEW} for your timestamp window.
  98. @change: In earlier versions, round-trip nonces were used and
  99. a nonce was only valid if it had been previously stored
  100. with C{storeNonce}. Version 2.0 uses one-way nonces,
  101. requiring a different implementation here that does not
  102. depend on a C{storeNonce} call. (C{storeNonce} is no
  103. longer part of the interface.)
  104. @param server_url: The URL of the server from which the nonce
  105. originated.
  106. @type server_url: C{str}
  107. @param timestamp: The time that the nonce was created (to the
  108. nearest second), in seconds since January 1 1970 UTC.
  109. @type timestamp: C{int}
  110. @param salt: A random string that makes two nonces from the
  111. same server issued during the same second unique.
  112. @type salt: str
  113. @return: Whether or not the nonce was valid.
  114. @rtype: C{bool}
  115. """
  116. raise NotImplementedError
  117. def cleanupNonces(self):
  118. """Remove expired nonces from the store.
  119. Discards any nonce from storage that is old enough that its
  120. timestamp would not pass L{useNonce}.
  121. This method is not called in the normal operation of the
  122. library. It provides a way for store admins to keep
  123. their storage from filling up with expired data.
  124. @return: the number of nonces expired.
  125. @returntype: int
  126. """
  127. raise NotImplementedError
  128. def cleanupAssociations(self):
  129. """Remove expired associations from the store.
  130. This method is not called in the normal operation of the
  131. library. It provides a way for store admins to keep
  132. their storage from filling up with expired data.
  133. @return: the number of associations expired.
  134. @returntype: int
  135. """
  136. raise NotImplementedError
  137. def cleanup(self):
  138. """Shortcut for C{L{cleanupNonces}()}, C{L{cleanupAssociations}()}.
  139. This method is not called in the normal operation of the
  140. library. It provides a way for store admins to keep
  141. their storage from filling up with expired data.
  142. """
  143. return self.cleanupNonces(), self.cleanupAssociations()