-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
[Security] add CAS 2.0 AccessToken handler #48276
Merged
fabpot
merged 1 commit into
symfony:7.1
from
nacorp:security-add-CAS-AccessToken-handler
Feb 3, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
...Bundle/SecurityBundle/DependencyInjection/Security/AccessToken/CasTokenHandlerFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\SecurityBundle\DependencyInjection\Security\AccessToken; | ||
|
||
use Symfony\Component\Config\Definition\Builder\NodeBuilder; | ||
use Symfony\Component\DependencyInjection\ChildDefinition; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
use Symfony\Component\Security\Http\AccessToken\Cas\Cas2Handler; | ||
|
||
class CasTokenHandlerFactory implements TokenHandlerFactoryInterface | ||
{ | ||
public function create(ContainerBuilder $container, string $id, array|string $config): void | ||
{ | ||
$container->setDefinition($id, new ChildDefinition('security.access_token_handler.cas')); | ||
|
||
$container | ||
->register('security.access_token_handler.cas', Cas2Handler::class) | ||
->setArguments([ | ||
new Reference('request_stack'), | ||
$config['validation_url'], | ||
$config['prefix'], | ||
$config['http_client'] ? new Reference($config['http_client']) : null, | ||
]); | ||
} | ||
|
||
public function getKey(): string | ||
{ | ||
return 'cas'; | ||
} | ||
|
||
public function addConfiguration(NodeBuilder $node): void | ||
{ | ||
$node | ||
->arrayNode($this->getKey()) | ||
->fixXmlConfig($this->getKey()) | ||
->children() | ||
->scalarNode('validation_url') | ||
->info('CAS server validation URL') | ||
->isRequired() | ||
->end() | ||
->scalarNode('prefix') | ||
->info('CAS prefix') | ||
->defaultValue('cas') | ||
->end() | ||
->scalarNode('http_client') | ||
->info('HTTP Client service') | ||
->defaultNull() | ||
->end() | ||
->end() | ||
->end(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/AccessToken/config_cas.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
imports: | ||
- { resource: ./../config/framework.yml } | ||
|
||
framework: | ||
http_method_override: false | ||
serializer: ~ | ||
|
||
security: | ||
password_hashers: | ||
Symfony\Component\Security\Core\User\InMemoryUser: plaintext | ||
|
||
providers: | ||
in_memory: | ||
memory: | ||
users: | ||
dunglas: { password: foo, roles: [ROLE_USER] } | ||
|
||
firewalls: | ||
main: | ||
pattern: ^/ | ||
access_token: | ||
token_handler: | ||
cas: | ||
validation_url: 'https://www.example.com/cas/serviceValidate' | ||
http_client: 'Symfony\Contracts\HttpClient\HttpClientInterface' | ||
token_extractors: | ||
- security.access_token_extractor.cas | ||
|
||
access_control: | ||
- { path: ^/foo, roles: ROLE_USER } | ||
|
||
services: | ||
_defaults: | ||
public: true | ||
|
||
security.access_token_extractor.cas: | ||
class: Symfony\Component\Security\Http\AccessToken\QueryAccessTokenExtractor | ||
arguments: | ||
- 'ticket' | ||
|
||
Symfony\Contracts\HttpClient\HttpClientInterface: ~ |
85 changes: 85 additions & 0 deletions
85
src/Symfony/Component/Security/Http/AccessToken/Cas/Cas2Handler.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Security\Http\AccessToken\Cas; | ||
|
||
use Symfony\Component\HttpClient\HttpClient; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Symfony\Component\Security\Core\Exception\AuthenticationException; | ||
use Symfony\Component\Security\Http\AccessToken\AccessTokenHandlerInterface; | ||
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
|
||
/** | ||
* @see https://apereo.github.io/cas/6.6.x/protocol/CAS-Protocol-V2-Specification.html | ||
* | ||
* @author Nicolas Attard <contact@nicolasattard.fr> | ||
*/ | ||
final class Cas2Handler implements AccessTokenHandlerInterface | ||
{ | ||
public function __construct( | ||
private readonly RequestStack $requestStack, | ||
private readonly string $validationUrl, | ||
private readonly string $prefix = 'cas', | ||
private ?HttpClientInterface $client = null, | ||
) { | ||
if (null === $client) { | ||
if (!class_exists(HttpClient::class)) { | ||
throw new \LogicException(sprintf('You cannot use "%s" as the HttpClient component is not installed. Try running "composer require symfony/http-client".', __CLASS__)); | ||
} | ||
|
||
$this->client = HttpClient::create(); | ||
} | ||
} | ||
|
||
/** | ||
* @throws AuthenticationException | ||
*/ | ||
public function getUserBadgeFrom(string $accessToken): UserBadge | ||
{ | ||
$response = $this->client->request('GET', $this->getValidationUrl($accessToken)); | ||
|
||
$xml = new \SimpleXMLElement($response->getContent(), 0, false, $this->prefix, true); | ||
|
||
if (isset($xml->authenticationSuccess)) { | ||
return new UserBadge((string) $xml->authenticationSuccess->user); | ||
} | ||
|
||
if (isset($xml->authenticationFailure)) { | ||
throw new AuthenticationException('CAS Authentication Failure: '.trim((string) $xml->authenticationFailure)); | ||
} | ||
|
||
throw new AuthenticationException('Invalid CAS response.'); | ||
} | ||
|
||
private function getValidationUrl(string $accessToken): string | ||
{ | ||
$request = $this->requestStack->getCurrentRequest(); | ||
|
||
if (null === $request) { | ||
throw new \LogicException('Request should exist so it can be processed for error.'); | ||
} | ||
|
||
$query = $request->query->all(); | ||
|
||
if (!isset($query['ticket'])) { | ||
throw new AuthenticationException('No ticket found in request.'); | ||
} | ||
unset($query['ticket']); | ||
$queryString = empty($query) ? '' : '?'.http_build_query($query); | ||
|
||
return sprintf('%s?ticket=%s&service=%s', | ||
$this->validationUrl, | ||
urlencode($accessToken), | ||
urlencode($request->getSchemeAndHttpHost().$request->getBaseUrl().$request->getPathInfo().$queryString) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if the extension is missing?