gabsi / spryker-social-oauth
Spryker 社交 OAuth 模块
dev-master
2023-03-02 09:42 UTC
Requires
- knpuniversity/oauth2-client-bundle: *
- league/oauth2-client: *
- propel/propel: ~2.0@dev
- spryker/gui: ^3.33.0
- spryker/kernel: ^3.30.0
- spryker/security-extension: ^1.0.0
- spryker/session: ^3.1.1 || ^4.0.0
- spryker/symfony: ^3.0.0
- spryker/transfer: ^3.25.0
- spryker/twig-extension: ^1.0.0
- spryker/user: ^3.13
- spryker/user-extension: ^1.1.0
- spryker/util-date-time: ^1.0.0
- spryker/util-text: ^1.1.0
This package is auto-updated.
Last update: 2024-09-20 23:23:53 UTC
README
A Spryker 包装器,用于 knpuniversity/oauth2-client-bundle
安装
docker/sdk cli composer require "towa/spryker-social-oauth" --no-update docker/sdk cli composer update "towa/spryker-social-oauth"
使用
配置
在您的 config_default.php
中添加您的提供者配置。例如。
// config-default.php
use Towa\Service\TowaSprykerOauth\TowaSprykerOauthConstants;
$config[TowaSprykerOauthConstants::TOWA_SPRYKER_AUTH_CONFIG] = [
'github' => [
'clientId' => '{github-client-id}',
'clientSecret' => '{github-client-secret}',
'redirectUri' => 'https://example.com/callback-url',
],
'google' => [
'clientId' => '{google-client-id}',
'clientSecret' => '{google-client-secret}',
'redirectUri' => 'https://example.com/callback-url',
'hostedDomain' => 'example.com', // optional; used to restrict access to users on your G Suite/Google Apps for Business accounts
]
]
确保您已经为您的提供者安装了正确的客户端。请参阅 https://github.com/knpuniversity/oauth2-client-bundle#configuring-a-client
将 Towa 命名空间添加到核心命名空间中。
// config_default.php
$config[KernelConstants::CORE_NAMESPACES] = [
...
'Towa'
];
代理配置
在项目级别扩展 SecurityApplicationPlugin。
// Pyz\Yves\Security\Plugin\Application\SecurityApplicationPlugin class SecurityApplicationPlugin extends SprykerSecurityApplicationPlugin { public const SERVICE_SECURITY_AUTHENTICATION_PROVIDER_TOWA = 'security.authentication_provider.agent.dao'; /** * @param \Spryker\Service\Container\ContainerInterface $container * * @return \Spryker\Service\Container\ContainerInterface */ protected function addListenerPrototypes(ContainerInterface $container): ContainerInterface { $container = $this->addTowaAuthenticator($container); $container = parent::addListenerPrototypes($container); return $container; } /** * @param \Spryker\Service\Container\ContainerInterface $container * * @return \Spryker\Service\Container\ContainerInterface */ protected function addAuthenticationProviderPrototypes(ContainerInterface $container): ContainerInterface { $container = parent::addAuthenticationProviderPrototypes($container); $container = $this->addTowaAuthenticationProvider($container); return $container; } /** * @param \Spryker\Service\Container\ContainerInterface $container * * @return \Spryker\Service\Container\ContainerInterface */ private function addTowaAuthenticator(ContainerInterface $container): ContainerInterface { $container->set( AgentAuthenticator::AUTHENTICATOR_KEY, new AgentAuthenticator( $this->getFactory()->getProvider(), $this->getFactory()->getProviderClient(), $this->getFactory()->getUserClient(), ) ); return $container; } /** * @param \Spryker\Service\Container\ContainerInterface $container * * @return \Spryker\Service\Container\ContainerInterface */ protected function addTowaAuthenticationProvider(ContainerInterface $container): ContainerInterface { $container->set( static::SERVICE_SECURITY_AUTHENTICATION_PROVIDER_KEYCLOAK, new AgentAuthenticationProvider( $container->get(AgentAuthenticator::AUTHENTICATOR_KEY), $this->getFactory()->createAgentUserProvider(), new UserChecker() ) ); return $container; } }
在 SecurityFactory 中,您现在需要为适当的提供者和客户端添加函数。
// Pyz\Yves\Security\Plugin\Application\SecurityApplicationPlugin
class SecurityFactory extends SprykerSecurityFactory
{
/**
* @return \Pyz\Yves\Security\Dependency\Client\SecurityToCustomerClientInterface
*/
public function getCustomerClient(): SecurityToCustomerClientInterface
{
return $this->getProvidedDependency(SecurityDependencyProvider::CLIENT_CUSTOMER);
}
/**
* @return \Pyz\Client\User\UserClientInterface
*/
public function getUserClient(): UserClientInterface
{
return $this->getProvidedDependency(SecurityDependencyProvider::CLIENT_USER);
}
/**
* @return \Towa\Service\TowaSprykerOauth\TowaSprykerOauthServiceInterface
*/
public function getTowaOauthService(): TowaOauthServiceInterface
{
return $this->getProvidedDependency(SecurityDependencyProvider::SERVICE_TOWAOAUTH);
}
/**
* @return \League\OAuth2\Client\Provider\AbstractProvider
*/
public function getProvider(): AbstractProvider
{
return $this->getTowaOauthService()->getSocialOauthProvider('github'); // replace github with whichever provider you are using
}
/**
* @return \KnpU\OAuth2ClientBundle\Client\OAuth2ClientInterface
*/
public function getProviderClient(): OAuth2ClientInterface
{
return $this->getTowaOauthService()->getSocialOauthClient('github'); // replace github with whichever provider you are using
}
/**
* @return \Symfony\Component\Security\Core\User\UserProviderInterface
*/
public function createAgentUserProvider(): UserProviderInterface
{
return new AgentUserProvider();
}
}
调整 DependencyProvider
class SecurityDependencyProvider extends SprykerSecurityDependencyProvider
{
public const CLIENT_CUSTOMER = 'CLIENT_CUSTOMER';
public const SERVICE_TOWAOAUTH = 'SERVICE_TOWAOAUTH';
public const CLIENT_USER = 'CLIENT_USER';
/**
* @param \Spryker\Yves\Kernel\Container $container
*
* @return \Spryker\Yves\Kernel\Container
*/
public function provideDependencies(Container $container): Container
{
$container = parent::provideDependencies($container);
$container = $this->addCustomerClient($container);
$container = $this->addTowaOauthService($container);
$container = $this->addUserClient($container);
return $container;
}
/**
* @param \Spryker\Yves\Kernel\Container $container
*
* @return \Spryker\Yves\Kernel\Container
*/
protected function addCustomerClient(Container $container): Container
{
$container->set(static::CLIENT_CUSTOMER, function (Container $container): SecurityToCustomerClientInterface {
return new SecurityToCustomerClientBridge(
$container->getLocator()->customer()->client()
);
});
return $container;
}
/**
* @param \Spryker\Yves\Kernel\Container $container
*
* @return \Spryker\Yves\Kernel\Container
*/
private function addTowaOauthService(Container $container): Container
{
$container->set(static::SERVICE_TOWAOAUTH, function (Container $container): TowaOauthServiceInterface {
return $container->getLocator()->TowaSprykerOauth()->service();
});
return $container;
}
/**
* @param \Spryker\Yves\Kernel\Container $container
*
* @return \Spryker\Yves\Kernel\Container
*/
private function addUserClient(Container $container): Container
{
$container->set(static::CLIENT_USER, function (Container $container): UserClientInterface {
return $container->getLocator()->user()->client();
});
return $container;
}
}
Yves 配置
TODO
Zed 配置
TODO