From 9d973d9252d818c62865e7e06439da04717e3e45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Sat, 2 Jul 2022 11:36:05 +0000 Subject: [PATCH 1/6] WIP: Podio provider --- app/config/providers.php | 10 ++ public/images/users/podio.png | Bin 0 -> 1947 bytes src/Appwrite/Auth/OAuth2/Podio.php | 196 +++++++++++++++++++++++++++++ 3 files changed, 206 insertions(+) create mode 100644 public/images/users/podio.png create mode 100644 src/Appwrite/Auth/OAuth2/Podio.php diff --git a/app/config/providers.php b/app/config/providers.php index e4f01fdf84..14ff7bfea8 100644 --- a/app/config/providers.php +++ b/app/config/providers.php @@ -201,6 +201,16 @@ return [ // Ordered by ABC. 'beta' => false, 'mock' => false ], + 'podio' => [ + 'name' => 'Podio', + 'developers' => 'https://developers.podio.com/doc/oauth-authorization', + 'icon' => 'icon-podio', + 'enabled' => true, + 'sandbox' => false, + 'form' => false, + 'beta' => false, + 'mock' => false, + ], 'salesforce' => [ 'name' => 'Salesforce', 'developers' => 'https://developer.salesforce.com/docs/', diff --git a/public/images/users/podio.png b/public/images/users/podio.png new file mode 100644 index 0000000000000000000000000000000000000000..0eb6b49ac9a27015dc6430f606480f68c81a166d GIT binary patch literal 1947 zcmV;M2W0q(P)yWtOa;4f` znAKR7)LWO-i?!je%<9hI^6T>Y{{H{x@cEm+-YNn z{OF(ZI)cog!sh$@{=?btON`J%ht9du?f3fq zgs|TvbjBld#_;z0-|F{FjnF)U%-H7jE_%to*6zU9??HynZKBzey5rU4^yBULzSi!S zz2tDD+C+%XA#%khb;qj7>CNBrEqTa%tKG}q^7Z-r)8q7nvEMR%$|iNj+UWH;fy|n| z<@WjgtI6p$i&+4@%H;7a>a3_+UW54?ezPBuiokL`E;h+FM7#1fXlSc>rjx< zq{Zhug3L#X&*kp;QjyZ_^ZMQC_VD%lj<@3P_4~-&@%j7xvd-%>eadN{*w*FrD|pC{ zxZ;Jf->AswnZD$ax#C8N&(Y!YLWj=D-0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id' => $this->appID, + 'state' => \json_encode($this->state), + // 'scope' => \implode(' ', $this->getScopes()), + 'redirect_uri' => $this->callback + ]); + + return $url; + } + + /** + * @param string $code + * + * @return array + */ + protected function getTokens(string $code): array + { + // /token?grant_type=refresh_token&client_id=YOUR_APP_ID&client_secret=YOUR_APP_SECRET&refresh_token=REFRESH_TOKEN + + if (empty($this->tokens)) { + $this->tokens = \json_decode($this->request( + 'POST', + $this->apiEndpoint . '/token', + ['Content-Type: application/x-www-form-urlencoded'], + \http_build_query([ + 'grant_type' => 'authorization_code', + 'code' => $code, + // 'redirect_uri' => $this->callback, + 'client_id' => $this->appID, + 'client_secret' => $this->appSecret, + // 'scope' => \implode(' ', $this->getScopes()) + ]) + ), true); + } + + return $this->tokens; + } + + /** + * @param string $refreshToken + * + * @return array + */ + public function refreshTokens(string $refreshToken): array + { + $this->tokens = \json_decode($this->request( + 'POST', + $this->apiEndpoint . '/oauth2/token', + ['Content-Type: application/x-www-form-urlencoded'], + \http_build_query([ + 'grant_type' => 'refresh_token', + 'refresh_token' => $refreshToken, + 'client_id' => $this->appID, + 'client_secret' => $this->appSecret, + ]) + ), true); + + if (empty($this->tokens['refresh_token'])) { + $this->tokens['refresh_token'] = $refreshToken; + } + + return $this->tokens; + } + + /** + * @param string $accessToken + * + * @return string + */ + public function getUserID(string $accessToken): string + { + $user = $this->getUser($accessToken); + + return $user['id'] ?? ''; + } + + /** + * @param string $accessToken + * + * @return string + */ + public function getUserEmail(string $accessToken): string + { + $user = $this->getUser($accessToken); + + return $user['email'] ?? ''; + } + + /** + * Check if the OAuth email is verified + * + * @link https://discord.com/developers/docs/resources/user + * + * @param string $accessToken + * + * @return bool + */ + public function isEmailVerified(string $accessToken): bool + { + $user = $this->getUser($accessToken); + + if ($user['verified'] ?? false) { + return true; + } + + return false; + } + + /** + * @param string $accessToken + * + * @return string + */ + public function getUserName(string $accessToken): string + { + $user = $this->getUser($accessToken); + + return $user['username'] ?? ''; + } + + /** + * @param string $accessToken + * + * @return array + */ + protected function getUser(string $accessToken): array + { + if (empty($this->user)) { + $user = $this->request( + 'GET', + $this->apiEndpoint . '/user', + ['Authorization: Bearer ' . \urlencode($accessToken)] + ); + \var_dump($user); + $this->user = \json_decode($user, true); + } + + return $this->user; + } +} From 6e8b11113e7112c984b9d1e8e87ac130085a771c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Sat, 2 Jul 2022 11:57:36 +0000 Subject: [PATCH 2/6] Fix Podio --- src/Appwrite/Auth/OAuth2/Podio.php | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/Appwrite/Auth/OAuth2/Podio.php b/src/Appwrite/Auth/OAuth2/Podio.php index d2f55f7ead..7bb7244d7c 100644 --- a/src/Appwrite/Auth/OAuth2/Podio.php +++ b/src/Appwrite/Auth/OAuth2/Podio.php @@ -22,7 +22,7 @@ class Podio extends OAuth2 /** * @var string */ - private string $apiEndpoint = 'https://api.podio.com/oauth'; + private string $apiEndpoint = 'https://api.podio.com'; /** * @var array @@ -76,12 +76,12 @@ class Podio extends OAuth2 if (empty($this->tokens)) { $this->tokens = \json_decode($this->request( 'POST', - $this->apiEndpoint . '/token', + $this->apiEndpoint . '/oauth/token', ['Content-Type: application/x-www-form-urlencoded'], \http_build_query([ 'grant_type' => 'authorization_code', 'code' => $code, - // 'redirect_uri' => $this->callback, + 'redirect_uri' => $this->callback, 'client_id' => $this->appID, 'client_secret' => $this->appSecret, // 'scope' => \implode(' ', $this->getScopes()) @@ -101,7 +101,7 @@ class Podio extends OAuth2 { $this->tokens = \json_decode($this->request( 'POST', - $this->apiEndpoint . '/oauth2/token', + $this->apiEndpoint . '/oauth/token', ['Content-Type: application/x-www-form-urlencoded'], \http_build_query([ 'grant_type' => 'refresh_token', @@ -127,7 +127,7 @@ class Podio extends OAuth2 { $user = $this->getUser($accessToken); - return $user['id'] ?? ''; + return \strval($user['user_id']) ?? ''; } /** @@ -139,7 +139,7 @@ class Podio extends OAuth2 { $user = $this->getUser($accessToken); - return $user['email'] ?? ''; + return $user['mail'] ?? ''; } /** @@ -155,7 +155,11 @@ class Podio extends OAuth2 { $user = $this->getUser($accessToken); - if ($user['verified'] ?? false) { + $mails = $user['mails']; + $mainMailIndex = \array_search($user['mail'], \array_map(fn($m) => $m['mail'], $mails)); + $mainMain = $mails[$mainMailIndex]; + + if ($mainMain['verified'] ?? false) { return true; } @@ -169,9 +173,7 @@ class Podio extends OAuth2 */ public function getUserName(string $accessToken): string { - $user = $this->getUser($accessToken); - - return $user['username'] ?? ''; + return ''; } /** @@ -187,7 +189,6 @@ class Podio extends OAuth2 $this->apiEndpoint . '/user', ['Authorization: Bearer ' . \urlencode($accessToken)] ); - \var_dump($user); $this->user = \json_decode($user, true); } From e26dc9a2b7f3de624b47c34086ab6ad7a74bdded Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Sun, 3 Jul 2022 10:05:49 +0200 Subject: [PATCH 3/6] Update docs, remove leftovers --- src/Appwrite/Auth/OAuth2/Podio.php | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/Appwrite/Auth/OAuth2/Podio.php b/src/Appwrite/Auth/OAuth2/Podio.php index 7bb7244d7c..dc87dd72ad 100644 --- a/src/Appwrite/Auth/OAuth2/Podio.php +++ b/src/Appwrite/Auth/OAuth2/Podio.php @@ -10,16 +10,15 @@ use Appwrite\Auth\OAuth2; class Podio extends OAuth2 { /** + * Endpoint used for initiating OAuth flow + * * @var string */ - private string $endpoint = 'http://oauth.net/2'; - - /** - * @var string - */ - private string $authEndpoint = 'https://podio.com/oauth'; + private string $endpoint = 'https://podio.com/oauth'; /** + * Endpoint for communication with API server + * * @var string */ private string $apiEndpoint = 'https://api.podio.com'; @@ -52,7 +51,7 @@ class Podio extends OAuth2 */ public function getLoginURL(): string { - $url = $this->authEndpoint . '/authorize?' . + $url = $this->endpoint . '/authorize?' . \http_build_query([ // 'response_type' => 'code', 'client_id' => $this->appID, From 7b5a35cbc6886b7be46be340c3a879ec04fd13a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Sun, 3 Jul 2022 10:09:26 +0200 Subject: [PATCH 4/6] Remove leftovers --- src/Appwrite/Auth/OAuth2/Podio.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Appwrite/Auth/OAuth2/Podio.php b/src/Appwrite/Auth/OAuth2/Podio.php index dc87dd72ad..57f450ea3f 100644 --- a/src/Appwrite/Auth/OAuth2/Podio.php +++ b/src/Appwrite/Auth/OAuth2/Podio.php @@ -36,7 +36,7 @@ class Podio extends OAuth2 /** * @var array */ - protected array $scopes = []; + protected array $scopes = []; // No scopes required /** * @return string @@ -70,8 +70,6 @@ class Podio extends OAuth2 */ protected function getTokens(string $code): array { - // /token?grant_type=refresh_token&client_id=YOUR_APP_ID&client_secret=YOUR_APP_SECRET&refresh_token=REFRESH_TOKEN - if (empty($this->tokens)) { $this->tokens = \json_decode($this->request( 'POST', @@ -144,8 +142,6 @@ class Podio extends OAuth2 /** * Check if the OAuth email is verified * - * @link https://discord.com/developers/docs/resources/user - * * @param string $accessToken * * @return bool From 9623974ed5badfc9f806a837e0d9cdb1f41a7bc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Sun, 3 Jul 2022 10:14:36 +0200 Subject: [PATCH 5/6] Linter fix --- src/Appwrite/Auth/OAuth2/Podio.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Appwrite/Auth/OAuth2/Podio.php b/src/Appwrite/Auth/OAuth2/Podio.php index 57f450ea3f..f91cc9b8e1 100644 --- a/src/Appwrite/Auth/OAuth2/Podio.php +++ b/src/Appwrite/Auth/OAuth2/Podio.php @@ -11,14 +11,14 @@ class Podio extends OAuth2 { /** * Endpoint used for initiating OAuth flow - * + * * @var string */ private string $endpoint = 'https://podio.com/oauth'; /** * Endpoint for communication with API server - * + * * @var string */ private string $apiEndpoint = 'https://api.podio.com'; From 79cbdb537e4efefe07225ed3bc32d31b9f48c44d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Mon, 4 Jul 2022 10:32:56 +0000 Subject: [PATCH 6/6] PR review changes, add name from Podio --- src/Appwrite/Auth/OAuth2/Podio.php | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/Appwrite/Auth/OAuth2/Podio.php b/src/Appwrite/Auth/OAuth2/Podio.php index f91cc9b8e1..e4194238d1 100644 --- a/src/Appwrite/Auth/OAuth2/Podio.php +++ b/src/Appwrite/Auth/OAuth2/Podio.php @@ -53,10 +53,8 @@ class Podio extends OAuth2 { $url = $this->endpoint . '/authorize?' . \http_build_query([ - // 'response_type' => 'code', 'client_id' => $this->appID, 'state' => \json_encode($this->state), - // 'scope' => \implode(' ', $this->getScopes()), 'redirect_uri' => $this->callback ]); @@ -80,8 +78,7 @@ class Podio extends OAuth2 'code' => $code, 'redirect_uri' => $this->callback, 'client_id' => $this->appID, - 'client_secret' => $this->appSecret, - // 'scope' => \implode(' ', $this->getScopes()) + 'client_secret' => $this->appSecret ]) ), true); } @@ -168,7 +165,9 @@ class Podio extends OAuth2 */ public function getUserName(string $accessToken): string { - return ''; + $user = $this->getUser($accessToken); + + return $user['name'] ?? ''; } /** @@ -179,12 +178,20 @@ class Podio extends OAuth2 protected function getUser(string $accessToken): array { if (empty($this->user)) { - $user = $this->request( + $user = \json_decode($this->request( 'GET', $this->apiEndpoint . '/user', ['Authorization: Bearer ' . \urlencode($accessToken)] - ); - $this->user = \json_decode($user, true); + ), true); + + $profile = \json_decode($this->request( + 'GET', + $this->apiEndpoint . '/user/profile', + ['Authorization: Bearer ' . \urlencode($accessToken)] + ), true); + + $this->user = $user; + $this->user['name'] = $profile['name']; } return $this->user;