1
0
Fork 0
mirror of synced 2024-07-05 22:51:24 +12:00
appwrite/src/Appwrite/Auth/OAuth2/Yandex.php

180 lines
4 KiB
PHP
Raw Normal View History

2020-01-20 17:09:01 +13:00
<?php
namespace Appwrite\Auth\OAuth2;
2020-01-20 17:09:01 +13:00
use Appwrite\Auth\OAuth2;
2020-01-20 17:09:01 +13:00
// Reference Material
// https://tech.yandex.com/passport/doc/dg/reference/request-docpage/
// https://tech.yandex.com/oauth/doc/dg/reference/web-client-docpage/
2020-02-17 00:41:03 +13:00
class Yandex extends OAuth2
2020-01-20 17:09:01 +13:00
{
/**
* @var array
*/
protected $user = [];
2022-02-01 23:42:11 +13:00
/**
* @var array
*/
protected $tokens = [];
2020-01-20 17:09:01 +13:00
/**
* @var array
*/
protected $scopes = [];
/**
* @return string
*/
public function getName(): string
{
return 'Yandex';
}
/**
* @param $state
*
* @return array
2020-01-20 17:09:01 +13:00
*/
public function parseState(string $state)
{
return \json_decode(\html_entity_decode($state), true);
2020-01-20 17:09:01 +13:00
}
/**
* @return string
*/
public function getLoginURL(): string
{
return 'https://oauth.yandex.com/authorize?'.\http_build_query([
2020-01-20 17:09:01 +13:00
'response_type' => 'code',
'client_id' => $this->appID,
'scope'=> \implode(' ', $this->getScopes()),
'state' => \json_encode($this->state)
2020-01-20 17:09:01 +13:00
]);
}
/**
* @param string $code
*
2022-02-01 09:20:17 +13:00
* @return array
2020-01-20 17:09:01 +13:00
*/
2022-02-02 05:47:19 +13:00
protected function getTokens(string $code): array
2020-01-20 17:09:01 +13:00
{
2022-02-01 23:42:11 +13:00
if(empty($this->tokens)) {
$headers = [
'Authorization: Basic ' . \base64_encode($this->appID . ':' . $this->appSecret),
'Content-Type: application/x-www-form-urlencoded',
];
$this->tokens = \json_decode($this->request(
'POST',
'https://oauth.yandex.com/token',
$headers,
\http_build_query([
'code' => $code,
'grant_type' => 'authorization_code'
])
), true);
}
return $this->tokens;
2020-01-20 17:09:01 +13:00
}
2022-02-02 04:54:20 +13:00
/**
* @param string $refreshToken
*
* @return array
*/
public function refreshTokens(string $refreshToken):array
{
$headers = [
'Authorization: Basic ' . \base64_encode($this->appID . ':' . $this->appSecret),
'Content-Type: application/x-www-form-urlencoded',
];
$this->tokens = \json_decode($this->request(
'POST',
'https://oauth.yandex.com/token',
$headers,
\http_build_query([
'refresh_token' => $refreshToken,
'grant_type' => 'authorization_code'
])
), true);
2022-02-02 04:54:20 +13: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-20 17:09:01 +13:00
/**
* @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['default_email'])) {
return $user['default_email'];
}
return '';
}
/**
* @param string $accessToken
*
* @return string
*/
public function getUserName(string $accessToken): string
{
$user = $this->getUser($accessToken);
if (isset($user['display_name'])) {
return $user['display_name'];
}
return '';
}
/**
* @param string $accessToken
*
* @return array
*/
protected function getUser(string $accessToken): array
{
if (empty($this->user)) {
$user = $this->request('GET', 'https://login.yandex.ru/info?'.\http_build_query([
2020-01-20 17:09:01 +13:00
'format' => 'json',
'oauth_token' => $accessToken
]));
$this->user = \json_decode($user, true);
2020-01-20 17:09:01 +13:00
}
return $this->user;
}
}