From 3303dd1f4b2a6ea258db0d9e5f98d4eb9680bfc1 Mon Sep 17 00:00:00 2001 From: Matej Baco Date: Mon, 31 Jan 2022 21:39:14 +0100 Subject: [PATCH] Refactored all providers --- src/Appwrite/Auth/OAuth2/Amazon.php | 38 +++++++++---------- src/Appwrite/Auth/OAuth2/Apple.php | 44 ++++++++++------------ src/Appwrite/Auth/OAuth2/Bitbucket.php | 40 +++++++++----------- src/Appwrite/Auth/OAuth2/Bitly.php | 37 ++++++++---------- src/Appwrite/Auth/OAuth2/Box.php | 40 +++++++++----------- src/Appwrite/Auth/OAuth2/Discord.php | 38 +++++++++---------- src/Appwrite/Auth/OAuth2/Dropbox.php | 38 +++++++++---------- src/Appwrite/Auth/OAuth2/Facebook.php | 30 +++++++-------- src/Appwrite/Auth/OAuth2/Github.php | 36 ++++++++---------- src/Appwrite/Auth/OAuth2/Gitlab.php | 32 +++++++--------- src/Appwrite/Auth/OAuth2/Google.php | 34 ++++++++--------- src/Appwrite/Auth/OAuth2/Linkedin.php | 36 ++++++++---------- src/Appwrite/Auth/OAuth2/Microsoft.php | 42 ++++++++++----------- src/Appwrite/Auth/OAuth2/Mock.php | 32 +++++++--------- src/Appwrite/Auth/OAuth2/Notion.php | 40 +++++++++----------- src/Appwrite/Auth/OAuth2/Paypal.php | 32 +++++++--------- src/Appwrite/Auth/OAuth2/Salesforce.php | 41 ++++++++++---------- src/Appwrite/Auth/OAuth2/Slack.php | 32 +++++++--------- src/Appwrite/Auth/OAuth2/Spotify.php | 30 +++++++-------- src/Appwrite/Auth/OAuth2/Stripe.php | 38 +++++++++---------- src/Appwrite/Auth/OAuth2/Tradeshift.php | 27 +++++++------ src/Appwrite/Auth/OAuth2/Vk.php | 50 ++++++++++++------------- src/Appwrite/Auth/OAuth2/WordPress.php | 36 ++++++++---------- src/Appwrite/Auth/OAuth2/Yahoo.php | 38 +++++++++---------- src/Appwrite/Auth/OAuth2/Yammer.php | 38 +++++++++---------- src/Appwrite/Auth/OAuth2/Yandex.php | 39 +++++++++---------- 26 files changed, 427 insertions(+), 531 deletions(-) diff --git a/src/Appwrite/Auth/OAuth2/Amazon.php b/src/Appwrite/Auth/OAuth2/Amazon.php index 25405795d6..0b8b78ad86 100644 --- a/src/Appwrite/Auth/OAuth2/Amazon.php +++ b/src/Appwrite/Auth/OAuth2/Amazon.php @@ -63,29 +63,25 @@ class Amazon extends OAuth2 */ public function getTokens(string $code): array { -// $headers = ['Content-Type: application/x-www-form-urlencoded;charset=UTF-8']; -// $accessToken = $this->request( -// 'POST', -// 'https://api.amazon.com/auth/o2/token', -// $headers, -// \http_build_query([ -// 'code' => $code, -// 'client_id' => $this->appID , -// 'client_secret' => $this->appSecret, -// 'redirect_uri' => $this->callback , -// 'grant_type' => 'authorization_code' -// ]) -// ); -// -// $accessToken = \json_decode($accessToken, true); -// -// if (isset($accessToken['access_token'])) { -// return $accessToken['access_token']; -// } + $headers = ['Content-Type: application/x-www-form-urlencoded;charset=UTF-8']; + $result = $this->request( + 'POST', + 'https://api.amazon.com/auth/o2/token', + $headers, + \http_build_query([ + 'code' => $code, + 'client_id' => $this->appID , + 'client_secret' => $this->appSecret, + 'redirect_uri' => $this->callback , + 'grant_type' => 'authorization_code' + ]) + ); + + $result = \json_decode($result, true); return [ - 'access' => '', - 'refresh' => '' + 'access' => $result['access_token'], + 'refresh' => $result['refresh_token'] ]; } diff --git a/src/Appwrite/Auth/OAuth2/Apple.php b/src/Appwrite/Auth/OAuth2/Apple.php index 0fe5002437..b272cf4df3 100644 --- a/src/Appwrite/Auth/OAuth2/Apple.php +++ b/src/Appwrite/Auth/OAuth2/Apple.php @@ -58,32 +58,28 @@ class Apple extends OAuth2 */ public function getTokens(string $code): array { -// $headers = ['Content-Type: application/x-www-form-urlencoded']; -// $accessToken = $this->request( -// 'POST', -// 'https://appleid.apple.com/auth/token', -// $headers, -// \http_build_query([ -// 'grant_type' => 'authorization_code', -// 'code' => $code, -// 'client_id' => $this->appID, -// 'client_secret' => $this->getAppSecret(), -// 'redirect_uri' => $this->callback, -// ]) -// ); -// -// $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']; -// } + $headers = ['Content-Type: application/x-www-form-urlencoded']; + $result = $this->request( + 'POST', + 'https://appleid.apple.com/auth/token', + $headers, + \http_build_query([ + 'grant_type' => 'authorization_code', + 'code' => $code, + 'client_id' => $this->appID, + 'client_secret' => $this->getAppSecret(), + 'redirect_uri' => $this->callback, + ]) + ); + + $result = \json_decode($result, true); + + $this->claims = (isset($result['id_token'])) ? \explode('.', $result['id_token']) : [0 => '', 1 => '']; + $this->claims = (isset($this->claims[1])) ? \json_decode(\base64_decode($this->claims[1]), true) : []; return [ - 'access' => '', - 'refresh' => '' + 'access' => $result['access_token'], + 'refresh' => $result['refresh_token'] ]; } diff --git a/src/Appwrite/Auth/OAuth2/Bitbucket.php b/src/Appwrite/Auth/OAuth2/Bitbucket.php index e31f038bc9..b82d722d5d 100644 --- a/src/Appwrite/Auth/OAuth2/Bitbucket.php +++ b/src/Appwrite/Auth/OAuth2/Bitbucket.php @@ -47,30 +47,26 @@ class Bitbucket extends OAuth2 */ public function getTokens(string $code): array { -// // Required as per Bitbucket Spec. -// $headers = ['Content-Type: application/x-www-form-urlencoded']; -// -// $accessToken = $this->request( -// 'POST', -// 'https://bitbucket.org/site/oauth2/access_token', -// $headers, -// \http_build_query([ -// 'code' => $code, -// 'client_id' => $this->appID, -// 'client_secret' => $this->appSecret, -// 'grant_type' => 'authorization_code' -// ]) -// ); -// -// $accessToken = \json_decode($accessToken, true); -// -// if (isset($accessToken['access_token'])) { -// return $accessToken['access_token']; -// } + // Required as per Bitbucket Spec. + $headers = ['Content-Type: application/x-www-form-urlencoded']; + + $result = $this->request( + 'POST', + 'https://bitbucket.org/site/oauth2/access_token', + $headers, + \http_build_query([ + 'code' => $code, + 'client_id' => $this->appID, + 'client_secret' => $this->appSecret, + 'grant_type' => 'authorization_code' + ]) + ); + + $result = \json_decode($result, true); return [ - 'access' => '', - 'refresh' => '' + 'access' => $result['access_token'], + 'refresh' => $result['refresh_token'] ]; } diff --git a/src/Appwrite/Auth/OAuth2/Bitly.php b/src/Appwrite/Auth/OAuth2/Bitly.php index f098d9c742..db3cbfdbf0 100644 --- a/src/Appwrite/Auth/OAuth2/Bitly.php +++ b/src/Appwrite/Auth/OAuth2/Bitly.php @@ -58,29 +58,24 @@ class Bitly extends OAuth2 */ public function getTokens(string $code): array { -// $response = $this->request( -// 'POST', -// $this->resourceEndpoint . 'oauth/access_token', -// ["Content-Type: application/x-www-form-urlencoded"], -// \http_build_query([ -// "client_id" => $this->appID, -// "client_secret" => $this->appSecret, -// "code" => $code, -// "redirect_uri" => $this->callback, -// "state" => \json_encode($this->state) -// ]) -// ); -// -// $result = null; -// -// if ($response) { -// \parse_str($response, $result); -// return $result['access_token']; -// } + $result = $this->request( + 'POST', + $this->resourceEndpoint . 'oauth/access_token', + ["Content-Type: application/x-www-form-urlencoded"], + \http_build_query([ + "client_id" => $this->appID, + "client_secret" => $this->appSecret, + "code" => $code, + "redirect_uri" => $this->callback, + "state" => \json_encode($this->state) + ]) + ); + + $result = \json_decode($result, true); return [ - 'access' => '', - 'refresh' => '' + 'access' => $result['access_token'], + 'refresh' => $result['refresh_token'] ]; } diff --git a/src/Appwrite/Auth/OAuth2/Box.php b/src/Appwrite/Auth/OAuth2/Box.php index d63230afb6..c5c842394b 100644 --- a/src/Appwrite/Auth/OAuth2/Box.php +++ b/src/Appwrite/Auth/OAuth2/Box.php @@ -63,30 +63,26 @@ class Box extends OAuth2 */ public function getTokens(string $code): array { -// $header = "Content-Type: application/x-www-form-urlencoded"; -// $accessToken = $this->request( -// 'POST', -// $this->endpoint . 'token', -// [$header], -// \http_build_query([ -// "client_id" => $this->appID, -// "client_secret" => $this->appSecret, -// "code" => $code, -// "grant_type" => "authorization_code", -// "scope" => \implode(',', $this->getScopes()), -// "redirect_uri" => $this->callback -// ]) -// ); -// -// $accessToken = \json_decode($accessToken, true); -// -// if (array_key_exists('access_token', $accessToken)) { -// return $accessToken['access_token']; -// } + $header = "Content-Type: application/x-www-form-urlencoded"; + $result = $this->request( + 'POST', + $this->endpoint . 'token', + [$header], + \http_build_query([ + "client_id" => $this->appID, + "client_secret" => $this->appSecret, + "code" => $code, + "grant_type" => "authorization_code", + "scope" => \implode(',', $this->getScopes()), + "redirect_uri" => $this->callback + ]) + ); + + $result = \json_decode($result, true); return [ - 'access' => '', - 'refresh' => '' + 'access' => $result['access_token'], + 'refresh' => $result['refresh_token'] ]; } diff --git a/src/Appwrite/Auth/OAuth2/Discord.php b/src/Appwrite/Auth/OAuth2/Discord.php index 7c448bf2bc..ed324e62f4 100644 --- a/src/Appwrite/Auth/OAuth2/Discord.php +++ b/src/Appwrite/Auth/OAuth2/Discord.php @@ -59,29 +59,25 @@ class Discord extends OAuth2 */ public function getTokens(string $code): array { -// $accessToken = $this->request( -// 'POST', -// $this->endpoint . '/oauth2/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()) -// ]) -// ); -// -// $accessToken = \json_decode($accessToken, true); -// -// if (isset($accessToken['access_token'])) { -// return $accessToken['access_token']; -// } + $result = $this->request( + 'POST', + $this->endpoint . '/oauth2/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()) + ]) + ); + + $result = \json_decode($result, true); return [ - 'access' => '', - 'refresh' => '' + 'access' => $result['access_token'], + 'refresh' => $result['refresh_token'] ]; } diff --git a/src/Appwrite/Auth/OAuth2/Dropbox.php b/src/Appwrite/Auth/OAuth2/Dropbox.php index a8cb1a911f..44cd3e9783 100644 --- a/src/Appwrite/Auth/OAuth2/Dropbox.php +++ b/src/Appwrite/Auth/OAuth2/Dropbox.php @@ -48,29 +48,25 @@ class Dropbox extends OAuth2 */ public function getTokens(string $code): array { -// $headers = ['Content-Type: application/x-www-form-urlencoded']; -// $accessToken = $this->request( -// 'POST', -// 'https://api.dropboxapi.com/oauth2/token', -// $headers, -// \http_build_query([ -// 'code' => $code, -// 'client_id' => $this->appID, -// 'client_secret' => $this->appSecret, -// 'redirect_uri' => $this->callback, -// 'grant_type' => 'authorization_code' -// ]) -// ); -// -// $accessToken = \json_decode($accessToken, true); -// -// if (isset($accessToken['access_token'])) { -// return $accessToken['access_token']; -// } + $headers = ['Content-Type: application/x-www-form-urlencoded']; + $result = $this->request( + 'POST', + 'https://api.dropboxapi.com/oauth2/token', + $headers, + \http_build_query([ + 'code' => $code, + 'client_id' => $this->appID, + 'client_secret' => $this->appSecret, + 'redirect_uri' => $this->callback, + 'grant_type' => 'authorization_code' + ]) + ); + + $result = \json_decode($result, true); return [ - 'access' => '', - 'refresh' => '' + 'access' => $result['access_token'], + 'refresh' => $result['refresh_token'] ]; } diff --git a/src/Appwrite/Auth/OAuth2/Facebook.php b/src/Appwrite/Auth/OAuth2/Facebook.php index 4e831c511e..e4b886d9c1 100644 --- a/src/Appwrite/Auth/OAuth2/Facebook.php +++ b/src/Appwrite/Auth/OAuth2/Facebook.php @@ -51,25 +51,21 @@ class Facebook extends OAuth2 */ public function getTokens(string $code): array { -// $accessToken = $this->request( -// 'GET', -// 'https://graph.facebook.com/'.$this->version.'/oauth/access_token?'.\http_build_query([ -// 'client_id' => $this->appID, -// 'redirect_uri' => $this->callback, -// 'client_secret' => $this->appSecret, -// 'code' => $code -// ]) -// ); -// -// $accessToken = \json_decode($accessToken, true); -// -// if (isset($accessToken['access_token'])) { -// return $accessToken['access_token']; -// } + $result = $this->request( + 'GET', + 'https://graph.facebook.com/'.$this->version.'/oauth/access_token?'.\http_build_query([ + 'client_id' => $this->appID, + 'redirect_uri' => $this->callback, + 'client_secret' => $this->appSecret, + 'code' => $code + ]) + ); + + $result = \json_decode($result, true); return [ - 'access' => '', - 'refresh' => '' + 'access' => $result['access_token'], + 'refresh' => $result['refresh_token'] ]; } diff --git a/src/Appwrite/Auth/OAuth2/Github.php b/src/Appwrite/Auth/OAuth2/Github.php index 18886ca272..18cd9779ae 100644 --- a/src/Appwrite/Auth/OAuth2/Github.php +++ b/src/Appwrite/Auth/OAuth2/Github.php @@ -46,29 +46,23 @@ class Github extends OAuth2 */ public function getTokens(string $code): array { -// $accessToken = $this->request( -// 'POST', -// 'https://github.com/login/oauth/access_token', -// [], -// \http_build_query([ -// 'client_id' => $this->appID, -// 'redirect_uri' => $this->callback, -// 'client_secret' => $this->appSecret, -// 'code' => $code -// ]) -// ); -// -// $output = []; -// -// \parse_str($accessToken, $output); -// -// if (isset($output['access_token'])) { -// return $output['access_token']; -// } + $result = $this->request( + 'POST', + 'https://github.com/login/oauth/access_token', + [], + \http_build_query([ + 'client_id' => $this->appID, + 'redirect_uri' => $this->callback, + 'client_secret' => $this->appSecret, + 'code' => $code + ]) + ); + + $result = \json_decode($result, true); return [ - 'access' => '', - 'refresh' => '' + 'access' => $result['access_token'], + 'refresh' => $result['refresh_token'] ]; } diff --git a/src/Appwrite/Auth/OAuth2/Gitlab.php b/src/Appwrite/Auth/OAuth2/Gitlab.php index 81d98a2baa..d63f25bade 100644 --- a/src/Appwrite/Auth/OAuth2/Gitlab.php +++ b/src/Appwrite/Auth/OAuth2/Gitlab.php @@ -50,26 +50,22 @@ class Gitlab extends OAuth2 */ public function getTokens(string $code): array { -// $accessToken = $this->request( -// 'POST', -// 'https://gitlab.com/oauth/token?'.\http_build_query([ -// 'code' => $code, -// 'client_id' => $this->appID, -// 'client_secret' => $this->appSecret, -// 'redirect_uri' => $this->callback, -// 'grant_type' => 'authorization_code' -// ]) -// ); -// -// $accessToken = \json_decode($accessToken, true); -// -// if (isset($accessToken['access_token'])) { -// return $accessToken['access_token']; -// } + $result = $this->request( + 'POST', + 'https://gitlab.com/oauth/token?'.\http_build_query([ + 'code' => $code, + 'client_id' => $this->appID, + 'client_secret' => $this->appSecret, + 'redirect_uri' => $this->callback, + 'grant_type' => 'authorization_code' + ]) + ); + + $result = \json_decode($result, true); return [ - 'access' => '', - 'refresh' => '' + 'access' => $result['access_token'], + 'refresh' => $result['refresh_token'] ]; } diff --git a/src/Appwrite/Auth/OAuth2/Google.php b/src/Appwrite/Auth/OAuth2/Google.php index 49d1802acf..50c1e7a9e3 100644 --- a/src/Appwrite/Auth/OAuth2/Google.php +++ b/src/Appwrite/Auth/OAuth2/Google.php @@ -59,27 +59,23 @@ class Google extends OAuth2 */ public function getTokens(string $code): array { -// $accessToken = $this->request( -// 'POST', -// 'https://oauth2.googleapis.com/token?'.\http_build_query([ -// 'code' => $code, -// 'client_id' => $this->appID, -// 'client_secret' => $this->appSecret, -// 'redirect_uri' => $this->callback, -// 'scope' => null, -// 'grant_type' => 'authorization_code' -// ]) -// ); -// -// $accessToken = \json_decode($accessToken, true); -// -// if (isset($accessToken['access_token'])) { -// return $accessToken['access_token']; -// } + $result = $this->request( + 'POST', + 'https://oauth2.googleapis.com/token?'.\http_build_query([ + 'code' => $code, + 'client_id' => $this->appID, + 'client_secret' => $this->appSecret, + 'redirect_uri' => $this->callback, + 'scope' => null, + 'grant_type' => 'authorization_code' + ]) + ); + + $result = \json_decode($result, true); return [ - 'access' => '', - 'refresh' => '' + 'access' => $result['access_token'], + 'refresh' => $result['refresh_token'] ]; } diff --git a/src/Appwrite/Auth/OAuth2/Linkedin.php b/src/Appwrite/Auth/OAuth2/Linkedin.php index 657fb8ff5e..cc26a7cd0f 100644 --- a/src/Appwrite/Auth/OAuth2/Linkedin.php +++ b/src/Appwrite/Auth/OAuth2/Linkedin.php @@ -61,28 +61,24 @@ class Linkedin extends OAuth2 */ public function getTokens(string $code): array { -// $accessToken = $this->request( -// 'POST', -// 'https://www.linkedin.com/oauth/v2/accessToken', -// ['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, -// ]) -// ); -// -// $accessToken = \json_decode($accessToken, true); -// -// if (isset($accessToken['access_token'])) { -// return $accessToken['access_token']; -// } + $result = $this->request( + 'POST', + 'https://www.linkedin.com/oauth/v2/accessToken', + ['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, + ]) + ); + + $result = \json_decode($result, true); return [ - 'access' => '', - 'refresh' => '' + 'access' => $result['access_token'], + 'refresh' => $result['refresh_token'] ]; } diff --git a/src/Appwrite/Auth/OAuth2/Microsoft.php b/src/Appwrite/Auth/OAuth2/Microsoft.php index 5950465833..efd6b4dd63 100644 --- a/src/Appwrite/Auth/OAuth2/Microsoft.php +++ b/src/Appwrite/Auth/OAuth2/Microsoft.php @@ -53,31 +53,27 @@ class Microsoft extends OAuth2 */ public function getTokens(string $code): array { -// $headers = ['Content-Type: application/x-www-form-urlencoded']; -// -// $accessToken = $this->request( -// 'POST', -// 'https://login.microsoftonline.com/'.$this->getTenantId().'/oauth2/v2.0/token', -// $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' -// ]) -// ); -// -// $accessToken = \json_decode($accessToken, true); -// -// if (isset($accessToken['access_token'])) { -// return $accessToken['access_token']; -// } + $headers = ['Content-Type: application/x-www-form-urlencoded']; + + $result = $this->request( + 'POST', + 'https://login.microsoftonline.com/'.$this->getTenantId().'/oauth2/v2.0/token', + $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' + ]) + ); + + $result = \json_decode($result, true); return [ - 'access' => '', - 'refresh' => '' + 'access' => $result['access_token'], + 'refresh' => $result['refresh_token'] ]; } diff --git a/src/Appwrite/Auth/OAuth2/Mock.php b/src/Appwrite/Auth/OAuth2/Mock.php index 149691025d..cc8e12e2eb 100644 --- a/src/Appwrite/Auth/OAuth2/Mock.php +++ b/src/Appwrite/Auth/OAuth2/Mock.php @@ -51,26 +51,22 @@ class Mock extends OAuth2 */ public function getTokens(string $code): array { -// $accessToken = $this->request( -// 'GET', -// 'http://localhost/'.$this->version.'/mock/tests/general/oauth2/token?'. -// \http_build_query([ -// 'client_id' => $this->appID, -// 'redirect_uri' => $this->callback, -// 'client_secret' => $this->appSecret, -// 'code' => $code -// ]) -// ); -// -// $accessToken = \json_decode($accessToken, true); // -// -// if (isset($accessToken['access_token'])) { -// return $accessToken['access_token']; -// } + $result = $this->request( + 'GET', + 'http://localhost/'.$this->version.'/mock/tests/general/oauth2/token?'. + \http_build_query([ + 'client_id' => $this->appID, + 'redirect_uri' => $this->callback, + 'client_secret' => $this->appSecret, + 'code' => $code + ]) + ); + + $result = \json_decode($result, true); return [ - 'access' => '', - 'refresh' => '' + 'access' => $result['access_token'], + 'refresh' => $result['refresh_token'] ]; } diff --git a/src/Appwrite/Auth/OAuth2/Notion.php b/src/Appwrite/Auth/OAuth2/Notion.php index 7e7ddd2f37..28b98ff72f 100644 --- a/src/Appwrite/Auth/OAuth2/Notion.php +++ b/src/Appwrite/Auth/OAuth2/Notion.php @@ -55,30 +55,26 @@ class Notion extends OAuth2 */ public function getTokens(string $code): array { -// $headers = [ -// "Authorization: Basic " . \base64_encode($this->appID . ":" . $this->appSecret), -// ]; -// -// $response = $this->request( -// 'POST', -// $this->endpoint . '/oauth/token', -// $headers, -// \http_build_query([ -// 'grant_type' => 'authorization_code', -// 'redirect_uri' => $this->callback, -// 'code' => $code -// ]) -// ); -// -// $response = \json_decode($response, true); -// -// if (isset($response['access_token'])) { -// return $response['access_token']; -// } + $headers = [ + "Authorization: Basic " . \base64_encode($this->appID . ":" . $this->appSecret), + ]; + + $result = $this->request( + 'POST', + $this->endpoint . '/oauth/token', + $headers, + \http_build_query([ + 'grant_type' => 'authorization_code', + 'redirect_uri' => $this->callback, + 'code' => $code + ]) + ); + + $result = \json_decode($result, true); return [ - 'access' => '', - 'refresh' => '' + 'access' => $result['access_token'], + 'refresh' => $result['refresh_token'] ]; } diff --git a/src/Appwrite/Auth/OAuth2/Paypal.php b/src/Appwrite/Auth/OAuth2/Paypal.php index a05f3b35ed..17414cafe3 100644 --- a/src/Appwrite/Auth/OAuth2/Paypal.php +++ b/src/Appwrite/Auth/OAuth2/Paypal.php @@ -78,27 +78,21 @@ class Paypal extends OAuth2 */ public function getTokens(string $code): array { -// $accessToken = $this->request( -// 'POST', -// $this->resourceEndpoint[$this->environment] . 'oauth2/token', -// ['Authorization: Basic ' . \base64_encode($this->appID . ':' . $this->appSecret)], -// \http_build_query([ -// 'code' => $code, -// 'grant_type' => 'authorization_code', -// ]) -// ); -// -// -// $accessToken = \json_decode($accessToken, true); -// -// -// if (isset($accessToken['access_token'])) { -// return $accessToken['access_token']; -// } + $result = $this->request( + 'POST', + $this->resourceEndpoint[$this->environment] . 'oauth2/token', + ['Authorization: Basic ' . \base64_encode($this->appID . ':' . $this->appSecret)], + \http_build_query([ + 'code' => $code, + 'grant_type' => 'authorization_code', + ]) + ); + + $result = \json_decode($result, true); return [ - 'access' => '', - 'refresh' => '' + 'access' => $result['access_token'], + 'refresh' => $result['refresh_token'] ]; } diff --git a/src/Appwrite/Auth/OAuth2/Salesforce.php b/src/Appwrite/Auth/OAuth2/Salesforce.php index 203af8f115..a49d8ace98 100644 --- a/src/Appwrite/Auth/OAuth2/Salesforce.php +++ b/src/Appwrite/Auth/OAuth2/Salesforce.php @@ -63,30 +63,27 @@ class Salesforce extends OAuth2 */ public function getTokens(string $code): array { -// $headers = [ -// "Authorization: Basic " . \base64_encode($this->appID . ":" . $this->appSecret), -// "Content-Type: application/x-www-form-urlencoded", -// ]; -// -// $accessToken = $this->request( -// 'POST', -// 'https://login.salesforce.com/services/oauth2/token', -// $headers, -// \http_build_query([ -// 'code' => $code, -// 'redirect_uri' => $this->callback , -// 'grant_type' => 'authorization_code' -// ]) -// ); -// $accessToken = \json_decode($accessToken, true); -// -// if (isset($accessToken['access_token'])) { -// return $accessToken['access_token']; -// } + $headers = [ + "Authorization: Basic " . \base64_encode($this->appID . ":" . $this->appSecret), + "Content-Type: application/x-www-form-urlencoded", + ]; + + $result = $this->request( + 'POST', + 'https://login.salesforce.com/services/oauth2/token', + $headers, + \http_build_query([ + 'code' => $code, + 'redirect_uri' => $this->callback , + 'grant_type' => 'authorization_code' + ]) + ); + + $result = \json_decode($result, true); return [ - 'access' => '', - 'refresh' => '' + 'access' => $result['access_token'], + 'refresh' => $result['refresh_token'] ]; } diff --git a/src/Appwrite/Auth/OAuth2/Slack.php b/src/Appwrite/Auth/OAuth2/Slack.php index bf9647893b..26605356a0 100644 --- a/src/Appwrite/Auth/OAuth2/Slack.php +++ b/src/Appwrite/Auth/OAuth2/Slack.php @@ -50,26 +50,22 @@ class Slack extends OAuth2 */ public function getTokens(string $code): array { -// // https://api.slack.com/docs/oauth#step_3_-_exchanging_a_verification_code_for_an_access_token -// $accessToken = $this->request( -// 'GET', -// 'https://slack.com/api/oauth.access?'.\http_build_query([ -// 'client_id' => $this->appID, -// 'client_secret' => $this->appSecret, -// 'code' => $code, -// 'redirect_uri' => $this->callback -// ]) -// ); -// -// $accessToken = \json_decode($accessToken, true); // -// -// if (isset($accessToken['access_token'])) { -// return $accessToken['access_token']; -// } + // https://api.slack.com/docs/oauth#step_3_-_exchanging_a_verification_code_for_an_access_token + $result = $this->request( + 'GET', + 'https://slack.com/api/oauth.access?'.\http_build_query([ + 'client_id' => $this->appID, + 'client_secret' => $this->appSecret, + 'code' => $code, + 'redirect_uri' => $this->callback + ]) + ); + + $result = \json_decode($result, true); return [ - 'access' => '', - 'refresh' => '' + 'access' => $result['access_token'], + 'refresh' => $result['refresh_token'] ]; } diff --git a/src/Appwrite/Auth/OAuth2/Spotify.php b/src/Appwrite/Auth/OAuth2/Spotify.php index f55484330b..89d5e2bd78 100644 --- a/src/Appwrite/Auth/OAuth2/Spotify.php +++ b/src/Appwrite/Auth/OAuth2/Spotify.php @@ -62,25 +62,21 @@ class Spotify extends OAuth2 */ public function getTokens(string $code): array { -// $header = "Authorization: Basic " . \base64_encode($this->appID . ":" . $this->appSecret); -// $result = \json_decode($this->request( -// 'POST', -// $this->endpoint . 'api/token', -// [$header], -// \http_build_query([ -// "code" => $code, -// "grant_type" => "authorization_code", -// "redirect_uri" => $this->callback -// ]) -// ), true); -// -// if (isset($result['access_token'])) { -// return $result['access_token']; -// } + $header = "Authorization: Basic " . \base64_encode($this->appID . ":" . $this->appSecret); + $result = \json_decode($this->request( + 'POST', + $this->endpoint . 'api/token', + [$header], + \http_build_query([ + "code" => $code, + "grant_type" => "authorization_code", + "redirect_uri" => $this->callback + ]) + ), true); return [ - 'access' => '', - 'refresh' => '' + 'access' => $result['access_token'], + 'refresh' => $result['refresh_token'] ]; } diff --git a/src/Appwrite/Auth/OAuth2/Stripe.php b/src/Appwrite/Auth/OAuth2/Stripe.php index 17b32140b1..b8f29b315c 100644 --- a/src/Appwrite/Auth/OAuth2/Stripe.php +++ b/src/Appwrite/Auth/OAuth2/Stripe.php @@ -61,29 +61,25 @@ class Stripe extends OAuth2 */ public function getTokens(string $code): array { -// $response = $this->request( -// 'POST', -// 'https://connect.stripe.com/oauth/token', -// [], -// \http_build_query([ -// 'grant_type' => $this->grantType['authorize'], -// 'code' => $code -// ]) -// ); -// -// $response = \json_decode($response, true); -// -// if (isset($response['stripe_user_id'])) { -// $this->stripeAccountId = $response['stripe_user_id']; -// } -// -// if (isset($response['access_token'])) { -// return $response['access_token']; -// } + $result = $this->request( + 'POST', + 'https://connect.stripe.com/oauth/token', + [], + \http_build_query([ + 'grant_type' => $this->grantType['authorize'], + 'code' => $code + ]) + ); + + $result = \json_decode($result, true); + + if (isset($result['stripe_user_id'])) { + $this->stripeAccountId = $result['stripe_user_id']; + } return [ - 'access' => '', - 'refresh' => '' + 'access' => $result['access_token'], + 'refresh' => $result['refresh_token'] ]; } diff --git a/src/Appwrite/Auth/OAuth2/Tradeshift.php b/src/Appwrite/Auth/OAuth2/Tradeshift.php index e730a41b6a..51b668fca7 100644 --- a/src/Appwrite/Auth/OAuth2/Tradeshift.php +++ b/src/Appwrite/Auth/OAuth2/Tradeshift.php @@ -73,22 +73,21 @@ class Tradeshift extends OAuth2 */ public function getTokens(string $code): array { -// $response = $this->request( -// 'POST', -// $this->endpoint[$this->environment] . 'auth/token', -// ['Authorization: Basic ' . \base64_encode($this->appID . ':' . $this->appSecret)], -// \http_build_query([ -// 'grant_type' => 'authorization_code', -// 'code' => $code, -// ]) -// ); -// -// $accessToken = \json_decode($response, true); -// return $accessToken['access_token'] ?? ''; + $result = $this->request( + 'POST', + $this->endpoint[$this->environment] . 'auth/token', + ['Authorization: Basic ' . \base64_encode($this->appID . ':' . $this->appSecret)], + \http_build_query([ + 'grant_type' => 'authorization_code', + 'code' => $code, + ]) + ); + + $result = \json_decode($result, true); return [ - 'access' => '', - 'refresh' => '' + 'access' => $result['access_token'], + 'refresh' => $result['refresh_token'] ]; } diff --git a/src/Appwrite/Auth/OAuth2/Vk.php b/src/Appwrite/Auth/OAuth2/Vk.php index 9ccf3b4d68..f7b9c5c9d8 100644 --- a/src/Appwrite/Auth/OAuth2/Vk.php +++ b/src/Appwrite/Auth/OAuth2/Vk.php @@ -61,35 +61,31 @@ class Vk extends OAuth2 */ public function getTokens(string $code): array { -// $headers = ['Content-Type: application/x-www-form-urlencoded;charset=UTF-8']; -// $accessToken = $this->request( -// 'POST', -// 'https://oauth.vk.com/access_token?', -// $headers, -// \http_build_query([ -// 'code' => $code, -// 'client_id' => $this->appID, -// 'client_secret' => $this->appSecret, -// 'redirect_uri' => $this->callback -// ]) -// ); -// $accessToken = \json_decode($accessToken, true); -// -// if (isset($accessToken['email'])) { -// $this->user['email'] = $accessToken['email']; -// } -// -// if (isset($accessToken['user_id'])) { -// $this->user['user_id'] = $accessToken['user_id']; -// } -// -// if (isset($accessToken['access_token'])) { -// return $accessToken['access_token']; -// } + $headers = ['Content-Type: application/x-www-form-urlencoded;charset=UTF-8']; + $result = $this->request( + 'POST', + 'https://oauth.vk.com/access_token?', + $headers, + \http_build_query([ + 'code' => $code, + 'client_id' => $this->appID, + 'client_secret' => $this->appSecret, + 'redirect_uri' => $this->callback + ]) + ); + $result = \json_decode($result, true); + + if (isset($result['email'])) { + $this->user['email'] = $result['email']; + } + + if (isset($result['user_id'])) { + $this->user['user_id'] = $result['user_id']; + } return [ - 'access' => '', - 'refresh' => '' + 'access' => $result['access_token'], + 'refresh' => $result['refresh_token'] ]; } diff --git a/src/Appwrite/Auth/OAuth2/WordPress.php b/src/Appwrite/Auth/OAuth2/WordPress.php index 3c012aa5b8..e11777e642 100644 --- a/src/Appwrite/Auth/OAuth2/WordPress.php +++ b/src/Appwrite/Auth/OAuth2/WordPress.php @@ -50,28 +50,24 @@ class WordPress extends OAuth2 */ public function getTokens(string $code): array { -// $accessToken = $this->request( -// 'POST', -// 'https://public-api.wordpress.com/oauth2/token', -// [], -// \http_build_query([ -// 'client_id' => $this->appID, -// 'redirect_uri' => $this->callback, -// 'client_secret' => $this->appSecret, -// 'grant_type' => 'authorization_code', -// 'code' => $code -// ]) -// ); -// -// $accessToken = \json_decode($accessToken, true); -// -// if (isset($accessToken['access_token'])) { -// return $accessToken['access_token']; -// } + $result = $this->request( + 'POST', + 'https://public-api.wordpress.com/oauth2/token', + [], + \http_build_query([ + 'client_id' => $this->appID, + 'redirect_uri' => $this->callback, + 'client_secret' => $this->appSecret, + 'grant_type' => 'authorization_code', + 'code' => $code + ]) + ); + + $result = \json_decode($result, true); return [ - 'access' => '', - 'refresh' => '' + 'access' => $result['access_token'], + 'refresh' => $result['refresh_token'] ]; } diff --git a/src/Appwrite/Auth/OAuth2/Yahoo.php b/src/Appwrite/Auth/OAuth2/Yahoo.php index 0abe1eb1dc..113c99ca7e 100644 --- a/src/Appwrite/Auth/OAuth2/Yahoo.php +++ b/src/Appwrite/Auth/OAuth2/Yahoo.php @@ -74,29 +74,25 @@ class Yahoo extends OAuth2 */ public function getTokens(string $code): array { -// $header = [ -// "Authorization: Basic " . \base64_encode($this->appID . ":" . $this->appSecret), -// "Content-Type: application/x-www-form-urlencoded", -// ]; -// -// $result = \json_decode($this->request( -// 'POST', -// $this->endpoint . 'get_token', -// $header, -// \http_build_query([ -// "code" => $code, -// "grant_type" => "authorization_code", -// "redirect_uri" => $this->callback -// ]) -// ), true); -// -// if (isset($result['access_token'])) { -// return $result['access_token']; -// } + $header = [ + "Authorization: Basic " . \base64_encode($this->appID . ":" . $this->appSecret), + "Content-Type: application/x-www-form-urlencoded", + ]; + + $result = \json_decode($this->request( + 'POST', + $this->endpoint . 'get_token', + $header, + \http_build_query([ + "code" => $code, + "grant_type" => "authorization_code", + "redirect_uri" => $this->callback + ]) + ), true); return [ - 'access' => '', - 'refresh' => '' + 'access' => $result['access_token'], + 'refresh' => $result['refresh_token'] ]; } diff --git a/src/Appwrite/Auth/OAuth2/Yammer.php b/src/Appwrite/Auth/OAuth2/Yammer.php index 3e26e7cfe1..6c0fe85897 100644 --- a/src/Appwrite/Auth/OAuth2/Yammer.php +++ b/src/Appwrite/Auth/OAuth2/Yammer.php @@ -48,29 +48,25 @@ class Yammer extends OAuth2 */ public function getTokens(string $code): array { -// $headers = ['Content-Type: application/x-www-form-urlencoded']; -// -// $accessToken = $this->request( -// 'POST', -// $this->endpoint . 'access_token?', -// $headers, -// \http_build_query([ -// 'client_id' => $this->appID, -// 'client_secret' => $this->appSecret, -// 'code' => $code, -// 'grant_type' => 'authorization_code' -// ]) -// ); -// -// $accessToken = \json_decode($accessToken, true); -// -// if (isset($accessToken['access_token']['token'])) { -// return $accessToken['access_token']['token']; -// } + $headers = ['Content-Type: application/x-www-form-urlencoded']; + + $result = $this->request( + 'POST', + $this->endpoint . 'access_token?', + $headers, + \http_build_query([ + 'client_id' => $this->appID, + 'client_secret' => $this->appSecret, + 'code' => $code, + 'grant_type' => 'authorization_code' + ]) + ); + + $result = \json_decode($result, true); return [ - 'access' => '', - 'refresh' => '' + 'access' => $result['access_token'], + 'refresh' => $result['refresh_token'] ]; } diff --git a/src/Appwrite/Auth/OAuth2/Yandex.php b/src/Appwrite/Auth/OAuth2/Yandex.php index ecb904181f..2a60ac0e8d 100644 --- a/src/Appwrite/Auth/OAuth2/Yandex.php +++ b/src/Appwrite/Auth/OAuth2/Yandex.php @@ -60,29 +60,26 @@ class Yandex extends OAuth2 */ public function getTokens(string $code): array { -// $headers = [ -// "Authorization: Basic " . \base64_encode($this->appID . ":" . $this->appSecret), -// "Content-Type: application/x-www-form-urlencoded", -// ]; -// -// $accessToken = $this->request( -// 'POST', -// 'https://oauth.yandex.com/token', -// $headers, -// \http_build_query([ -// 'code' => $code, -// 'grant_type' => 'authorization_code' -// ]) -// ); -// $accessToken = \json_decode($accessToken, true); -// -// if (isset($accessToken['access_token'])) { -// return $accessToken['access_token']; -// } + $headers = [ + "Authorization: Basic " . \base64_encode($this->appID . ":" . $this->appSecret), + "Content-Type: application/x-www-form-urlencoded", + ]; + + $result = $this->request( + 'POST', + 'https://oauth.yandex.com/token', + $headers, + \http_build_query([ + 'code' => $code, + 'grant_type' => 'authorization_code' + ]) + ); + + $result = \json_decode($result, true); return [ - 'access' => '', - 'refresh' => '' + 'access' => $result['access_token'], + 'refresh' => $result['refresh_token'] ]; }