Coverage for rest_framework/authentication : 80%
![](keybd_closed.png)
Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
""" Provides various authentication policies. """
""" Return request's 'Authorization:' header, as a bytestring.
Hide some test client ickyness where the header can be unicode. """ # Work around django test client oddness
""" All authentication classes should extend BaseAuthentication. """
""" Authenticate the request and return a two-tuple of (user, token). """ raise NotImplementedError(".authenticate() must be overridden.")
""" Return a string to be used as the value of the `WWW-Authenticate` header in a `401 Unauthenticated` response, or `None` if the authentication scheme should return `403 Permission Denied` responses. """
""" HTTP Basic authentication against username/password. """
""" Returns a `User` if a correct username and password have been supplied using HTTP Basic authentication. Otherwise returns `None`. """
msg = 'Invalid basic header. No credentials provided.' raise exceptions.AuthenticationFailed(msg) msg = 'Invalid basic header. Credentials string should not contain spaces.' raise exceptions.AuthenticationFailed(msg)
except (TypeError, UnicodeDecodeError): msg = 'Invalid basic header. Credentials not correctly base64 encoded' raise exceptions.AuthenticationFailed(msg)
""" Authenticate the userid and password against username and password. """ raise exceptions.AuthenticationFailed('Invalid username/password')
""" Use Django's session framework for authentication. """
""" Returns a `User` if the request session currently has a logged in user. Otherwise returns `None`. """
# Get the underlying HttpRequest object
# Unauthenticated, CSRF validation not required
# Enforce CSRF validation for session based authentication. # Return the failure reason instead of an HttpResponse
# CSRF failed, bail with explicit error message
# CSRF passed with authenticated user
""" Simple token based authentication.
Clients should authenticate by passing the token key in the "Authorization" HTTP header, prepended with the string "Token ". For example:
Authorization: Token 401f7ac837da42b97f613d789819ff93537bee6a """
""" A custom token model may be used, but must have the following properties.
* key -- The string identifying the token * user -- The user to which the token belongs """
msg = 'Invalid token header. No credentials provided.' raise exceptions.AuthenticationFailed(msg) msg = 'Invalid token header. Token string should not contain spaces.' raise exceptions.AuthenticationFailed(msg)
except self.model.DoesNotExist: raise exceptions.AuthenticationFailed('Invalid token')
raise exceptions.AuthenticationFailed('User inactive or deleted')
""" OAuth 1.0a authentication backend using `django-oauth-plus` and `oauth2`.
Note: The `oauth2` package actually provides oauth1.0a support. Urg. We import it from the `compat` module as `oauth`. """
raise ImproperlyConfigured( "The 'oauth2' package could not be imported." "It is required for use with the 'OAuthAuthentication' class.")
raise ImproperlyConfigured( "The 'django-oauth-plus' package could not be imported." "It is required for use with the 'OAuthAuthentication' class.")
""" Returns two-tuple of (user, token) if authentication succeeds, or None otherwise. """ except oauth.Error as err: raise exceptions.AuthenticationFailed(err.message)
return None
# OAuth authentication was not attempted. return None
# OAuth was attempted but missing parameters. msg = 'Missing parameters: %s' % (', '.join(missing)) raise exceptions.AuthenticationFailed(msg)
except oauth.Error as err: raise exceptions.AuthenticationFailed(err.message)
msg = 'User inactive or deleted: %s' % user.username raise exceptions.AuthenticationFailed(msg)
""" If permission is denied, return a '401 Unauthorized' response, with an appropraite 'WWW-Authenticate' header. """
""" Check the token and raise an `oauth.Error` exception if invalid. """
""" Checks nonce of request, and return True if valid. """
""" OAuth 2 authentication backend using `django-oauth2-provider` """
raise ImproperlyConfigured( "The 'django-oauth2-provider' package could not be imported. " "It is required for use with the 'OAuth2Authentication' class.")
""" Returns two-tuple of (user, token) if authentication succeeds, or None otherwise. """
msg = 'Invalid bearer header. No credentials provided.' raise exceptions.AuthenticationFailed(msg)
""" Authenticate the request, given the access token. """
# TODO: Change to timezone aware datetime when oauth2_provider add # support to it.
msg = 'User inactive or deleted: %s' % user.username raise exceptions.AuthenticationFailed(msg)
""" Bearer is the only finalized type currently
Check details on the `OAuth2Authentication.authenticate` method """ |