Skip to content

Commit

Permalink
Merge "Expose auth failure details in debug mode"
Browse files Browse the repository at this point in the history
  • Loading branch information
Jenkins authored and openstack-gerrit committed Nov 20, 2012
2 parents d8aa7fd + 01fccdb commit 07c1aaf
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 37 deletions.
4 changes: 2 additions & 2 deletions keystone/common/wsgi.py
Expand Up @@ -246,8 +246,8 @@ def assert_admin(self, context):
try:
user_token_ref = self.token_api.get_token(
context=context, token_id=context['token_id'])
except exception.TokenNotFound:
raise exception.Unauthorized()
except exception.TokenNotFound as e:
raise exception.Unauthorized(e)

creds = user_token_ref['metadata'].copy()

Expand Down
12 changes: 6 additions & 6 deletions keystone/contrib/ec2/core.py
Expand Up @@ -294,11 +294,11 @@ def _assert_identity(self, context, user_id):
token_ref = self.token_api.get_token(
context=context,
token_id=context['token_id'])
except exception.TokenNotFound:
raise exception.Unauthorized()
token_user_id = token_ref['user'].get('id')
if not token_user_id == user_id:
raise exception.Forbidden()
except exception.TokenNotFound as e:
raise exception.Unauthorized(e)

if token_ref['user'].get('id') != user_id:
raise exception.Forbidden('Token belongs to another user')

def _is_admin(self, context):
"""Wrap admin assertion error return statement.
Expand All @@ -324,7 +324,7 @@ def _assert_owner(self, context, user_id, credential_id):
"""
cred_ref = self.ec2_api.get_credential(context, credential_id)
if not user_id == cred_ref['user_id']:
raise exception.Forbidden()
raise exception.Forbidden('Credential belongs to another user')

