From f1ad926caced4227e97e2bb3459d4b5f0683dcca Mon Sep 17 00:00:00 2001 From: ExPl0siF <13750598+ExPl0siF@users.noreply.github.com> Date: Fri, 4 Mar 2022 16:11:07 +0100 Subject: [PATCH] Added Azure Provider (#237) Co-authored-by: Theo --- src/Providers/Azure.php | 72 ++++++++++++++++++++++++++++++++++++++++ src/SocialiteManager.php | 1 + 2 files changed, 73 insertions(+) create mode 100644 src/Providers/Azure.php diff --git a/src/Providers/Azure.php b/src/Providers/Azure.php new file mode 100644 index 0000000..29df0f3 --- /dev/null +++ b/src/Providers/Azure.php @@ -0,0 +1,72 @@ +buildAuthUrlFromBase($this->getBaseUrl() . '/oauth2/v2.0/authorize'); + } + + protected function getBaseUrl(): string + { + return 'https://login.microsoftonline.com/'.$this->config["tenant"]; + } + + protected function getTokenUrl(): string + { + return $this->getBaseUrl() . '/oauth2/v2.0/token'; + } + + /** + * @throws \GuzzleHttp\Exception\GuzzleException + */ + protected function getUserByToken(string $token, ?array $query = []): array + { + $response = $this->getHttpClient()->get( + 'https://graph.microsoft.com/v1.0/me', + ['headers' => [ + 'Accept' => 'application/json', + 'Authorization' => 'Bearer '.$token, + ], + ] + ); + + return \json_decode($response->getBody()->getContents(), true) ?? []; + } + + #[Pure] + protected function mapUserToObject(array $user): UserInterface + { + return new User([ + 'id' => $user['id'] ?? null, + 'nickname' => null, + 'name' => $user['displayName'] ?? null, + 'email' => $user['userPrincipalName'] ?? null, + 'avatar' => null, + ]); + } + + #[ArrayShape([ + 'client_id' => "\null|string", + 'client_secret' => "\null|string", + 'code' => "string", + 'redirect_uri' => "mixed" + ])] + protected function getTokenFields(string $code): array + { + return parent::getTokenFields($code) + [ + 'grant_type' => 'authorization_code', + ]; + } +} diff --git a/src/SocialiteManager.php b/src/SocialiteManager.php index 56e0c63..a550e1c 100644 --- a/src/SocialiteManager.php +++ b/src/SocialiteManager.php @@ -28,6 +28,7 @@ class SocialiteManager implements FactoryInterface Providers\Taobao::NAME => Providers\Taobao::class, Providers\FeiShu::NAME => Providers\FeiShu::class, Providers\Outlook::NAME => Providers\Outlook::class, + Providers\Azure::NAME => Providers\Azure::class, Providers\Linkedin::NAME => Providers\Linkedin::class, Providers\Facebook::NAME => Providers\Facebook::class, Providers\DingTalk::NAME => Providers\DingTalk::class,