<?php
 namespace Greew\OAuth2\Client\Provider; use GuzzleHttp\Psr7\Uri; use League\OAuth2\Client\Provider\AbstractProvider; use League\OAuth2\Client\Provider\Exception\IdentityProviderException; use League\OAuth2\Client\Token\AccessToken; use Psr\Http\Message\ResponseInterface; class Azure extends AbstractProvider { protected $tenantId = ''; public $defaultScopes = [ 'https://outlook.office.com/SMTP.Send', 'offline_access', ]; protected $urlAuthorize; protected $urlResourceOwnerDetails; public function __construct( array $options = [], array $collaborators = [], string $urlAuthorize = 'https://login.microsoftonline.com', string $urlResourceOwnerDetails = 'https://graph.microsoft.com/v1.0/me' ) { $this->urlAuthorize = $urlAuthorize; $this->urlResourceOwnerDetails = $urlResourceOwnerDetails; parent::__construct($options, $collaborators); } public function getBaseAuthorizationUrl() { return $this->urlAuthorize . '/' . $this->tenantId . '/oauth2/v2.0/authorize'; } public function getBaseAccessTokenUrl(array $params) { return $this->urlAuthorize . '/' . $this->tenantId . '/oauth2/v2.0/token'; } protected function getAuthorizationParameters(array $options) { $options += [ 'response_mode' => 'query', ]; return parent::getAuthorizationParameters($options); } protected function getDefaultScopes() { return $this->defaultScopes; } protected function checkResponse(ResponseInterface $response, $data) { if (isset($data['error'])) { throw new IdentityProviderException( (isset($data['error_description']) ? $data['error_description'] : $response->getReasonPhrase()), $response->getStatusCode(), (string) $response->getBody(), ); } } protected function createResourceOwner(array $response, AccessToken $token) { return new AzureResourceOwner($response); } public function getResourceOwnerDetailsUrl(AccessToken $token) { $uri = new Uri($this->urlResourceOwnerDetails); return (string) Uri::withQueryValue($uri, 'access_token', (string) $token); } protected function getScopeSeparator() { return ' '; } } 