From d8a0e9bd14f389ddc644ec33174044ec6808dd06 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Thu, 24 Dec 2020 18:04:55 +0530 Subject: [PATCH 1/3] feat: updated new Linkedin scopes --- app/controllers/api/account.php | 4 ---- src/Appwrite/Auth/OAuth2/LinkedIn.php | 2 +- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index f4ccd225c..dee63154f 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -292,8 +292,6 @@ App::get('/v1/account/sessions/oauth2/:provider') $appId = $project->getAttribute('usersOauth2'.\ucfirst($provider).'Appid', ''); $appSecret = $project->getAttribute('usersOauth2'.\ucfirst($provider).'Secret', '{}'); - $appSecret = \json_decode($appSecret, true); - if (!empty($appSecret) && isset($appSecret['version'])) { $key = App::getEnv('_APP_OPENSSL_KEY_V'.$appSecret['version']); $appSecret = OpenSSL::decrypt($appSecret['data'], $appSecret['method'], $key, 0, \hex2bin($appSecret['iv']), \hex2bin($appSecret['tag'])); @@ -395,8 +393,6 @@ App::get('/v1/account/sessions/oauth2/:provider/redirect') $appId = $project->getAttribute('usersOauth2'.\ucfirst($provider).'Appid', ''); $appSecret = $project->getAttribute('usersOauth2'.\ucfirst($provider).'Secret', '{}'); - $appSecret = \json_decode($appSecret, true); - if (!empty($appSecret) && isset($appSecret['version'])) { $key = App::getEnv('_APP_OPENSSL_KEY_V'.$appSecret['version']); $appSecret = OpenSSL::decrypt($appSecret['data'], $appSecret['method'], $key, 0, \hex2bin($appSecret['iv']), \hex2bin($appSecret['tag'])); diff --git a/src/Appwrite/Auth/OAuth2/LinkedIn.php b/src/Appwrite/Auth/OAuth2/LinkedIn.php index d2315980b..87d4783ef 100644 --- a/src/Appwrite/Auth/OAuth2/LinkedIn.php +++ b/src/Appwrite/Auth/OAuth2/LinkedIn.php @@ -15,7 +15,7 @@ class LinkedIn extends OAuth2 * @var array */ protected $scopes = [ - 'r_basicprofile', + 'r_liteprofile', 'r_emailaddress', ]; From fac964a0444a8de0bc938316c9b07472eeaa98e6 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Thu, 24 Dec 2020 18:43:31 +0530 Subject: [PATCH 2/3] feat: removed LinkedIn.php --- src/Appwrite/Auth/OAuth2/LinkedIn.php | 157 -------------------------- 1 file changed, 157 deletions(-) delete mode 100644 src/Appwrite/Auth/OAuth2/LinkedIn.php diff --git a/src/Appwrite/Auth/OAuth2/LinkedIn.php b/src/Appwrite/Auth/OAuth2/LinkedIn.php deleted file mode 100644 index 87d4783ef..000000000 --- a/src/Appwrite/Auth/OAuth2/LinkedIn.php +++ /dev/null @@ -1,157 +0,0 @@ - '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; - } -} From b2e86ce6c7500f92a93074c8e16e8120be24f27d Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Thu, 24 Dec 2020 18:44:21 +0530 Subject: [PATCH 3/3] 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; + } +}