diff --git a/app/config/providers.php b/app/config/providers.php index 5dc43e8e56..3b5661790f 100644 --- a/app/config/providers.php +++ b/app/config/providers.php @@ -67,6 +67,12 @@ return [ 'enabled' => true, 'mock' => false, ], + 'salesforce' => [ + 'developers' => 'https://developer.salesforce.com/docs/', + 'icon' => 'icon-salesforce', + 'enabled' => true, + 'mock' => false, + ], // 'apple' => [ // 'developers' => 'https://developer.apple.com/', // 'icon' => 'icon-apple', diff --git a/public/images/oauth/salesforce.png b/public/images/oauth/salesforce.png new file mode 100644 index 0000000000..2b8e4c9846 Binary files /dev/null and b/public/images/oauth/salesforce.png differ diff --git a/src/Auth/OAuth/Salesforce.php b/src/Auth/OAuth/Salesforce.php new file mode 100644 index 0000000000..89957e227e --- /dev/null +++ b/src/Auth/OAuth/Salesforce.php @@ -0,0 +1,151 @@ + 'code', + 'client_id' => $this->appID, + 'redirect_uri'=> $this->callback, + 'scope'=> implode(' ', $this->getScopes()), + 'state' => json_encode($this->state) + ]); + } + + /** + * @param string $code + * + * @return string + */ + public function getAccessToken(string $code): string + { + $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']; + } + + return ''; + } + + /** + * @param string $accessToken + * + * @return string + */ + public function getUserID(string $accessToken): string + { + $user = $this->getUser($accessToken); + + if (isset($user['user_id'])) { + return $user['user_id']; + } + + return ''; + } + + /** + * @param string $accessToken + * + * @return string + */ + public function getUserEmail(string $accessToken): string + { + $user = $this->getUser($accessToken); + + if (isset($user['email'])) { + return $user['email']; + } + + return ''; + } + + /** + * @param string $accessToken + * + * @return string + */ + public function getUserName(string $accessToken): string + { + $user = $this->getUser($accessToken); + + if (isset($user['name'])) { + return $user['name']; + } + + return ''; + } + + /** + * @param string $accessToken + * + * @return array + */ + protected function getUser(string $accessToken): array + { + if (empty($this->user)) { + $user = $this->request('GET', 'https://login.salesforce.com/services/oauth2/userinfo?access_token='.urlencode($accessToken)); + $this->user = json_decode($user, true); + } + return $this->user; + } +}