keystone.oauth1.validator module

oAuthlib request validator.

class keystone.oauth1.validator.OAuthValidator[source]

Bases: ProviderAPIMixin, RequestValidator

check_access_token(access_token)[source]

Checks that the token contains only safe characters and is no shorter than lower and no longer than upper.

check_client_key(client_key)[source]

Check that the client key only contains safe characters and is no shorter than lower and no longer than upper.

check_nonce(nonce)[source]

Checks that the nonce only contains only safe characters and is no shorter than lower and no longer than upper.

check_request_token(request_token)[source]

Checks that the request token contains only safe characters and is no shorter than lower and no longer than upper.

check_verifier(verifier)[source]

Checks that the verifier contains only safe characters and is no shorter than lower and no longer than upper.

property enforce_ssl
get_access_token_secret(client_key, token, request)[source]

Retrieves the shared secret associated with the access token.

Parameters:
  • client_key – The client/consumer key.

  • token – The access token string.

  • request (oauthlib.common.Request) – OAuthlib request.

Returns:

The token secret as a string.

This method must allow the use of a dummy values and the running time must be roughly equivalent to that of the running time of valid values:

# Unlikely to be near constant time as it uses two database
# lookups for a valid client, and only one for an invalid.
from your_datastore import AccessTokenSecret
if AccessTokenSecret.has(client_key):
    return AccessTokenSecret.get((client_key, request_token))
else:
    return 'dummy'

# Aim to mimic number of latency inducing operations no matter
# whether the client is valid or not.
from your_datastore import AccessTokenSecret
return ClientSecret.get((client_key, request_token), 'dummy')

Note that the returned key must be in plaintext.

This method is used by

  • ResourceEndpoint

get_client_secret(client_key, request)[source]

Retrieves the client secret associated with the client key.

Parameters:
  • client_key – The client/consumer key.

  • request (oauthlib.common.Request) – OAuthlib request.

Returns:

The client secret as a string.

This method must allow the use of a dummy client_key value. Fetching the secret using the dummy key must take the same amount of time as fetching a secret for a valid client:

# Unlikely to be near constant time as it uses two database
# lookups for a valid client, and only one for an invalid.
from your_datastore import ClientSecret
if ClientSecret.has(client_key):
    return ClientSecret.get(client_key)
else:
    return 'dummy'

# Aim to mimic number of latency inducing operations no matter
# whether the client is valid or not.
from your_datastore import ClientSecret
return ClientSecret.get(client_key, 'dummy')

Note that the returned key must be in plaintext.

This method is used by

  • AccessTokenEndpoint

  • RequestTokenEndpoint

  • ResourceEndpoint

  • SignatureOnlyEndpoint

get_default_realms(client_key, request)[source]

Get the default realms for a client.

Parameters:
  • client_key – The client/consumer key.

  • request (oauthlib.common.Request) – OAuthlib request.

Returns:

The list of default realms associated with the client.

The list of default realms will be set during client registration and is outside the scope of OAuthLib.

This method is used by

  • RequestTokenEndpoint

get_realms(token, request)[source]

Get realms associated with a request token.

Parameters:
  • token – The request token string.

  • request (oauthlib.common.Request) – OAuthlib request.

Returns:

The list of realms associated with the request token.

This method is used by

  • AuthorizationEndpoint

  • AccessTokenEndpoint

get_redirect_uri(token, request)[source]

Get the redirect URI associated with a request token.

Parameters:
  • token – The request token string.

  • request (oauthlib.common.Request) – OAuthlib request.

Returns:

The redirect URI associated with the request token.

It may be desirable to return a custom URI if the redirect is set to “oob”. In this case, the user will be redirected to the returned URI and at that endpoint the verifier can be displayed.

This method is used by

  • AuthorizationEndpoint

get_request_token_secret(client_key, token, request)[source]

Retrieves the shared secret associated with the request token.

Parameters:
  • client_key – The client/consumer key.

  • token – The request token string.

  • request (oauthlib.common.Request) – OAuthlib request.

Returns:

The token secret as a string.

This method must allow the use of a dummy values and the running time must be roughly equivalent to that of the running time of valid values:

# Unlikely to be near constant time as it uses two database
# lookups for a valid client, and only one for an invalid.
from your_datastore import RequestTokenSecret
if RequestTokenSecret.has(client_key):
    return RequestTokenSecret.get((client_key, request_token))
else:
    return 'dummy'

# Aim to mimic number of latency inducing operations no matter
# whether the client is valid or not.
from your_datastore import RequestTokenSecret
return ClientSecret.get((client_key, request_token), 'dummy')

Note that the returned key must be in plaintext.

This method is used by

  • AccessTokenEndpoint

get_rsa_key(client_key, request)[source]

Retrieves a previously stored client provided RSA key.

Parameters:
  • client_key – The client/consumer key.

  • request (oauthlib.common.Request) – OAuthlib request.

Returns:

The rsa public key as a string.

This method must allow the use of a dummy client_key value. Fetching the rsa key using the dummy key must take the same amount of time as fetching a key for a valid client. The dummy key must also be of the same bit length as client keys.

