Created
August 21, 2012 13:21
-
-
Save Mikulas/3415331 to your computer and use it in GitHub Desktop.
GoogleAuth
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
<?php | |
use \Nette\Utils\Json; | |
/** | |
* Minimalistic Google OAuth2 connector | |
* @author Mikuláš Dítě | |
* @license BSD-3 | |
*/ | |
class Google extends Nette\Object | |
{ | |
const URL_AUTH = 'https://accounts.google.com/o/oauth2/auth'; | |
const URL_TOKEN = 'https://accounts.google.com/o/oauth2/token'; | |
const URL_INFO = 'https://www.googleapis.com/oauth2/v1/userinfo'; | |
/** @var string client_id */ | |
private $id; | |
/** @var string client_secret */ | |
private $secret; | |
public function __construct(array $config) | |
{ | |
$this->id = $config['id']; | |
$this->secret = $config['secret']; | |
} | |
public function getLoginUrl(array $args) | |
{ | |
$query = [ | |
'response_type' => 'code', | |
'client_id' => $this->id, | |
'redirect_uri' => $args['redirect_uri'], | |
'access_type' => 'online', | |
'scope' => implode(' ', $args['scope']), | |
//'approval_prompt' => 'force', | |
]; | |
return self::URL_AUTH . '?' . http_build_query($query); | |
} | |
public function getToken($code, $uri) | |
{ | |
$query = [ | |
'code' => $code, | |
'redirect_uri' => $uri, | |
'client_id' => $this->id, | |
'client_secret' => $this->secret, | |
'grant_type' => 'authorization_code', | |
]; | |
$content = http_build_query($query); | |
$c = curl_init(); | |
curl_setopt_array($c, [ | |
CURLOPT_URL => self::URL_TOKEN, | |
CURLOPT_POST => TRUE, | |
CURLOPT_POSTFIELDS => $content, | |
CURLOPT_RETURNTRANSFER => TRUE, | |
CURLOPT_SSL_VERIFYHOST => FALSE, | |
CURLOPT_SSL_VERIFYPEER => FALSE, | |
CURLOPT_HTTPHEADER => [ | |
'Content-type: application/x-www-form-urlencoded', | |
'Content-length: ' . strlen($content) | |
], | |
]); | |
$res = curl_exec($c); | |
curl_close($c); | |
$data = Json::decode($res); | |
if (isset($data->error)) { | |
throw new GoogleException("Error while obtaining token from code: {$data->error}"); | |
} | |
return $data->access_token; | |
} | |
public function getInfo($token) | |
{ | |
$res = file_get_contents(self::URL_INFO . '?' . http_build_query([ | |
'access_token' => $token, | |
])); | |
return Json::decode($res); | |
} | |
} | |
class GoogleException extends RuntimeException {} |
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
<?php | |
class GoogleAuth extends Nette\Object implements \Nette\Security\IAuthenticator | |
{ | |
/** @var Google */ | |
protected $google; | |
/** @var Table\Users */ | |
protected $users; | |
public function __construct(Google $google, Users $users) | |
{ | |
$this->google = $google; | |
$this->users = $users; | |
} | |
public function authenticate(array $data) | |
{ | |
$info = end($data); | |
$user = $this->users->findOneBy(['google_id' => $info->id]); | |
// If user with this email exists, link the accounts | |
if (!$user) { | |
$user = $this->users->findOneBy(['mail' => $info->email]); | |
if ($user) { | |
$user->google_id = $info->id; | |
$user->update(); | |
} | |
} | |
// otherwise, register new user | |
if (!$user) { | |
$user = $this->users->insert([ | |
'name' => $info->name, | |
'google_id' => $info->id, | |
'mail' => $info->email, | |
'role' => '', | |
]); | |
} | |
$roles = explode(';', $user->role); | |
$roles[] = 'google'; | |
return new \Nette\Security\Identity($user->id, $roles, []); | |
} | |
} |
Nette addon + packagist pls? :)
Example: https://github.com/Mikulas/sandbox/blob/bootstrap/app/presenters/SignPresenter.php#L1
Ok nahodim to :)
Všude rvát pole a nenamespacovat třídy je v nette norma? (ptám se zcela vážně - nechci flamovat) Mám na mysli:
public function __construct(array $config)
public function getLoginUrl(array $args)
Co když udělám new Google(array()) - to mi přece vyhodí notice, ne? A v čem je to lepší než
public function __construct($id, $secret)
To jo, vyhodí. Lepší to nutně není, ale chtěl jsem to api podobné jako má facebook. Nechci přemýšlet, která služba má jaké api.
Jasně, nějaké ošetření by tam být mohlo. Ale to by to potom nebylo tak minimalistické ;)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
license? :-)