demroos / oauth2-alvario
PHP League OAuth2-Client 的 Alvario OAuth 2.0 客户端提供者
1.1.0
2020-03-10 15:09 UTC
Requires
- league/oauth2-client: ^2.0
Requires (Dev)
- phpunit/phpunit: ^6.0
- squizlabs/php_codesniffer: ^2.0
This package is auto-updated.
Last update: 2024-09-11 00:37:23 UTC
README
此包为 PHP League 的 OAuth 2.0 客户端 提供了 Alvario OAuth 2.0 支持。
安装
要安装,请使用 composer
composer require demroos/oauth2-alvario
使用 Symfony 的用法
安装客户端包
composer require knpuniversity/oauth2-client-bundle
创建控制器
<?php namespace App\Controller; use Alvario\OAuth2\AlvarioUser; use KnpU\OAuth2ClientBundle\Client\ClientRegistry; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; class AlvarioController extends AbstractController { /** * @Route("/connect/alvario", name="connect_alvario_start") * @param ClientRegistry $clientRegistry * @return \Symfony\Component\HttpFoundation\RedirectResponse */ public function connectAction(ClientRegistry $clientRegistry) { $response = $clientRegistry ->getClient('alvario') ->redirect([ 'public_profile' ]); return $response; } /** * @Route("/connect/alvario/check", name="connect_alvario_check") * @param Request $request * @param ClientRegistry $clientRegistry * @return Response * @throws \Exception */ public function connectCheckAction(Request $request, ClientRegistry $clientRegistry) { return Response::create('OK'); } }
创建验证器
<?php namespace App\Security; use App\Entity\User; use Doctrine\ORM\EntityManagerInterface; use KnpU\OAuth2ClientBundle\Client\ClientRegistry; use KnpU\OAuth2ClientBundle\Security\Authenticator\SocialAuthenticator; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; use Symfony\Component\Security\Core\Exception\AuthenticationException; use Symfony\Component\Security\Core\User\UserInterface; use Symfony\Component\Security\Core\User\UserProviderInterface; class AlvarioAuthenticator extends SocialAuthenticator { /** @var ClientRegistry */ private $clientRegistry; /** @var EntityManagerInterface */ private $em; public function __construct(ClientRegistry $clientRegistry, EntityManagerInterface $em) { $this->clientRegistry = $clientRegistry; $this->em = $em; } public function start(Request $request, AuthenticationException $authException = null) { return new RedirectResponse( '/connect/alvario', // might be the site, where users choose their oauth provider Response::HTTP_TEMPORARY_REDIRECT ); } public function supports(Request $request) { return $request->attributes->get('_route') === 'connect_alvario_check'; } public function getCredentials(Request $request) { return $this->fetchAccessToken($this->getAlvarioClient()); } public function getUser($credentials, UserProviderInterface $userProvider) { /** @var AlvarioUser $alvarioUser */ $alvarioUser = $this->getAlvarioClient() ->fetchUserFromToken($credentials); $user = $this->em->getRepository(User::class) ->findByOauthId($alvarioUser->getId()); if ($user instanceof UserInterface) { return $user; } // create new local user $user = new User(); $user->setOauthId($alvarioUser->getId()); $user->setFirstName($alvarioUser->getFirstName()); $user->setLastName($alvarioUser->getLastName()); $this->em->persist($user); $this->em->flush(); return $user; } public function onAuthenticationFailure(Request $request, AuthenticationException $exception) { $message = strtr($exception->getMessageKey(), $exception->getMessageData()); return new Response($message, Response::HTTP_FORBIDDEN); } public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey) { // on success, let the request continue // or redirect to other page return null; } private function getAlvarioClient() { return $this->clientRegistry ->getClient('alvario'); } }
在 security.yaml
中的 guard
部分注册您的验证器
security: # ... firewalls: # ... main: # ... + guard: + authenticators: + - App\Security\AlvarioAuthenticator
配置客户端包
knpu_oauth2_client: clients: # configure your clients as described here: https://github.com/knpuniversity/oauth2-client-bundle#configuration alvario: type: generic provider_class: Alvario\OAuth2\AlvarioAuthProvider # optional: a class that extends OAuth2Client # client_class: Some\Custom\Client # optional: if your provider has custom constructor options provider_options: {} # now, all the normal options! client_id: '%env(ALVARIO_AUTH_CLIENT_ID)%' client_secret: '%env(ALVARIO_AUTH_CLIENT_SECRET)%' redirect_route: connect_alvario_check redirect_params: {}
添加到 .env 文件中
ALVARIO_AUTH_CLIENT_ID=clien_id ALVARIO_AUTH_CLIENT_SECRET=client_secret
测试
可以使用以下方式运行测试
composer test
许可证
MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件。