def _assert_valid_user_id(self, context, user_id):
"""Ensure a valid user id.
Expand Down
2 changes: 1 addition & 1 deletion keystone/contrib/s3/core.py
Expand Up @@ -54,4 +54,4 @@ def check_signature(self, creds_ref, credentials):
signed = base64.encodestring(hmac.new(key, msg, sha1).digest()).strip()

if not utils.auth_str_equal(credentials['signature'], signed):
raise exception.Unauthorized()
raise exception.Unauthorized('Credential signature mismatch')
10 changes: 7 additions & 3 deletions keystone/contrib/user_crud/core.py
Expand Up @@ -42,16 +42,20 @@ def set_user_password(self, context, user_id, user):
token_id=token_id)
user_id_from_token = token_ref['user']['id']

if user_id_from_token != user_id or original_password is None:
raise exception.Forbidden()
if user_id_from_token != user_id:
raise exception.Forbidden('Token belongs to another user')
if original_password is None:
raise exception.ValidationError(target='user',
attribute='original password')

try:
user_ref = self.identity_api.authenticate(
context=context,
user_id=user_id_from_token,
password=original_password)[0]
if not user_ref.get('enabled', True):
raise exception.Unauthorized()
# NOTE(dolph): why can't you set a disabled user's password?
raise exception.Unauthorized('User is disabled')
except AssertionError:
raise exception.Unauthorized()

Expand Down
7 changes: 3 additions & 4 deletions keystone/identity/core.py
Expand Up @@ -513,10 +513,9 @@ def get_tenants_for_token(self, context, **kw):
try:
token_ref = self.token_api.get_token(context=context,
token_id=context['token_id'])
except exception.NotFound:
LOG.warning("Authentication failed. Could not find token " +
str(context['token_id']))
raise exception.Unauthorized()
except exception.NotFound as e:
LOG.warning('Authentication failed: %s' % e)
raise exception.Unauthorized(e)

user_ref = token_ref['user']
tenant_ids = self.identity_api.get_tenants_for_user(
Expand Down
42 changes: 21 additions & 21 deletions keystone/service.py
Expand Up @@ -484,13 +484,15 @@ def authenticate(self, context, auth=None):

# If the user is disabled don't allow them to authenticate
if not user_ref.get('enabled', True):
LOG.warning('User %s is disabled' % user_ref["id"])
raise exception.Unauthorized()
msg = 'User is disabled: %s' % user_ref['id']
LOG.warning(msg)
raise exception.Unauthorized(msg)

# If the tenant is disabled don't allow them to authenticate
if tenant_ref and not tenant_ref.get('enabled', True):
LOG.warning('Tenant %s is disabled' % tenant_ref["id"])
raise exception.Unauthorized()
msg = 'Tenant is disabled: %s' % tenant_ref['id']
LOG.warning(msg)
raise exception.Unauthorized(msg)

if tenant_ref:
catalog_ref = self.catalog_api.get_catalog(
Expand Down Expand Up @@ -562,9 +564,8 @@ def _authenticate_token(self, context, auth):
try:
old_token_ref = self.token_api.get_token(context=context,
token_id=old_token)
except exception.NotFound:
LOG.warning("Token not found: " + str(old_token))
raise exception.Unauthorized()
except exception.NotFound as e:
raise exception.Unauthorized(e)

user_ref = old_token_ref['user']
user_id = user_ref['id']
Expand Down Expand Up @@ -614,9 +615,8 @@ def _authenticate_local(self, context, auth):
user_ref = self.identity_api.get_user_by_name(
context=context, user_name=username)
user_id = user_ref['id']
except exception.UserNotFound:
LOG.warn("User not found: %s" % user_id)
raise exception.Unauthorized()
except exception.UserNotFound as e:
raise exception.Unauthorized(e)

tenant_id = self._get_tenant_id_from_auth(context, auth)

Expand All @@ -627,7 +627,7 @@ def _authenticate_local(self, context, auth):
password=password,
tenant_id=tenant_id)
except AssertionError as e:
raise exception.Unauthorized(str(e))
raise exception.Unauthorized(e)
(user_ref, tenant_ref, metadata_ref) = auth_info

expiry = self.token_api._get_default_expire_time(context=context)
Expand All @@ -651,9 +651,8 @@ def _authenticate_external(self, context, auth):
user_ref = self.identity_api.get_user_by_name(
context=context, user_name=username)
user_id = user_ref['id']
except exception.UserNotFound:
LOG.warn("User not found: %s" % username)
raise exception.Unauthorized()
except exception.UserNotFound as e:
raise exception.Unauthorized(e)

tenant_id = self._get_tenant_id_from_auth(context, auth)

Expand Down Expand Up @@ -686,8 +685,8 @@ def _get_tenant_id_from_auth(self, context, auth):
tenant_ref = self.identity_api.get_tenant_by_name(
context=context, tenant_name=tenant_name)
tenant_id = tenant_ref['id']
except exception.TenantNotFound:
raise exception.Unauthorized()
except exception.TenantNotFound as e:
raise exception.Unauthorized(e)
return tenant_id

def _get_tenant_ref(self, context, user_id, tenant_id):
Expand All @@ -696,15 +695,16 @@ def _get_tenant_ref(self, context, user_id, tenant_id):
if tenant_id:
tenants = self.identity_api.get_tenants_for_user(context, user_id)
if tenant_id not in tenants:
LOG.warning('User %s is unauthorized for tenant %s'
% (user_id, tenant_id))
raise exception.Unauthorized()
msg = 'User %s is unauthorized for tenant %s' % (
user_id, tenant_id)
LOG.warning(msg)
raise exception.Unauthorized(msg)

try:
tenant_ref = self.identity_api.get_tenant(context=context,
tenant_id=tenant_id)
except exception.TenantNotFound:
exception.Unauthorized()
except exception.TenantNotFound as e:
exception.Unauthorized(e)
return tenant_ref

def _get_metadata_ref(self, context, user_id, tenant_id):
Expand Down

0 comments on commit 07c1aaf

Please sign in to comment.