Skip to content

Commit

Permalink
Retrieve email from access token
Browse files Browse the repository at this point in the history
  • Loading branch information
kudmni committed Mar 26, 2015
1 parent 4a171df commit c8d6a0f
Showing 1 changed file with 88 additions and 6 deletions.
94 changes: 88 additions & 6 deletions src/Vkontakte.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class Vkontakte extends AbstractProvider
{
public $scopes = [];
public $scopes = ['email'];
public $uidKey = 'user_id';
public $responseType = 'json';

Expand All @@ -22,10 +22,94 @@ public function urlAccessToken()
{
return 'https://oauth.vk.com/access_token';
}

public function getAccessToken($grant = 'authorization_code', $params = [])
{
if (is_string($grant)) {
// PascalCase the grant. E.g: 'authorization_code' becomes 'AuthorizationCode'
$className = str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $grant)));
$grant = 'League\\OAuth2\\Client\\Grant\\'.$className;
if (! class_exists($grant)) {
throw new \InvalidArgumentException('Unknown grant "'.$grant.'"');
}
$grant = new $grant();
} elseif (! $grant instanceof GrantInterface) {
$message = get_class($grant).' is not an instance of League\OAuth2\Client\Grant\GrantInterface';
throw new \InvalidArgumentException($message);
}

$defaultParams = [
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret,
'redirect_uri' => $this->redirectUri,
'grant_type' => $grant,
];

$requestParams = $grant->prepRequestParams($defaultParams, $params);

try {
switch (strtoupper($this->method)) {
case 'GET':
// @codeCoverageIgnoreStart
// No providers included with this library use get but 3rd parties may
$client = $this->getHttpClient();
$client->setBaseUrl($this->urlAccessToken() . '?' . $this->httpBuildQuery($requestParams, '', '&'));
$request = $client->get(null, null, $requestParams)->send();
$response = $request->getBody();
break;
// @codeCoverageIgnoreEnd
case 'POST':
$client = $this->getHttpClient();
$client->setBaseUrl($this->urlAccessToken());
$request = $client->post(null, null, $requestParams)->send();
$response = $request->getBody();
break;
// @codeCoverageIgnoreStart
default:
throw new \InvalidArgumentException('Neither GET nor POST is specified for request');
// @codeCoverageIgnoreEnd
}
} catch (BadResponseException $e) {
// @codeCoverageIgnoreStart
$response = $e->getResponse()->getBody();
// @codeCoverageIgnoreEnd
}

switch ($this->responseType) {
case 'json':
$result = json_decode($response, true);

if (JSON_ERROR_NONE !== json_last_error()) {
$result = [];
}

break;
case 'string':
parse_str($response, $result);
break;
}

if (isset($result['error']) && ! empty($result['error'])) {
// @codeCoverageIgnoreStart
throw new IDPException($result);
// @codeCoverageIgnoreEnd
}

$result = $this->prepareAccessTokenResult($result);

$accessToken = $grant->handleResponse($result);

// Add email from response
if (!empty($result['email'])) {
$accessToken->email = $result['email'];
}
return $accessToken;
}

public function urlUserDetails(AccessToken $token)
{
$fields = ['nickname',
$fields = ['email',
'nickname',
'screen_name',
'sex',
'bdate',
Expand Down Expand Up @@ -61,7 +145,7 @@ public function userDetails($response, AccessToken $token)

$user = new User();

$email = (isset($response->email)) ? $response->email : null;
$email = (isset($token->email)) ? $token->email : null;
$location = (isset($response->country)) ? $response->country : null;
$description = (isset($response->status)) ? $response->status : null;

Expand Down Expand Up @@ -89,9 +173,7 @@ public function userUid($response, AccessToken $token)

public function userEmail($response, AccessToken $token)
{
$response = $response->response[0];

return isset($response->email) && $response->email ? $response->email : null;
return (isset($token->email)) ? $token->email : null;
}

public function userScreenName($response, AccessToken $token)
Expand Down

0 comments on commit c8d6a0f

Please sign in to comment.