From b2e86ce6c7500f92a93074c8e16e8120be24f27d Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Thu, 24 Dec 2020 18:44:21 +0530 Subject: [PATCH] feat: added Linkedin.php --- src/Appwrite/Auth/OAuth2/Linkedin.php | 157 ++++++++++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100644 src/Appwrite/Auth/OAuth2/Linkedin.php diff --git a/src/Appwrite/Auth/OAuth2/Linkedin.php b/src/Appwrite/Auth/OAuth2/Linkedin.php new file mode 100644 index 000000000..5f7a76202 --- /dev/null +++ b/src/Appwrite/Auth/OAuth2/Linkedin.php @@ -0,0 +1,157 @@ + '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 + { + $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']; + } + + return ''; + } + + /** + * @param $accessToken + * + * @return string + */ + public function getUserID(string $accessToken):string + { + $user = $this->getUser($accessToken); + + if (isset($user['id'])) { + return $user['id']; + } + + return ''; + } + + /** + * @param $accessToken + * + * @return string + */ + public function getUserEmail(string $accessToken):string + { + $email = \json_decode($this->request('GET', 'https://api.linkedin.com/v2/emailAddress?q=members&projection=(elements*(handle~))', ['Authorization: Bearer '.\urlencode($accessToken)]), true); + + if ( + isset($email['elements']) && + isset($email['elements'][0]) && + isset($email['elements'][0]['handle~']) && + isset($email['elements'][0]['handle~']['emailAddress']) + ) { + return $email['elements'][0]['handle~']['emailAddress']; + } + + return ''; + } + + /** + * @param $accessToken + * + * @return string + */ + public function getUserName(string $accessToken):string + { + $user = $this->getUser($accessToken); + $name = ''; + + if (isset($user['localizedFirstName'])) { + $name = $user['localizedFirstName']; + } + + if (isset($user['localizedLastName'])) { + $name = (empty($name)) ? $user['localizedLastName'] : $name.' '.$user['localizedLastName']; + } + + return $name; + } + + /** + * @param string $accessToken + * + * @return array + */ + protected function getUser(string $accessToken) + { + if (empty($this->user)) { + $this->user = \json_decode($this->request('GET', 'https://api.linkedin.com/v2/me', ['Authorization: Bearer '.\urlencode($accessToken)]), true); + } + + return $this->user; + } +}