Skip to content

Commit c5988a0

Browse files
When you log in to the main DOMjudge, allow to use the API with the same user
1 parent 63cde5b commit c5988a0

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

webapp/config/packages/security.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ security:
3333
# API does Basic Auth and IP address auth
3434
api:
3535
pattern: ^/api
36+
context: domjudge
3637
provider: domjudge_db_provider
37-
stateless: true
3838
user_checker: App\Security\UserChecker
3939
entry_point: App\Security\DOMJudgeIPAuthenticator
4040
# SEE NOTE ABOVE IF CHANGING ANYTHING HERE
@@ -45,6 +45,7 @@ security:
4545
# Provides prometheus metrics
4646
metrics:
4747
pattern: ^/prometheus/metrics
48+
context: domjudge
4849
provider: domjudge_db_provider
4950
stateless: true
5051
user_checker: App\Security\UserChecker
@@ -57,6 +58,7 @@ security:
5758
# rest of app does form_login
5859
main:
5960
pattern: ^/
61+
context: domjudge
6062
provider: domjudge_db_provider
6163
user_checker: App\Security\UserChecker
6264
entry_point: App\Security\DOMJudgeXHeadersAuthenticator
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace App\EventListener;
4+
5+
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
6+
use Symfony\Component\HttpKernel\Event\ResponseEvent;
7+
8+
// The AbstractSessionListener (which sets the cookie) has a priority of -1000, so we need to
9+
// set a priority of -1001 to run before it.
10+
#[AsEventListener(priority: -1001)]
11+
class NoSessionCookieForApiListener
12+
{
13+
public function __invoke(ResponseEvent $event): void
14+
{
15+
// We do not want to set the session cookie for API requests. Since the firewall is
16+
// stateful (because we want form logins to allow to access the API), we need to remove
17+
// the cookie
18+
$request = $event->getRequest();
19+
$response = $event->getResponse();
20+
if ($request->attributes->get('_firewall_context') === 'security.firewall.map.context.api') {
21+
$response->headers->removeCookie($request->getSession()->getName());
22+
}
23+
}
24+
}

webapp/tests/Unit/BaseTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ protected function loginHelper(
143143
*/
144144
protected function logIn(): void
145145
{
146-
$this->client->loginUser($this->setupUser());
146+
$this->client->loginUser($this->setupUser(), 'domjudge');
147147
}
148148

149149
/**

0 commit comments

Comments
 (0)