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

187 lines
4.1 KiB
PHP
Raw Normal View History

2020-01-16 04:50:22 +13:00
<?php
namespace Appwrite\Auth\OAuth2;
2020-01-16 04:50:22 +13:00
use Appwrite\Auth\OAuth2;
2020-01-16 04:50:22 +13:00
// Reference Material
// https://dev.twitch.tv/docs/authentication
2020-02-17 00:41:03 +13:00
class Twitch extends OAuth2
2020-01-16 04:50:22 +13:00
{
/**
* @var string
*/
2022-05-13 03:56:20 +12:00
private string $endpoint = 'https://id.twitch.tv/oauth2/';
2020-01-16 09:57:32 +13:00
/**
* @var string
*/
2022-05-13 03:56:20 +12:00
private string $resourceEndpoint = 'https://api.twitch.tv/helix/users';
2020-01-16 04:50:22 +13:00
/**
* @var array
*/
2022-05-13 03:56:20 +12:00
protected array $scopes = [
2021-05-28 20:23:46 +12:00
'user:read:email',
2020-01-16 04:50:22 +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-16 04:50:22 +13:00
/**
* @return string
*/
2022-05-13 03:56:20 +12:00
public function getName(): string
2020-01-16 04:50:22 +13:00
{
return 'twitch';
}
/**
* @return string
*/
2022-05-13 03:56:20 +12:00
public function getLoginURL(): string
2020-01-16 04:50:22 +13:00
{
2022-05-13 03:56:20 +12:00
return $this->endpoint . 'authorize?' .
\http_build_query([
2020-01-16 04:50:22 +13:00
'response_type' => 'code',
'client_id' => $this->appID,
'scope' => \implode(' ', $this->getScopes()),
2020-01-16 04:50:22 +13:00
'redirect_uri' => $this->callback,
2020-01-16 09:57:32 +13:00
'force_verify' => true,
'state' => \json_encode($this->state)
2020-01-16 04:50:22 +13:00
]);
}
/**
* @param string $code
*
2022-02-01 09:20:17 +13:00
* @return array
2020-01-16 04:50:22 +13:00
*/
2022-02-02 05:47:19 +13:00
protected function getTokens(string $code): array
2020-01-16 04:50:22 +13:00
{
2022-05-13 03:56:20 +12:00
if (empty($this->tokens)) {
2022-02-01 23:42:11 +13:00
$this->tokens = \json_decode($this->request(
'POST',
$this->endpoint . 'token?' . \http_build_query([
"client_id" => $this->appID,
"client_secret" => $this->appSecret,
"code" => $code,
"grant_type" => "authorization_code",
"redirect_uri" => $this->callback
])
), true);
}
return $this->tokens;
2020-01-16 04:50:22 +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
{
$this->tokens = \json_decode($this->request(
'POST',
$this->endpoint . 'token?' . \http_build_query([
"client_id" => $this->appID,
"client_secret" => $this->appSecret,
"refresh_token" => $refreshToken,
"grant_type" => "refresh_token",
])
), true);
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-16 04:50:22 +13:00
/**
2022-05-13 03:56:20 +12:00
* @param string $accessToken
2020-01-16 04:50:22 +13:00
*
* @return string
*/
2022-05-13 03:56:20 +12:00
public function getUserID(string $accessToken): string
2020-01-16 04:50:22 +13:00
{
$user = $this->getUser($accessToken);
2022-05-13 03:56:20 +12:00
return $user['id'] ?? '';
2020-01-16 04:50:22 +13:00
}
/**
2022-05-13 03:56:20 +12:00
* @param string $accessToken
2020-01-16 04:50:22 +13:00
*
* @return string
*/
2022-05-13 03:56:20 +12:00
public function getUserEmail(string $accessToken): string
2020-01-16 04:50:22 +13:00
{
2020-01-16 09:57:32 +13:00
$user = $this->getUser($accessToken);
2020-01-16 04:50:22 +13:00
2022-05-13 03:56:20 +12:00
return $user['email'] ?? '';
2020-01-16 04:50:22 +13:00
}
/**
* Check if the OAuth email is verified
2022-05-24 02:54:50 +12:00
*
2022-04-28 08:25:28 +12:00
* If present, the email is verified
2022-05-24 02:54:50 +12:00
*
2022-04-28 08:25:28 +12:00
* @link https://dev.twitch.tv/docs/api/reference#get-users
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:25:28 +12:00
$email = $this->getUserEmail($accessToken);
2022-05-13 03:56:20 +12:00
2022-04-28 08:25:28 +12:00
return !empty($email);
}
2020-01-16 04:50:22 +13:00
/**
2022-05-13 03:56:20 +12:00
* @param string $accessToken
2020-01-16 04:50:22 +13:00
*
* @return string
*/
2022-05-13 03:56:20 +12:00
public function getUserName(string $accessToken): string
2020-01-16 04:50:22 +13:00
{
$user = $this->getUser($accessToken);
2022-05-13 03:56:20 +12:00
return $user['display_name'] ?? '';
2020-01-16 04:50:22 +13:00
}
/**
* @param string $accessToken
*
* @return array
*/
protected function getUser(string $accessToken)
{
if (empty($this->user)) {
2021-05-28 20:23:46 +12:00
$response = \json_decode($this->request(
2020-06-25 09:05:16 +12:00
'GET',
2021-10-06 08:23:04 +13:00
$this->resourceEndpoint,
[
2022-05-13 03:56:20 +12:00
'Authorization: Bearer ' . \urlencode($accessToken),
'Client-Id: ' . \urlencode($this->appID)
2021-05-28 20:23:46 +12:00
]
), true);
$this->user = $response['data']['0'] ?? [];
2020-01-16 04:50:22 +13:00
}
return $this->user;
}
}