/DependencyInjection/Security/LoginzaListener.php

https://github.com/zim32/Loginza-for-Symfony-2 · PHP · 87 lines · 73 code · 14 blank · 0 comment · 6 complexity · c9d16e22eb5c516d52be61edf229ef7f MD5 · raw file

  1. <?php
  2. namespace Zim32\LoginzaBundle\DependencyInjection\Security;
  3. use Symfony\Component\Security\Http\Firewall\ListenerInterface;
  4. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  5. use Symfony\Component\Security\Core\SecurityContextInterface;
  6. use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
  7. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  8. use Symfony\Component\Security\Core\User\User;
  9. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  10. use Symfony\Component\DependencyInjection\Container;
  11. class LoginzaListener implements ListenerInterface {
  12. protected $securityContext;
  13. protected $authenticationManager;
  14. protected $container;
  15. public function __construct(SecurityContextInterface $securityContext, AuthenticationManagerInterface $authenticationManager, Container $container)
  16. {
  17. $this->securityContext = $securityContext;
  18. $this->authenticationManager = $authenticationManager;
  19. $this->container = $container;
  20. }
  21. public function handle(GetResponseEvent $event){
  22. $request = $event->getRequest();
  23. if($request->request->has('token') !== false){
  24. $loginzaToken = $request->request->get('token');
  25. $signature = md5($loginzaToken.$this->container->getParameter('security.loginza.secret_key'));
  26. $ch = curl_init("http://loginza.ru/api/authinfo?token={$loginzaToken}&id={$this->container->getParameter('security.loginza.widget_id')}&sig={$signature}");
  27. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  28. $result = curl_exec($ch);
  29. $decoded = json_decode($result,true);
  30. if(empty($decoded)) throw new AuthenticationException("Wrong loginza responce format");
  31. if(isset($decoded['error_message'])) throw new AuthenticationException($decoded['error_message']);
  32. $user = false;
  33. if($repo = $this->container->getParameter('security.loginza.entity')){
  34. $user = $this->loadUserFromDoctrine($decoded, $repo);
  35. }
  36. if(!$user){
  37. $user = new User($decoded['name']['first_name'], $decoded['uid'], $roles = array('ROLE_USER'));
  38. }
  39. $token = new LoginzaToken($user->getRoles());
  40. $token->setUser($user);
  41. $token->setAuthenticated(true);
  42. $token->setAttribute('loginza_info', $decoded);
  43. $this->securityContext->setToken($token);
  44. }
  45. }
  46. protected function loadUserFromDoctrine($data, $repository){
  47. $em = $this->container->get('doctrine')->getEntityManager();
  48. try{
  49. $repo = $em->getRepository($repository);
  50. }catch(\Exception $e){
  51. throw $e;
  52. return null;
  53. }
  54. try{
  55. $user = $repo->findOneBy(array('uid'=>$data['uid']));
  56. if($user === null){
  57. $user = new $repository();
  58. $user->setUid($data['uid']);
  59. $user->setPassword('');
  60. $user->setRoles(serialize(array()));
  61. $user->setSalt('');
  62. $user->setUsername($data['name']['first_name']);
  63. $em->persist($user);
  64. $em->flush();
  65. return $user;
  66. }
  67. }catch(\Exception $e){
  68. throw $e;
  69. return null;
  70. }
  71. return $user;
  72. }
  73. }