~lazr-developers/lazr.authentication/trunk

« back to all changes in this revision

Viewing changes to src/lazr/authentication/testing/oauth.py

  • Committer: Jürgen Gmach
  • Date: 2021-11-05 14:27:41 UTC
  • Revision ID: juergen.gmach@canonical.com-20211105142741-crqrjqm3dhkcy9wd
Moved to git

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright 2009 Canonical Ltd.  All rights reserved.
2
 
#
3
 
# This file is part of lazr.authentication
4
 
#
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
8
 
# License.
9
 
#
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.
14
 
#
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/>.
18
 
 
19
 
"""Utility classes for testing OAuth authentication."""
20
 
 
21
 
__metaclass__ = type
22
 
__all__ = [
23
 
    'SimpleOAuthDataStore',
24
 
]
25
 
 
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
30
 
 
31
 
 
32
 
class SimpleOAuthDataStore(OAuthDataStore):
33
 
    """A very simple implementation of the oauth library's OAuthDataStore."""
34
 
 
35
 
    def __init__(self, consumers=None, tokens=None):
36
 
        """Initialize with no nonces."""
37
 
        self.consumers = consumers or {}
38
 
        self.tokens = tokens or {}
39
 
        self.nonces = set()
40
 
 
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)
44
 
 
45
 
    def lookup_consumer(self, consumer):
46
 
        """Turn a consumer key into an OAuthConsumer object."""
47
 
        return self.consumers.get(consumer)
48
 
 
49
 
    def lookup_nonce(self, consumer, token, nonce):
50
 
        """Make sure a nonce has not already been used.
51
 
 
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.
54
 
        """
55
 
        key = (consumer, token, nonce)
56
 
        if key in self.nonces:
57
 
            return True
58
 
        self.nonces.add(key)
59
 
        return False