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

220 lines
5.1 KiB
PHP
Raw Normal View History

2022-04-01 10:34:56 +00:00
<?php
namespace Appwrite\Auth\OAuth2;
use Appwrite\Auth\OAuth2;
// Reference Material
// https://auth0.com/docs/api/authentication
class Auth0 extends OAuth2
2022-05-12 15:56:20 +00:00
{
/**
2022-04-01 10:34:56 +00:00
* @var array
*/
2022-05-12 15:56:20 +00:00
protected array $scopes = [
2022-04-01 10:34:56 +00:00
'openid',
'profile',
2022-04-01 11:24:58 +00:00
'email',
'offline_access'
2022-04-01 10:34:56 +00:00
];
2022-05-12 15:56:20 +00:00
2022-04-01 10:34:56 +00:00
/**
* @var array
*/
2022-05-12 15:56:20 +00:00
protected array $user = [];
2022-04-01 10:34:56 +00:00
/**
* @var array
*/
2022-05-12 15:56:20 +00:00
protected array $tokens = [];
2022-04-01 10:34:56 +00:00
/**
* @return string
*/
public function getName(): string
{
return 'auth0';
}
/**
* @return string
*/
public function getLoginURL(): string
{
2022-05-12 15:56:20 +00:00
return 'https://' . $this->getAuth0Domain() . '/authorize?' . \http_build_query([
2022-04-01 10:34:56 +00:00
'client_id' => $this->appID,
'redirect_uri' => $this->callback,
2022-05-12 15:56:20 +00:00
'state' => \json_encode($this->state),
'scope' => \implode(' ', $this->getScopes()),
2022-04-01 10:34:56 +00:00
'response_type' => 'code'
]);
}
/**
* @param string $code
*
* @return array
*/
protected function getTokens(string $code): array
{
2022-05-12 15:56:20 +00:00
if (empty($this->tokens)) {
2022-04-01 10:34:56 +00:00
$headers = ['Content-Type: application/x-www-form-urlencoded'];
$this->tokens = \json_decode($this->request(
'POST',
2022-05-12 15:56:20 +00:00
'https://' . $this->getAuth0Domain() . '/oauth/token',
2022-04-01 10:34:56 +00:00
$headers,
\http_build_query([
'code' => $code,
'client_id' => $this->appID,
'client_secret' => $this->getClientSecret(),
'redirect_uri' => $this->callback,
'scope' => \implode(' ', $this->getScopes()),
'grant_type' => 'authorization_code'
])
), true);
}
return $this->tokens;
}
2022-05-12 15:56:20 +00:00
2022-04-01 10:34:56 +00:00
/**
* @param string $refreshToken
*
* @return array
*/
public function refreshTokens(string $refreshToken): array
2022-04-01 10:34:56 +00:00
{
$headers = ['Content-Type: application/x-www-form-urlencoded'];
$this->tokens = \json_decode($this->request(
'POST',
2022-05-12 15:56:20 +00:00
'https://' . $this->getAuth0Domain() . '/oauth/token',
2022-04-01 10:34:56 +00:00
$headers,
\http_build_query([
'refresh_token' => $refreshToken,
'client_id' => $this->appID,
'client_secret' => $this->getClientSecret(),
'grant_type' => 'refresh_token'
])
), true);
2022-05-12 15:56:20 +00:00
if (empty($this->tokens['refresh_token'])) {
2022-04-01 10:34:56 +00:00
$this->tokens['refresh_token'] = $refreshToken;
}
return $this->tokens;
}
/**
* @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['sub'] ?? '';
2022-04-01 10:34:56 +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['email'] ?? '';
2022-04-01 10:34:56 +00:00
}
2022-05-04 22:45:16 +00:00
/**
* Check if the OAuth email is verified
*
2022-05-07 12:10:28 +00:00
* @link https://auth0.com/docs/api/authentication?javascript#user-profile
*
2022-05-12 15:56:20 +00:00
* @param string $accessToken
2022-05-04 22:45:16 +00:00
*
* @return bool
*/
public function isEmailVerified(string $accessToken): bool
{
2022-05-07 12:10:28 +00:00
$user = $this->getUser($accessToken);
2022-05-12 15:56:20 +00:00
if ($user['email_verified'] ?? false) {
2022-05-07 12:10:28 +00:00
return true;
}
2022-05-04 22:45:16 +00:00
return false;
}
2022-04-01 10:34:56 +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'] ?? '';
2022-04-01 10:34:56 +00:00
}
2022-05-12 15:56:20 +00:00
/**
2022-04-01 10:34:56 +00:00
* @param string $accessToken
*
* @return array
*/
protected function getUser(string $accessToken): array
2022-04-01 10:34:56 +00:00
{
if (empty($this->user)) {
2022-05-12 15:56:20 +00:00
$headers = ['Authorization: Bearer ' . \urlencode($accessToken)];
$user = $this->request('GET', 'https://' . $this->getAuth0Domain() . '/userinfo', $headers);
2022-04-01 10:34:56 +00:00
$this->user = \json_decode($user, true);
}
return $this->user;
}
/**
* Extracts the Client Secret from the JSON stored in appSecret
2022-04-03 16:37:49 +00:00
*
2022-04-01 10:34:56 +00:00
* @return string
*/
protected function getClientSecret(): string
{
2022-04-03 16:37:49 +00:00
$secret = $this->getAppSecret();
2022-04-01 10:34:56 +00:00
2022-05-12 15:56:20 +00:00
return $secret['clientSecret'] ?? '';
2022-04-01 10:34:56 +00:00
}
2022-05-12 15:56:20 +00:00
/**
2022-04-03 16:37:49 +00:00
* Extracts the Auth0 Domain from the JSON stored in appSecret
*
2022-04-01 10:34:56 +00:00
* @return string
*/
protected function getAuth0Domain(): string
{
2022-04-03 16:37:49 +00:00
$secret = $this->getAppSecret();
2022-05-12 15:56:20 +00:00
return $secret['auth0Domain'] ?? '';
2022-04-01 10:34:56 +00:00
}
/**
* Decode the JSON stored in appSecret
2022-04-03 16:37:49 +00:00
*
2022-04-01 10:34:56 +00:00
* @return array
*/
protected function getAppSecret(): array
2022-05-12 15:56:20 +00:00
{
2022-04-01 10:34:56 +00:00
try {
$secret = \json_decode($this->appSecret, true, 512, JSON_THROW_ON_ERROR);
2022-04-01 10:34:56 +00:00
} catch (\Throwable $th) {
throw new \Exception('Invalid secret');
2022-04-01 10:34:56 +00:00
}
return $secret;
}
2022-05-12 15:56:20 +00:00
}