Skip to content

Commit

Permalink
Merge pull request #10 from mshevtsov/patch-3
Browse files Browse the repository at this point in the history
Added new provider: Okru
  • Loading branch information
dracony authored Jan 11, 2017
2 parents 7adf221 + d10e779 commit aa0d6e0
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/PHPixie/Social/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Builder
'github' => '\PHPixie\Social\Providers\Github\Provider',
'dropbox' => '\PHPixie\Social\Providers\Dropbox\Provider',
'yandex' => '\PHPixie\Social\Providers\Yandex\Provider',
'okru' => '\PHPixie\Social\Providers\Okru\Provider',
);

public function __construct($configData)
Expand Down
85 changes: 85 additions & 0 deletions src/PHPixie/Social/Providers/Okru/Provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace PHPixie\Social\Providers\Okru;

class Provider extends \PHPixie\Social\OAuth\OAuth2\Provider
{

protected function endpointUrl($url = NULL)
{
return "https://api.ok.ru/fb.do";
}

public function loginUrl($callbackUrl, $additionalScope = array())
{
return $this->buildLoginUrl(
'https://connect.ok.ru/oauth/authorize',
$callbackUrl,
array(
'scope' => implode(',', $this->configData->get('scope', array())),
'response_type' => 'code',
)
);
}

public function handleCallback($callbackUrl, $callbackData)
{
if(!isset($callbackData['code'])) {
return null;
}

$baseParameters = array(
'client_id' => $this->configData->getRequired('appId'),
'client_secret' => $this->configData->getRequired('appSecret'),
'redirect_uri' => $callbackUrl,
'code' => $callbackData['code']
);

$tokenData = $this->getTokenResponse($callbackData, $baseParameters);
$tokenData = $this->decodeApiResponse($tokenData);

$loginData = $this->apiCall(
$tokenData->access_token,
'GET',
NULL,
array(
"application_key" => $this->configData->getRequired('appPublic'),
"format" => "json",
"method" => "users.getCurrentUser",
"sig" => md5("application_key=". $this->configData->getRequired('appPublic') ."format=jsonmethod=users.getCurrentUser" . md5($tokenData->access_token . $this->configData->getRequired('appSecret')))
)
);

$loginData = $this->normalizeLoginData($loginData, $tokenData);

$token = $this->buildToken($tokenData, $loginData);

return $this->user($token, $loginData);
}

protected function getTokenResponse($callbackData, $baseParameters)
{
$baseParameters['grant_type'] = 'authorization_code';

return $this->http()->call(
'POST',
'https://api.ok.ru/oauth/token.do',
array(),
http_build_query($baseParameters)
);
}

protected function buildToken($tokenData, $loginData)
{
return $this->token(
$loginData->uid,
$tokenData->access_token,
$tokenData->expires_in
);
}

public function type()
{
return 'okru';
}
}

0 comments on commit aa0d6e0

Please sign in to comment.