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

181 lines
4.1 KiB
PHP
Raw Normal View History

2022-03-26 13:54:41 +13:00
<?php
namespace Appwrite\Auth\OAuth2;
use Appwrite\Auth\OAuth2;
class Zoom extends OAuth2
{
/**
* @var string
*/
2022-05-13 03:56:20 +12:00
private string $endpoint = 'https://zoom.us';
2022-03-26 13:54:41 +13:00
/**
* @var string
*/
2022-05-13 03:56:20 +12:00
private string $version = '2022-03-26';
2022-03-26 13:54:41 +13:00
/**
* @var array
*/
2022-05-13 03:56:20 +12:00
protected array $user = [];
2022-03-26 13:54:41 +13:00
/**
* @var array
*/
2022-05-13 03:56:20 +12:00
protected array $tokens = [];
2022-03-26 13:54:41 +13:00
/**
* @var array
*/
2022-05-13 03:56:20 +12:00
protected array $scopes = [
2022-05-20 23:51:35 +12:00
'user_info:read'
2022-03-26 14:33:21 +13:00
];
2022-03-26 13:54:41 +13:00
/**
* @return string
*/
2022-05-13 03:56:20 +12:00
public function getName(): string
2022-03-26 13:54:41 +13:00
{
return 'zoom';
}
/**
* @return string
*/
2022-05-13 03:56:20 +12:00
public function getLoginURL(): string
2022-03-26 13:54:41 +13:00
{
2022-05-13 03:56:20 +12:00
return $this->endpoint . '/oauth/authorize?' . \http_build_query([
2022-03-26 13:54:41 +13:00
'client_id' => $this->appID,
'redirect_uri' => $this->callback,
'response_type' => 'code',
2022-03-26 14:33:21 +13:00
'scope' => \implode(' ', $this->getScopes()),
2022-03-26 13:54:41 +13:00
'state' => \json_encode($this->state),
]);
}
/**
* @param string $code
*
* @return array
*/
protected function getTokens(string $code): array
{
2022-05-13 03:56:20 +12:00
if (empty($this->tokens)) {
2022-03-26 13:54:41 +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 . '/oauth/token',
$headers,
\http_build_query([
'grant_type' => 'authorization_code',
'redirect_uri' => $this->callback,
'code' => $code
])
), true);
}
return $this->tokens;
}
/**
* @param string $refreshToken
*
* @return array
*/
2022-05-13 03:56:20 +12:00
public function refreshTokens(string $refreshToken): array
2022-03-26 13:54:41 +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 . '/oauth/token',
$headers,
\http_build_query([
'grant_type' => 'refresh_token',
'refresh_token' => $refreshToken,
])
), true);
2022-05-13 03:56:20 +12:00
if (empty($this->tokens['refresh_token'])) {
2022-03-26 13:54:41 +13:00
$this->tokens['refresh_token'] = $refreshToken;
}
return $this->tokens;
}
/**
2022-05-13 03:56:20 +12:00
* @param string $accessToken
2022-03-26 13:54:41 +13:00
*
* @return string
*/
2022-05-13 03:56:20 +12:00
public function getUserID(string $accessToken): string
2022-03-26 13:54:41 +13:00
{
$response = $this->getUser($accessToken);
2022-05-13 03:56:20 +12:00
2022-03-26 13:54:41 +13:00
return $response['id'] ?? '';
}
/**
2022-05-13 03:56:20 +12:00
* @param string $accessToken
2022-03-26 13:54:41 +13:00
*
* @return string
*/
2022-05-13 03:56:20 +12:00
public function getUserEmail(string $accessToken): string
2022-03-26 13:54:41 +13:00
{
$response = $this->getUser($accessToken);
2022-05-13 03:56:20 +12:00
2022-03-26 13:54:41 +13:00
return $response['email'] ?? '';
}
/**
* Check if the OAuth email is verified
2022-05-24 02:54:50 +12:00
*
* @link https://marketplace.zoom.us/docs/api-reference/zoom-api/methods/#operation/user
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
{
$user = $this->getUser($accessToken);
2022-05-13 03:56:20 +12:00
if (($user['verified'] ?? false) === 1) {
return true;
}
2022-05-13 03:56:20 +12:00
return false;
}
2022-03-26 13:54:41 +13:00
/**
2022-05-13 03:56:20 +12:00
* @param string $accessToken
2022-03-26 13:54:41 +13:00
*
* @return string
*/
2022-05-13 03:56:20 +12:00
public function getUserName(string $accessToken): string
2022-03-26 13:54:41 +13:00
{
$response = $this->getUser($accessToken);
2022-05-13 03:56:20 +12:00
2022-03-26 14:38:13 +13:00
return ($response['first_name'] ?? '') . ' ' . ($response['last_name'] ?? '');
2022-03-26 13:54:41 +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)
2022-03-26 13:54:41 +13:00
];
if (empty($this->user)) {
$this->user = \json_decode($this->request('GET', 'https://api.zoom.us/v2/users/me', $headers), true);
}
return $this->user;
}
}