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

189 lines
4.1 KiB
PHP
Raw Normal View History

2020-01-13 15:30:10 +00:00
<?php
namespace Appwrite\Auth\OAuth2;
2020-01-13 15:30:10 +00:00
use Appwrite\Auth\OAuth2;
2020-01-13 15:30:10 +00:00
// Reference Material
// https://discordapp.com/developers/docs/topics/oauth2
2020-02-16 11:41:03 +00:00
class Discord extends OAuth2
2020-01-13 15:30:10 +00:00
{
/**
* @var string
*/
private $endpoint = 'https://discordapp.com/api';
/**
* @var array
*/
protected $user = [];
2022-02-01 10:42:11 +00:00
/**
* @var array
*/
protected $tokens = [];
2020-01-13 15:30:10 +00:00
2020-01-13 20:59:53 +00:00
/**
* @var array
*/
2020-01-18 20:12:41 +00:00
protected $scopes = [
2020-01-13 15:30:10 +00:00
'identify',
'email'
];
/**
* @return string
*/
public function getName(): string
{
return 'discord';
}
/**
* @return string
*/
public function getLoginURL(): string
{
$url = $this->endpoint . '/oauth2/authorize?'.
\http_build_query([
2020-01-13 15:30:10 +00:00
'response_type' => 'code',
'client_id' => $this->appID,
'state' => \json_encode($this->state),
'scope' => \implode(' ', $this->getScopes()),
2020-01-13 15:30:10 +00:00
'redirect_uri' => $this->callback
]);
return $url;
}
/**
* @param string $code
*
2022-01-31 20:20:17 +00:00
* @return array
2020-01-13 15:30:10 +00:00
*/
2022-02-01 16:47:19 +00:00
protected function getTokens(string $code): array
2020-01-13 15:30:10 +00:00
{
2022-02-01 10:42:11 +00:00
if(empty($this->tokens)) {
$this->tokens = \json_decode($this->request(
'POST',
$this->endpoint . '/oauth2/token',
['Content-Type: application/x-www-form-urlencoded'],
\http_build_query([
'grant_type' => 'authorization_code',
'code' => $code,
'redirect_uri' => $this->callback,
'client_id' => $this->appID,
'client_secret' => $this->appSecret,
'scope' => \implode(' ', $this->getScopes())
])
), true);
}
2022-01-31 20:20:17 +00:00
2022-02-01 10:42:11 +00:00
return $this->tokens;
2020-01-13 15:30:10 +00:00
}
2022-02-01 15:54:20 +00:00
/**
* @param string $refreshToken
*
* @return array
*/
public function refreshTokens(string $refreshToken):array
{
$this->tokens = \json_decode($this->request(
'POST',
$this->endpoint . '/oauth2/token',
['Content-Type: application/x-www-form-urlencoded'],
\http_build_query([
'grant_type' => 'refresh_token',
'refresh_token' => $refreshToken,
'client_id' => $this->appID,
'client_secret' => $this->appSecret,
])
), true);
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-13 15:30:10 +00: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['email'])) {
return $user['email'];
}
return '';
}
/**
* Is the OAuth email verified?
*
* @return bool
*/
public function isEmailVerififed(): bool
{
return false;
}
2020-01-13 15:30:10 +00:00
/**
* @param string $accessToken
*
* @return string
*/
public function getUserName(string $accessToken): string
{
$user = $this->getUser($accessToken);
if (isset($user['username'])) {
return $user['username'];
}
return '';
}
/**
* @param string $accessToken
*
* @return array
*/
protected function getUser(string $accessToken): array
{
if (empty($this->user)) {
$user = $this->request(
'GET',
$this->endpoint . '/users/@me',
['Authorization: Bearer '.\urlencode($accessToken)]
2020-01-13 15:30:10 +00:00
);
$this->user = \json_decode($user, true);
2020-01-13 15:30:10 +00:00
}
return $this->user;
}
}