Skip to content

Enhancing CAS authentication handling by extracting user attributes #59951

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 7.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,23 @@ public function getUserBadgeFrom(string $accessToken): UserBadge
$xml = new \SimpleXMLElement($response->getContent(), 0, false, $this->prefix, true);

if (isset($xml->authenticationSuccess)) {
return new UserBadge((string) $xml->authenticationSuccess->user);
$userIdentifier = (string) $xml->authenticationSuccess->user;
$attributes = [];
if (isset($xml->authenticationSuccess->attributes)) {
// Extract all attributes without using namespace
foreach ($xml->authenticationSuccess->attributes->children($this->prefix, true) as $child) {
$key = $child->getName();
if (isset($attributes[$key])) {
if (!\is_array($attributes[$key])) {
$attributes[$key] = [$attributes[$key]];
}
$attributes[$key][] = (string) $child;
} else {
$attributes[$key] = (string) $child;
}
}
}
return new UserBadge($userIdentifier, null, $attributes);
}

if (isset($xml->authenticationFailure)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public function testWithValidTicket()
<cas:authenticationSuccess>
<cas:user>lobster</cas:user>
<cas:proxyGrantingTicket>PGTIOU-84678-8a9d</cas:proxyGrantingTicket>
<cas:attributes>
<cas:email>lobster@example.com</cas:email>
<cas:role>ROLE_USER</cas:role>
</cas:attributes>
</cas:authenticationSuccess>
</cas:serviceResponse>
BODY
Expand All @@ -40,7 +44,64 @@ public function testWithValidTicket()

$cas2Handler = new Cas2Handler(requestStack: $requestStack, validationUrl: 'https://www.example.com/cas', client: $httpClient);
$userbadge = $cas2Handler->getUserBadgeFrom('PGTIOU-84678-8a9d');
$this->assertEquals(new UserBadge('lobster'), $userbadge);
$this->assertEquals(new UserBadge('lobster', null, [
'email' => 'lobster@example.com',
'role' => 'ROLE_USER',
]), $userbadge);
}

public function testWithNoAttributes()
{
$response = new MockResponse(<<<BODY
<cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'>
<cas:authenticationSuccess>
<cas:user>lobster</cas:user>
<cas:proxyGrantingTicket>PGTIOU-84678-8a9d</cas:proxyGrantingTicket>
</cas:authenticationSuccess>
</cas:serviceResponse>
BODY
);

$httpClient = new MockHttpClient([$response]);
$requestStack = new RequestStack();
$requestStack->push(new Request(['ticket' => 'PGTIOU-84678-8a9d']));

$cas2Handler = new Cas2Handler(requestStack: $requestStack, validationUrl: 'https://www.example.com/cas', client: $httpClient);
$userbadge = $cas2Handler->getUserBadgeFrom('PGTIOU-84678-8a9d');
$this->assertEquals(new UserBadge('lobster', null, []), $userbadge);
}

public function testWithMultipleAffiliations()
{
$response = new MockResponse(<<<BODY
<cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'>
<cas:authenticationSuccess>
<cas:user>lobster</cas:user>
<cas:proxyGrantingTicket>PGTIOU-84678-8a9d</cas:proxyGrantingTicket>
<cas:attributes>
<cas:firstname>John</cas:firstname>
<cas:lastname>Doe</cas:lastname>
<cas:email>jdoe@example.org</cas:email>
<cas:affiliation>staff</cas:affiliation>
<cas:affiliation>faculty</cas:affiliation>
</cas:attributes>
</cas:authenticationSuccess>
</cas:serviceResponse>
BODY
);

$httpClient = new MockHttpClient([$response]);
$requestStack = new RequestStack();
$requestStack->push(new Request(['ticket' => 'PGTIOU-84678-8a9d']));

$cas2Handler = new Cas2Handler(requestStack: $requestStack, validationUrl: 'https://www.example.com/cas', client: $httpClient);
$userbadge = $cas2Handler->getUserBadgeFrom('PGTIOU-84678-8a9d');
$this->assertEquals(new UserBadge('lobster', null, [
'firstname' => 'John',
'lastname' => 'Doe',
'email' => 'jdoe@example.org',
'affiliation' => ['staff', 'faculty'],
]), $userbadge);
}

public function testWithInvalidTicket()
Expand Down
Loading