|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Azine\HybridAuthBundle\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Hybridauth\Adapter\AbstractAdapter; |
|
6
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
7
|
|
|
use Symfony\Component\HttpFoundation\Cookie; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
12
|
|
|
|
|
13
|
|
|
class HybridEndPointController extends Controller |
|
|
|
|
|
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var ParameterBag |
|
17
|
|
|
*/ |
|
18
|
|
|
private $requestQuery; |
|
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Process the current request. |
|
22
|
|
|
* |
|
23
|
|
|
* $request - The current request parameters. Leave as NULL to default to use $_REQUEST. |
|
24
|
|
|
* |
|
25
|
|
|
* @param Request $request |
|
26
|
|
|
* |
|
27
|
|
|
* @return RedirectResponse|Response |
|
28
|
|
|
*/ |
|
29
|
|
|
public function processAction(Request $request) |
|
30
|
|
|
{ |
|
31
|
|
|
$provider = 'linkedin'; |
|
32
|
|
|
$cookieName = $this->getAzineHybridAuthService()->getCookieName($provider); |
|
33
|
|
|
|
|
34
|
|
|
$adapter = $this->getAzineHybridAuthService()->getInstance($request->cookies->get($cookieName), $provider); |
|
35
|
|
|
|
|
36
|
|
|
try { |
|
37
|
|
|
$adapter->authenticate(); |
|
38
|
|
|
$result = $this->getAzineHybridAuthService()->storeHybridAuthSessionData($request, $provider, json_encode($adapter->getAccessToken())); |
|
39
|
|
|
} catch (\Exception $e) { |
|
40
|
|
|
throw new \Exception("Unable to create adapter for provider '$provider'. Is it configured properly?", $e->getCode(), $e); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$response = $this->returnToCallbackUrl($adapter, $provider); |
|
44
|
|
|
|
|
45
|
|
|
if ($result instanceof Cookie) { |
|
46
|
|
|
$response->headers->setCookie($result); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return $response; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
private function returnToCallbackUrl(AbstractAdapter $adapter, $provider) |
|
53
|
|
|
{ |
|
54
|
|
|
// get the stored callback url |
|
55
|
|
|
$callback_url = $adapter->getStorage()->get("hauth_session.$provider.hauth_return_to"); |
|
56
|
|
|
|
|
57
|
|
|
// remove some unneeded stored data |
|
58
|
|
|
$adapter->getStorage()->delete("hauth_session.$provider.hauth_return_to"); |
|
59
|
|
|
$adapter->getStorage()->delete("hauth_session.$provider.hauth_endpoint"); |
|
60
|
|
|
$adapter->getStorage()->delete("hauth_session.$provider.id_provider_params"); |
|
61
|
|
|
|
|
62
|
|
|
// back to home |
|
63
|
|
|
return new RedirectResponse($callback_url); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @return AzineHybridAuth |
|
68
|
|
|
*/ |
|
69
|
|
|
private function getAzineHybridAuthService() |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->get('azine_hybrid_auth_service'); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|