@@ -75,18 +75,24 @@ async def _saml_login(self, request):
75
75
self .log .error ('Error when processing SAML response: %s' % ', ' .join (errors ))
76
76
else :
77
77
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 :
82
84
# 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 )
84
88
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 ))
86
92
else :
87
93
self .log .error ('No NameID or username attribute provided in SAML response.' )
88
94
else :
89
- self .log .warn ('Not authenticated.' )
95
+ self .log .warn ('SAML request not authenticated.' )
90
96
91
97
@staticmethod
92
98
def _get_saml_login_username (saml_auth ):
@@ -96,6 +102,12 @@ def _get_saml_login_username(saml_auth):
96
102
name_id = saml_auth .get_nameid ()
97
103
if name_id :
98
104
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."""
99
111
attributes = saml_auth .get_attributes ()
100
112
username_attr_list = attributes .get ('username' , [])
101
113
return username_attr_list [0 ] if len (username_attr_list ) > 0 else None
0 commit comments