Skip to content

Commit

Permalink
refactor: login via MS Entra ID now uses Symfony HTTP client instead …
Browse files Browse the repository at this point in the history
…Guzzle's HTTP client
  • Loading branch information
thorsten committed Jun 1, 2024
1 parent bb22970 commit be9f2dc
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
4 changes: 2 additions & 2 deletions phpmyfaq/services/azure/callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
* @since 2022-09-09
*/

use GuzzleHttp\Exception\GuzzleException;
use phpMyFAQ\Auth\AuthAzureActiveDirectory;
use phpMyFAQ\Configuration;
use phpMyFAQ\Enums\AuthenticationSourceType;
Expand All @@ -24,6 +23,7 @@
use phpMyFAQ\Auth\Azure\OAuth;
use phpMyFAQ\User\CurrentUser;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;

if (session_status() !== PHP_SESSION_ACTIVE) {
session_start();
Expand Down Expand Up @@ -88,7 +88,7 @@

// @todo -> redirect to where the user came from
$redirect->send();
} catch (GuzzleException $exception) {
} catch (TransportExceptionInterface $exception) {
echo sprintf(
'Entra ID Login failed: %s at line %d at %s',
$exception->getMessage(),
Expand Down
33 changes: 17 additions & 16 deletions phpmyfaq/src/phpMyFAQ/Auth/Azure/OAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@

namespace phpMyFAQ\Auth\Azure;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use phpMyFAQ\Configuration;
use phpMyFAQ\Session;
use stdClass;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
* Class OAuth
Expand All @@ -30,6 +31,8 @@
*/
class OAuth
{
private HttpClientInterface $client;

/** @var stdClass|null JWT */
private ?stdClass $token = null;

Expand All @@ -42,6 +45,7 @@ class OAuth
*/
public function __construct(private readonly Configuration $configuration, private readonly Session $session)
{
$this->client = HttpClient::create();
}

/**
Expand All @@ -55,23 +59,21 @@ public function errorMessage(string $message): string
/**
* Returns the Authorization Code from Entra ID.
*
* @throws GuzzleException
* @throws \JsonException
* @throws TransportExceptionInterface
*/
public function getOAuthToken(string $code): stdClass
{
$client = new Client([
'base_uri' => 'https://login.microsoftonline.com/' . AAD_OAUTH_TENANTID . '/oauth2/v2.0/',
]);
$url = 'https://login.microsoftonline.com/' . AAD_OAUTH_TENANTID . '/oauth2/v2.0/token';

if ($this->session->get(Session::PMF_AZURE_AD_OAUTH_VERIFIER) !== '') {
$codeVerifier = $this->session->get(Session::PMF_AZURE_AD_OAUTH_VERIFIER);
} else {
$codeVerifier = $this->session->getCookie(Session::PMF_AZURE_AD_OAUTH_VERIFIER);
}

$response = $client->request('POST', 'token', [
'form_params' => [
$response = $this->client->request('POST', $url, [
'body' => [
'grant_type' => 'authorization_code',
'client_id' => AAD_OAUTH_CLIENTID,
'redirect_uri' => $this->configuration->getDefaultUrl() . 'services/azure/callback.php',
Expand All @@ -81,28 +83,27 @@ public function getOAuthToken(string $code): stdClass
]
]);

return json_decode($response->getBody(), null, 512, JSON_THROW_ON_ERROR);
return json_decode($response->getContent(), null, 512, JSON_THROW_ON_ERROR);
}

/**
* @throws GuzzleException|\JsonException
* @throws \JsonException
* @throws TransportExceptionInterface
*/
public function refreshToken()
{
$client = new Client([
'base_uri' => 'https://login.microsoftonline.com/' . AAD_OAUTH_TENANTID . '/oauth2/v2.0/',
]);
$url = 'https://login.microsoftonline.com/' . AAD_OAUTH_TENANTID . '/oauth2/v2.0/token';

$response = $client->request('POST', 'token', [
'form_params' => [
$response = $this->client->request('POST', $url, [
'body' => [
'grant_type' => 'refresh_token',
'refresh_token' => $this->getRefreshToken(),
'client_id' => AAD_OAUTH_CLIENTID,
'scope' => AAD_OAUTH_SCOPE
]
]);

return json_decode($response->getBody(), null, 512, JSON_THROW_ON_ERROR);
return json_decode($response->getContent(), null, 512, JSON_THROW_ON_ERROR);
}

public function getToken(): stdClass
Expand Down
1 change: 0 additions & 1 deletion phpmyfaq/src/phpMyFAQ/Setup/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,6 @@ public function startInstall(array|null $setup = null): void

$classLoader = new ClassLoader();
$classLoader->addPsr4('Elasticsearch\\', PMF_SRC_DIR . '/libs/elasticsearch/src/Elasticsearch');
$classLoader->addPsr4('GuzzleHttp\\Ring\\', PMF_SRC_DIR . '/libs/guzzlehttp/ringphp/src');
$classLoader->addPsr4('Monolog\\', PMF_SRC_DIR . '/libs/monolog/src/Monolog');
$classLoader->addPsr4('Psr\\', PMF_SRC_DIR . '/libs/psr/log/Psr');
$classLoader->addPsr4('React\\Promise\\', PMF_SRC_DIR . '/libs/react/promise/src');
Expand Down

0 comments on commit be9f2dc

Please sign in to comment.