1
# Copyright 2009 Canonical Ltd. All rights reserved.
3
# This file is part of lazr.authentication
5
# lazr.authentication is free software: you can redistribute it and/or
6
# modify it under the terms of the GNU Lesser General Public License
7
# as published by the Free Software Foundation, version 3 of the
10
# lazr.authentication is distributed in the hope that it will be
11
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
# Lesser General Public License for more details.
15
# You should have received a copy of the GNU Lesser General Public
16
# License along with lazr.authentication. If not, see
17
# <http://www.gnu.org/licenses/>.
19
"""Utility classes for testing OAuth authentication."""
23
'SimpleOAuthDataStore',
26
# Work around relative import behavior. The below is equivalent to
27
# from oauth import oauth
28
oauth = __import__('oauth.oauth', {}).oauth
29
OAuthDataStore = oauth.OAuthDataStore
32
class SimpleOAuthDataStore(OAuthDataStore):
33
"""A very simple implementation of the oauth library's OAuthDataStore."""
35
def __init__(self, consumers=None, tokens=None):
36
"""Initialize with no nonces."""
37
self.consumers = consumers or {}
38
self.tokens = tokens or {}
41
def lookup_token(self, token_type, token_field):
42
"""Turn a token key into an OAuthToken object."""
43
return self.tokens.get(token_field)
45
def lookup_consumer(self, consumer):
46
"""Turn a consumer key into an OAuthConsumer object."""
47
return self.consumers.get(consumer)
49
def lookup_nonce(self, consumer, token, nonce):
50
"""Make sure a nonce has not already been used.
52
If the nonce has not been used, add it to the set
53
so that a future call to this method will return False.
55
key = (consumer, token, nonce)
56
if key in self.nonces: