Skip to content

Commit 325b99a

Browse files
author
Dennis Coorn
committed
Use username mapper to resolve username when creating a default user
* Username mapper is being used for resolving the username when a default user should be created * Two small adjustment to the LightsSamlSpAuthenticationProviderTest to make sure the username mapper is being called without breaking other behavior
1 parent 2cb00e7 commit 325b99a

2 files changed

Lines changed: 11 additions & 30 deletions

File tree

src/LightSaml/SpBundle/Security/Authentication/Provider/LightsSamlSpAuthenticationProvider.php

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111

1212
namespace LightSaml\SpBundle\Security\Authentication\Provider;
1313

14-
use LightSaml\ClaimTypes;
15-
use LightSaml\SamlConstants;
1614
use LightSaml\SpBundle\Security\Authentication\Token\SamlSpResponseToken;
1715
use LightSaml\SpBundle\Security\Authentication\Token\SamlSpToken;
1816
use LightSaml\SpBundle\Security\Authentication\Token\SamlSpTokenFactoryInterface;
@@ -207,36 +205,11 @@ private function createUser(SamlSpResponseToken $token)
207205
*/
208206
private function createDefaultUser(SamlSpResponseToken $token)
209207
{
210-
if ($token->getResponse()->getFirstAssertion() &&
211-
$token->getResponse()->getFirstAssertion()->getSubject() &&
212-
$token->getResponse()->getFirstAssertion()->getSubject()->getNameID() &&
213-
$token->getResponse()->getFirstAssertion()->getSubject()->getNameID()->getFormat() != SamlConstants::NAME_ID_FORMAT_TRANSIENT &&
214-
$token->getResponse()->getFirstAssertion()->getSubject()->getNameID()->getValue()
215-
) {
216-
return $token->getResponse()->getFirstAssertion()->getSubject()->getNameID()->getValue();
217-
}
218-
219-
if ($token->getResponse()->getFirstAssertion() &&
220-
$attributeStatement = $token->getResponse()->getFirstAssertion()->getFirstAttributeStatement()
221-
) {
222-
$names = [
223-
ClaimTypes::COMMON_NAME,
224-
ClaimTypes::EMAIL_ADDRESS,
225-
ClaimTypes::WINDOWS_ACCOUNT_NAME,
226-
ClaimTypes::ADFS_1_EMAIL,
227-
ClaimTypes::UPN,
228-
ClaimTypes::ADFS_1_UPN,
229-
];
230-
231-
foreach ($names as $name) {
232-
$attribute = $attributeStatement->getFirstAttributeByName($name);
233-
if ($attribute && $attribute->getFirstAttributeValue()) {
234-
return $attribute->getFirstAttributeValue();
235-
}
236-
}
208+
if (null === $this->usernameMapper) {
209+
return null;
237210
}
238211

239-
return null;
212+
return $this->usernameMapper->getUsername($token->getResponse());
240213
}
241214

242215
/**

tests/LightSaml/SpBundle/Tests/Security/Authentication/Provider/LightsSamlSpAuthenticationProviderTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,10 @@ public function test_creates_authenticated_token_with_default_user_from_name_id_
189189
)
190190
);
191191

192+
$usernameMapperMock->expects($this->exactly(2))
193+
->method('getUsername')
194+
->willReturn($nameIdValue);
195+
192196
$authenticatedToken = $provider->authenticate($token);
193197

194198
$this->assertTrue($authenticatedToken->isAuthenticated());
@@ -226,6 +230,10 @@ public function test_creates_authenticated_token_with_default_user_from_attribut
226230
)
227231
);
228232

233+
$usernameMapperMock->expects($this->exactly(2))
234+
->method('getUsername')
235+
->willReturn($email);
236+
229237
$authenticatedToken = $provider->authenticate($token);
230238

231239
$this->assertTrue($authenticatedToken->isAuthenticated());

0 commit comments

Comments
 (0)