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

201 lines
4.4 KiB
PHP
Raw Normal View History

2020-01-18 06:58:00 +13:00
<?php
namespace Appwrite\Auth\OAuth2;
2020-01-18 06:58:00 +13:00
use Appwrite\Auth\OAuth2;
2020-01-18 06:58:00 +13:00
// Reference Material
2020-01-19 01:32:02 +13:00
// https://developer.yahoo.com/oauth2/guide/
2020-01-18 06:58:00 +13:00
2020-02-17 00:41:03 +13:00
class Yahoo extends OAuth2
2020-01-18 06:58:00 +13:00
{
/**
* @var string
*/
2022-05-13 03:56:20 +12:00
private string $endpoint = 'https://api.login.yahoo.com/oauth2/';
2020-01-18 06:58:00 +13:00
/**
* @var string
*/
2022-05-13 03:56:20 +12:00
private string $resourceEndpoint = 'https://api.login.yahoo.com/openid/v1/userinfo';
2020-01-18 06:58:00 +13:00
/**
* @var array
*/
2022-05-13 03:56:20 +12:00
protected array $scopes = [
2020-01-18 06:58:00 +13:00
'sdct-r',
'sdpp-w',
];
/**
* @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-18 06:58:00 +13:00
/**
* @return string
*/
2022-05-13 03:56:20 +12:00
public function getName(): string
2020-01-18 06:58:00 +13:00
{
return 'yahoo';
}
2020-01-18 07:48:46 +13:00
/**
* @param $state
*
* @return array
2020-01-18 07:48:46 +13:00
*/
public function parseState(string $state)
{
return \json_decode(\html_entity_decode($state), true);
2020-01-18 07:48:46 +13:00
}
2020-01-18 06:58:00 +13:00
/**
* @return string
*/
2022-05-13 03:56:20 +12:00
public function getLoginURL(): string
2020-01-18 06:58:00 +13:00
{
2022-05-13 03:56:20 +12:00
return $this->endpoint . 'request_auth?' .
\http_build_query([
2020-01-18 06:58:00 +13:00
'response_type' => 'code',
'client_id' => $this->appID,
'scope' => \implode(' ', $this->getScopes()),
2020-01-18 06:58:00 +13:00
'redirect_uri' => $this->callback,
'state' => \json_encode($this->state)
2020-01-18 06:58:00 +13:00
]);
}
/**
* @param string $code
*
2022-02-01 09:20:17 +13:00
* @return array
2020-01-18 06:58:00 +13:00
*/
2022-02-02 05:47:19 +13:00
protected function getTokens(string $code): array
2020-01-18 06:58:00 +13:00
{
2022-05-13 03:56:20 +12:00
if (empty($this->tokens)) {
2022-02-01 23:42:11 +13:00
$headers = [
'Authorization: Basic ' . \base64_encode($this->appID . ':' . $this->appSecret),
'Content-Type: application/x-www-form-urlencoded',
];
$this->tokens = \json_decode($this->request(
'POST',
$this->endpoint . 'get_token',
$headers,
\http_build_query([
"code" => $code,
"grant_type" => "authorization_code",
"redirect_uri" => $this->callback
])
), true);
}
return $this->tokens;
2020-01-18 06:58:00 +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
{
$headers = [
'Authorization: Basic ' . \base64_encode($this->appID . ':' . $this->appSecret),
'Content-Type: application/x-www-form-urlencoded',
];
$this->tokens = \json_decode($this->request(
'POST',
$this->endpoint . 'get_token',
$headers,
\http_build_query([
"refresh_token" => $refreshToken,
"grant_type" => "refresh_token",
])
), true);
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-18 06:58:00 +13:00
/**
2022-05-13 03:56:20 +12:00
* @param string $accessToken
2020-01-18 06:58:00 +13:00
*
* @return string
*/
2022-05-13 03:56:20 +12:00
public function getUserID(string $accessToken): string
2020-01-18 06:58:00 +13:00
{
$user = $this->getUser($accessToken);
2022-05-13 03:56:20 +12:00
return $user['sub'] ?? '';
2020-01-18 06:58:00 +13:00
}
/**
2022-05-13 03:56:20 +12:00
* @param string $accessToken
2020-01-18 06:58:00 +13:00
*
* @return string
*/
2022-05-13 03:56:20 +12:00
public function getUserEmail(string $accessToken): string
2020-01-18 06:58:00 +13:00
{
$user = $this->getUser($accessToken);
2022-05-13 03:56:20 +12:00
return $user['email'] ?? '';
2020-01-18 06:58:00 +13:00
}
/**
* Check if the OAuth email is verified
2022-05-24 02:54:50 +12:00
*
2022-04-28 08:46:34 +12:00
* If present, the email is verified. This was verfied through a manual Yahoo sign up process
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-04-28 08:46:34 +12:00
$email = $this->getUserEmail($accessToken);
return !empty($email);
}
2020-01-18 06:58:00 +13:00
/**
2022-05-13 03:56:20 +12:00
* @param string $accessToken
2020-01-18 06:58:00 +13:00
*
* @return string
*/
2022-05-13 03:56:20 +12:00
public function getUserName(string $accessToken): string
2020-01-18 06:58:00 +13:00
{
$user = $this->getUser($accessToken);
2022-05-13 03:56:20 +12:00
return $user['name'] ?? '';
2020-01-18 06:58:00 +13:00
}
/**
* @param string $accessToken
*
* @return array
*/
protected function getUser(string $accessToken)
{
if (empty($this->user)) {
2020-06-25 09:05:16 +12:00
$this->user = \json_decode($this->request(
'GET',
$this->resourceEndpoint,
2022-05-13 03:56:20 +12:00
['Authorization: Bearer ' . \urlencode($accessToken)]
2020-06-25 09:05:16 +12:00
), true);
2020-01-18 06:58:00 +13:00
}
return $this->user;
}
}