diff --git a/app/config/providers.php b/app/config/providers.php index e22d4bdd39..cf58282107 100644 --- a/app/config/providers.php +++ b/app/config/providers.php @@ -211,6 +211,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 0000000000..0eb6b49ac9 Binary files /dev/null and b/public/images/users/podio.png differ diff --git a/src/Appwrite/Auth/OAuth2/Podio.php b/src/Appwrite/Auth/OAuth2/Podio.php new file mode 100644 index 0000000000..e4194238d1 --- /dev/null +++ b/src/Appwrite/Auth/OAuth2/Podio.php @@ -0,0 +1,199 @@ +endpoint . '/authorize?' . + \http_build_query([ + 'client_id' => $this->appID, + 'state' => \json_encode($this->state), + 'redirect_uri' => $this->callback + ]); + + return $url; + } + + /** + * @param string $code + * + * @return array + */ + protected function getTokens(string $code): array + { + if (empty($this->tokens)) { + $this->tokens = \json_decode($this->request( + 'POST', + $this->apiEndpoint . '/oauth/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 + ]) + ), true); + } + + return $this->tokens; + } + + /** + * @param string $refreshToken + * + * @return array + */ + public function refreshTokens(string $refreshToken): array + { + $this->tokens = \json_decode($this->request( + 'POST', + $this->apiEndpoint . '/oauth/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 \strval($user['user_id']) ?? ''; + } + + /** + * @param string $accessToken + * + * @return string + */ + public function getUserEmail(string $accessToken): string + { + $user = $this->getUser($accessToken); + + return $user['mail'] ?? ''; + } + + /** + * Check if the OAuth email is verified + * + * @param string $accessToken + * + * @return bool + */ + public function isEmailVerified(string $accessToken): bool + { + $user = $this->getUser($accessToken); + + $mails = $user['mails']; + $mainMailIndex = \array_search($user['mail'], \array_map(fn($m) => $m['mail'], $mails)); + $mainMain = $mails[$mainMailIndex]; + + if ($mainMain['verified'] ?? false) { + return true; + } + + return false; + } + + /** + * @param string $accessToken + * + * @return string + */ + public function getUserName(string $accessToken): string + { + $user = $this->getUser($accessToken); + + return $user['name'] ?? ''; + } + + /** + * @param string $accessToken + * + * @return array + */ + protected function getUser(string $accessToken): array + { + if (empty($this->user)) { + $user = \json_decode($this->request( + 'GET', + $this->apiEndpoint . '/user', + ['Authorization: Bearer ' . \urlencode($accessToken)] + ), 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; + } +}