Skip to content

[WIP][Security] OAuth2 component #31952

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

Closed
wants to merge 2 commits into from
Closed
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
@@ -0,0 +1,73 @@
<?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\UserProvider;

use Symfony\Component\Config\Definition\Builder\NodeDefinition;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* @author Guillaume Loulier <contact@guillaumeloulier.fr>
*/
final class OAuthClientFactory implements UserProviderFactoryInterface
{
public function create(ContainerBuilder $container, $id, $config)
{
$container
->setDefinition($id, new ChildDefinition('security.user.provider.oauth'))
;
}

public function getKey()
{
return 'oauth';
}

public function addConfiguration(NodeDefinition $builder)
{
$builder
->children()
->enumNode('type')
->info('The type of OAuth client needed: authorization_code, implicit, client_credentials, resource_owner')
->values(['authorization_code', 'implicit', 'client_credentials', 'resource_owner'])
->isRequired()
->cannotBeEmpty()
->end()
->scalarNode('client_id')
->isRequired()
->cannotBeEmpty()
->defaultValue('12345678')
->end()
->scalarNode('client_secret')
->isRequired()
->cannotBeEmpty()
->defaultValue('12345678')
->end()
->scalarNode('authorization_url')
->isRequired()
->cannotBeEmpty()
->defaultValue('https://foo.com/authenticate')
->end()
->scalarNode('redirect_uri')
->isRequired()
->cannotBeEmpty()
->defaultValue('https://myapp.com/oauth')
->end()
->scalarNode('access_token_url')
->isRequired()
->cannotBeEmpty()
->defaultValue('https://foo.com/token')
->end()
->end()
;
}
}
48 changes: 48 additions & 0 deletions src/Symfony/Component/Security/Core/User/OauthUserProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?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\Core\User;

use Symfony\Component\Security\Core\Exception\UnsupportedUserException;

/**
* OauthUserProvider is a provider built on top of the Oauth component.
*
* @author Guillaume Loulier <contact@guillaumeloulier.fr>
*/
final class OauthUserProvider implements UserProviderInterface
{
private const USER_ROLES = ['ROLE_USER', 'ROLE_OAUTH_USER'];

public function loadUserByUsername($username)
{
}

/**
* {@inheritdoc}
*/
public function refreshUser(UserInterface $user)
{
if (!$user instanceof User) {
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', \get_class($user)));
}

return new User($user->getUsername(), null, $user->getRoles());
}

/**
* {@inheritdoc}
*/
public function supportsClass($class)
{
return 'Symfony\Component\Security\Core\User\User' === $class;
}
}
3 changes: 3 additions & 0 deletions src/Symfony/Component/Security/OAuth2Client/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor/
composer.lock
phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?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\OAuth2Client\Authorization;

/**
* @author Guillaume Loulier <contact@guillaumeloulier.fr>
*/
final class AuthorizationCodeResponse
{
private $code;
private $state;
public const TYPE = 'code';

public function __construct(string $code, string $state)
{
$this->code = $code;
$this->state = $state;
}

public function getCode(): string
{
return $this->code;
}

public function getState(): string
{
return $this->state;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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\OAuth2Client\Event;

use Symfony\Component\Security\OAuth2Client\Token\AbstractToken;
use Symfony\Contracts\EventDispatcher\Event;

/**
* @author Guillaume Loulier <contact@guillaumeloulier.fr>
*/
final class AccessTokenFetchEvent extends Event
{
private $token;

public function __construct(AbstractToken $token)
{
$this->token = $token;
}

public function getToken(): AbstractToken
{
return $this->token;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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\OAuth2Client\Event;

use Symfony\Component\Security\OAuth2Client\Token\AbstractToken;
use Symfony\Contracts\EventDispatcher\Event;

/**
* @author Guillaume Loulier <contact@guillaumeloulier.fr>
*/
final class RefreshTokenFetchEvent extends Event
{
private $token;

public function __construct(AbstractToken $token)
{
$this->token = $token;
}

public function getToken(): AbstractToken
{
return $this->token;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?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\OAuth2Client\Exception;

/**
* @author Guillaume Loulier <contact@guillaumeloulier.fr>
*/
final class InvalidJWTAuthorizationOptions extends \LogicException
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?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\OAuth2Client\Exception;

/**
* Represent an error linked to the usage of an invalid JWT token.
*
* @author Guillaume Loulier <contact@guillaumeloulier.fr>
*/
final class InvalidJWTTokenTypeException extends \LogicException
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?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\OAuth2Client\Exception;

/**
* Represent an error linked to the request (can be for an authentication code, access_token or refresh_token).
*
* @author Guillaume Loulier <contact@guillaumeloulier.fr>
*/
final class InvalidRequestException extends \LogicException
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?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\OAuth2Client\Exception;

/**
* @author Guillaume Loulier <contact@guillaumeloulier.fr>
*/
final class InvalidUrlException extends \RuntimeException
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?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\OAuth2Client\Exception;

/**
* Thrown if the provider does not receive all the required options.
*
* {@see GenericProvider::defineOptions}
*
* @author Guillaume Loulier <contact@guillaumeloulier.fr>
*/
final class MissingOptionsException extends \RuntimeException
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?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\OAuth2Client\Helper;

use Symfony\Component\Security\OAuth2Client\Token\IntrospectedToken;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
* @see https://tools.ietf.org/html/rfc7662
*
* @author Guillaume Loulier <contact@guillaumeloulier.fr>
*/
final class TokenIntrospectionHelper
{
private $client;

public function __construct(HttpClientInterface $client)
{
$this->client = $client;
}

public function introspecte(string $introspectionEndpointURI, string $token, array $headers = [], array $extraQuery = [], string $tokenTypeHint = null, string $method = 'POST'): IntrospectedToken
{
$defaultHeaders = [
'Accept' => 'application/json',
'Content-Type' => 'application/x-www-form-urlencoded',
];

$defaultQuery = ['token' => $token];

if ($tokenTypeHint) {
$defaultQuery['token_type_hint'] = $tokenTypeHint;
}

$finalHeaders = array_unique(array_merge($defaultHeaders, $headers));
$finalQuery = array_unique(array_merge($defaultQuery, $extraQuery));

$response = $this->client->request($method, $introspectionEndpointURI, [
'headers' => $finalHeaders,
'query' => $finalQuery,
]);

$body = $response->toArray();

return new IntrospectedToken($body);
}
}
Loading