From b06a887d858cb29559d3034f73455c125643fedc Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Sat, 2 Apr 2022 16:12:31 +0300 Subject: [PATCH] feat: update Microsoft OAuth Adapter --- public/scripts/views/forms/oauth-custom.js | 6 +-- src/Appwrite/Auth/OAuth2/Microsoft.php | 44 +++++++++++----------- 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/public/scripts/views/forms/oauth-custom.js b/public/scripts/views/forms/oauth-custom.js index 323c874411..fd8bd855d0 100644 --- a/public/scripts/views/forms/oauth-custom.js +++ b/public/scripts/views/forms/oauth-custom.js @@ -10,11 +10,11 @@ let providers = { "Microsoft": { "clientSecret": "oauth2MicrosoftClientSecret", - "tenantId": "oauth2MicrosoftTenantId" + "tenantID": "oauth2MicrosoftTenantId" }, "Apple": { - "keyId": "oauth2AppleKeyId", - "teamId": "oauth2AppleTeamId", + "keyID": "oauth2AppleKeyId", + "teamID": "oauth2AppleTeamId", "p8": "oauth2AppleP8" } } diff --git a/src/Appwrite/Auth/OAuth2/Microsoft.php b/src/Appwrite/Auth/OAuth2/Microsoft.php index 417c2ef3d5..ebfd2e4e83 100644 --- a/src/Appwrite/Auth/OAuth2/Microsoft.php +++ b/src/Appwrite/Auth/OAuth2/Microsoft.php @@ -41,7 +41,7 @@ class Microsoft extends OAuth2 */ public function getLoginURL(): string { - return 'https://login.microsoftonline.com/'.$this->getTenantId().'/oauth2/v2.0/authorize?'.\http_build_query([ + return 'https://login.microsoftonline.com/'.$this->getTenantID().'/oauth2/v2.0/authorize?'.\http_build_query([ 'client_id' => $this->appID, 'redirect_uri' => $this->callback, 'state'=> \json_encode($this->state), @@ -62,7 +62,7 @@ class Microsoft extends OAuth2 $headers = ['Content-Type: application/x-www-form-urlencoded']; $this->tokens = \json_decode($this->request( 'POST', - 'https://login.microsoftonline.com/' . $this->getTenantId() . '/oauth2/v2.0/token', + 'https://login.microsoftonline.com/' . $this->getTenantID() . '/oauth2/v2.0/token', $headers, \http_build_query([ 'code' => $code, @@ -88,7 +88,7 @@ class Microsoft extends OAuth2 $headers = ['Content-Type: application/x-www-form-urlencoded']; $this->tokens = \json_decode($this->request( 'POST', - 'https://login.microsoftonline.com/' . $this->getTenantId() . '/oauth2/v2.0/token', + 'https://login.microsoftonline.com/' . $this->getTenantID() . '/oauth2/v2.0/token', $headers, \http_build_query([ 'refresh_token' => $refreshToken, @@ -169,38 +169,40 @@ class Microsoft extends OAuth2 return $this->user; } - /** - * Extracts the Client Secret from the JSON stored in appSecret - * @return string - */ - protected function getClientSecret(): string - { - $secret = $this->decodeJson(); - - return (isset($secret['clientSecret'])) ? $secret['clientSecret'] : ''; - } - /** * Decode the JSON stored in appSecret + * * @return array */ - protected function decodeJson(): array + protected function getAppSecret(): array { try { - $secret = \json_decode($this->appSecret, true); + $secret = \json_decode($this->appSecret, true, 512, JSON_THROW_ON_ERROR); } catch (\Throwable $th) { - throw new Exception('Invalid secret'); + throw new \Exception('Invalid secret'); } return $secret; } /** - * Extracts the Tenant Id from the JSON stored in appSecret. Defaults to 'common' as a fallback + * Extracts the Client Secret from the JSON stored in appSecret + * * @return string */ - protected function getTenantId(): string + protected function getClientSecret(): string { - $secret = $this->decodeJson(); - return (isset($secret['tenantId'])) ? $secret['tenantId'] : 'common'; + $secret = $this->getAppSecret(); + return (isset($secret['clientSecret'])) ? $secret['clientSecret'] : ''; + } + + /** + * Extracts the Tenant Id from the JSON stored in appSecret. Defaults to 'common' as a fallback + * + * @return string + */ + protected function getTenantID(): string + { + $secret = $this->getAppSecret(); + return (isset($secret['tenantID'])) ? $secret['tenantID'] : 'common'; } }