diff --git a/app/config/providers.php b/app/config/providers.php index 79c0a8efb7..1a623faeb9 100644 --- a/app/config/providers.php +++ b/app/config/providers.php @@ -31,6 +31,16 @@ return [ // Ordered by ABC. 'beta' => false, 'mock' => false, ], + 'autodesk' => [ + 'name' => 'Autodesk', + 'developers' => 'https://forge.autodesk.com/en/docs/oauth/v2/developers_guide/overview/', + 'icon' => 'icon-autodesk', + 'enabled' => true, + 'sandbox' => false, + 'form' => false, + 'beta' => false, + 'mock' => false, + ], 'bitbucket' => [ 'name' => 'BitBucket', 'developers' => 'https://developer.atlassian.com/bitbucket', diff --git a/public/images/users/autodesk.png b/public/images/users/autodesk.png new file mode 100644 index 0000000000..c7ad94f61a Binary files /dev/null and b/public/images/users/autodesk.png differ diff --git a/src/Appwrite/Auth/OAuth2/Autodesk.php b/src/Appwrite/Auth/OAuth2/Autodesk.php new file mode 100644 index 0000000000..0b268ead3b --- /dev/null +++ b/src/Appwrite/Auth/OAuth2/Autodesk.php @@ -0,0 +1,178 @@ + $this->appID, + 'scope' => \implode(' ', $this->getScopes()), + 'state' => \json_encode($this->state), + 'redirect_uri' => $this->callback, + 'response_type' => 'code' + ]); + } + + /** + * @param string $code + * + * @return array + */ + protected function getTokens(string $code): array + { + if (empty($this->tokens)) { + $headers = ['Content-Type: application/x-www-form-urlencoded']; + $response = $this->request( + 'POST', + 'https://developer.api.autodesk.com/authentication/v1/gettoken', + $headers, + \http_build_query([ + 'client_id' => $this->appID, + 'redirect_uri' => $this->callback, + 'client_secret' => $this->appSecret, + 'code' => $code, + 'grant_type' => 'authorization_code' + ]) + ); + + $this->tokens = \json_decode($response, true); + } + + return $this->tokens; + } + + /** + * @param string $refreshToken + * + * @return array + */ + public function refreshTokens(string $refreshToken): array + { + $response = $this->request( + 'POST', + 'https://developer.api.autodesk.com/authentication/v1/refreshtoken', + [], + \http_build_query([ + 'client_id' => $this->appID, + 'client_secret' => $this->appSecret, + 'grant_type' => 'refresh_token', + 'code' => $code, + 'redirect_uri' => $this->callback, + ]) + ); + + $output = []; + \parse_str($response, $output); + $this->tokens = $output; + + 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['userId'] ?? ''; + } + + /** + * @param string $accessToken + * + * @return string + */ + public function getUserEmail(string $accessToken): string + { + $user = $this->getUser($accessToken); + + return $user['emailId'] ?? ''; + } + + /** + * Check if the OAuth email is verified + * + * @link https://docs.github.com/en/rest/users/emails#list-email-addresses-for-the-authenticated-user + * + * @param string $accessToken + * + * @return bool + */ + public function isEmailVerified(string $accessToken): bool + { + $user = $this->getUser($accessToken); + + if ($user['emailVerified'] ?? 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)) { + $headers = ['Authorization: Bearer ' . \urlencode($accessToken)]; + $user = $this->request('GET', 'https://developer.api.autodesk.com/userprofile/v1/users/@me', $headers); + $this->user = \json_decode($user, true); + } + + return $this->user; + } +}