1
0
Fork 0
mirror of synced 2024-06-29 11:40:45 +12:00
appwrite/src/Appwrite/Auth/OAuth2/Bitly.php

194 lines
4.3 KiB
PHP
Raw Normal View History

2020-01-22 09:59:44 +13:00
<?php
namespace Appwrite\Auth\OAuth2;
2020-01-22 09:59:44 +13:00
use Appwrite\Auth\OAuth2;
2020-01-22 09:59:44 +13:00
// Reference Material
// https://dev.bitly.com/v4_documentation.html
2020-02-17 00:41:03 +13:00
class Bitly extends OAuth2
2020-01-22 09:59:44 +13:00
{
/**
* @var string
*/
2022-05-13 03:56:20 +12:00
private string $endpoint = 'https://bitly.com/oauth/';
2020-01-22 09:59:44 +13:00
/**
* @var string
*/
2022-05-13 03:56:20 +12:00
private string $resourceEndpoint = 'https://api-ssl.bitly.com/';
2020-01-22 09:59:44 +13:00
/**
* @var array
*/
2022-05-13 03:56:20 +12:00
protected array $scopes = [];
2020-01-22 09:59:44 +13:00
/**
* @var array
*/
2022-05-13 03:56:20 +12:00
protected array $user = [];
2022-02-01 23:42:11 +13:00
/**
* @var array
*/
2022-05-13 03:56:20 +12:00
protected array $tokens = [];
2020-01-22 09:59:44 +13:00
/**
* @return string
*/
2022-05-13 03:56:20 +12:00
public function getName(): string
2020-01-22 09:59:44 +13:00
{
return 'bitly';
}
/**
* @return string
*/
2022-05-13 03:56:20 +12:00
public function getLoginURL(): string
2020-01-22 09:59:44 +13:00
{
2022-05-13 03:56:20 +12:00
return $this->endpoint . 'authorize?' .
\http_build_query([
2020-01-22 09:59:44 +13:00
'client_id' => $this->appID,
'redirect_uri' => $this->callback,
'state' => \json_encode($this->state)
2020-01-22 09:59:44 +13:00
]);
}
/**
* @param string $code
*
2022-02-01 09:20:17 +13:00
* @return array
2020-01-22 09:59:44 +13:00
*/
2022-02-02 05:47:19 +13:00
protected function getTokens(string $code): array
2020-01-22 09:59:44 +13:00
{
2022-05-13 03:56:20 +12:00
if (empty($this->tokens)) {
2022-02-06 04:38:08 +13:00
$response = $this->request(
2022-02-01 23:42:11 +13: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-06 04:38:08 +13:00
);
2022-02-06 04:49:34 +13:00
$output = [];
\parse_str($response, $output);
$this->tokens = $output;
}
2022-02-01 23:42:11 +13:00
return $this->tokens;
2020-01-22 09:59:44 +13:00
}
2022-02-02 04:54:20 +13:00
/**
* @param string $refreshToken
*
* @return array
*/
2022-05-13 03:56:20 +12:00
public function refreshTokens(string $refreshToken): array
2022-02-02 04:54:20 +13:00
{
2022-02-06 04:38:08 +13: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-06 04:38:08 +13:00
);
$output = [];
\parse_str($response, $output);
$this->tokens = $output;
2022-02-02 04:54:20 +13:00
2022-05-13 03:56:20 +12:00
if (empty($this->tokens['refresh_token'])) {
$this->tokens['refresh_token'] = $refreshToken;
}
2022-02-02 04:54:20 +13:00
return $this->tokens;
}
2020-01-22 09:59:44 +13:00
/**
2022-05-13 03:56:20 +12:00
* @param string $accessToken
2020-01-22 09:59:44 +13:00
*
* @return string
*/
2022-05-13 03:56:20 +12:00
public function getUserID(string $accessToken): string
2020-01-22 09:59:44 +13:00
{
$user = $this->getUser($accessToken);
2022-05-13 03:56:20 +12:00
return $user['login'] ?? '';
2020-01-22 09:59:44 +13:00
}
/**
2022-05-13 03:56:20 +12:00
* @param string $accessToken
2020-01-22 09:59:44 +13:00
*
* @return string
*/
2022-05-13 03:56:20 +12:00
public function getUserEmail(string $accessToken): string
2020-01-22 09:59:44 +13:00
{
$user = $this->getUser($accessToken);
if (isset($user['emails'])) {
2022-05-08 00:06:08 +12:00
foreach ($user['emails'] as $email) {
if ($email['is_verified'] === true) {
return $email['email'];
}
}
2020-01-22 09:59:44 +13:00
}
return '';
}
/**
* Check if the OAuth email is verified
2022-05-24 02:54:50 +12:00
*
2022-05-08 00:06:08 +12:00
* @link https://dev.bitly.com/api-reference#getUser
2022-05-24 02:54:50 +12:00
*
2022-05-13 03:56:20 +12:00
* @param string $accessToken
2022-05-24 02:54:50 +12:00
*
* @return bool
*/
2022-04-28 08:27:21 +12:00
public function isEmailVerified(string $accessToken): bool
{
2022-05-08 00:06:08 +12:00
return true;
}
2020-01-22 09:59:44 +13:00
/**
2022-05-13 03:56:20 +12:00
* @param string $accessToken
2020-01-22 09:59:44 +13:00
*
* @return string
*/
2022-05-13 03:56:20 +12:00
public function getUserName(string $accessToken): string
2020-01-22 09:59:44 +13:00
{
$user = $this->getUser($accessToken);
2022-05-13 03:56:20 +12:00
return $user['name'] ?? '';
2020-01-22 09:59:44 +13:00
}
/**
* @param string $accessToken
*
* @return array
*/
protected function getUser(string $accessToken)
{
$headers = [
2022-05-13 03:56:20 +12:00
'Authorization: Bearer ' . \urlencode($accessToken),
2020-01-22 09:59:44 +13:00
"Accept: application/json"
];
if (empty($this->user)) {
$this->user = \json_decode($this->request('GET', $this->resourceEndpoint . "v4/user", $headers), true);
2020-01-22 09:59:44 +13:00
}
return $this->user;
}
}