Merge branch '0.16.x' into feat-variables-api

This commit is contained in:
Matej Bačo
2022-08-31 09:37:47 +02:00
committed by GitHub
64 changed files with 874 additions and 222 deletions
+200
View File
@@ -0,0 +1,200 @@
<?php
namespace Appwrite\Auth\OAuth2;
use Appwrite\Auth\OAuth2;
class Etsy extends OAuth2
{
/**
* @var string
*/
private string $endpoint = 'https://api.etsy.com/v3/public';
/**
* @var string
*/
private string $version = '2022-07-14';
/**
* @var array
*/
protected array $user = [];
/**
* @var array
*/
protected array $tokens = [];
/**
* @var array
*/
protected array $scopes = [
"email_r",
"profile_r",
];
/**
* @var string
*/
private string $pkce = '';
/**
* @return string
*/
private function getPKCE(): string
{
if (empty($this->pkce)) {
$this->pkce = \bin2hex(\random_bytes(rand(43, 128)));
}
return $this->pkce;
}
/**
* @return string
*/
public function getName(): string
{
return 'etsy';
}
/**
* @return string
*/
public function getLoginURL(): string
{
return 'https://www.etsy.com/oauth/connect/oauth/authorize?' . \http_build_query([
'client_id' => $this->appID,
'redirect_uri' => $this->callback,
'response_type' => 'code',
'state' => \json_encode($this->state),
'scope' => $this->scopes,
'code_challenge' => $this->getPKCE(),
'code_challenge_method' => 'S256',
]);
}
/**
* @param string $code
*
* @return array
*/
protected function getTokens(string $code): array
{
if (empty($this->tokens)) {
$headers = ['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',
'client_id' => $this->appID,
'redirect_uri' => $this->callback,
'code' => $code,
'code_verifier' => $this->getPKCE(),
])
), true);
}
return $this->tokens;
}
/**
* @param string $refreshToken
*
* @return array
*/
public function refreshTokens(string $refreshToken): array
{
$headers = ['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',
'client_id' => $this->appID,
'refresh_token' => $refreshToken,
])
), true);
if (empty($this->tokens['refresh_token'])) {
$this->tokens['refresh_token'] = $refreshToken;
}
return $this->tokens;
}
/**
* @param $accessToken
*
* @return string
*/
public function getUserID(string $accessToken): string
{
$components = explode('.', $accessToken);
return $components[0];
}
/**
* @param $accessToken
*
* @return string
*/
public function getUserEmail(string $accessToken): string
{
return $this->getUser($accessToken)['primary_email'];
}
/**
* Check if the OAuth email is verified
*
* OAuth is only allowed if account has been verified through Etsy, itself.
*
* @param string $accessToken
*
* @return bool
*/
public function isEmailVerified(string $accessToken): bool
{
$email = $this->getUserEmail($accessToken);
return !empty($email);
}
/**
* @param $accessToken
*
* @return string
*/
public function getUserName(string $accessToken): string
{
return $this->getUser($accessToken)['login_name'];
}
/**
* @param string $accessToken
*
* @return array
*/
protected function getUser(string $accessToken): array
{
if (!empty($this->user)) {
return $this->user;
}
$headers = ['Authorization: Bearer ' . $accessToken];
$this->user = \json_decode($this->request(
'GET',
'https://api.etsy.com/v3/application/users/' . $this->getUserID($accessToken),
), true);
return $this->user;
}
}
@@ -304,7 +304,7 @@ class OpenAPI3 extends Format
case 'Utopia\Database\Validator\DatetimeValidator':
$node['schema']['type'] = $validator->getType();
$node['schema']['format'] = 'datetime';
$node['schema']['x-example'] = '2022-06-15T13:45:30.496';
$node['schema']['x-example'] = Model::TYPE_DATETIME_EXAMPLE;
break;
case 'Appwrite\Network\Validator\Email':
$node['schema']['type'] = $validator->getType();
@@ -300,7 +300,7 @@ class Swagger2 extends Format
case 'Utopia\Database\Validator\DatetimeValidator':
$node['type'] = $validator->getType();
$node['format'] = 'datetime';
$node['x-example'] = '2022-06-15T13:45:30.496';
$node['x-example'] = Model::TYPE_DATETIME_EXAMPLE;
break;
case 'Appwrite\Network\Validator\Email':
$node['type'] = $validator->getType();
@@ -114,6 +114,7 @@ class Queries extends Validator
return true;
}
/**
* Is array
*
@@ -2,8 +2,6 @@
namespace Appwrite\Utopia\Database\Validator\Queries;
use Appwrite\Utopia\Database\Validator\Queries\Base;
class Buckets extends Base
{
public const ALLOWED_ATTRIBUTES = [
@@ -2,8 +2,6 @@
namespace Appwrite\Utopia\Database\Validator\Queries;
use Appwrite\Utopia\Database\Validator\Queries\Base;
class Collections extends Base
{
public const ALLOWED_ATTRIBUTES = [
@@ -2,8 +2,6 @@
namespace Appwrite\Utopia\Database\Validator\Queries;
use Appwrite\Utopia\Database\Validator\Queries\Base;
class Databases extends Base
{
public const ALLOWED_ATTRIBUTES = [
@@ -2,8 +2,6 @@
namespace Appwrite\Utopia\Database\Validator\Queries;
use Appwrite\Utopia\Database\Validator\Queries\Base;
class Deployments extends Base
{
public const ALLOWED_ATTRIBUTES = [
@@ -2,8 +2,6 @@
namespace Appwrite\Utopia\Database\Validator\Queries;
use Appwrite\Utopia\Database\Validator\Queries\Base;
class Executions extends Base
{
public const ALLOWED_ATTRIBUTES = [
@@ -2,8 +2,6 @@
namespace Appwrite\Utopia\Database\Validator\Queries;
use Appwrite\Utopia\Database\Validator\Queries\Base;
class Files extends Base
{
public const ALLOWED_ATTRIBUTES = [
@@ -2,8 +2,6 @@
namespace Appwrite\Utopia\Database\Validator\Queries;
use Appwrite\Utopia\Database\Validator\Queries\Base;
class Functions extends Base
{
public const ALLOWED_ATTRIBUTES = [
@@ -2,8 +2,6 @@
namespace Appwrite\Utopia\Database\Validator\Queries;
use Appwrite\Utopia\Database\Validator\Queries\Base;
class Memberships extends Base
{
public const ALLOWED_ATTRIBUTES = [
@@ -2,8 +2,6 @@
namespace Appwrite\Utopia\Database\Validator\Queries;
use Appwrite\Utopia\Database\Validator\Queries\Base;
class Teams extends Base
{
public const ALLOWED_ATTRIBUTES = [
@@ -2,8 +2,6 @@
namespace Appwrite\Utopia\Database\Validator\Queries;
use Appwrite\Utopia\Database\Validator\Queries\Base;
class Users extends Base
{
public const ALLOWED_ATTRIBUTES = [
@@ -54,15 +54,6 @@ abstract class Base extends Validator
return self::TYPE_OBJECT;
}
/**
* Is valid.
*
* @param Query $value
*
* @return bool
*/
abstract public function isValid($query): bool;
/**
* Returns what type of query this Validator is for
*/
@@ -58,6 +58,19 @@ class Key extends Model
'default' => '',
'example' => '919c2d18fb5d4...a2ae413da83346ad2',
])
->addRule('accessedAt', [
'type' => self::TYPE_DATETIME,
'description' => 'Most recent access date in Unix timestamp.',
'default' => '',
'example' => self::TYPE_DATETIME_EXAMPLE
])
->addRule('sdks', [
'type' => self::TYPE_STRING,
'description' => 'List of SDK user agents that used this key.',
'default' => null,
'example' => 'appwrite:flutter',
'array' => true
])
;
}
+1 -1
View File
@@ -65,7 +65,7 @@ class User extends Model
->addRule('registration', [
'type' => self::TYPE_DATETIME,
'description' => 'User registration date in Datetime.',
'default' => null,
'default' => '',
'example' => self::TYPE_DATETIME_EXAMPLE,
])
->addRule('status', [