This repository has been archived by the owner on Aug 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from rankster/staging
upcoming v1.0
- Loading branch information
Showing
17 changed files
with
421 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace Rankster\Api\V1\Oauth; | ||
|
||
use Rankster\Entity\User; | ||
use Rankster\Service\AuthSession; | ||
use Yaoi\Command; | ||
use Yaoi\Command\Definition; | ||
|
||
class Google extends Command | ||
{ | ||
public $state; | ||
public $code; | ||
|
||
/** | ||
* @param Definition $definition | ||
* @param \stdClass|static $options | ||
*/ | ||
static function setUpDefinition(Definition $definition, $options) | ||
{ | ||
$options->state = Command\Option::create()->setType() | ||
->setDescription('State information (contains initial url)'); | ||
$options->code = Command\Option::create()->setType()->setIsRequired() | ||
->setDescription('Auth security code'); | ||
} | ||
|
||
public function performAction() | ||
{ | ||
$google = \Rankster\Service\Google::getInstance(); | ||
$token = $google->getToken($this->code); | ||
$info = $google->getUserInfo(); | ||
|
||
if (empty($info['id'])) { | ||
throw new \Exception('Bad response'); | ||
} | ||
|
||
$user = new User(); | ||
$user->email = $info['email']; | ||
if ($saved = $user->findSaved()) { | ||
$user = $saved; | ||
//$user->downloadImage($info['picture'], $user->googleId); | ||
//$user->save(); | ||
if (empty($user->googleId)) { | ||
$user->googleId = $info['id']; | ||
$user->save(); | ||
} | ||
} else { | ||
$user->name = $info['name']; | ||
$user->googleId = $info['id']; | ||
if (!empty($info['picture'])) { | ||
$user->downloadImage($info['picture'], $user->googleId); | ||
} | ||
$user->save(); | ||
} | ||
AuthSession::set($user->id); | ||
header("Location: $this->state"); | ||
exit(); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Rankster\Api\V1\Oauth; | ||
|
||
|
||
use Yaoi\Command; | ||
|
||
class Oauth extends Command | ||
{ | ||
/** | ||
* @var Command | ||
*/ | ||
public $action; | ||
|
||
/** | ||
* @param Command\Definition $definition | ||
* @param \stdClass|static $options | ||
*/ | ||
static function setUpDefinition(Command\Definition $definition, $options) | ||
{ | ||
$options->action = Command\Option::create() | ||
->addToEnum(Google::definition()) | ||
->setIsUnnamed() | ||
->setIsRequired(); | ||
} | ||
|
||
|
||
public function performAction() | ||
{ | ||
$this->action->performAction(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace Rankster\Service; | ||
|
||
|
||
use Rankster\Service\Google\GoogleSettings; | ||
use Yaoi\Command\Web\RequestMapper; | ||
use Yaoi\Http\Client; | ||
use Yaoi\Service; | ||
|
||
/** | ||
* @method GoogleSettings getSettings() | ||
*/ | ||
class Google extends Service | ||
{ | ||
protected static function getSettingsClassName() | ||
{ | ||
return GoogleSettings::className(); | ||
} | ||
|
||
|
||
public function createAuthUrl() | ||
{ | ||
$params = array( | ||
'response_type' => 'code', | ||
'client_id' => $this->getSettings()->clientId, | ||
'redirect_uri' => $this->getSettings()->redirectUrl, | ||
'scope' => 'email profile', | ||
'state' => $_SERVER['REQUEST_URI'], | ||
'access_type' => 'online', | ||
'prompt' => 'select_account', | ||
); | ||
|
||
return 'https://accounts.google.com/o/oauth2/v2/auth?' . http_build_query($params); | ||
} | ||
|
||
|
||
private $accessToken; | ||
|
||
public function getToken($code) { | ||
$params = array( | ||
'code' => $code, | ||
'client_id' => $this->getSettings()->clientId, | ||
'client_secret' => $this->getSettings()->secret, | ||
'redirect_uri' => $this->getSettings()->redirectUrl, | ||
'grant_type' => 'authorization_code', | ||
); | ||
|
||
$client = new Client(); | ||
$client->post = $params; | ||
$tokenResponse = json_decode($client->fetch('https://www.googleapis.com/oauth2/v4/token'),true); | ||
$this->accessToken = $tokenResponse['access_token']; | ||
return $this->accessToken; | ||
} | ||
|
||
public function getUserInfo() { | ||
$url = 'https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token=' . $this->accessToken; | ||
$response = file_get_contents($url); | ||
return json_decode($response, true); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace Rankster\Service\Google; | ||
|
||
use Yaoi\Service\Settings; | ||
|
||
class GoogleSettings extends Settings | ||
{ | ||
public $clientId; | ||
public $secret; | ||
public $redirectUrl; | ||
} |
Oops, something went wrong.