From 039954bc7ffff34465a52666111d8145e4631c04 Mon Sep 17 00:00:00 2001 From: Josh Crawford Date: Thu, 12 Sep 2024 23:59:08 +1000 Subject: [PATCH] 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 5bd6d7b..7c26bd0 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', @@ -339,6 +340,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