feat: added Google OAuth Support
This commit is contained in:
parent
ef3d72ff54
commit
faa63aba97
6 changed files with 33 additions and 145 deletions
|
@ -19,7 +19,7 @@ return [
|
||||||
'gitlab' => [
|
'gitlab' => [
|
||||||
'developers' => 'https://docs.gitlab.com/ee/api/',
|
'developers' => 'https://docs.gitlab.com/ee/api/',
|
||||||
'icon' => 'icon-gitlab',
|
'icon' => 'icon-gitlab',
|
||||||
'enabled' => true,
|
'enabled' => false,
|
||||||
],
|
],
|
||||||
'google' => [
|
'google' => [
|
||||||
'developers' => 'https://developers.google.com/',
|
'developers' => 'https://developers.google.com/',
|
||||||
|
|
|
@ -740,6 +740,7 @@ $utopia->get('/v1/auth/oauth/:provider/redirect')
|
||||||
],
|
],
|
||||||
]) : $user;
|
]) : $user;
|
||||||
|
|
||||||
|
|
||||||
if (empty($user)) { // No user logged in or with oauth provider ID, create new one or connect with account with same email
|
if (empty($user)) { // No user logged in or with oauth provider ID, create new one or connect with account with same email
|
||||||
$name = $oauth->getUserName($accessToken);
|
$name = $oauth->getUserName($accessToken);
|
||||||
$email = $oauth->getUserEmail($accessToken);
|
$email = $oauth->getUserEmail($accessToken);
|
||||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 4 KiB After Width: | Height: | Size: 716 KiB |
|
@ -94,13 +94,15 @@ abstract class OAuth
|
||||||
curl_setopt($ch, CURLOPT_HEADER, 0);
|
curl_setopt($ch, CURLOPT_HEADER, 0);
|
||||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||||
curl_setopt($ch, CURLOPT_USERAGENT, 'Console_OAuth_Agent');
|
curl_setopt($ch, CURLOPT_USERAGENT, 'Console_OAuth_Agent');
|
||||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
|
||||||
|
|
||||||
if (!empty($payload)) {
|
if (!empty($payload)) {
|
||||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send the request & save response to $resp
|
$headers[] = "Content-length: ".strlen($payload);
|
||||||
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||||||
|
|
||||||
|
// Send the request & save response to $response
|
||||||
$response = curl_exec($ch);
|
$response = curl_exec($ch);
|
||||||
|
|
||||||
curl_close($ch);
|
curl_close($ch);
|
||||||
|
|
|
@ -1,121 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace Auth\OAuth;
|
|
||||||
|
|
||||||
use Auth\OAuth;
|
|
||||||
|
|
||||||
class Gitlab extends OAuth
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
protected $version = 'v2.8';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected $user = [];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getName():string
|
|
||||||
{
|
|
||||||
return 'google';
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getLoginURL():string
|
|
||||||
{
|
|
||||||
return 'https://www.google.com/'.$this->version.'/dialog/oauth?client_id='.urlencode($this->appID).'&redirect_uri='.urlencode($this->callback).'&scope=email&state='.urlencode(json_encode($this->state));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $code
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getAccessToken(string $code):string
|
|
||||||
{
|
|
||||||
$accessToken = $this->request('GET', 'https://graph.google.com/'.$this->version.'/oauth/access_token?'.
|
|
||||||
'client_id='.urlencode($this->appID).
|
|
||||||
'&redirect_uri='.urlencode($this->callback).
|
|
||||||
'&client_secret='.urlencode($this->appSecret).
|
|
||||||
'&code='.urlencode($code)
|
|
||||||
);
|
|
||||||
|
|
||||||
$accessToken = json_decode($accessToken, true); //
|
|
||||||
|
|
||||||
if (isset($accessToken['access_token'])) {
|
|
||||||
return $accessToken['access_token'];
|
|
||||||
}
|
|
||||||
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $accessToken
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getUserID(string $accessToken):string
|
|
||||||
{
|
|
||||||
$user = $this->getUser($accessToken);
|
|
||||||
|
|
||||||
if (isset($user['id'])) {
|
|
||||||
return $user['id'];
|
|
||||||
}
|
|
||||||
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $accessToken
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getUserEmail(string $accessToken):string
|
|
||||||
{
|
|
||||||
$user = $this->getUser($accessToken);
|
|
||||||
|
|
||||||
if (isset($user['email'])) {
|
|
||||||
return $user['email'];
|
|
||||||
}
|
|
||||||
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $accessToken
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getUserName(string $accessToken):string
|
|
||||||
{
|
|
||||||
$user = $this->getUser($accessToken);
|
|
||||||
|
|
||||||
if (isset($user['name'])) {
|
|
||||||
return $user['name'];
|
|
||||||
}
|
|
||||||
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $accessToken
|
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
protected function getUser(string $accessToken):array
|
|
||||||
{
|
|
||||||
if (empty($this->user)) {
|
|
||||||
$user = $this->request('GET', 'https://graph.google.com/'.$this->version.'/me?fields=email,name&access_token='.urlencode($accessToken));
|
|
||||||
|
|
||||||
$this->user = json_decode($user, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->user;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -9,8 +9,7 @@ class Google extends OAuth
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected $version = 'v2.8';
|
protected $version = 'v4';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
|
@ -29,7 +28,12 @@ class Google extends OAuth
|
||||||
*/
|
*/
|
||||||
public function getLoginURL(): string
|
public function getLoginURL(): string
|
||||||
{
|
{
|
||||||
return 'https://www.google.com/'.$this->version.'/dialog/oauth?client_id='.urlencode($this->appID).'&redirect_uri='.urlencode($this->callback).'&scope=email&state='.urlencode(json_encode($this->state));
|
return 'https://accounts.google.com/o/oauth2/v2/auth?' .
|
||||||
|
'client_id=' . urlencode($this->appID) .
|
||||||
|
'&redirect_uri=' . urlencode($this->callback) .
|
||||||
|
'&scope=https://www.googleapis.com/auth/userinfo.email+https://www.googleapis.com/auth/userinfo.profile' .
|
||||||
|
'&state=' . urlencode(json_encode($this->state)) .
|
||||||
|
'&response_type=code';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -39,14 +43,18 @@ class Google extends OAuth
|
||||||
*/
|
*/
|
||||||
public function getAccessToken(string $code): string
|
public function getAccessToken(string $code): string
|
||||||
{
|
{
|
||||||
$accessToken = $this->request('GET', 'https://graph.google.com/'.$this->version.'/oauth/access_token?'.
|
$accessToken = $this->request(
|
||||||
'client_id='.urlencode($this->appID).
|
'POST',
|
||||||
'&redirect_uri='.urlencode($this->callback).
|
'https://www.googleapis.com/oauth2/' . $this->version . '/token?' .
|
||||||
|
'code=' . urlencode($code) .
|
||||||
|
'&client_id=' . urlencode($this->appID) .
|
||||||
'&client_secret=' . urlencode($this->appSecret) .
|
'&client_secret=' . urlencode($this->appSecret) .
|
||||||
'&code='.urlencode($code)
|
'&redirect_uri=' . urlencode($this->callback) .
|
||||||
|
'&scope=' .
|
||||||
|
'&grant_type=authorization_code'
|
||||||
);
|
);
|
||||||
|
|
||||||
$accessToken = json_decode($accessToken, true); //
|
$accessToken = json_decode($accessToken, true);
|
||||||
|
|
||||||
if (isset($accessToken['access_token'])) {
|
if (isset($accessToken['access_token'])) {
|
||||||
return $accessToken['access_token'];
|
return $accessToken['access_token'];
|
||||||
|
@ -111,11 +119,9 @@ class Google extends OAuth
|
||||||
protected function getUser(string $accessToken): array
|
protected function getUser(string $accessToken): array
|
||||||
{
|
{
|
||||||
if (empty($this->user)) {
|
if (empty($this->user)) {
|
||||||
$user = $this->request('GET', 'https://graph.google.com/'.$this->version.'/me?fields=email,name&access_token='.urlencode($accessToken));
|
$user = $this->request('GET', 'https://www.googleapis.com/oauth2/v2/userinfo?access_token=' . urlencode($accessToken));
|
||||||
|
|
||||||
$this->user = json_decode($user, true);
|
$this->user = json_decode($user, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->user;
|
return $this->user;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue