Improves error handling; fixes failing test.

Fixes #8, thanks to mrunge for the report.

Fixes #10.
This commit is contained in:
Gabriel Hurley 2012-12-09 18:39:53 -08:00
parent 8a88978154
commit f8b1be84bf
4 changed files with 15 additions and 7 deletions

View File

@ -1,2 +1,2 @@
# following PEP 386
__version__ = "1.0.4"
__version__ = "1.0.5"

View File

@ -66,7 +66,9 @@ class KeystoneBackend(object):
unscoped_token = Token(TokenManager(None),
unscoped_token_data,
loaded=True)
except keystone_exceptions.Unauthorized:
except (keystone_exceptions.Unauthorized,
keystone_exceptions.Forbidden,
keystone_exceptions.NotFound):
msg = _('Invalid user name or password.')
raise KeystoneAuthException(msg)
except (keystone_exceptions.ClientException,

View File

@ -198,5 +198,5 @@ class OpenStackAuthTests(test.TestCase):
response = self.client.get(url, form_data)
self.assertRedirects(response, settings.LOGIN_REDIRECT_URL)
self.assertEqual(self.client.session['tenant_id'],
self.assertEqual(self.client.session['token']['token']['tenant']['id'],
scoped.tenant['id'])

View File

@ -12,6 +12,7 @@ from django.views.decorators.cache import never_cache
from django.views.decorators.csrf import csrf_protect
from keystoneclient.v2_0 import client as keystone_client
from keystoneclient import exceptions as keystone_exceptions
from .forms import Login
from .user import set_session_from_user, create_user_from_token
@ -73,8 +74,13 @@ def switch(request, tenant_id):
% (tenant_id, request.user.username))
endpoint = request.user.endpoint
client = keystone_client.Client(endpoint=endpoint)
token = client.tokens.authenticate(tenant_id=tenant_id,
token=request.user.token.id)
user = create_user_from_token(request, token, endpoint)
set_session_from_user(request, user)
try:
token = client.tokens.authenticate(tenant_id=tenant_id,
token=request.user.token.id)
except keystone_exceptions.ClientException:
token = None
LOG.exception('An error occurred while switching sessions.')
if token:
user = create_user_from_token(request, token, endpoint)
set_session_from_user(request, user)
return shortcuts.redirect(settings.LOGIN_REDIRECT_URL)