Note that the key must be returned in plaintext.

This method is used by

  • AccessTokenEndpoint

  • RequestTokenEndpoint

  • ResourceEndpoint

  • SignatureOnlyEndpoint

invalidate_request_token(client_key, request_token, request)[source]

Invalidate a used request token.

Parameters:
  • client_key – The client/consumer key.

  • request_token – The request token string.

  • request – An oauthlib.common.Request object.

Returns:

None

Per Section 2.3 of the spec:

“The server MUST (…) ensure that the temporary credentials have not expired or been used before.”

This method should ensure that provided token won’t validate anymore. It can be simply removing RequestToken from storage or setting specific flag that makes it invalid (note that such flag should be also validated during request token validation).

This method is used by

  • AccessTokenEndpoint

property safe_characters
save_access_token(token, request)[source]

Save an OAuth1 access token.

Parameters:
  • token – A dict with token credentials.

  • request (oauthlib.common.Request) – OAuthlib request.

The token dictionary will at minimum include

  • oauth_token the access token string.

  • oauth_token_secret the token specific secret used in signing.

  • oauth_authorized_realms a space separated list of realms.

Client key can be obtained from request.client_key.

The list of realms (not joined string) can be obtained from request.realm.

This method is used by

  • AccessTokenEndpoint

save_request_token(token, request)[source]

Save an OAuth1 request token.

Parameters:
  • token – A dict with token credentials.

  • request (oauthlib.common.Request) – OAuthlib request.

The token dictionary will at minimum include

  • oauth_token the request token string.

  • oauth_token_secret the token specific secret used in signing.

  • oauth_callback_confirmed the string true.

Client key can be obtained from request.client_key.

This method is used by

  • RequestTokenEndpoint

save_verifier(token, verifier, request)[source]

Associate an authorization verifier with a request token.

Parameters:
  • token – A request token string.

  • verifier – A dictionary containing the oauth_verifier and oauth_token

  • request – An oauthlib.common.Request object.

We need to associate verifiers with tokens for validation during the access token request.

Note that unlike save_x_token token here is the oauth_token token string from the request token saved previously.

This method is used by

  • AuthorizationEndpoint

validate_access_token(client_key, token, request)[source]

Validates that supplied access token is registered and valid.

Parameters:
  • client_key – The client/consumer key.

  • token – The access token string.

  • request (oauthlib.common.Request) – OAuthlib request.

Returns:

True or False

Note that if the dummy access token is supplied it should validate in the same or nearly the same amount of time as a valid one.

Ensure latency inducing tasks are mimiced even for dummy clients. For example, use:

from your_datastore import AccessToken
try:
    return AccessToken.exists(client_key, access_token)
except DoesNotExist:
    return False

Rather than:

from your_datastore import AccessToken
if access_token == self.dummy_access_token:
    return False
else:
    return AccessToken.exists(client_key, access_token)

This method is used by

  • ResourceEndpoint

validate_client_key(client_key, request)[source]

Validates that supplied client key is a registered and valid client.

Parameters:
  • client_key – The client/consumer key.

  • request (oauthlib.common.Request) – OAuthlib request.

Returns:

True or False

Note that if the dummy client is supplied it should validate in same or nearly the same amount of time as a valid one.

Ensure latency inducing tasks are mimiced even for dummy clients. For example, use:

from your_datastore import Client
try:
    return Client.exists(client_key, access_token)
except DoesNotExist:
    return False

Rather than:

from your_datastore import Client
if access_token == self.dummy_access_token:
    return False
else:
    return Client.exists(client_key, access_token)

This method is used by

  • AccessTokenEndpoint

  • RequestTokenEndpoint

  • ResourceEndpoint

  • SignatureOnlyEndpoint

validate_realms(client_key, token, request, uri=None, realms=None)[source]

Validates access to the request realm.

Parameters:
  • client_key – The client/consumer key.

  • token – A request token string.

  • request (oauthlib.common.Request) – OAuthlib request.

  • uri – The URI the realms is protecting.

  • realms – A list of realms that must have been granted to the access token.

Returns:

True or False

How providers choose to use the realm parameter is outside the OAuth specification but it is commonly used to restrict access to a subset of protected resources such as “photos”.

realms is a convenience parameter which can be used to provide a per view method pre-defined list of allowed realms.

Can be as simple as:

from your_datastore import RequestToken
request_token = RequestToken.get(token, None)

if not request_token:
    return False
return set(request_token.realms).issuperset(set(realms))

This method is used by

  • ResourceEndpoint

validate_redirect_uri(client_key, redirect_uri, request)[source]

Validates the client supplied redirection URI.

Parameters:
  • client_key – The client/consumer key.

  • redirect_uri – The URI the client which to redirect back to after authorization is successful.

  • request (oauthlib.common.Request) – OAuthlib request.

Returns:

True or False

It is highly recommended that OAuth providers require their clients to register all redirection URIs prior to using them in requests and register them as absolute URIs. See CWE-601 for more information about open redirection attacks.

