Skip to content

Commit

Permalink
Added Azure Provider (#237)
Browse files Browse the repository at this point in the history
Co-authored-by: Theo <[email protected]>
  • Loading branch information
ExPl0siF and Theo authored Mar 4, 2022
1 parent bdbc87a commit f1ad926
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
72 changes: 72 additions & 0 deletions src/Providers/Azure.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Overtrue\Socialite\Providers;

use JetBrains\PhpStorm\ArrayShape;
use JetBrains\PhpStorm\Pure;
use Overtrue\Socialite\Contracts\UserInterface;
use Overtrue\Socialite\User;

class Azure extends Base
{
public const NAME = 'azure';
protected array $scopes = ['User.Read'];
protected string $scopeSeparator = ' ';

protected function getAuthUrl(): string
{
return $this->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',
];
}
}
1 change: 1 addition & 0 deletions src/SocialiteManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit f1ad926

Please sign in to comment.