4242use SimpleSAML \Module \oidc \Utils \ClaimTranslatorExtractor ;
4343use SimpleSAML \Module \oidc \Utils \RequestParamsResolver ;
4444use SimpleSAML \Module \oidc \Utils \Routes ;
45+ use SimpleSAML \Module \oidc \Utils \UserIdentifierResolver ;
4546
4647class AuthenticationService
4748{
@@ -51,9 +52,10 @@ class AuthenticationService
5152 private ?string $ authSourceId = null ;
5253
5354 /**
54- * @var string
55+ * Ordered list of candidate user identifier attributes.
56+ * @var string[]
5557 */
56- private string $ userIdAttr ;
58+ private array $ userIdAttrs ;
5759
5860 /**
5961 * @throws \Exception
@@ -71,8 +73,9 @@ public function __construct(
7173 private readonly RequestParamsResolver $ requestParamsResolver ,
7274 private readonly UserEntityFactory $ userEntityFactory ,
7375 private readonly Routes $ routes ,
76+ private readonly UserIdentifierResolver $ userIdentifierResolver ,
7477 ) {
75- $ this ->userIdAttr = $ this ->moduleConfig ->getUserIdentifierAttribute ();
78+ $ this ->userIdAttrs = $ this ->moduleConfig ->getUserIdentifierAttributes ();
7679 }
7780
7881 /**
@@ -136,20 +139,19 @@ public function getAuthenticateUser(
136139
137140 $ claims = $ state ['Attributes ' ];
138141
139- if (!array_key_exists ($ this ->userIdAttr , $ claims ) || !is_array ($ claims [$ this ->userIdAttr ])) {
140- $ attr = implode (', ' , array_keys ($ claims ));
142+ $ userId = $ this ->userIdentifierResolver ->resolve ($ this ->userIdAttrs , $ claims );
143+
144+ if ($ userId === null ) {
141145 throw new Error \Exception (
142146 sprintf (
143- 'User identifier attribute `%s` does not exist in the user attribute state. ' .
147+ 'None of the configured user identifier attributes (%s) exist in the user attribute state. ' .
144148 ' Available attributes are: %s. ' ,
145- $ this ->userIdAttr ,
146- $ attr ,
149+ implode ( ' , ' , $ this ->userIdAttrs ) ,
150+ implode ( ' , ' , array_keys ( $ claims )) ,
147151 ),
148152 );
149153 }
150154
151- $ userId = (string )$ claims [$ this ->userIdAttr ][0 ];
152-
153155 $ user = $ this ->userRepository ->getUserEntityByIdentifier ($ userId );
154156
155157 if ($ user ) {
0 commit comments