From 977a5cf7be1f8a526af87661d9710a550c758287 Mon Sep 17 00:00:00 2001 From: juanifioren Date: Fri, 9 Jan 2015 14:59:23 -0300 Subject: [PATCH] Add custom template errors. (ClientID and RedirectURI) --- README.rst | 2 +- openid_provider/lib/endpoints/authorize.py | 23 ++++--------------- openid_provider/lib/endpoints/token.py | 2 +- openid_provider/lib/endpoints/userinfo.py | 2 +- openid_provider/lib/errors.py | 12 ++-------- .../templates/openid_provider/error.html | 20 ++++++++++++++++ openid_provider/views.py | 7 +++++- 7 files changed, 35 insertions(+), 33 deletions(-) create mode 100644 openid_provider/templates/openid_provider/error.html diff --git a/README.rst b/README.rst index 2347b72..0ee25f6 100644 --- a/README.rst +++ b/README.rst @@ -7,7 +7,7 @@ Django OpenID Provider Important things that you should know: - Although OpenID was built on top of OAuth2, this isn't an OAuth2 server. Maybe in a future it will be. -- This cover ``authorization_code`` flow and ``implicit`` flow, NO support for ``hibrid`` flow at this moment. +- This cover ``authorization_code`` flow and ``implicit`` flow, NO support for ``hybrid`` flow at this moment. - Only support for requesting Claims using Scope Values. ************ diff --git a/openid_provider/lib/endpoints/authorize.py b/openid_provider/lib/endpoints/authorize.py index e32e600..03c9a68 100644 --- a/openid_provider/lib/endpoints/authorize.py +++ b/openid_provider/lib/endpoints/authorize.py @@ -13,7 +13,7 @@ class AuthorizeEndpoint(object): self.request = request - self.params = Params + self.params = Params() # Because in this endpoint we handle both GET # and POST request. @@ -52,22 +52,6 @@ class AuthorizeEndpoint(object): ''' self.params.nonce = self.query_dict.get('nonce', '') - def is_code_flow(self): - ''' - True if the client is using Authorization Code Flow. - - Return a boolean. - ''' - return self.grant_type == 'authorization_code' - - def is_implicit_flow(self): - ''' - True if the client is using Implicit Flow. - - Return a boolean. - ''' - return self.grant_type == 'implicit' - def validate_params(self): if not self.params.redirect_uri: @@ -96,7 +80,7 @@ class AuthorizeEndpoint(object): try: self.validate_params() - if self.is_code_flow(): + if (self.grant_type == 'authorization_code'): code = Code() code.user = self.request.user @@ -107,7 +91,8 @@ class AuthorizeEndpoint(object): code.save() uri = self.params.redirect_uri + '?code={0}'.format(code.code) - else: + + else: # Implicit Flow id_token_dic = create_id_token_dic( self.request.user, diff --git a/openid_provider/lib/endpoints/token.py b/openid_provider/lib/endpoints/token.py index b052ce8..59661e6 100644 --- a/openid_provider/lib/endpoints/token.py +++ b/openid_provider/lib/endpoints/token.py @@ -11,7 +11,7 @@ class TokenEndpoint(object): def __init__(self, request): self.request = request - self.params = Params + self.params = Params() self._extract_params() def _extract_params(self): diff --git a/openid_provider/lib/endpoints/userinfo.py b/openid_provider/lib/endpoints/userinfo.py index 7e3a40a..caa8723 100644 --- a/openid_provider/lib/endpoints/userinfo.py +++ b/openid_provider/lib/endpoints/userinfo.py @@ -11,7 +11,7 @@ class UserInfoEndpoint(object): def __init__(self, request): self.request = request - self.params = Params + self.params = Params() self._extract_params() def _extract_params(self): diff --git a/openid_provider/lib/errors.py b/openid_provider/lib/errors.py index 263c8e2..08d5627 100644 --- a/openid_provider/lib/errors.py +++ b/openid_provider/lib/errors.py @@ -3,21 +3,14 @@ import urllib class RedirectUriError(Exception): - error = None + error = 'Redirect URI Error' description = 'The request fails due to a missing, invalid, or mismatching redirection URI (redirect_uri).' - class ClientIdError(Exception): - error = None + error = 'Client ID Error' description = 'The client identifier (client_id) is missing or invalid.' -class MissingScopeError(Exception): - - error = 'openid scope' - description = 'The openid scope value is missing.' - - class AuthorizeError(Exception): _errors = { @@ -72,7 +65,6 @@ class AuthorizeError(Exception): def response(self): pass - class TokenError(Exception): _errors = { diff --git a/openid_provider/templates/openid_provider/error.html b/openid_provider/templates/openid_provider/error.html new file mode 100644 index 0000000..b5288eb --- /dev/null +++ b/openid_provider/templates/openid_provider/error.html @@ -0,0 +1,20 @@ +{% extends "openid_provider/base.html" %} + +{% load i18n %} + +{% block content %} + +
+
+
+
+

{{ error }}

+
+
+

{{ description }}

+
+
+
+
+ +{% endblock %} \ No newline at end of file diff --git a/openid_provider/views.py b/openid_provider/views.py index 51a7d38..6f23f8c 100644 --- a/openid_provider/views.py +++ b/openid_provider/views.py @@ -34,7 +34,12 @@ class AuthorizeView(View): return HttpResponseRedirect(login_url) except (ClientIdError, RedirectUriError) as error: - return HttpResponse(error.description) + data = { + 'error': error.error, + 'description': error.description, + } + + return render(request, 'openid_provider/error.html', data) except (AuthorizeError) as error: uri = error.create_uri(authorize.params.redirect_uri, authorize.params.state)