-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
363 additions
and
0 deletions.
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') | ||
Check failure on line 51 in src/Symfony/Bundle/SecurityBundle/DependencyInjection/Security/AccessToken/CasTokenHandlerFactory.php GitHub Actions / PsalmUndefinedInterfaceMethod
Check failure on line 51 in src/Symfony/Bundle/SecurityBundle/DependencyInjection/Security/AccessToken/CasTokenHandlerFactory.php GitHub Actions / PsalmUndefinedInterfaceMethod
|
||
->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.