mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
Merge branch 'master' of github.com:appwrite/appwrite into preloader
This commit is contained in:
commit
d3d98d4d05
92 files changed
+551
-256
No files matched your search
@@ -3,6 +3,7 @@
|
||||
namespace Appwrite\Auth\OAuth2;
|
||||
|
||||
use Appwrite\Auth\OAuth2;
|
||||
use Exception;
|
||||
|
||||
// Reference Material
|
||||
// https://developer.okta.com/blog/2019/06/04/what-the-heck-is-sign-in-with-apple
|
||||
@@ -22,6 +23,11 @@ class Apple extends OAuth2
|
||||
"email"
|
||||
];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $claims = [];
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
@@ -58,15 +64,18 @@ class Apple extends OAuth2
|
||||
'https://appleid.apple.com/auth/token',
|
||||
$headers,
|
||||
http_build_query([
|
||||
'grant_type' => 'authorization_code',
|
||||
'code' => $code,
|
||||
'client_id' => $this->appID,
|
||||
'client_secret' => $this->appSecret,
|
||||
'client_secret' => $this->getAppSecret(),
|
||||
'redirect_uri' => $this->callback,
|
||||
'grant_type' => 'authorization_code'
|
||||
])
|
||||
);
|
||||
|
||||
$accessToken = json_decode($accessToken, true);
|
||||
$accessToken = json_decode($accessToken, true);
|
||||
|
||||
$this->claims = (isset($accessToken['id_token'])) ? explode('.', $accessToken['id_token']) : [0 => '', 1 => ''];
|
||||
$this->claims = (isset($this->claims[1])) ? json_decode(base64_decode($this->claims[1]), true) : [];
|
||||
|
||||
if (isset($accessToken['access_token'])) {
|
||||
return $accessToken['access_token'];
|
||||
@@ -82,10 +91,8 @@ class Apple extends OAuth2
|
||||
*/
|
||||
public function getUserID(string $accessToken): string
|
||||
{
|
||||
$user = $this->getUser($accessToken);
|
||||
|
||||
if (isset($user['account_id'])) {
|
||||
return $user['account_id'];
|
||||
if (isset($this->claims['sub']) && !empty($this->claims['sub'])) {
|
||||
return $this->claims['sub'];
|
||||
}
|
||||
|
||||
return '';
|
||||
@@ -98,10 +105,11 @@ class Apple extends OAuth2
|
||||
*/
|
||||
public function getUserEmail(string $accessToken): string
|
||||
{
|
||||
$user = $this->getUser($accessToken);
|
||||
|
||||
if (isset($user['email'])) {
|
||||
return $user['email'];
|
||||
if (isset($this->claims['email']) &&
|
||||
!empty($this->claims['email']) &&
|
||||
isset($this->claims['email_verified']) &&
|
||||
$this->claims['email_verified'] === 'true') {
|
||||
return $this->claims['email'];
|
||||
}
|
||||
|
||||
return '';
|
||||
@@ -114,28 +122,111 @@ class Apple extends OAuth2
|
||||
*/
|
||||
public function getUserName(string $accessToken): string
|
||||
{
|
||||
$user = $this->getUser($accessToken);
|
||||
|
||||
if (isset($user['name'])) {
|
||||
return $user['name']['display_name'];
|
||||
if (isset($this->claims['email']) &&
|
||||
!empty($this->claims['email']) &&
|
||||
isset($this->claims['email_verified']) &&
|
||||
$this->claims['email_verified'] === 'true') {
|
||||
return $this->claims['email'];
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $accessToken
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getUser(string $accessToken): array
|
||||
protected function getAppSecret():string
|
||||
{
|
||||
if (empty($this->user)) {
|
||||
$headers[] = 'Authorization: Bearer '. urlencode($accessToken);
|
||||
$user = $this->request('POST', '', $headers);
|
||||
$this->user = json_decode($user, true);
|
||||
try {
|
||||
$secret = json_decode($this->appSecret, true);
|
||||
} catch (\Throwable $th) {
|
||||
throw new Exception('Invalid secret');
|
||||
}
|
||||
|
||||
return $this->user;
|
||||
$keyfile = (isset($secret['p8'])) ? $secret['p8'] : ''; // Your p8 Key file
|
||||
$keyID = (isset($secret['keyID'])) ? $secret['keyID'] : ''; // Your Key ID
|
||||
$teamID = (isset($secret['teamID'])) ? $secret['teamID'] : ''; // Your Team ID (see Developer Portal)
|
||||
$bundleID = $this->appID; // Your Bundle ID
|
||||
|
||||
$headers = [
|
||||
'alg' => 'ES256',
|
||||
'kid' => $keyID,
|
||||
];
|
||||
|
||||
$claims = [
|
||||
'iss' => $teamID,
|
||||
'iat' => time(),
|
||||
'exp' => time() + 86400*180,
|
||||
'aud' => 'https://appleid.apple.com',
|
||||
'sub' => $bundleID,
|
||||
];
|
||||
|
||||
$pkey = openssl_pkey_get_private($keyfile);
|
||||
|
||||
$payload = $this->encode(json_encode($headers)).'.'.$this->encode(json_encode($claims));
|
||||
|
||||
$signature = '';
|
||||
|
||||
$success = openssl_sign($payload, $signature, $pkey, OPENSSL_ALGO_SHA256);
|
||||
|
||||
if (!$success) return '';
|
||||
|
||||
return $payload.'.'.$this->encode($this->fromDER($signature, 64));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $data
|
||||
*/
|
||||
protected function encode($data)
|
||||
{
|
||||
return str_replace(['+', '/', '='], ['-', '_', ''], base64_encode($data));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $data
|
||||
*/
|
||||
protected function retrievePositiveInteger(string $data): string
|
||||
{
|
||||
while ('00' === mb_substr($data, 0, 2, '8bit') && mb_substr($data, 2, 2, '8bit') > '7f') {
|
||||
$data = mb_substr($data, 2, null, '8bit');
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $der
|
||||
* @param int $partLength
|
||||
*/
|
||||
protected function fromDER(string $der, int $partLength):string
|
||||
{
|
||||
$hex = \unpack('H*', $der)[1];
|
||||
|
||||
if ('30' !== \mb_substr($hex, 0, 2, '8bit')) { // SEQUENCE
|
||||
throw new \RuntimeException();
|
||||
}
|
||||
|
||||
if ('81' === \mb_substr($hex, 2, 2, '8bit')) { // LENGTH > 128
|
||||
$hex = \mb_substr($hex, 6, null, '8bit');
|
||||
}
|
||||
else {
|
||||
$hex = \mb_substr($hex, 4, null, '8bit');
|
||||
}
|
||||
if ('02' !== \mb_substr($hex, 0, 2, '8bit')) { // INTEGER
|
||||
throw new \RuntimeException();
|
||||
}
|
||||
|
||||
$Rl = \hexdec(\mb_substr($hex, 2, 2, '8bit'));
|
||||
$R = $this->retrievePositiveInteger(\mb_substr($hex, 4, $Rl * 2, '8bit'));
|
||||
$R = \str_pad($R, $partLength, '0', STR_PAD_LEFT);
|
||||
|
||||
$hex = \mb_substr($hex, 4 + $Rl * 2, null, '8bit');
|
||||
|
||||
if ('02' !== \mb_substr($hex, 0, 2, '8bit')) { // INTEGER
|
||||
throw new \RuntimeException();
|
||||
}
|
||||
|
||||
$Sl = \hexdec(\mb_substr($hex, 2, 2, '8bit'));
|
||||
$S = $this->retrievePositiveInteger(\mb_substr($hex, 4, $Sl * 2, '8bit'));
|
||||
$S = \str_pad($S, $partLength, '0', STR_PAD_LEFT);
|
||||
|
||||
return \pack('H*', $R.$S);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user