$attributes */ public function __construct( private array $attributes ) { } public function getUser(Response $response): User { $user = new User(); foreach ($response->getAllAssertions() as $assertion) { $this->setValuesFromAssertion($assertion, $user); } return $user; } public function getUsername(Response $response): ?string { $user = $this->getUser($response); return $user->getUserIdentifier(); } private function setValuesFromAssertion(Assertion $assertion, User $user): void { $attributes = $this->extractAttributes($assertion); // use email as the user by default if (isset($attributes['email'])) { $user->setEmail($attributes['email']); $user->setUsername($attributes['email']); } if (isset($attributes['username']) && !empty($attributes['username'])) { $user->setUsername($attributes['username']); } if (isset($attributes['firstname'])) { $user->setFirstname($attributes['firstname']); } if (isset($attributes['lastname'])) { $user->setLastName($attributes['lastname']); } } private function extractAttributes(Assertion $assertion): array { $attributes = []; foreach ($this->attributes as $key => $attributeName) { if (!$attributeName) { continue; } foreach ($assertion->getAllAttributeStatements() as $attributeStatement) { $attribute = $attributeStatement->getFirstAttributeByName($attributeName); if ($attribute && $attribute->getFirstAttributeValue()) { $attributes[$key] = $attribute->getFirstAttributeValue(); } } } return $attributes; } }