Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set Linkedin->accessToken in method getAccessToken() #5

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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/'
);

Expand All @@ -34,4 +34,4 @@ public function __construct($appId, $appSecret, array $permissions = array())
$this->appSecret = $appSecret;
$this->permissions = $permissions;
}
}
}
13 changes: 10 additions & 3 deletions src/Linkedin.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Linkedin extends \Nette\Object
* @var \Nette\Http\Session
*/
private $session;

/**
* @var \Fabian\Linkedin\Configuration
*/
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -133,4 +133,11 @@ public function call($endpoint)

return $json;
}

public function clearSession()
{
foreach ($this->session as $name => $item) {
unset($this->session->$name);
}
}
}
25 changes: 18 additions & 7 deletions src/LoginDialog.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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!')
);
Expand Down