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

192 lines
4.3 KiB
PHP
Raw Normal View History

2020-07-04 13:59:17 +00:00
<?php
namespace Appwrite\Auth\OAuth2;
use Appwrite\Auth\OAuth2;
// Reference Material
// https://developer.box.com/reference/
class Box extends OAuth2
{
2020-10-27 19:44:15 +00:00
/**
* @var string
*/
2022-05-12 15:56:20 +00:00
private string $endpoint = 'https://account.box.com/api/oauth2/';
2020-07-04 13:59:17 +00:00
2020-10-27 19:44:15 +00:00
/**
* @var string
*/
2022-05-12 15:56:20 +00:00
private string $resourceEndpoint = 'https://api.box.com/2.0/';
2020-07-04 13:59:17 +00:00
/**
* @var array
*/
2022-05-12 15:56:20 +00:00
protected array $user = [];
2022-02-01 10:42:11 +00:00
/**
* @var array
*/
2022-05-12 15:56:20 +00:00
protected array $tokens = [];
2020-07-04 13:59:17 +00:00
2020-10-27 19:44:15 +00:00
/**
* @var array
*/
2022-05-12 15:56:20 +00:00
protected array $scopes = [
2021-10-05 19:23:04 +00:00
'manage_app_users',
2020-07-04 13:59:17 +00:00
];
/**
* @return string
*/
public function getName(): string
{
return 'box';
}
/**
* @return string
*/
public function getLoginURL(): string
{
2022-05-12 15:56:20 +00:00
$url = $this->endpoint . 'authorize?' .
2021-10-05 19:23:04 +00:00
\http_build_query([
2020-07-04 13:59:17 +00:00
'response_type' => 'code',
'client_id' => $this->appID,
2021-10-05 19:23:04 +00:00
'scope' => \implode(',', $this->getScopes()),
2020-07-04 13:59:17 +00:00
'redirect_uri' => $this->callback,
'state' => \json_encode($this->state),
]);
return $url;
}
/**
* @param string $code
*
2022-01-31 20:20:17 +00:00
* @return array
2020-07-04 13:59:17 +00:00
*/
2022-02-01 16:47:19 +00:00
protected function getTokens(string $code): array
2020-07-04 13:59:17 +00:00
{
2022-05-12 15:56:20 +00:00
if (empty($this->tokens)) {
2022-02-01 10:42:11 +00:00
$headers = ['Content-Type: application/x-www-form-urlencoded'];
$this->tokens = \json_decode($this->request(
'POST',
$this->endpoint . 'token',
$headers,
\http_build_query([
"client_id" => $this->appID,
"client_secret" => $this->appSecret,
"code" => $code,
"grant_type" => "authorization_code",
"scope" => \implode(',', $this->getScopes()),
"redirect_uri" => $this->callback
])
), true);
}
return $this->tokens;
2020-07-04 13:59:17 +00:00
}
2022-02-01 15:54:20 +00:00
/**
* @param string $refreshToken
*
* @return array
*/
2022-05-12 15:56:20 +00:00
public function refreshTokens(string $refreshToken): array
2022-02-01 15:54:20 +00:00
{
$headers = ['Content-Type: application/x-www-form-urlencoded'];
$this->tokens = \json_decode($this->request(
'POST',
$this->endpoint . 'token',
$headers,
\http_build_query([
"client_id" => $this->appID,
"client_secret" => $this->appSecret,
"refresh_token" => $refreshToken,
"grant_type" => "refresh_token",
])
), true);
2022-02-01 15:54:20 +00:00
2022-05-12 15:56: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-07-04 13:59:17 +00:00
/**
* @param string $accessToken
*
* @return string
*/
public function getUserID(string $accessToken): string
{
$user = $this->getUser($accessToken);
2022-05-12 15:56:20 +00:00
return $user['id'] ?? '';
2020-07-04 13:59:17 +00:00
}
/**
* @param string $accessToken
*
* @return string
*/
public function getUserEmail(string $accessToken): string
{
$user = $this->getUser($accessToken);
2022-05-12 15:56:20 +00:00
return $user['login'] ?? '';
2020-07-04 13:59:17 +00:00
}
/**
* Check if the OAuth email is verified
2022-05-23 14:54:50 +00:00
*
2022-05-07 11:03:00 +00:00
* If present, the email is verified. This was verfied through a manual Box sign up process
2022-05-23 14:54:50 +00:00
*
2022-05-12 15:56:20 +00:00
* @param string $accessToken
2022-05-23 14:54:50 +00:00
*
* @return bool
*/
2022-04-27 20:27:21 +00:00
public function isEmailVerified(string $accessToken): bool
{
2022-05-07 11:03:00 +00:00
$email = $this->getUserEmail($accessToken);
2022-05-07 11:03:00 +00:00
return !empty($email);
}
2022-05-12 15:56:20 +00:00
2020-07-04 13:59:17 +00:00
/**
* @param string $accessToken
*
* @return string
*/
public function getUserName(string $accessToken): string
{
$user = $this->getUser($accessToken);
2022-05-12 15:56:20 +00:00
return $user['name'] ?? '';
2020-07-04 13:59:17 +00:00
}
/**
* @param string $accessToken
*
* @return array
*/
protected function getUser(string $accessToken): array
{
$header = [
2022-05-12 15:56:20 +00:00
'Authorization: Bearer ' . \urlencode($accessToken),
2020-07-04 13:59:17 +00:00
];
if (empty($this->user)) {
$user = $this->request(
'GET',
$this->resourceEndpoint . 'me',
$header
);
$this->user = \json_decode($user, true);
}
return $this->user;
}
2021-10-05 19:23:04 +00:00
}