Sync master
This commit is contained in:
parent
6dade9e758
commit
44970cc628
7 changed files with 222 additions and 4 deletions
|
@ -121,6 +121,16 @@ return [ // Ordered by ABC.
|
||||||
'beta' => false,
|
'beta' => false,
|
||||||
'mock' => false,
|
'mock' => false,
|
||||||
],
|
],
|
||||||
|
'etsy' => [
|
||||||
|
'name' => 'Etsy',
|
||||||
|
'developers' => 'https://developers.etsy.com/',
|
||||||
|
'icon' => 'icon-etsy',
|
||||||
|
'enabled' => true,
|
||||||
|
'sandbox' => false,
|
||||||
|
'form' => false,
|
||||||
|
'beta' => false,
|
||||||
|
'mock' => false,
|
||||||
|
],
|
||||||
'facebook' => [
|
'facebook' => [
|
||||||
'name' => 'Facebook',
|
'name' => 'Facebook',
|
||||||
'developers' => 'https://developers.facebook.com/',
|
'developers' => 'https://developers.facebook.com/',
|
||||||
|
|
|
@ -74,6 +74,10 @@ App::post('/v1/teams')
|
||||||
])));
|
])));
|
||||||
|
|
||||||
if (!$isPrivilegedUser && !$isAppUser) { // Don't add user on server mode
|
if (!$isPrivilegedUser && !$isAppUser) { // Don't add user on server mode
|
||||||
|
if (!\in_array('owner', $roles)) {
|
||||||
|
$roles[] = 'owner';
|
||||||
|
}
|
||||||
|
|
||||||
$membershipId = ID::unique();
|
$membershipId = ID::unique();
|
||||||
$membership = new Document([
|
$membership = new Document([
|
||||||
'$id' => $membershipId,
|
'$id' => $membershipId,
|
||||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 882 B After Width: | Height: | Size: 1.2 KiB |
BIN
public/images/users/etsy.png
Normal file
BIN
public/images/users/etsy.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 5.6 KiB |
200
src/Appwrite/Auth/OAuth2/Etsy.php
Normal file
200
src/Appwrite/Auth/OAuth2/Etsy.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -19,7 +19,8 @@ trait TeamsBase
|
||||||
'x-appwrite-project' => $this->getProject()['$id'],
|
'x-appwrite-project' => $this->getProject()['$id'],
|
||||||
], $this->getHeaders()), [
|
], $this->getHeaders()), [
|
||||||
'teamId' => ID::unique(),
|
'teamId' => ID::unique(),
|
||||||
'name' => 'Arsenal'
|
'name' => 'Arsenal',
|
||||||
|
'roles' => ['player'],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->assertEquals(201, $response1['headers']['status-code']);
|
$this->assertEquals(201, $response1['headers']['status-code']);
|
||||||
|
|
|
@ -30,7 +30,8 @@ trait TeamsBaseClient
|
||||||
$this->assertEquals($this->getUser()['name'], $response['body']['memberships'][0]['userName']);
|
$this->assertEquals($this->getUser()['name'], $response['body']['memberships'][0]['userName']);
|
||||||
$this->assertEquals($this->getUser()['email'], $response['body']['memberships'][0]['userEmail']);
|
$this->assertEquals($this->getUser()['email'], $response['body']['memberships'][0]['userEmail']);
|
||||||
$this->assertEquals($teamName, $response['body']['memberships'][0]['teamName']);
|
$this->assertEquals($teamName, $response['body']['memberships'][0]['teamName']);
|
||||||
$this->assertEquals('owner', $response['body']['memberships'][0]['roles'][0]);
|
$this->assertContains('owner', $response['body']['memberships'][0]['roles']);
|
||||||
|
$this->assertContains('player', $response['body']['memberships'][0]['roles']);
|
||||||
|
|
||||||
$membershipId = $response['body']['memberships'][0]['$id'];
|
$membershipId = $response['body']['memberships'][0]['$id'];
|
||||||
|
|
||||||
|
@ -47,7 +48,8 @@ trait TeamsBaseClient
|
||||||
$this->assertEquals($this->getUser()['name'], $response['body']['memberships'][0]['userName']);
|
$this->assertEquals($this->getUser()['name'], $response['body']['memberships'][0]['userName']);
|
||||||
$this->assertEquals($this->getUser()['email'], $response['body']['memberships'][0]['userEmail']);
|
$this->assertEquals($this->getUser()['email'], $response['body']['memberships'][0]['userEmail']);
|
||||||
$this->assertEquals($teamName, $response['body']['memberships'][0]['teamName']);
|
$this->assertEquals($teamName, $response['body']['memberships'][0]['teamName']);
|
||||||
$this->assertEquals('owner', $response['body']['memberships'][0]['roles'][0]);
|
$this->assertContains('owner', $response['body']['memberships'][0]['roles']);
|
||||||
|
$this->assertContains('player', $response['body']['memberships'][0]['roles']);
|
||||||
|
|
||||||
$response = $this->client->call(Client::METHOD_GET, '/teams/' . $teamUid . '/memberships', array_merge([
|
$response = $this->client->call(Client::METHOD_GET, '/teams/' . $teamUid . '/memberships', array_merge([
|
||||||
'content-type' => 'application/json',
|
'content-type' => 'application/json',
|
||||||
|
@ -62,7 +64,8 @@ trait TeamsBaseClient
|
||||||
$this->assertEquals($this->getUser()['name'], $response['body']['memberships'][0]['userName']);
|
$this->assertEquals($this->getUser()['name'], $response['body']['memberships'][0]['userName']);
|
||||||
$this->assertEquals($this->getUser()['email'], $response['body']['memberships'][0]['userEmail']);
|
$this->assertEquals($this->getUser()['email'], $response['body']['memberships'][0]['userEmail']);
|
||||||
$this->assertEquals($teamName, $response['body']['memberships'][0]['teamName']);
|
$this->assertEquals($teamName, $response['body']['memberships'][0]['teamName']);
|
||||||
$this->assertEquals('owner', $response['body']['memberships'][0]['roles'][0]);
|
$this->assertContains('owner', $response['body']['memberships'][0]['roles']);
|
||||||
|
$this->assertContains('player', $response['body']['memberships'][0]['roles']);
|
||||||
|
|
||||||
$response = $this->client->call(Client::METHOD_GET, '/teams/' . $teamUid . '/memberships', array_merge([
|
$response = $this->client->call(Client::METHOD_GET, '/teams/' . $teamUid . '/memberships', array_merge([
|
||||||
'content-type' => 'application/json',
|
'content-type' => 'application/json',
|
||||||
|
|
Loading…
Reference in a new issue