Skip to content

Commit 15164bb

Browse files
committed
refactor: renamed EntraID session class to make usage more clear
1 parent 20659bb commit 15164bb

File tree

9 files changed

+43
-40
lines changed

9 files changed

+43
-40
lines changed

phpmyfaq/services/azure/callback.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
use phpMyFAQ\Auth\AuthEntraId;
1919
use phpMyFAQ\Auth\EntraId\OAuth;
20-
use phpMyFAQ\Auth\EntraId\Session as EntraIdSession;
20+
use phpMyFAQ\Auth\EntraId\EntraIdSession as EntraIdSession;
2121
use phpMyFAQ\Configuration;
2222
use phpMyFAQ\Enums\AuthenticationSourceType;
2323
use phpMyFAQ\Filter;

phpmyfaq/services/azure/index.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
use phpMyFAQ\Auth\AuthEntraId;
1919
use phpMyFAQ\Auth\EntraId\OAuth;
20-
use phpMyFAQ\Auth\EntraId\Session as EntraIdSession;
20+
use phpMyFAQ\Auth\EntraId\EntraIdSession as EntraIdSession;
2121
use phpMyFAQ\Configuration;
2222

2323
//
@@ -34,8 +34,8 @@
3434

3535
$faqConfig = Configuration::getConfigurationInstance();
3636

37-
$enraIdSession = new EntraIdSession($faqConfig, $session);
38-
$oAuth = new OAuth($faqConfig, $enraIdSession);
37+
$entraIdSession = new EntraIdSession($faqConfig, $session);
38+
$oAuth = new OAuth($faqConfig, $entraIdSession);
3939
$auth = new AuthEntraId($faqConfig, $oAuth);
4040

