| 1 | <?php |
||
| 2 | |||
| 3 | namespace Azine\HybridAuthBundle\Services; |
||
| 4 | |||
| 5 | use Azine\HybridAuthBundle\DependencyInjection\AzineHybridAuthExtension; |
||
| 6 | use Azine\HybridAuthBundle\Entity\HybridAuthSessionData; |
||
| 7 | use Doctrine\Common\Persistence\ObjectManager; |
||
| 8 | use Hybridauth\Hybridauth; |
||
| 9 | use Symfony\Component\HttpFoundation\Cookie; |
||
| 10 | use Symfony\Component\HttpFoundation\Request; |
||
| 11 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
||
| 12 | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
||
| 13 | use Symfony\Component\Security\Core\User\UserInterface; |
||
| 14 | |||
| 15 | class AzineHybridAuth |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * ID of the sessionDataCookie. |
||
| 19 | */ |
||
| 20 | const cookieName = 'azine_hybridauth_session'; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var ObjectManager |
||
| 24 | */ |
||
| 25 | private $objectManager; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var UserInterface |
||
| 29 | */ |
||
| 30 | private $currentUser; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var bool |
||
| 34 | */ |
||
| 35 | private $storeForUser; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var bool |
||
| 39 | */ |
||
| 40 | private $storeAsCookie; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var int |
||
| 44 | */ |
||
| 45 | private $expiresInDays; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Configured Instances of HybridAuth. |
||
| 49 | * |
||
| 50 | * @var array or HybridAuth |
||
| 51 | */ |
||
| 52 | private $instances = array(); |
||
| 53 | |||
| 54 | /** |
||
| 55 | * HybridAuth configuration. |
||
| 56 | * |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | private $config; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param UrlGeneratorInterface $router |
||
| 63 | * @param TokenStorageInterface $tokenStorage |
||
| 64 | * @param ObjectManager $manager |
||
| 65 | * @param array $config |
||
| 66 | * @param bool $storeForUser |
||
| 67 | 2 | * @param $storeAsCookie |
|
| 68 | 2 | * @param $expiresInDays |
|
| 69 | 2 | */ |
|
| 70 | 2 | public function __construct(UrlGeneratorInterface $router, TokenStorageInterface $tokenStorage, ObjectManager $manager, $config, $storeForUser, $storeAsCookie, $expiresInDays) |
|
| 71 | 2 | { |
|
| 72 | 2 | $base_url = $router->generate($config[AzineHybridAuthExtension::ENDPOINT_ROUTE], array(), UrlGeneratorInterface::ABSOLUTE_URL); |
|
| 73 | 2 | $config[AzineHybridAuthExtension::BASE_URL] = $base_url; |
|
| 74 | 2 | $this->config = $config; |
|
| 75 | 2 | $this->config['callback'] = $base_url; |
|
| 76 | $this->objectManager = $manager; |
||
| 77 | $this->storeForUser = $storeForUser; |
||
| 78 | 2 | $this->storeAsCookie = $storeAsCookie; |
|
| 79 | 2 | $user = $tokenStorage->getToken()->getUser(); |
|
| 80 | if ($user instanceof UserInterface) { |
||
| 81 | $this->currentUser = $user; |
||
| 82 | } |
||
| 83 | $this->expiresInDays = $expiresInDays; |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Get a AdapterInterface instance initialised for the given provider. |
||
| 88 | * HybridAuthSessions will be restored from DB and/or cookies, according to the bundle configuration. |
||
| 89 | * |
||
| 90 | * @param $cookieSessionData |
||
| 91 | * @param $provider |
||
| 92 | * |
||
| 93 | * @return \Hybridauth\Adapter\AdapterInterface |
||
| 94 | */ |
||
| 95 | public function getInstance($cookieSessionData, $provider) |
||
| 96 | { |
||
| 97 | if (array_key_exists($provider, $this->instances)) { |
||
| 98 | $hybridAuth = $this->instances[$provider]; |
||
| 99 | } else { |
||
| 100 | $hybridAuth = new Hybridauth($this->config); |
||
| 101 | $this->instances[$provider] = $hybridAuth; |
||
| 102 | } |
||
| 103 | |||
| 104 | $adapter = $this->instances[$provider]->getAdapter($provider); |
||
| 105 | $restoredFromDB = false; |
||
| 106 | $sessionData = null; |
||
| 107 | $isExpiredSession = false; |
||
| 108 | |||
| 109 | $result = null; |
||
| 110 | if ($this->currentUser instanceof UserInterface) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 111 | $result = $this->objectManager->getRepository('AzineHybridAuthBundle:HybridAuthSessionData')->findOneBy(array('username' => $this->currentUser->getUsername(), 'provider' => $provider)); |
||
| 112 | } |
||
| 113 | |||
| 114 | if ($result instanceof HybridAuthSessionData) { |
||
| 115 | $isExpiredSession = $this->isExpiredSession($result); |
||
| 116 | } |
||
| 117 | |||
| 118 | if ($isExpiredSession) { |
||
| 119 | $this->deleteSession($provider); |
||
| 120 | } |
||
| 121 | |||
| 122 | if (!$isExpiredSession && $this->storeForUser && $this->currentUser instanceof UserInterface) { |
||
| 123 | // try from database |
||
| 124 | if ($result) { |
||
| 125 | $sessionData = $result->getSessionData(); |
||
| 126 | $restoredFromDB = true; |
||
| 127 | } |
||
| 128 | } |
||
| 129 | if (null === $sessionData && null !== $cookieSessionData) { |
||
| 130 | // try from cookie |
||
| 131 | $sessionData = gzinflate($cookieSessionData); |
||
| 132 | |||
| 133 | // user is looged in but auth session is not yet stored in db => store now |
||
| 134 | if (!$restoredFromDB) { |
||
| 135 | $this->saveAuthSessionData($sessionData, $provider); |
||
| 136 | } |
||
| 137 | } |
||
| 138 | if ($sessionData) { |
||
| 139 | $adapter->setAccessToken(json_decode($sessionData, true)); |
||
| 140 | } |
||
| 141 | |||
| 142 | return $adapter; |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @param Request $request |
||
| 147 | * @param $provider |
||
| 148 | * @param $sessionData |
||
| 149 | * |
||
| 150 | * @return Cookie | null |
||
| 151 | */ |
||
| 152 | public function storeHybridAuthSessionData(Request $request, $provider, $sessionData) |
||
| 153 | { |
||
| 154 | $this->saveAuthSessionData($sessionData, $provider); |
||
| 155 | |||
| 156 | if ($this->storeAsCookie) { |
||
| 157 | return new Cookie($this->getCookieName($provider), gzdeflate($sessionData), new \DateTime($this->expiresInDays.' days'), '/', $request->getHost(), $request->isSecure(), true); |
||
| 158 | } |
||
| 159 | |||
| 160 | return null; |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Delete the HybridAuthSessionData entity from the database. |
||
| 165 | * |
||
| 166 | * @param $provider |
||
| 167 | */ |
||
| 168 | public function deleteSession($provider) |
||
| 169 | { |
||
| 170 | if ($this->currentUser instanceof UserInterface) { |
||
|
0 ignored issues
–
show
|
|||
| 171 | $result = $this->objectManager->getRepository('AzineHybridAuthBundle:HybridAuthSessionData')->findOneBy(array('username' => $this->currentUser->getUsername(), 'provider' => $provider)); |
||
| 172 | if ($result) { |
||
| 173 | $this->objectManager->remove($result); |
||
| 174 | $this->objectManager->flush(); |
||
| 175 | } |
||
| 176 | } |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Save as HybridAuthSessionData entity to the database. |
||
| 181 | * Checks the bundle configuration before saving. |
||
| 182 | * |
||
| 183 | * @param $sessionData |
||
| 184 | * @param $provider |
||
| 185 | */ |
||
| 186 | private function saveAuthSessionData($sessionData, $provider) |
||
| 187 | { |
||
| 188 | if ($this->storeForUser && $this->currentUser instanceof UserInterface) { |
||
| 189 | $hybridAuthData = $this->objectManager->getRepository('AzineHybridAuthBundle:HybridAuthSessionData')->findOneBy(array('username' => $this->currentUser->getUsername(), 'provider' => strtolower($provider))); |
||
| 190 | if (!$hybridAuthData) { |
||
| 191 | $hybridAuthData = new HybridAuthSessionData(); |
||
| 192 | $hybridAuthData->setUserName($this->currentUser->getUsername()); |
||
| 193 | $hybridAuthData->setProvider(strtolower($provider)); |
||
| 194 | |||
| 195 | $expirationDate = new \DateTime(); |
||
| 196 | $expirationDate->modify('+ '.$this->expiresInDays.' day'); |
||
| 197 | |||
| 198 | $hybridAuthData->setExpiresAt($expirationDate); |
||
| 199 | $this->objectManager->persist($hybridAuthData); |
||
| 200 | } |
||
| 201 | $hybridAuthData->setSessionData($sessionData); |
||
| 202 | $this->objectManager->flush(); |
||
| 203 | } |
||
| 204 | } |
||
| 205 | |||
| 206 | public function getCookieName($provider) |
||
| 207 | { |
||
| 208 | return self::cookieName.'_'.strtolower($provider); |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Use this function to get access to a \Hybridauth\Adapter\AdapterInterface instance. |
||
| 213 | * |
||
| 214 | * Calling this method will log the user in (make a roundtrip to the providers site and back to your site again) |
||
| 215 | * and call the page again that you came from. |
||
| 216 | * |
||
| 217 | * When logged (allready) it will return the hybridAuth provider. |
||
| 218 | * |
||
| 219 | * @param $authSessionData |
||
| 220 | * @param string $provider_id |
||
| 221 | * @param bool $require_login |
||
| 222 | * |
||
| 223 | * @return \Hybridauth\Adapter\AdapterInterface |
||
| 224 | */ |
||
| 225 | public function getProvider($authSessionData, $provider_id, $require_login = true) |
||
| 226 | { |
||
| 227 | $adapter = $this->getInstance($authSessionData, $provider_id); |
||
| 228 | if ($require_login && !$adapter->isConnected()) { |
||
| 229 | $adapter->login(); |
||
| 230 | } |
||
| 231 | |||
| 232 | return $adapter; |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Check if the current user has allowed access to the given provider. |
||
| 237 | * |
||
| 238 | * @param Request $request |
||
| 239 | * @param string $provider_id |
||
| 240 | * |
||
| 241 | * @return bool true if access to the provider is granted for this app |
||
| 242 | */ |
||
| 243 | public function isConnected(Request $request, $provider_id) |
||
| 244 | { |
||
| 245 | $sessionData = $request->cookies->get($this->getCookieName($provider_id)); |
||
| 246 | $adapter = $this->getInstance($sessionData, $provider_id); |
||
| 247 | $connected = $adapter->isConnected(); |
||
| 248 | |||
| 249 | return $connected; |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Get the LinkedIn Adapter. |
||
| 254 | * |
||
| 255 | * @return \Hybrid_Providers_LinkedIn |
||
| 256 | */ |
||
| 257 | public function getLinkedIn() |
||
| 258 | { |
||
| 259 | return $this->getProvider(null, 'linkedin'); |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Get the LinkedIn api (LinkedIn PHP-client). |
||
| 264 | * |
||
| 265 | * @return \LinkedIn |
||
| 266 | */ |
||
| 267 | 2 | public function getLinkedInApi() |
|
| 268 | { |
||
| 269 | 2 | return $this->getLinkedIn()->api(); |
|
| 270 | } |
||
| 271 | 1 | ||
| 272 | /** |
||
| 273 | * Get if auth token is expired. |
||
| 274 | 1 | * |
|
| 275 | * @param HybridAuthSessionData $data |
||
| 276 | * |
||
| 277 | * @return bool |
||
| 278 | */ |
||
| 279 | public function isExpiredSession(HybridAuthSessionData $data) |
||
| 280 | { |
||
| 281 | if ($data->getExpiresAt() < new \DateTime()) { |
||
| 282 | return true; |
||
| 283 | } |
||
| 284 | |||
| 285 | return false; |
||
| 286 | } |
||
| 287 | } |
||
| 288 |