By requiring registration of all redirection URIs it should be straightforward for the provider to verify whether the supplied redirect_uri is valid or not.

Alternatively per Section 2.1 of the spec:

“If the client is unable to receive callbacks or a callback URI has been established via other means, the parameter value MUST be set to “oob” (case sensitive), to indicate an out-of-band configuration.”

This method is used by

  • RequestTokenEndpoint

validate_request_token(client_key, token, request)[source]

Validates that supplied request token is registered and valid.

Parameters:
  • client_key – The client/consumer key.

  • token – The request token string.

  • request (oauthlib.common.Request) – OAuthlib request.

Returns:

True or False

Note that if the dummy request_token is supplied it should validate in the same nearly the same amount of time as a valid one.

Ensure latency inducing tasks are mimiced even for dummy clients. For example, use:

from your_datastore import RequestToken
try:
    return RequestToken.exists(client_key, access_token)
except DoesNotExist:
    return False

Rather than:

from your_datastore import RequestToken
if access_token == self.dummy_access_token:
    return False
else:
    return RequestToken.exists(client_key, access_token)

This method is used by

  • AccessTokenEndpoint

validate_requested_realms(client_key, realms, request)[source]

Validates that the client may request access to the realm.

Parameters:
  • client_key – The client/consumer key.

  • realms – The list of realms that client is requesting access to.

  • request (oauthlib.common.Request) – OAuthlib request.

Returns:

True or False

This method is invoked when obtaining a request token and should tie a realm to the request token and after user authorization this realm restriction should transfer to the access token.

This method is used by

  • RequestTokenEndpoint

validate_timestamp_and_nonce(client_key, timestamp, nonce, request, request_token=None, access_token=None)[source]

Validates that the nonce has not been used before.

Parameters:
  • client_key – The client/consumer key.

  • timestamp – The oauth_timestamp parameter.

  • nonce – The oauth_nonce parameter.

  • request_token – Request token string, if any.

  • access_token – Access token string, if any.

  • request (oauthlib.common.Request) – OAuthlib request.

Returns:

True or False

Per Section 3.3 of the spec.

“A nonce is a random string, uniquely generated by the client to allow the server to verify that a request has never been made before and helps prevent replay attacks when requests are made over a non-secure channel. The nonce value MUST be unique across all requests with the same timestamp, client credentials, and token combinations.”

One of the first validation checks that will be made is for the validity of the nonce and timestamp, which are associated with a client key and possibly a token. If invalid then immediately fail the request by returning False. If the nonce/timestamp pair has been used before and you may just have detected a replay attack. Therefore it is an essential part of OAuth security that you not allow nonce/timestamp reuse. Note that this validation check is done before checking the validity of the client and token.:

nonces_and_timestamps_database = [
   (u'foo', 1234567890, u'rannoMstrInghere', u'bar')
]

def validate_timestamp_and_nonce(self, client_key, timestamp, nonce,
   request_token=None, access_token=None):

   return ((client_key, timestamp, nonce, request_token or access_token)
            not in self.nonces_and_timestamps_database)

This method is used by

  • AccessTokenEndpoint

  • RequestTokenEndpoint

  • ResourceEndpoint

  • SignatureOnlyEndpoint

validate_verifier(client_key, token, verifier, request)[source]

Validates a verification code.

Parameters:
  • client_key – The client/consumer key.

  • token – A request token string.

  • verifier – The authorization verifier string.

  • request (oauthlib.common.Request) – OAuthlib request.

Returns:

True or False

OAuth providers issue a verification code to clients after the resource owner authorizes access. This code is used by the client to obtain token credentials and the provider must verify that the verifier is valid and associated with the client as well as the resource owner.

Verifier validation should be done in near constant time (to avoid verifier enumeration). To achieve this we need a constant time string comparison which is provided by OAuthLib in oauthlib.common.safe_string_equals:

from your_datastore import Verifier
correct_verifier = Verifier.get(client_key, request_token)
from oauthlib.common import safe_string_equals
return safe_string_equals(verifier, correct_verifier)

This method is used by

  • AccessTokenEndpoint

verify_realms(token, realms, request)[source]

Verify authorized realms to see if they match those given to token.

Parameters:
  • token – An access token string.

  • realms – A list of realms the client attempts to access.

  • request (oauthlib.common.Request) – OAuthlib request.

Returns:

True or False

This prevents the list of authorized realms sent by the client during the authorization step to be altered to include realms outside what was bound with the request token.

Can be as simple as:

valid_realms = self.get_realms(token)
return all((r in valid_realms for r in realms))

This method is used by

  • AuthorizationEndpoint

verify_request_token(token, request)[source]

Verify that the given OAuth1 request token is valid.

Parameters:
  • token – A request token string.

  • request (oauthlib.common.Request) – OAuthlib request.

Returns:

True or False

This method is used only in AuthorizationEndpoint to check whether the oauth_token given in the authorization URL is valid or not. This request is not signed and thus similar validate_request_token method can not be used.

This method is used by

  • AuthorizationEndpoint