Skip to content

Commit dfb2896

Browse files
committed
Log Identity Provider usernames
1 parent a3f3644 commit dfb2896

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

app/calderasaml_svc.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,24 @@ async def _saml_login(self, request):
7575
self.log.error('Error when processing SAML response: %s' % ', '.join(errors))
7676
else:
7777
if saml_auth.is_authenticated():
78-
username = self._get_saml_login_username(saml_auth)
79-
self.log.debug('SAML provided username: %s' % username)
80-
if username:
81-
if username in self.auth_svc.user_map:
78+
app_username = self._get_saml_login_username(saml_auth)
79+
username_attr = self._get_saml_username_attribute(saml_auth)
80+
self.log.debug('SAML provided application username: %s' % app_username)
81+
self.log.debug('SAML provided username attribute: %s' % username_attr)
82+
if app_username:
83+
if app_username in self.auth_svc.user_map:
8284
# Will raise redirect on success
83-
await self.auth_svc.provide_verified_login_response(request, username)
85+
self.log.info('User "%s" authenticated via SAML under application user "%s"' %
86+
(username_attr, app_username))
87+
await self.auth_svc.provide_verified_login_response(request, app_username)
8488
else:
85-
self.log.warn('Username %s not configured for login' % username)
89+
self.log.warn('Application username "%s" not configured for login' % app_username)
90+
self.log.info('User "%s" failed to authenticate via SAML under application user "%s"' %
91+
(username_attr, app_username))
8692
else:
8793
self.log.error('No NameID or username attribute provided in SAML response.')
8894
else:
89-
self.log.warn('Not authenticated.')
95+
self.log.warn('SAML request not authenticated.')
9096

9197
@staticmethod
9298
def _get_saml_login_username(saml_auth):
@@ -96,6 +102,12 @@ def _get_saml_login_username(saml_auth):
96102
name_id = saml_auth.get_nameid()
97103
if name_id:
98104
return name_id
105+
return CalderaSamlService._get_saml_username_attribute(saml_auth)
106+
107+
@staticmethod
108+
def _get_saml_username_attribute(saml_auth):
109+
"""Returns the "username" attribute for the SAML request. This should be the username
110+
for the identity provider, not necessarily the username for the application."""
99111
attributes = saml_auth.get_attributes()
100112
username_attr_list = attributes.get('username', [])
101113
return username_attr_list[0] if len(username_attr_list) > 0 else None

0 commit comments

Comments
 (0)