appwrite/src/Appwrite/Auth/OAuth2/Bitly.php

187 lines
4 KiB
PHP
Raw Normal View History

2020-01-21 20:59:44 +00:00
<?php
namespace Appwrite\Auth\OAuth2;
2020-01-21 20:59:44 +00:00
use Appwrite\Auth\OAuth2;
use Utopia\Exception;
2020-01-21 20:59:44 +00:00
// Reference Material
// https://dev.bitly.com/v4_documentation.html
2020-02-16 11:41:03 +00:00
class Bitly extends OAuth2
2020-01-21 20:59:44 +00:00
{
/**
* @var string
*/
private $endpoint = 'https://bitly.com/oauth/';
/**
* @var string
*/
private $resourceEndpoint = 'https://api-ssl.bitly.com/';
/**
* @var array
*/
protected $scopes = [];
/**
* @var array
*/
protected $user = [];
2022-02-01 10:42:11 +00:00
/**
* @var array
*/
protected $tokens = [];
2020-01-21 20:59:44 +00:00
/**
* @return string
*/
public function getName():string
{
return 'bitly';
}
/**
* @return string
*/
public function getLoginURL():string
{
return $this->endpoint . 'authorize?'.
\http_build_query([
2020-01-21 20:59:44 +00:00
'client_id' => $this->appID,
'redirect_uri' => $this->callback,
'state' => \json_encode($this->state)
2020-01-21 20:59:44 +00:00
]);
}
/**
* @param string $code
*
2022-01-31 20:20:17 +00:00
* @return array
2020-01-21 20:59:44 +00:00
*/
2022-02-01 16:47:19 +00:00
protected function getTokens(string $code): array
2020-01-21 20:59:44 +00:00
{
2022-02-01 10:42:11 +00:00
if(empty($this->tokens)) {
2022-02-05 15:38:08 +00:00
$response = $this->request(
2022-02-01 10:42:11 +00:00
'POST',
$this->resourceEndpoint . 'oauth/access_token',
["Content-Type: application/x-www-form-urlencoded"],
\http_build_query([
"client_id" => $this->appID,
"client_secret" => $this->appSecret,
"code" => $code,
"redirect_uri" => $this->callback,
"state" => \json_encode($this->state)
])
2022-02-05 15:38:08 +00:00
);
$output = [];
\parse_str($response, $output);
$this->tokens = $output;
}
2022-02-01 10:42:11 +00:00
return $this->tokens;
2020-01-21 20:59:44 +00:00
}
2022-02-01 15:54:20 +00:00
/**
* @param string $refreshToken
*
* @return array
*/
public function refreshTokens(string $refreshToken):array
{
2022-02-05 15:38:08 +00:00
$response = $this->request(
'POST',
$this->resourceEndpoint . 'oauth/access_token',
["Content-Type: application/x-www-form-urlencoded"],
\http_build_query([
"client_id" => $this->appID,
"client_secret" => $this->appSecret,
"refresh_token" => $refreshToken,
'grant_type' => 'refresh_token'
])
2022-02-05 15:38:08 +00:00
);
$output = [];
\parse_str($response, $output);
$this->tokens = $output;
2022-02-01 15:54:20 +00:00
if(empty($this->tokens['refresh_token'])) {
$this->tokens['refresh_token'] = $refreshToken;
}
2022-02-01 15:54:20 +00:00
return $this->tokens;
}
2020-01-21 20:59:44 +00:00
/**
* @param $accessToken
*
* @return string
*/
public function getUserID(string $accessToken):string
{
$user = $this->getUser($accessToken);
if (isset($user['login'])) {
return $user['login'];
}
return '';
}
/**
* @param $accessToken
*
* @return string
*/
public function getUserEmail(string $accessToken):string
{
$user = $this->getUser($accessToken);
if (isset($user['emails'])) {
return $user['emails'][0]['email'];
}
return '';
}
/**
* @param $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)
{
$headers = [
'Authorization: Bearer '. \urlencode($accessToken),
2020-01-21 20:59:44 +00:00
"Accept: application/json"
];
if (empty($this->user)) {
$this->user = \json_decode($this->request('GET', $this->resourceEndpoint . "v4/user", $headers), true);
2020-01-21 20:59:44 +00:00
}
return $this->user;
}
}