medmij_oauth.exceptions module¶
Module for handling OAuth related errors as specified in rfc6749
OAuthException¶
- exception medmij_oauth.exceptions.OAuthException(error_code, error_description='', redirect=False, base_redirect_url='')[source]¶
OAuthException class, represents a oauth error as described in rfc6749
- Parameters
error_code (
error code
) – Int that represents a type of errorerror_description (string) – Human readable description of the error e.g. ‘no such resource’
redirect (bool) – Indication if on handling of the exception the user should be redirected back to the client application of if the error should be rendered to the screen
base_redirect_url (string (optional)) – The base of the redirect url (on redirection the error params are appended to the base_redirect_url as a query string)
Usage examples:
raise OAuthException(ERRORS.INVALID_REQUEST, error_description='Invalid redirect url', redirect=False)
raise OAuthException(ERRORS.UNAUTHORIZED_CLIENT, error_description='No such resource', redirect=True, base_redirect_url='https://oauthclient.com')
- get_dict()[source]¶
Return dict representation of the exception that is targeted at the end user. Included properties are ‘error’ and ‘error_description’.
- get_json()[source]¶
Return json representation of the exception that is targeted at the end user Included properties are ‘error’ and ‘error_description’
{ 'error': 'unauthorized_client', 'error_description': 'no such resource' }
- get_redirect_url()[source]¶
Return redirect url to which the end user should be redirected. The redirect_url constists of two parts, self.base_redirect_url and a query string that contains the error and error description
Raises a Exception if self.direct != True or if self.base_redirect_url is not set.
e.g.
https://oauthclient.com/cb/?error=unauthorized_client&error_description=No%20such%20resource
Error codes¶
- medmij_oauth.exceptions.lookup_error_code(error)[source]¶
Lookup error code by text. When an oauth client receives a error response, it can reproduce the exception by looking up the error code with the ‘error’ query param that it received.
Raises a ValueError if the error passed to it is unknown.
Example:
error = query_params.get('error') error_description = query_params.get('error_description') raise OAuthException(error_code=lookup_error_code(error), error_description=error_description)
- class medmij_oauth.exceptions.ERRORS(value)[source]¶
Error codes enum to be used as error_code for instantiation of OAuthException
Usage example:
raise OAuthException(ERRORS.UNAUTHORIZED_CLIENT, 'no such resource', ...)
- ACCESS_DENIED = 2¶
- INVALID_CLIENT = 8¶
- INVALID_GRANT = 9¶
- INVALID_REQUEST = 1¶
- INVALID_SCOPE = 5¶
- SERVER_ERROR = 6¶
- TEMPORARILY_UNAVAILABLE = 7¶
- UNAUTHORIZED_CLIENT = 3¶
- UNSUPPORTED_GRANT_TYPE = 10¶
- UNSUPPORTED_RESPONSE_TYPE = 4¶