feat: adjust oauth2 implementation for TYPO3 backend login

This commit is contained in:
Björn Fromme
2026-03-16 12:00:56 +01:00
parent ee84690cd9
commit d1e807b11f
7 changed files with 24 additions and 9 deletions
+1 -1
View File
@@ -11,7 +11,7 @@ league_oauth2_server:
resource_server: resource_server:
public_key: '%env(resolve:OAUTH_PUBLIC_KEY)%' public_key: '%env(resolve:OAUTH_PUBLIC_KEY)%'
scopes: scopes:
available: ['email','profile','api'] available: ['email','id','profile','roles','api']
default: ['email'] default: ['email']
persistence: persistence:
doctrine: null doctrine: null
+4
View File
@@ -49,6 +49,8 @@ class PersonalData
#[Assert\Valid(groups: ['personal_data'])] #[Assert\Valid(groups: ['personal_data'])]
public Communication $communication; public Communication $communication;
public array $roles = [];
public function __construct() public function __construct()
{ {
$this->address = new Address(); $this->address = new Address();
@@ -124,7 +126,9 @@ class PersonalData
public function getClaims(): array public function getClaims(): array
{ {
return [ return [
'id' => $this->personId,
'email' => $this->communication->email, 'email' => $this->communication->email,
'roles' => $this->roles,
'profile' => [ 'profile' => [
'first_name' => $this->firstName, 'first_name' => $this->firstName,
'last_name' => $this->name, 'last_name' => $this->name,
@@ -29,9 +29,15 @@ class UserinfoController extends AbstractController
$scopes = ['email']; $scopes = ['email'];
// extend scopes depending on granted permissions // extend scopes depending on granted permissions
if ($this->isGranted('ROLE_OAUTH2_ID')) {
$scopes[] = 'id';
}
if ($this->isGranted('ROLE_OAUTH2_PROFILE')) { if ($this->isGranted('ROLE_OAUTH2_PROFILE')) {
$scopes[] = 'profile'; $scopes[] = 'profile';
} }
if ($this->isGranted('ROLE_OAUTH2_ROLES')) {
$scopes[] = 'roles';
}
/** @var User $user */ /** @var User $user */
$user = $this->getUser(); $user = $this->getUser();
@@ -45,6 +51,9 @@ class UserinfoController extends AbstractController
return new JsonResponse(['message' => $data->message, 'code' => $data->code], Response::HTTP_BAD_REQUEST); return new JsonResponse(['message' => $data->message, 'code' => $data->code], Response::HTTP_BAD_REQUEST);
} }
// Patch current user's roles
$data->roles = $user->getRoles();
// extract userdata for resulting claims // extract userdata for resulting claims
$userData = $this->getClaims($data, $scopes); $userData = $this->getClaims($data, $scopes);
+3 -3
View File
@@ -43,9 +43,8 @@ class SecurityController extends AbstractController
// authentication (see App\EventListener\AuthorizationCodeListener). // authentication (see App\EventListener\AuthorizationCodeListener).
$session = $request->getSession(); $session = $request->getSession();
$targetPath = $session->get('_security.main.target_path'); $targetPath = $session->get('_security.main.target_path');
if (null !== $targetPath && str_contains($targetPath, '/authorize')) { $isOauth2 = null !== $targetPath && str_contains($targetPath, '/authorize');
$session->set('_oauth2', true); $session->set('_oauth2', $isOauth2);
}
// For booking flow, set target path to Step 1 (after successful auth, redirect there) // For booking flow, set target path to Step 1 (after successful auth, redirect there)
if (true === $isBookingFlow) { if (true === $isBookingFlow) {
@@ -71,6 +70,7 @@ class SecurityController extends AbstractController
'travel_date_from' => $bookingDto?->travel->dateFrom, 'travel_date_from' => $bookingDto?->travel->dateFrom,
'travel_date_to' => $bookingDto?->travel->dateTo, 'travel_date_to' => $bookingDto?->travel->dateTo,
'cmsData' => $cmsData, 'cmsData' => $cmsData,
'oauth2' => $isOauth2,
]); ]);
} }
@@ -37,8 +37,6 @@ class AccessDeniedListener implements EventSubscriberInterface
// Treat OAuth2 routes differently // Treat OAuth2 routes differently
if (in_array($route, ['oauth2_authorize', 'oauth2_token'])) { if (in_array($route, ['oauth2_authorize', 'oauth2_token'])) {
$request->getSession()->getFlashBag()->add('info', 'Bitte melde dich an.');
$this->authLogger->info('OAuth2 authorization request', [ $this->authLogger->info('OAuth2 authorization request', [
'uri' => $request->getRequestUri(), 'uri' => $request->getRequestUri(),
]); ]);
+6 -2
View File
@@ -117,9 +117,12 @@ class BpnAuthenticator extends AbstractLoginFormAuthenticator implements Authent
private function collectRoles(CrmAttributes $crmAttributes): array private function collectRoles(CrmAttributes $crmAttributes): array
{ {
// Collect user's roles from CRM attributes // All users inherit the default role 'customer'
$roles = []; $roles = [
'ROLE_CUSTOMER',
];
// Get user's base role from CRM attributes
if ($crmAttributes->admin) { if ($crmAttributes->admin) {
$roles[] = 'ROLE_ADMIN'; $roles[] = 'ROLE_ADMIN';
} elseif ($crmAttributes->manager) { } elseif ($crmAttributes->manager) {
@@ -128,6 +131,7 @@ class BpnAuthenticator extends AbstractLoginFormAuthenticator implements Authent
$roles[] = 'ROLE_HOUSE_MANAGER'; $roles[] = 'ROLE_HOUSE_MANAGER';
} }
// All users can have role 'teamer' additionally
if ($crmAttributes->teamer) { if ($crmAttributes->teamer) {
$roles[] = 'ROLE_TEAMER'; $roles[] = 'ROLE_TEAMER';
} }
+1 -1
View File
@@ -11,7 +11,7 @@
{{ encore_entry_script_tags('app') }} {{ encore_entry_script_tags('app') }}
{% endblock %} {% endblock %}
</head> </head>
<body class="bg-white text-gray-700 font-sans antialiased {{ booking_theme() }}" hx-boost="true"> <body class="bg-white text-gray-700 font-sans antialiased {{ booking_theme() }}"{% if not oauth2 %} hx-boost="true"{% endif %}>
{% block body %}{% endblock %} {% block body %}{% endblock %}
</body> </body>
</html> </html>