diff --git a/src/Configuration.php b/src/Configuration.php index 019d022..773081d 100644 --- a/src/Configuration.php +++ b/src/Configuration.php @@ -24,7 +24,7 @@ class Configuration extends \Nette\Object */ public $url = array( 'authorization' => 'https://www.linkedin.com/uas/oauth2/authorization', - 'accessToken' => 'https://www.linkedin.com/uas/oauth2/accessToken/', + 'accessToken' => 'https://www.linkedin.com/uas/oauth2/accessToken', 'api' => 'https://api.linkedin.com/v1/' ); @@ -34,4 +34,4 @@ public function __construct($appId, $appSecret, array $permissions = array()) $this->appSecret = $appSecret; $this->permissions = $permissions; } -} \ No newline at end of file +} diff --git a/src/Linkedin.php b/src/Linkedin.php index 5ceb85b..7c24c23 100644 --- a/src/Linkedin.php +++ b/src/Linkedin.php @@ -13,7 +13,7 @@ class Linkedin extends \Nette\Object * @var \Nette\Http\Session */ private $session; - + /** * @var \Fabian\Linkedin\Configuration */ @@ -44,7 +44,7 @@ public function getState() public function getRedirectUrl($backLink) { - $this->session->csrfToken = \Nette\Utils\Strings::random(); + $this->session->csrfToken = \Nette\Utils\Random::generate(); return $this->config->url['authorization'].'?' . http_build_query(array( @@ -87,7 +87,7 @@ public function getAccessToken($code, $redirectUri) throw new Exception('token error'); } - $this->session->access_token = $token->access_token; + $this->session->access_token = $this->accessToken = $token->access_token; return $token->access_token; } @@ -133,4 +133,11 @@ public function call($endpoint) return $json; } + + public function clearSession() + { + foreach ($this->session as $name => $item) { + unset($this->session->$name); + } + } } \ No newline at end of file diff --git a/src/LoginDialog.php b/src/LoginDialog.php index b3aece2..be58507 100644 --- a/src/LoginDialog.php +++ b/src/LoginDialog.php @@ -3,7 +3,7 @@ namespace Fabian\Linkedin; /** - * @method onResponse(\Fabian\Linkedin\LinkedinLoginDialog $dialog) + * @method onResponse(\Fabian\Linkedin\LoginDialog $dialog) */ class LoginDialog extends \Nette\Application\UI\PresenterComponent { @@ -41,13 +41,24 @@ private function open() public function handleResponse() { $params = $this->presenter->params; - if (!isset($params['code'])) { - throw new Exception('no code!'); - } - // check state token for CSRF attack - if ($params['state'] != $this->linkedin->getState()) { - throw new Exception('CSRF attack!'); + + try { + if (!isset($params['code'])) { + throw new Exception('no code!'); + } + + // check state token for CSRF attack + if ($params['state'] != $this->linkedin->getState()) { + throw new Exception('CSRF attack!'); + } + + } catch (\Exception $e) { + + $this->linkedin->clearSession(); + $this->onResponse($this); + return; } + $accessToken = $this->linkedin->getAccessToken( $params['code'], $this->link('//response!') );