4141
try {

phpmyfaq/services/azure/logout.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
use phpMyFAQ\Auth\AuthEntraId;
1919
use phpMyFAQ\Auth\EntraId\OAuth;
20-
use phpMyFAQ\Auth\EntraId\Session as EntraIdSession;
20+
use phpMyFAQ\Auth\EntraId\EntraIdSession as EntraIdSession;
2121
use phpMyFAQ\Configuration;
2222

2323
//
@@ -34,8 +34,8 @@
3434

3535
$faqConfig = Configuration::getConfigurationInstance();
3636

37-
$enraIdSession = new EntraIdSession($faqConfig, $session);
38-
$oAuth = new OAuth($faqConfig, $enraIdSession);
37+
$entraIdSession = new EntraIdSession($faqConfig, $session);
38+
$oAuth = new OAuth($faqConfig, $entraIdSession);
3939
$auth = new AuthEntraId($faqConfig, $oAuth);
4040

4141
$auth->logout();

phpmyfaq/src/phpMyFAQ/Auth/AuthEntraId.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
use phpMyFAQ\Auth;
2121
use phpMyFAQ\Auth\EntraId\OAuth;
22-
use phpMyFAQ\Auth\EntraId\Session;
22+
use phpMyFAQ\Auth\EntraId\EntraIdSession;
2323
use phpMyFAQ\Configuration;
2424
use phpMyFAQ\Core\Exception;
2525
use phpMyFAQ\Enums\AuthenticationSourceType;
@@ -132,9 +132,14 @@ public function isValidLogin(string $login, ?array $optionalData = []): int
132132
public function authorize(): void
133133
{
134134
$this->createOAuthChallenge();
135-
$this->oAuth->getSession()->setCurrentSessionKey();
136-
$this->oAuth->getSession()->set(Session::ENTRA_ID_OAUTH_VERIFIER, $this->oAuthVerifier);
137-
$this->oAuth->getSession()->setCookie(Session::ENTRA_ID_OAUTH_VERIFIER, $this->oAuthVerifier, 7200, false);
135+
$this->oAuth->getEntraIdSession()->setCurrentSessionKey();
136+
$this->oAuth->getEntraIdSession()->set(EntraIdSession::ENTRA_ID_OAUTH_VERIFIER, $this->oAuthVerifier);
137+
$this->oAuth->getEntraIdSession()->setCookie(
138+
EntraIdSession::ENTRA_ID_OAUTH_VERIFIER,
139+
$this->oAuthVerifier,
140+
7200,
141+
false
142+
);
138143

139144
$oAuthURL = sprintf(
140145
'https://login.microsoftonline.com/%s/oauth2/v2.0/authorize' .

phpmyfaq/src/phpMyFAQ/Auth/EntraId/Session.php renamed to phpmyfaq/src/phpMyFAQ/Auth/EntraId/EntraIdSession.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
use phpMyFAQ\Session\AbstractSession;
2323
use Symfony\Component\HttpFoundation\Cookie;
2424
use Symfony\Component\HttpFoundation\Request;
25-
use Symfony\Component\HttpFoundation\Session\Session as SymfonySession;
25+
use Symfony\Component\HttpFoundation\Session\Session;
2626
use Symfony\Component\Uid\Uuid;
2727

28-
class Session extends AbstractSession
28+
class EntraIdSession extends AbstractSession
2929
{
3030
/** @var string EntraID session key */
3131
final public const string ENTRA_ID_SESSION_KEY = 'pmf-entra-id-session-key';
@@ -36,7 +36,7 @@ class Session extends AbstractSession
3636

3737
private string $currentSessionKey;
3838

39-
public function __construct(private readonly Configuration $configuration, private readonly SymfonySession $session)
39+
public function __construct(private readonly Configuration $configuration, private readonly Session $session)
4040
{
4141
parent::__construct($session);
4242

@@ -64,7 +64,7 @@ public function getCurrentSessionKey(): ?string
6464
*
6565
* @throws Exception
6666
*/
67-
public function setCurrentSessionKey(): Session
67+
public function setCurrentSessionKey(): EntraIdSession
6868
{
6969
if (!isset($this->currentSessionKey)) {
7070
$this->createCurrentSessionKey();
@@ -90,7 +90,7 @@ public function setCookie(string $name, int|string|null $sessionId, int $timeout
9090
Cookie::create($name)
9191
->withValue($sessionId ?? '')
9292
->withExpires($request->server->get('REQUEST_TIME') + $timeout)
93-
->withPath(dirname($request->server->get('SCRIPT_NAME')))
93+
->withPath(dirname((string) $request->server->get('SCRIPT_NAME')))
9494
->withDomain(parse_url($this->configuration->getDefaultUrl(), PHP_URL_HOST))
9595
->withSameSite($strict ? 'strict' : '')
9696
->withSecure($request->isSecure())

phpmyfaq/src/phpMyFAQ/Auth/EntraId/OAuth.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@ class OAuth
4242
/**
4343
* Constructor.
4444
*/
45-
public function __construct(private readonly Configuration $configuration, private readonly Session $session)
46-
{
45+
public function __construct(
46+
private readonly Configuration $configuration,
47+
private readonly EntraIdSession $entraIdSession
48+
) {
4749
$this->httpClient = HttpClient::create();
4850
}
4951

@@ -65,10 +67,10 @@ public function getOAuthToken(string $code): stdClass
6567
{
6668
$url = 'https://login.microsoftonline.com/' . AAD_OAUTH_TENANTID . '/oauth2/v2.0/token';
6769

68-
if ($this->session->get(Session::ENTRA_ID_OAUTH_VERIFIER) !== '') {
69-
$codeVerifier = $this->session->get(Session::ENTRA_ID_OAUTH_VERIFIER);
70+
if ($this->entraIdSession->get(EntraIdSession::ENTRA_ID_OAUTH_VERIFIER) !== '') {
71+
$codeVerifier = $this->entraIdSession->get(EntraIdSession::ENTRA_ID_OAUTH_VERIFIER);
7072
} else {
71-
$codeVerifier = $this->session->getCookie(Session::ENTRA_ID_OAUTH_VERIFIER);
73+
$codeVerifier = $this->entraIdSession->getCookie(EntraIdSession::ENTRA_ID_OAUTH_VERIFIER);
7274
}
7375

7476
$response = $this->httpClient->request('POST', $url, [
@@ -117,13 +119,13 @@ public function setToken(stdClass $token): OAuth
117119
{
118120
$idToken = base64_decode(explode('.', (string) $token->id_token)[1]);
119121
$this->token = json_decode($idToken, null, 512, JSON_THROW_ON_ERROR);
120-
$this->session->set(Session::ENTRA_ID_JWT, json_encode($this->token, JSON_THROW_ON_ERROR));
122+
$this->entraIdSession->set(EntraIdSession::ENTRA_ID_JWT, json_encode($this->token, JSON_THROW_ON_ERROR));
121123
return $this;
122124
}
123125

124-
public function getSession(): Session
126+
public function getEntraIdSession(): EntraIdSession
125127
{
126-
return $this->session;
128+
return $this->entraIdSession;
127129
}
128130

129131
public function getRefreshToken(): ?string

phpmyfaq/src/phpMyFAQ/Language.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -195,19 +195,15 @@ private function getDetectionLanguage(bool $configDetection): ?string
195195
*/
196196
private function selectLanguage(array $detectedLanguage): string
197197
{
198-
if (isset($detectedLanguage['post'])) {
199-
return $detectedLanguage['post'];
200-
}
201-
202-
if (isset($detectedLanguage['get'])) {
203-
return $detectedLanguage['get'];
204-
}
198+
$priorityOrder = ['post', 'get', 'artget', 'session', 'detection', 'config'];
205199

206-
if (isset($detectedLanguage['artget'])) {
207-
return $detectedLanguage['artget'];
200+
foreach ($priorityOrder as $source) {
201+
if (!empty($detectedLanguage[$source])) {
202+
return $detectedLanguage[$source];
203+
}
208204
}
209205

210-
return $detectedLanguage['session'] ?? $detectedLanguage['detection'] ?? $detectedLanguage['config'] ?? 'en';
206+
return 'en';
211207
}
212208

213209
/**

tests/phpMyFAQ/Auth/EntraId/OAuthTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
class OAuthTest extends TestCase
1919
{
2020
private HttpClientInterface $mockClient;
21-
private Session $mockSession;
21+
private EntraIdSession $mockSession;
2222
private OAuth $oAuth;
2323

2424
/**
@@ -27,7 +27,7 @@ class OAuthTest extends TestCase
2727
protected function setUp(): void
2828
{
2929
$this->mockClient = $this->createMock(HttpClientInterface::class);
30-
$this->mockSession = $this->createMock(Session::class);
30+
$this->mockSession = $this->createMock(EntraIdSession::class);
3131
$mockConfiguration = $this->createMock(Configuration::class);
3232

3333
$this->oAuth = new OAuth($mockConfiguration, $this->mockSession);
@@ -48,7 +48,7 @@ public function testGetOAuthTokenSuccess(): void
4848

4949
$this->mockSession->expects($this->exactly(1))
5050
->method('get')
51-
->with(Session::ENTRA_ID_OAUTH_VERIFIER)
51+
->with(EntraIdSession::ENTRA_ID_OAUTH_VERIFIER)
5252
->willReturnOnConsecutiveCalls('', 'code_verifier');
5353

5454
$this->mockClient->expects($this->once())
@@ -113,7 +113,7 @@ public function testSetToken(): void
113113

114114
$this->mockSession->expects($this->once())
115115
->method('set')
116-
->with(Session::ENTRA_ID_JWT, $this->stringContains('John Doe'));
116+
->with(EntraIdSession::ENTRA_ID_JWT, $this->stringContains('John Doe'));
117117

118118
$this->oAuth->setToken($token);
119119

tests/phpMyFAQ/Auth/EntraId/SessionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ class SessionTest extends TestCase
1010
{
1111
private Configuration $configurationMock;
1212
private SymfonySession $sessionMock;
13-
private Session $session;
13+
private EntraIdSession $session;
1414

1515
protected function setUp(): void
1616
{
1717
$this->configurationMock = $this->createMock(Configuration::class);
1818
$this->sessionMock = $this->createMock(SymfonySession::class);
19-
$this->session = new Session($this->configurationMock, $this->sessionMock);
19+
$this->session = new EntraIdSession($this->configurationMock, $this->sessionMock);
2020
}
2121

2222
public function testCreateCurrentSessionKey(): void

0 commit comments

Comments
 (0)