From 0722f34347ff0151419d19749493be86b1e59f2a Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Sun, 16 Jan 2022 02:47:49 +0400 Subject: [PATCH] feat: add notion oauth provider --- app/config/providers.php | 10 ++ public/images/users/notion.png | Bin 0 -> 1919 bytes src/Appwrite/Auth/OAuth2/Notion.php | 148 ++++++++++++++++++++++++++++ 3 files changed, 158 insertions(+) create mode 100644 public/images/users/notion.png create mode 100644 src/Appwrite/Auth/OAuth2/Notion.php diff --git a/app/config/providers.php b/app/config/providers.php index bb823c9ab..3aabbf60c 100644 --- a/app/config/providers.php +++ b/app/config/providers.php @@ -131,6 +131,16 @@ return [ // Ordered by ABC. 'beta' => false, 'mock' => false, ], + 'notion' => [ + 'name' => 'Notion', + 'developers' => 'https://developers.notion.com/docs', + 'icon' => 'icon-notion', + 'enabled' => true, + 'sandbox' => false, + 'form' => false, + 'beta' => false, + 'mock' => false, + ], 'paypal' => [ 'name' => 'PayPal', 'developers' => 'https://developer.paypal.com/docs/api/overview/', diff --git a/public/images/users/notion.png b/public/images/users/notion.png new file mode 100644 index 0000000000000000000000000000000000000000..4eff411a429ef2362ff3116b06360de2c56c6561 GIT binary patch literal 1919 zcmV-_2Y~pAP)JyO5BOcXxMTVPR2GQ9?pO zGBPqGBqSOd8WIu`3JMAX0|P%lKi}Wqo}QkEhlhB0cxY&7R#sL%K0YZaDG(443kwUG znVI^iSXggwZ-aw_k&%&(j*f6}a3Ufi0s;bVZf*nw z1QQbzF)=YfKtNDXP+wnPetv$Flas2ds=>j**VotNFMeA_V)1b@W#f*Qc_YUCnwU<($LV*!^6Y4xVVy% zl7)qZ5fKr~%ggKQ>)F}av$M0Ip`nzNlzV%7RaI3CdAy}g!}mIw$44-XF^At6IULrzXkT3T9WW@dkXe~XKYrKP2}x3|j5%J1**CMG63 zJ3Eh$kDQ#GsHmv4w6ws$z|73do12?sV`Jgr;nmgEwY9Y~Gc)@7`f_q|WMpJWNJu(5 zIxH+KU|?WnWo4|atl;3_A0Hn#H#cWzXDux)E-o&BfPi&%bJ6%~Pjf#&AsL_|dD>guqtuv1f0(b3V~ z-rinbUPeYnIXO8A2?;bbG`YFC;^N|KYimqQOc)p#H8nM_udmwL+BP;eY;0`j=jVBO zd77G<2_=)H000D9NklrLCi@r!R{Q$Qc-tVK$$Uv5Bdf zxrL=1Rzop?m9-6tRxsJh5Hi%x-oepH)7iz<&E3P(3n<{Fp+uS`-dN4`@%8lc4+wM# z3JwVk3s;IzgekXCi3G_@az~M1N3MQuyG>{Cnp%=^^)Crq5wHhIcas#qd7ZTgIvvu5|tnLBab`~|EF z(bBmIS(Z#)w0Nd^zw45v%a*TL86RFjpscYb$CA{Qq!g|U46DeoWHm`vkZFm@NU}tM zdCl5Pm336LB#K!9!Esyfs0UFfX+yas8zybsl(>1z)?Q0}*=_K=u@sSg%&6d#?MPyF zJ78*E^Sr5C}ks z`b?&SC9=B`=wONLc6wMM0}C)(S|YobnRb@!gUf8Boh7QJFc~iu+F2521(RCOOEXIt z_QM)TatCNL~&VS6GMq5 z#a={N!f<>l1(u*Tv2a_$aDoC$q6&z#0H!Bv`_5R1!1d zi2|x5Si+#JOO_=?GfA<8As8}b-cO|RD5f&jYsFU}y*{EXVT7nJ#U)>U`Fc&|{CU9_ zTyALGytU6W`~0-q(Bz^oK&&MU$#=k+;x3fSd#{$Ad#S3c+Wo)>i!C1F8@==8Bia%M zLtl^%x$4D_EF7B%4GAIxoW92f9+P4+0ysS(#gZr!(gq*pnj)W;KHIT@6gz-W$QxOo;;5r zZM+R9DB@Rr5hwC8YeCq(qtS>tlTk1VM!_f;1)~5B008X2D^0-mgvtN_002ovPDHLk FV1hRVcJ=@O literal 0 HcmV?d00001 diff --git a/src/Appwrite/Auth/OAuth2/Notion.php b/src/Appwrite/Auth/OAuth2/Notion.php new file mode 100644 index 000000000..748e7958e --- /dev/null +++ b/src/Appwrite/Auth/OAuth2/Notion.php @@ -0,0 +1,148 @@ +endpoint . '/oauth/authorize?'. \http_build_query([ + 'client_id' => $this->appID, + 'redirect_uri' => $this->callback, + 'response_type' => 'code', + 'state' => \json_encode($this->state), + 'owner' => 'user' + ]); + } + + /** + * @param string $code + * + * @return string + */ + public function getAccessToken(string $code):string + { + $headers = [ + "Authorization: Basic " . \base64_encode($this->appID . ":" . $this->appSecret), + ]; + + $response = $this->request( + 'POST', + $this->endpoint . '/oauth/token', + $headers, + \http_build_query([ + 'grant_type' => 'authorization_code', + 'redirect_uri' => $this->callback, + 'code' => $code + ]) + ); + + $response = \json_decode($response, true); + + if (isset($response['access_token'])) { + return $response['access_token']; + } + + return ''; + } + + /** + * @param $accessToken + * + * @return string + */ + public function getUserID(string $accessToken):string + { + $response = $this->getUser($accessToken); + + if (isset($response['bot']['owner']['user']['id'])) { + return $response['bot']['owner']['user']['id']; + } + + return ''; + } + + /** + * @param $accessToken + * + * @return string + */ + public function getUserEmail(string $accessToken):string + { + $response = $this->getUser($accessToken); + + if(isset($response['bot']['owner']['user']['person']['email'])){ + return $response['bot']['owner']['user']['person']['email']; + } + + return ''; + } + + /** + * @param $accessToken + * + * @return string + */ + public function getUserName(string $accessToken):string + { + $response = $this->getUser($accessToken); + + if (isset($response['bot']['owner']['user']['name'])) { + return $response['bot']['owner']['user']['name']; + } + + return ''; + } + + /** + * @param string $accessToken + * + * @return array + */ + protected function getUser(string $accessToken) + { + $headers = [ + 'Notion-Version: ' . $this->version, + 'Authorization: Bearer '.\urlencode($accessToken) + ]; + + if (empty($this->user)) { + $this->user = \json_decode($this->request('GET', $this->endpoint . '/users/me', $headers), true); + } + + return $this->user; + } +}