[英]How to use Google OAuth as autentification for Symfony2 Rest API
[英]Authenticate Symfony2 REST API with Google Account
我正在使用REST API中的Symfony2和FOSOauthServerBundle。 例如,我希望某些用戶可以使用他們的Google帳戶通過客戶端應用登錄。
通過Web,可以從REST服務器通過Web登錄我的Google帳戶(使用HWIOauthBundle),但是我需要向客戶端應用發送一個access_token(就像FOSOauthServerBundle一樣)。
我有興趣將Google發送給我的access_token保留在我的數據庫中,同時將Google的json消息{'access_token':'XMekfmns ....}發送給客戶端應用(現在是我的REST)也有API)access_token。
我不知道我的方法是否正確。 有任何想法嗎?
(對不起我的英語不好 ;-) )
非常感謝你
這是我的(骯臟的)解決方案,但我希望人們理解我想要的。
從客戶端,用戶發送請求以獲取來自Google帳戶的授權(我將Symfony2與HWIOauthBundle結合使用)
http://myserver.com/connect/google
這是Google登錄表單。 用戶填寫表格並提交。 如果成功存在,將有一個重定向到myserver.com的用戶將在其中登錄。
我在onAuthenticationSuccess上捕獲了事件...
<?php
namespace App\Bundle\Handler;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\HttpFoundation\Request,
Symfony\Component\HttpFoundation\RedirectResponse,
Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Router;
class SecurityHandler implements AuthenticationSuccessHandlerInterface
{
private $router;
public function __construct(Router $router)
{
$this->router = $router;
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
$user = $token->getUser();
return new RedirectResponse($this->router->generate('api_get_token', array(
'clientRandomId' => '5ewv02jcis08wsgggk4wow4so0gokco0g4s8kkoc4so4s0gw4c'
)));
}
#clientRandomId value is an existing value in the table (entity) Client ...
#... in the database.
}
...重定向到Controller,它將在其中生成access_token和refresh_token,並將它們保存在數據庫中。 最后,它將像FOSOauthServerBundle一樣向用戶發送一個json響應。
<?php
namespace App\Bundle\Controller\Api;
use Symfony\Bundle\FrameworkBundle\Controller\Controller,
Symfony\Component\HttpFoundation\JsonResponse,
Symfony\Component\Security\Core\Exception\AccessDeniedException;
use FOS\RestBundle\Controller\FOSRestController,
FOS\RestBundle\Controller\Annotations\Route,
FOS\RestBundle\Controller\Annotations\NamePrefix,
FOS\RestBundle\Controller\Annotations\Prefix;
use FOS\RestBundle\Routing\ClassResourceInterface;
use App\Bundle\Entity\AccessToken;
use App\Bundle\Entity\RefreshToken;
class TokenController extends FOSRestController implements ClassResourceInterface {
#this method has the route named 'api_get_token'
public function getAction($clientRandomId)
{
$user = $this->get('security.context')->getToken()->getUser();
#control if user exist ...
#We force the loggout
$this->container->get('security.context')->setToken(null);
$em = $this->get('doctrine')->getManager();
$client = $em->getRepository('AppBundle:Client')->findOneBy(array('randomId' => $clientRandomId));
#control if client exist ...
$expiresAt = time() + 3600;
$accessToken = new AccessToken;
$accessToken->setClient($client);
$accessToken->setToken('access_token'); #This is only an example
$accessToken->setExpiresAt($expiresAt);
$accessToken->setUser($user);
$refreshToken = new RefreshToken;
$refreshToken->setClient($client);
$refreshToken->setToken('refresh_token'); #This is only an example
$refreshToken->setExpiresAt($expiresAt);
$refreshToken->setUser($user);
$em->persist($accessToken);
$em->persist($refreshToken);
$em->flush();
$jsonData = array(
'access_token' => $accessToken->getToken(),
'expires_in' => 3600,
'token_type' => 'bearer',
'scope' => null,
'refresh_token' => $refreshToken->getToken()
);
$response = new JsonResponse($jsonData);
return $response;
}}
我知道這不是最佳解決方案,但也許它可以引導您找到更好的解決方案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.