1""" 
    2oauthlib.oauth1.rfc5849 
    3~~~~~~~~~~~~~~ 
    4 
    5This module is an implementation of various logic needed 
    6for signing and checking OAuth 1.0 RFC 5849 requests. 
    7""" 
    8from . import SIGNATURE_METHODS, utils 
    9 
    10 
    11class RequestValidator: 
    12 
    13    """A validator/datastore interaction base class for OAuth 1 providers. 
    14 
    15    OAuth providers should inherit from RequestValidator and implement the 
    16    methods and properties outlined below. Further details are provided in the 
    17    documentation for each method and property. 
    18 
    19    Methods used to check the format of input parameters. Common tests include 
    20    length, character set, membership, range or pattern. These tests are 
    21    referred to as `whitelisting or blacklisting`_. Whitelisting is better 
    22    but blacklisting can be useful to spot malicious activity. 
    23    The following have methods a default implementation: 
    24 
    25    - check_client_key 
    26    - check_request_token 
    27    - check_access_token 
    28    - check_nonce 
    29    - check_verifier 
    30    - check_realms 
    31 
    32    The methods above default to whitelist input parameters, checking that they 
    33    are alphanumerical and between a minimum and maximum length. Rather than 
    34    overloading the methods a few properties can be used to configure these 
    35    methods. 
    36 
    37    * @safe_characters -> (character set) 
    38    * @client_key_length -> (min, max) 
    39    * @request_token_length -> (min, max) 
    40    * @access_token_length -> (min, max) 
    41    * @nonce_length -> (min, max) 
    42    * @verifier_length -> (min, max) 
    43    * @realms -> [list, of, realms] 
    44 
    45    Methods used to validate/invalidate input parameters. These checks usually 
    46    hit either persistent or temporary storage such as databases or the 
    47    filesystem. See each methods documentation for detailed usage. 
    48    The following methods must be implemented: 
    49 
    50    - validate_client_key 
    51    - validate_request_token 
    52    - validate_access_token 
    53    - validate_timestamp_and_nonce 
    54    - validate_redirect_uri 
    55    - validate_requested_realms 
    56    - validate_realms 
    57    - validate_verifier 
    58    - invalidate_request_token 
    59 
    60    Methods used to retrieve sensitive information from storage. 
    61    The following methods must be implemented: 
    62 
    63    - get_client_secret 
    64    - get_request_token_secret 
    65    - get_access_token_secret 
    66    - get_rsa_key 
    67    - get_realms 
    68    - get_default_realms 
    69    - get_redirect_uri 
    70 
    71    Methods used to save credentials. 
    72    The following methods must be implemented: 
    73 
    74    - save_request_token 
    75    - save_verifier 
    76    - save_access_token 
    77 
    78    Methods used to verify input parameters. This methods are used during 
    79    authorizing request token by user (AuthorizationEndpoint), to check if 
    80    parameters are valid. During token authorization request is not signed, 
    81    thus 'validation' methods can not be used. The following methods must be 
    82    implemented: 
    83 
    84    - verify_realms 
    85    - verify_request_token 
    86 
    87    To prevent timing attacks it is necessary to not exit early even if the 
    88    client key or resource owner key is invalid. Instead dummy values should 
    89    be used during the remaining verification process. It is very important 
    90    that the dummy client and token are valid input parameters to the methods 
    91    get_client_secret, get_rsa_key and get_(access/request)_token_secret and 
    92    that the running time of those methods when given a dummy value remain 
    93    equivalent to the running time when given a valid client/resource owner. 
    94    The following properties must be implemented: 
    95 
    96    * @dummy_client 
    97    * @dummy_request_token 
    98    * @dummy_access_token 
    99 
    100    Example implementations have been provided, note that the database used is 
    101    a simple dictionary and serves only an illustrative purpose. Use whichever 
    102    database suits your project and how to access it is entirely up to you. 
    103    The methods are introduced in an order which should make understanding 
    104    their use more straightforward and as such it could be worth reading what 
    105    follows in chronological order. 
    106 
    107    .. _`whitelisting or blacklisting`: https://www.schneier.com/blog/archives/2011/01/whitelisting_vs.html 
    108    """ 
    109 
    110    def __init__(self): 
    111        pass 
    112 
    113    @property 
    114    def allowed_signature_methods(self): 
    115        return SIGNATURE_METHODS 
    116 
    117    @property 
    118    def safe_characters(self): 
    119        return set(utils.UNICODE_ASCII_CHARACTER_SET) 
    120 
    121    @property 
    122    def client_key_length(self): 
    123        return 20, 30 
    124 
    125    @property 
    126    def request_token_length(self): 
    127        return 20, 30 
    128 
    129    @property 
    130    def access_token_length(self): 
    131        return 20, 30 
    132 
    133    @property 
    134    def timestamp_lifetime(self): 
    135        return 600 
    136 
    137    @property 
    138    def nonce_length(self): 
    139        return 20, 30 
    140 
    141    @property 
    142    def verifier_length(self): 
    143        return 20, 30 
    144 
    145    @property 
    146    def realms(self): 
    147        return [] 
    148 
    149    @property 
    150    def enforce_ssl(self): 
    151        return True 
    152 
    153    def check_client_key(self, client_key): 
    154        """Check that the client key only contains safe characters 
    155        and is no shorter than lower and no longer than upper. 
    156        """ 
    157        lower, upper = self.client_key_length 
    158        return (set(client_key) <= self.safe_characters and 
    159                lower <= len(client_key) <= upper) 
    160 
    161    def check_request_token(self, request_token): 
    162        """Checks that the request token contains only safe characters 
    163        and is no shorter than lower and no longer than upper. 
    164        """ 
    165        lower, upper = self.request_token_length 
    166        return (set(request_token) <= self.safe_characters and 
    167                lower <= len(request_token) <= upper) 
    168 
    169    def check_access_token(self, request_token): 
    170        """Checks that the token contains only safe characters 
    171        and is no shorter than lower and no longer than upper. 
    172        """ 
    173        lower, upper = self.access_token_length 
    174        return (set(request_token) <= self.safe_characters and 
    175                lower <= len(request_token) <= upper) 
    176 
    177    def check_nonce(self, nonce): 
    178        """Checks that the nonce only contains only safe characters 
    179        and is no shorter than lower and no longer than upper. 
    180        """ 
    181        lower, upper = self.nonce_length 
    182        return (set(nonce) <= self.safe_characters and 
    183                lower <= len(nonce) <= upper) 
    184 
    185    def check_verifier(self, verifier): 
    186        """Checks that the verifier contains only safe characters 
    187        and is no shorter than lower and no longer than upper. 
    188        """ 
    189        lower, upper = self.verifier_length 
    190        return (set(verifier) <= self.safe_characters and 
    191                lower <= len(verifier) <= upper) 
    192 
    193    def check_realms(self, realms): 
    194        """Check that the realm is one of a set allowed realms.""" 
    195        return all(r in self.realms for r in realms) 
    196 
    197    def _subclass_must_implement(self, fn): 
    198        """ 
    199        Returns a NotImplementedError for a function that should be implemented. 
    200        :param fn: name of the function 
    201        """ 
    202        m = "Missing function implementation in {}: {}".format(type(self), fn) 
    203        return NotImplementedError(m) 
    204 
    205    @property 
    206    def dummy_client(self): 
    207        """Dummy client used when an invalid client key is supplied. 
    208 
    209        :returns: The dummy client key string. 
    210 
    211        The dummy client should be associated with either a client secret, 
    212        a rsa key or both depending on which signature methods are supported. 
    213        Providers should make sure that 
    214 
    215        get_client_secret(dummy_client) 
    216        get_rsa_key(dummy_client) 
    217 
    218        return a valid secret or key for the dummy client. 
    219 
    220        This method is used by 
    221 
    222        * AccessTokenEndpoint 
    223        * RequestTokenEndpoint 
    224        * ResourceEndpoint 
    225        * SignatureOnlyEndpoint 
    226        """ 
    227        raise self._subclass_must_implement("dummy_client") 
    228 
    229    @property 
    230    def dummy_request_token(self): 
    231        """Dummy request token used when an invalid token was supplied. 
    232 
    233        :returns: The dummy request token string. 
    234 
    235        The dummy request token should be associated with a request token 
    236        secret such that get_request_token_secret(.., dummy_request_token) 
    237        returns a valid secret. 
    238 
    239        This method is used by 
    240 
    241        * AccessTokenEndpoint 
    242        """ 
    243        raise self._subclass_must_implement("dummy_request_token") 
    244 
    245    @property 
    246    def dummy_access_token(self): 
    247        """Dummy access token used when an invalid token was supplied. 
    248 
    249        :returns: The dummy access token string. 
    250 
    251        The dummy access token should be associated with an access token 
    252        secret such that get_access_token_secret(.., dummy_access_token) 
    253        returns a valid secret. 
    254 
    255        This method is used by 
    256 
    257        * ResourceEndpoint 
    258        """ 
    259        raise self._subclass_must_implement("dummy_access_token") 
    260 
    261    def get_client_secret(self, client_key, request): 
    262        """Retrieves the client secret associated with the client key. 
    263 
    264        :param client_key: The client/consumer key. 
    265        :param request: OAuthlib request. 
    266        :type request: oauthlib.common.Request 
    267        :returns: The client secret as a string. 
    268 
    269        This method must allow the use of a dummy client_key value. 
    270        Fetching the secret using the dummy key must take the same amount of 
    271        time as fetching a secret for a valid client:: 
    272 
    273            # Unlikely to be near constant time as it uses two database 
    274            # lookups for a valid client, and only one for an invalid. 
    275            from your_datastore import ClientSecret 
    276            if ClientSecret.has(client_key): 
    277                return ClientSecret.get(client_key) 
    278            else: 
    279                return 'dummy' 
    280 
    281            # Aim to mimic number of latency inducing operations no matter 
    282            # whether the client is valid or not. 
    283            from your_datastore import ClientSecret 
    284            return ClientSecret.get(client_key, 'dummy') 
    285 
    286        Note that the returned key must be in plaintext. 
    287 
    288        This method is used by 
    289 
    290        * AccessTokenEndpoint 
    291        * RequestTokenEndpoint 
    292        * ResourceEndpoint 
    293        * SignatureOnlyEndpoint 
    294        """ 
    295        raise self._subclass_must_implement('get_client_secret') 
    296 
    297    def get_request_token_secret(self, client_key, token, request): 
    298        """Retrieves the shared secret associated with the request token. 
    299 
    300        :param client_key: The client/consumer key. 
    301        :param token: The request token string. 
    302        :param request: OAuthlib request. 
    303        :type request: oauthlib.common.Request 
    304        :returns: The token secret as a string. 
    305 
    306        This method must allow the use of a dummy values and the running time 
    307        must be roughly equivalent to that of the running time of valid values:: 
    308 
    309            # Unlikely to be near constant time as it uses two database 
    310            # lookups for a valid client, and only one for an invalid. 
    311            from your_datastore import RequestTokenSecret 
    312            if RequestTokenSecret.has(client_key): 
    313                return RequestTokenSecret.get((client_key, request_token)) 
    314            else: 
    315                return 'dummy' 
    316 
    317            # Aim to mimic number of latency inducing operations no matter 
    318            # whether the client is valid or not. 
    319            from your_datastore import RequestTokenSecret 
    320            return ClientSecret.get((client_key, request_token), 'dummy') 
    321 
    322        Note that the returned key must be in plaintext. 
    323 
    324        This method is used by 
    325 
    326        * AccessTokenEndpoint 
    327        """ 
    328        raise self._subclass_must_implement('get_request_token_secret') 
    329 
    330    def get_access_token_secret(self, client_key, token, request): 
    331        """Retrieves the shared secret associated with the access token. 
    332 
    333        :param client_key: The client/consumer key. 
    334        :param token: The access token string. 
    335        :param request: OAuthlib request. 
    336        :type request: oauthlib.common.Request 
    337        :returns: The token secret as a string. 
    338 
    339        This method must allow the use of a dummy values and the running time 
    340        must be roughly equivalent to that of the running time of valid values:: 
    341 
    342            # Unlikely to be near constant time as it uses two database 
    343            # lookups for a valid client, and only one for an invalid. 
    344            from your_datastore import AccessTokenSecret 
    345            if AccessTokenSecret.has(client_key): 
    346                return AccessTokenSecret.get((client_key, request_token)) 
    347            else: 
    348                return 'dummy' 
    349 
    350            # Aim to mimic number of latency inducing operations no matter 
    351            # whether the client is valid or not. 
    352            from your_datastore import AccessTokenSecret 
    353            return ClientSecret.get((client_key, request_token), 'dummy') 
    354 
    355        Note that the returned key must be in plaintext. 
    356 
    357        This method is used by 
    358 
    359        * ResourceEndpoint 
    360        """ 
    361        raise self._subclass_must_implement("get_access_token_secret") 
    362 
    363    def get_default_realms(self, client_key, request): 
    364        """Get the default realms for a client. 
    365 
    366        :param client_key: The client/consumer key. 
    367        :param request: OAuthlib request. 
    368        :type request: oauthlib.common.Request 
    369        :returns: The list of default realms associated with the client. 
    370 
    371        The list of default realms will be set during client registration and 
    372        is outside the scope of OAuthLib. 
    373 
    374        This method is used by 
    375 
    376        * RequestTokenEndpoint 
    377        """ 
    378        raise self._subclass_must_implement("get_default_realms") 
    379 
    380    def get_realms(self, token, request): 
    381        """Get realms associated with a request token. 
    382 
    383        :param token: The request token string. 
    384        :param request: OAuthlib request. 
    385        :type request: oauthlib.common.Request 
    386        :returns: The list of realms associated with the request token. 
    387 
    388        This method is used by 
    389 
    390        * AuthorizationEndpoint 
    391        * AccessTokenEndpoint 
    392        """ 
    393        raise self._subclass_must_implement("get_realms") 
    394 
    395    def get_redirect_uri(self, token, request): 
    396        """Get the redirect URI associated with a request token. 
    397 
    398        :param token: The request token string. 
    399        :param request: OAuthlib request. 
    400        :type request: oauthlib.common.Request 
    401        :returns: The redirect URI associated with the request token. 
    402 
    403        It may be desirable to return a custom URI if the redirect is set to "oob". 
    404        In this case, the user will be redirected to the returned URI and at that 
    405        endpoint the verifier can be displayed. 
    406 
    407        This method is used by 
    408 
    409        * AuthorizationEndpoint 
    410        """ 
    411        raise self._subclass_must_implement("get_redirect_uri") 
    412 
    413    def get_rsa_key(self, client_key, request): 
    414        """Retrieves a previously stored client provided RSA key. 
    415 
    416        :param client_key: The client/consumer key. 
    417        :param request: OAuthlib request. 
    418        :type request: oauthlib.common.Request 
    419        :returns: The rsa public key as a string. 
    420 
    421        This method must allow the use of a dummy client_key value. Fetching 
    422        the rsa key using the dummy key must take the same amount of time 
    423        as fetching a key for a valid client. The dummy key must also be of 
    424        the same bit length as client keys. 
    425 
    426        Note that the key must be returned in plaintext. 
    427 
    428        This method is used by 
    429 
    430        * AccessTokenEndpoint 
    431        * RequestTokenEndpoint 
    432        * ResourceEndpoint 
    433        * SignatureOnlyEndpoint 
    434        """ 
    435        raise self._subclass_must_implement("get_rsa_key") 
    436 
    437    def invalidate_request_token(self, client_key, request_token, request): 
    438        """Invalidates a used request token. 
    439 
    440        :param client_key: The client/consumer key. 
    441        :param request_token: The request token string. 
    442        :param request: OAuthlib request. 
    443        :type request: oauthlib.common.Request 
    444        :returns: None 
    445 
    446        Per `Section 2.3`_ of the spec: 
    447 
    448        "The server MUST (...) ensure that the temporary 
    449        credentials have not expired or been used before." 
    450 
    451        .. _`Section 2.3`: https://tools.ietf.org/html/rfc5849#section-2.3 
    452 
    453        This method should ensure that provided token won't validate anymore. 
    454        It can be simply removing RequestToken from storage or setting 
    455        specific flag that makes it invalid (note that such flag should be 
    456        also validated during request token validation). 
    457 
    458        This method is used by 
    459 
    460        * AccessTokenEndpoint 
    461        """ 
    462        raise self._subclass_must_implement("invalidate_request_token") 
    463 
    464    def validate_client_key(self, client_key, request): 
    465        """Validates that supplied client key is a registered and valid client. 
    466 
    467        :param client_key: The client/consumer key. 
    468        :param request: OAuthlib request. 
    469        :type request: oauthlib.common.Request 
    470        :returns: True or False 
    471 
    472        Note that if the dummy client is supplied it should validate in same 
    473        or nearly the same amount of time as a valid one. 
    474 
    475        Ensure latency inducing tasks are mimiced even for dummy clients. 
    476        For example, use:: 
    477 
    478            from your_datastore import Client 
    479            try: 
    480                return Client.exists(client_key, access_token) 
    481            except DoesNotExist: 
    482                return False 
    483 
    484        Rather than:: 
    485 
    486            from your_datastore import Client 
    487            if access_token == self.dummy_access_token: 
    488                return False 
    489            else: 
    490                return Client.exists(client_key, access_token) 
    491 
    492        This method is used by 
    493 
    494        * AccessTokenEndpoint 
    495        * RequestTokenEndpoint 
    496        * ResourceEndpoint 
    497        * SignatureOnlyEndpoint 
    498        """ 
    499        raise self._subclass_must_implement("validate_client_key") 
    500 
    501    def validate_request_token(self, client_key, token, request): 
    502        """Validates that supplied request token is registered and valid. 
    503 
    504        :param client_key: The client/consumer key. 
    505        :param token: The request token string. 
    506        :param request: OAuthlib request. 
    507        :type request: oauthlib.common.Request 
    508        :returns: True or False 
    509 
    510        Note that if the dummy request_token is supplied it should validate in 
    511        the same nearly the same amount of time as a valid one. 
    512 
    513        Ensure latency inducing tasks are mimiced even for dummy clients. 
    514        For example, use:: 
    515 
    516            from your_datastore import RequestToken 
    517            try: 
    518                return RequestToken.exists(client_key, access_token) 
    519            except DoesNotExist: 
    520                return False 
    521 
    522        Rather than:: 
    523 
    524            from your_datastore import RequestToken 
    525            if access_token == self.dummy_access_token: 
    526                return False 
    527            else: 
    528                return RequestToken.exists(client_key, access_token) 
    529 
    530        This method is used by 
    531 
    532        * AccessTokenEndpoint 
    533        """ 
    534        raise self._subclass_must_implement("validate_request_token") 
    535 
    536    def validate_access_token(self, client_key, token, request): 
    537        """Validates that supplied access token is registered and valid. 
    538 
    539        :param client_key: The client/consumer key. 
    540        :param token: The access token string. 
    541        :param request: OAuthlib request. 
    542        :type request: oauthlib.common.Request 
    543        :returns: True or False 
    544 
    545        Note that if the dummy access token is supplied it should validate in 
    546        the same or nearly the same amount of time as a valid one. 
    547 
    548        Ensure latency inducing tasks are mimiced even for dummy clients. 
    549        For example, use:: 
    550 
    551            from your_datastore import AccessToken 
    552            try: 
    553                return AccessToken.exists(client_key, access_token) 
    554            except DoesNotExist: 
    555                return False 
    556 
    557        Rather than:: 
    558 
    559            from your_datastore import AccessToken 
    560            if access_token == self.dummy_access_token: 
    561                return False 
    562            else: 
    563                return AccessToken.exists(client_key, access_token) 
    564 
    565        This method is used by 
    566 
    567        * ResourceEndpoint 
    568        """ 
    569        raise self._subclass_must_implement("validate_access_token") 
    570 
    571    def validate_timestamp_and_nonce(self, client_key, timestamp, nonce, 
    572                                     request, request_token=None, access_token=None): 
    573        """Validates that the nonce has not been used before. 
    574 
    575        :param client_key: The client/consumer key. 
    576        :param timestamp: The ``oauth_timestamp`` parameter. 
    577        :param nonce: The ``oauth_nonce`` parameter. 
    578        :param request_token: Request token string, if any. 
    579        :param access_token: Access token string, if any. 
    580        :param request: OAuthlib request. 
    581        :type request: oauthlib.common.Request 
    582        :returns: True or False 
    583 
    584        Per `Section 3.3`_ of the spec. 
    585 
    586        "A nonce is a random string, uniquely generated by the client to allow 
    587        the server to verify that a request has never been made before and 
    588        helps prevent replay attacks when requests are made over a non-secure 
    589        channel.  The nonce value MUST be unique across all requests with the 
    590        same timestamp, client credentials, and token combinations." 
    591 
    592        .. _`Section 3.3`: https://tools.ietf.org/html/rfc5849#section-3.3 
    593 
    594        One of the first validation checks that will be made is for the validity 
    595        of the nonce and timestamp, which are associated with a client key and 
    596        possibly a token. If invalid then immediately fail the request 
    597        by returning False. If the nonce/timestamp pair has been used before and 
    598        you may just have detected a replay attack. Therefore it is an essential 
    599        part of OAuth security that you not allow nonce/timestamp reuse. 
    600        Note that this validation check is done before checking the validity of 
    601        the client and token.:: 
    602 
    603           nonces_and_timestamps_database = [ 
    604              (u'foo', 1234567890, u'rannoMstrInghere', u'bar') 
    605           ] 
    606 
    607           def validate_timestamp_and_nonce(self, client_key, timestamp, nonce, 
    608              request_token=None, access_token=None): 
    609 
    610              return ((client_key, timestamp, nonce, request_token or access_token) 
    611                       not in self.nonces_and_timestamps_database) 
    612 
    613        This method is used by 
    614 
    615        * AccessTokenEndpoint 
    616        * RequestTokenEndpoint 
    617        * ResourceEndpoint 
    618        * SignatureOnlyEndpoint 
    619        """ 
    620        raise self._subclass_must_implement("validate_timestamp_and_nonce") 
    621 
    622    def validate_redirect_uri(self, client_key, redirect_uri, request): 
    623        """Validates the client supplied redirection URI. 
    624 
    625        :param client_key: The client/consumer key. 
    626        :param redirect_uri: The URI the client which to redirect back to after 
    627                             authorization is successful. 
    628        :param request: OAuthlib request. 
    629        :type request: oauthlib.common.Request 
    630        :returns: True or False 
    631 
    632        It is highly recommended that OAuth providers require their clients 
    633        to register all redirection URIs prior to using them in requests and 
    634        register them as absolute URIs. See `CWE-601`_ for more information 
    635        about open redirection attacks. 
    636 
    637        By requiring registration of all redirection URIs it should be 
    638        straightforward for the provider to verify whether the supplied 
    639        redirect_uri is valid or not. 
    640 
    641        Alternatively per `Section 2.1`_ of the spec: 
    642 
    643        "If the client is unable to receive callbacks or a callback URI has 
    644        been established via other means, the parameter value MUST be set to 
    645        "oob" (case sensitive), to indicate an out-of-band configuration." 
    646 
    647        .. _`CWE-601`: http://cwe.mitre.org/top25/index.html#CWE-601 
    648        .. _`Section 2.1`: https://tools.ietf.org/html/rfc5849#section-2.1 
    649 
    650        This method is used by 
    651 
    652        * RequestTokenEndpoint 
    653        """ 
    654        raise self._subclass_must_implement("validate_redirect_uri") 
    655 
    656    def validate_requested_realms(self, client_key, realms, request): 
    657        """Validates that the client may request access to the realm. 
    658 
    659        :param client_key: The client/consumer key. 
    660        :param realms: The list of realms that client is requesting access to. 
    661        :param request: OAuthlib request. 
    662        :type request: oauthlib.common.Request 
    663        :returns: True or False 
    664 
    665        This method is invoked when obtaining a request token and should 
    666        tie a realm to the request token and after user authorization 
    667        this realm restriction should transfer to the access token. 
    668 
    669        This method is used by 
    670 
    671        * RequestTokenEndpoint 
    672        """ 
    673        raise self._subclass_must_implement("validate_requested_realms") 
    674 
    675    def validate_realms(self, client_key, token, request, uri=None, 
    676                        realms=None): 
    677        """Validates access to the request realm. 
    678 
    679        :param client_key: The client/consumer key. 
    680        :param token: A request token string. 
    681        :param request: OAuthlib request. 
    682        :type request: oauthlib.common.Request 
    683        :param uri: The URI the realms is protecting. 
    684        :param realms: A list of realms that must have been granted to 
    685                       the access token. 
    686        :returns: True or False 
    687 
    688        How providers choose to use the realm parameter is outside the OAuth 
    689        specification but it is commonly used to restrict access to a subset 
    690        of protected resources such as "photos". 
    691 
    692        realms is a convenience parameter which can be used to provide 
    693        a per view method pre-defined list of allowed realms. 
    694 
    695        Can be as simple as:: 
    696 
    697            from your_datastore import RequestToken 
    698            request_token = RequestToken.get(token, None) 
    699 
    700            if not request_token: 
    701                return False 
    702            return set(request_token.realms).issuperset(set(realms)) 
    703 
    704        This method is used by 
    705 
    706        * ResourceEndpoint 
    707        """ 
    708        raise self._subclass_must_implement("validate_realms") 
    709 
    710    def validate_verifier(self, client_key, token, verifier, request): 
    711        """Validates a verification code. 
    712 
    713        :param client_key: The client/consumer key. 
    714        :param token: A request token string. 
    715        :param verifier: The authorization verifier string. 
    716        :param request: OAuthlib request. 
    717        :type request: oauthlib.common.Request 
    718        :returns: True or False 
    719 
    720        OAuth providers issue a verification code to clients after the 
    721        resource owner authorizes access. This code is used by the client to 
    722        obtain token credentials and the provider must verify that the 
    723        verifier is valid and associated with the client as well as the 
    724        resource owner. 
    725 
    726        Verifier validation should be done in near constant time 
    727        (to avoid verifier enumeration). To achieve this we need a 
    728        constant time string comparison which is provided by OAuthLib 
    729        in ``oauthlib.common.safe_string_equals``:: 
    730 
    731            from your_datastore import Verifier 
    732            correct_verifier = Verifier.get(client_key, request_token) 
    733            from oauthlib.common import safe_string_equals 
    734            return safe_string_equals(verifier, correct_verifier) 
    735 
    736        This method is used by 
    737 
    738        * AccessTokenEndpoint 
    739        """ 
    740        raise self._subclass_must_implement("validate_verifier") 
    741 
    742    def verify_request_token(self, token, request): 
    743        """Verify that the given OAuth1 request token is valid. 
    744 
    745        :param token: A request token string. 
    746        :param request: OAuthlib request. 
    747        :type request: oauthlib.common.Request 
    748        :returns: True or False 
    749 
    750        This method is used only in AuthorizationEndpoint to check whether the 
    751        oauth_token given in the authorization URL is valid or not. 
    752        This request is not signed and thus similar ``validate_request_token`` 
    753        method can not be used. 
    754 
    755        This method is used by 
    756 
    757        * AuthorizationEndpoint 
    758        """ 
    759        raise self._subclass_must_implement("verify_request_token") 
    760 
    761    def verify_realms(self, token, realms, request): 
    762        """Verify authorized realms to see if they match those given to token. 
    763 
    764        :param token: An access token string. 
    765        :param realms: A list of realms the client attempts to access. 
    766        :param request: OAuthlib request. 
    767        :type request: oauthlib.common.Request 
    768        :returns: True or False 
    769 
    770        This prevents the list of authorized realms sent by the client during 
    771        the authorization step to be altered to include realms outside what 
    772        was bound with the request token. 
    773 
    774        Can be as simple as:: 
    775 
    776            valid_realms = self.get_realms(token) 
    777            return all((r in valid_realms for r in realms)) 
    778 
    779        This method is used by 
    780 
    781        * AuthorizationEndpoint 
    782        """ 
    783        raise self._subclass_must_implement("verify_realms") 
    784 
    785    def save_access_token(self, token, request): 
    786        """Save an OAuth1 access token. 
    787 
    788        :param token: A dict with token credentials. 
    789        :param request: OAuthlib request. 
    790        :type request: oauthlib.common.Request 
    791 
    792        The token dictionary will at minimum include 
    793 
    794        * ``oauth_token`` the access token string. 
    795        * ``oauth_token_secret`` the token specific secret used in signing. 
    796        * ``oauth_authorized_realms`` a space separated list of realms. 
    797 
    798        Client key can be obtained from ``request.client_key``. 
    799 
    800        The list of realms (not joined string) can be obtained from 
    801        ``request.realm``. 
    802 
    803        This method is used by 
    804 
    805        * AccessTokenEndpoint 
    806        """ 
    807        raise self._subclass_must_implement("save_access_token") 
    808 
    809    def save_request_token(self, token, request): 
    810        """Save an OAuth1 request token. 
    811 
    812        :param token: A dict with token credentials. 
    813        :param request: OAuthlib request. 
    814        :type request: oauthlib.common.Request 
    815 
    816        The token dictionary will at minimum include 
    817 
    818        * ``oauth_token`` the request token string. 
    819        * ``oauth_token_secret`` the token specific secret used in signing. 
    820        * ``oauth_callback_confirmed`` the string ``true``. 
    821 
    822        Client key can be obtained from ``request.client_key``. 
    823 
    824        This method is used by 
    825 
    826        * RequestTokenEndpoint 
    827        """ 
    828        raise self._subclass_must_implement("save_request_token") 
    829 
    830    def save_verifier(self, token, verifier, request): 
    831        """Associate an authorization verifier with a request token. 
    832 
    833        :param token: A request token string. 
    834        :param verifier: A dictionary containing the oauth_verifier and 
    835                        oauth_token 
    836        :param request: OAuthlib request. 
    837        :type request: oauthlib.common.Request 
    838 
    839        We need to associate verifiers with tokens for validation during the 
    840        access token request. 
    841 
    842        Note that unlike save_x_token token here is the ``oauth_token`` token 
    843        string from the request token saved previously. 
    844 
    845        This method is used by 
    846 
    847        * AuthorizationEndpoint 
    848        """ 
    849        raise self._subclass_must_implement("save_verifier")