From 78c62b3b1993bd553cb972df0396d6977fc97ca4 Mon Sep 17 00:00:00 2001 From: Josh Crawford Date: Thu, 12 Sep 2024 23:59:08 +1000 Subject: [PATCH 1/4] Add Microsoft Entra provider --- .../provider/MicrosoftEntra.php | 75 +++++++++++++++++++ .../provider/MicrosoftEntraResourceOwner.php | 67 +++++++++++++++++ src/helpers/Provider.php | 3 + src/providers/MicrosoftEntra.php | 30 ++++++++ 4 files changed, 175 insertions(+) create mode 100644 src/clients/microsoftentra/provider/MicrosoftEntra.php create mode 100644 src/clients/microsoftentra/provider/MicrosoftEntraResourceOwner.php create mode 100644 src/providers/MicrosoftEntra.php diff --git a/src/clients/microsoftentra/provider/MicrosoftEntra.php b/src/clients/microsoftentra/provider/MicrosoftEntra.php new file mode 100644 index 0000000..53bed7c --- /dev/null +++ b/src/clients/microsoftentra/provider/MicrosoftEntra.php @@ -0,0 +1,75 @@ +tenant; + } + + public function getBaseAuthorizationUrl(): string + { + return $this->baseUrl() . '/oauth2/v2.0/authorize'; + } + + public function getBaseAccessTokenUrl(array $params): string + { + return $this->baseUrl() . '/oauth2/v2.0/token'; + } + + public function getResourceOwnerDetailsUrl(AccessToken $token): string + { + return 'https://graph.microsoft.com/v1.0/me'; + } + + protected function getDefaultScopes(): array + { + return ['User.Read']; + } + + protected function getScopeSeparator(): string + { + return ' '; + } + + protected function checkResponse(ResponseInterface $response, $data): void + { + if (isset($data['error'])) { + $statusCode = $response->getStatusCode(); + $error = $data['error']; + $errorDescription = $data['error_description']; + $errorLink = ($data['error_uri'] ?? false); + + throw new IdentityProviderException( + $statusCode . ' - ' . $errorDescription . ': ' . $error . ($errorLink ? ' (see: ' . $errorLink . ')' : ''), + $response->getStatusCode(), + $response + ); + } + } + + protected function createResourceOwner(array $response, AccessToken $token): MicrosoftEntraResourceOwner + { + return new MicrosoftEntraResourceOwner($response); + } + + protected function getAccessTokenRequest(array $params): RequestInterface + { + $request = parent::getAccessTokenRequest($params); + $uri = $request->getUri()->withUserInfo($this->clientId, $this->clientSecret); + + return $request->withUri($uri); + } +} diff --git a/src/clients/microsoftentra/provider/MicrosoftEntraResourceOwner.php b/src/clients/microsoftentra/provider/MicrosoftEntraResourceOwner.php new file mode 100644 index 0000000..c47a184 --- /dev/null +++ b/src/clients/microsoftentra/provider/MicrosoftEntraResourceOwner.php @@ -0,0 +1,67 @@ +response = $response; + } + + public function getId(): ?string + { + return $this->getValueByKey($this->response, 'id'); + } + + public function getFullName(): ?string + { + return $this->getValueByKey($this->response, 'displayName'); + } + + public function getFirstName(): ?string + { + return $this->getValueByKey($this->response, 'givenName'); + } + + public function getLastName(): ?string + { + return $this->getValueByKey($this->response, 'surname'); + } + + public function getEmail(): ?string + { + return $this->getValueByKey($this->response, 'mail'); + } + + public function getUpn(): ?string + { + return $this->getValueByKey($this->response, 'userPrincipalName'); + } + + public function getJobTitle(): ?string + { + return $this->getValueByKey($this->response, 'jobTitle'); + } + + public function getMobilePhone(): ?string + { + return $this->getValueByKey($this->response, 'mobilePhone'); + } + + public function getBusinessPhone(): ?string + { + return $this->getValueByKey($this->response, 'businessPhones.0'); + } + + public function toArray(): array + { + return $this->response; + } +} diff --git a/src/helpers/Provider.php b/src/helpers/Provider.php index d722d99..0154d19 100644 --- a/src/helpers/Provider.php +++ b/src/helpers/Provider.php @@ -94,6 +94,7 @@ public static function getPrimaryColor(string $handle): ?string 'meneame' => '#ff6400', 'messenger' => '#0278ff', 'microsoft' => '#5e5e5e', + 'microsoftEntra' => '#5e5e5e', 'mix' => '#fd8334', 'mixCloud' => '#324259', 'myob' => '#6100a5', @@ -338,6 +339,8 @@ public static function getIcon(string $handle): ?string 'messenger' => '', 'microsoft' => '', + + 'microsoftEntra' => '', 'mix' => '', diff --git a/src/providers/MicrosoftEntra.php b/src/providers/MicrosoftEntra.php new file mode 100644 index 0000000..d05b698 --- /dev/null +++ b/src/providers/MicrosoftEntra.php @@ -0,0 +1,30 @@ + (string)$token->getToken(), + ]; + } +} \ No newline at end of file From 572ace9c27a3cafe4240ffa014d2e3a7551e1603 Mon Sep 17 00:00:00 2001 From: Josh Crawford Date: Fri, 13 Sep 2024 00:14:05 +1000 Subject: [PATCH 2/4] version 1.0.33 --- CHANGELOG.md | 5 +++++ composer.json | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1998e07..895096b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## 1.0.33 - 2024-09-13 + +### Added +- Add Microsoft Entra provider. + ## 1.0.32 - 2024-08-29 ### Fixed diff --git a/composer.json b/composer.json index 6a29353..a5054c4 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "verbb/auth", "description": "A Craft CMS module to make working with authentication for third-parties a breeze.", - "version": "1.0.32", + "version": "1.0.33", "support": { "email": "support@verbb.io", "issues": "https://github.com/verbb/auth/issues?state=open", From 5f4c6fce847cdd5939f9eedd35b157e56023a9e1 Mon Sep 17 00:00:00 2001 From: Josh Crawford Date: Fri, 13 Sep 2024 00:56:56 +1000 Subject: [PATCH 3/4] Fix an error with Microsoft Entra --- src/providers/MicrosoftEntra.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/providers/MicrosoftEntra.php b/src/providers/MicrosoftEntra.php index d05b698..ce96999 100644 --- a/src/providers/MicrosoftEntra.php +++ b/src/providers/MicrosoftEntra.php @@ -16,12 +16,12 @@ class MicrosoftEntra extends MicrosoftEntraProvider // Public Methods // ========================================================================= - public function getBaseApiUrl(?Token $token): ?string + public function getBaseApiUrl(): ?string { return 'https://graph.microsoft.com/v1.0/'; } - public function getApiRequestQueryParams(?Token $token): array + public function getApiRequestQueryParams(Token $token): array { return [ 'access_token' => (string)$token->getToken(), From 88868e1df923b9ac0fc771fe1bac3d05005d6102 Mon Sep 17 00:00:00 2001 From: Josh Crawford Date: Fri, 13 Sep 2024 00:57:34 +1000 Subject: [PATCH 4/4] version 1.0.34 --- CHANGELOG.md | 5 +++++ composer.json | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 895096b..178cec9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## 1.0.34 - 2024-09-13 + +### Fixed +- Fix an error with Microsoft Entra provider. + ## 1.0.33 - 2024-09-13 ### Added diff --git a/composer.json b/composer.json index a5054c4..8cf296c 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "verbb/auth", "description": "A Craft CMS module to make working with authentication for third-parties a breeze.", - "version": "1.0.33", + "version": "1.0.34", "support": { "email": "support@verbb.io", "issues": "https://github.com/verbb/auth/issues?state=open",