Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#110 Change auth to session. #91

Merged
merged 32 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
49f27b2
Initial refactor to session auth.
martineiber Jun 5, 2024
cc31e97
Merge branch '1.x' into 110-auth-to-session
martineiber Jun 5, 2024
ffab95a
Apply php-cs-fixer changes
martineiber Jun 5, 2024
2e22276
Refactor path checker for the scan path on open api.
martineiber Jun 5, 2024
2afc7a4
Merge remote-tracking branch 'origin/110-auth-to-session' into 110-au…
martineiber Jun 5, 2024
13dd544
Apply php-cs-fixer changes
martineiber Jun 5, 2024
b6f30b9
Remove Security Schema.
martineiber Jun 5, 2024
d5676d8
use RuntimeException.
martineiber Jun 5, 2024
defebbf
Fix to long line.
martineiber Jun 5, 2024
2f20b1f
Remove SECURITY_SCHEME
martineiber Jun 5, 2024
412d054
Adapt SecurityServiceTest.
martineiber Jun 6, 2024
cdce286
Apply php-cs-fixer changes
martineiber Jun 6, 2024
c4f0752
Add installation Guide for firewall to docs.
martineiber Jun 6, 2024
0051993
Fix Exception Handling.
martineiber Jun 6, 2024
372bfac
Apply php-cs-fixer changes
martineiber Jun 6, 2024
befb378
Use named Routing.
martineiber Jun 6, 2024
172e4b5
Rename to EventSubscriber
martineiber Jun 6, 2024
a0f7a94
Switch to empty response on logout.
martineiber Jun 6, 2024
54d323e
Add DefaultResponses to login.
martineiber Jun 6, 2024
c4494f9
Add throws block.
martineiber Jun 6, 2024
95420e3
Update Login Schema.
martineiber Jun 6, 2024
93f36e6
Refactor SecurityService.
martineiber Jun 6, 2024
ac536be
Add required to schema. Small clean up.
martineiber Jun 6, 2024
d269457
Apply php-cs-fixer changes
martineiber Jun 6, 2024
0dab2ff
Update Readme.
martineiber Jun 6, 2024
2af49c5
Remove unused Property
martineiber Jun 6, 2024
29147e6
Add current user request.
martineiber Jun 10, 2024
f91540e
Merge branch '1.x' into 110-auth-to-session
martineiber Jun 10, 2024
3756a7f
Move current user request to user Section.
martineiber Jun 10, 2024
5caa6c5
Apply php-cs-fixer changes
martineiber Jun 10, 2024
1e29097
Merge branch '1.x' into 110-auth-to-session
martineiber Jun 11, 2024
4e0aa80
Remove added SECURITY_SCHEME
martineiber Jun 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions config/authorization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ services:
tags: [ 'controller.service_arguments' ]


Pimcore\Bundle\StudioBackendBundle\Authorization\Service\TokenServiceInterface:
class: Pimcore\Bundle\StudioBackendBundle\Authorization\Service\TokenService
Pimcore\Bundle\StudioBackendBundle\Authorization\EventListener\LogoutListener:
martineiber marked this conversation as resolved.
Show resolved Hide resolved
tags:
- { name: 'kernel.event_subscriber', dispatcher: 'security.event_dispatcher.pimcore_studio' }
4 changes: 4 additions & 0 deletions config/pimcore/config.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
imports:
- { resource: security.yaml }
- { resource: firewall.yaml }

pimcore:
translations:
domains:
Expand Down
14 changes: 14 additions & 0 deletions config/pimcore/firewall.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pimcore_studio_backend:
security_firewall:
pattern: ^/studio/api(/.*)?$
user_checker: Pimcore\Security\User\UserChecker
context: pimcore_admin
provider: pimcore_studio_backend
stateless: false
login_throttling:
max_attempts: 3
interval: '5 minutes'
logout:
path: /studio/api/logout
json_login:
check_path: /studio/api/login
4 changes: 4 additions & 0 deletions config/pimcore/security.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
security:
providers:
pimcore_studio_backend:
id: Pimcore\Security\User\UserProvider
16 changes: 15 additions & 1 deletion doc/01_Installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,18 @@ bin/console pimcore:bundle:install PimcoreStudioBackendBundle
## Setting up generic data index
Pimcore Studio Backend also requires the installation and setup of the generic data index.
The bundle is required by default and also automatically enabled in the bundles.
To install the generic data index refer to [Generic-Data-Index](https://github.com/pimcore/generic-data-index-bundle?tab=readme-ov-file)
To install the generic data index refer to [Generic-Data-Index](https://github.com/pimcore/generic-data-index-bundle?tab=readme-ov-file)

## Enable Firewall settings

To enable the firewall settings, add the following configuration to your `config/packages/security.yaml` file:

```yaml
security:
firewalls:
pimcore_studio: '%pimcore_studio_backend.firewall_settings%'
access_control:
- { path: ^/studio/api/docs$, roles: PUBLIC_ACCESS }
- { path: ^/studio/api/docs.json$, roles: PUBLIC_ACCESS }
- { path: ^/studio, roles: ROLE_PIMCORE_USER }
```
1 change: 0 additions & 1 deletion src/Asset/Controller/CollectionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public function __construct(
operationId: 'getAssets',
description: 'Get paginated assets',
summary: 'Get all assets',
security: self::SECURITY_SCHEME,
tags: [Tags::Assets->name]
)]
#[PageParameter]
Expand Down
1 change: 0 additions & 1 deletion src/Asset/Controller/CustomSettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public function __construct(
operationId: 'getAssetCustomSettingsById',
description: 'Get custom settings of an asset by its id by path parameter',
summary: 'Get custom settings of an asset by id',
security: self::SECURITY_SCHEME,
tags: [Tags::Assets->name]
)]
#[IdParameter(type: 'asset')]
Expand Down
1 change: 0 additions & 1 deletion src/Asset/Controller/Data/TextController.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ public function __construct(
path: self::API_PATH . '/assets/{id}/text',
operationId: 'getAssetDataTextById',
summary: 'Get asset data in text UTF8 representation by id',
security: self::SECURITY_SCHEME,
tags: [Tags::Assets->name]
)]
#[IdParameter(type: 'asset')]
Expand Down
1 change: 0 additions & 1 deletion src/Asset/Controller/GetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public function __construct(
operationId: 'getAssetById',
description: 'Get assets by id by path parameter',
summary: 'Get assets by id',
security: self::SECURITY_SCHEME,
tags: [Tags::Assets->name]
)]
#[IdParameter(type: 'asset')]
Expand Down
1 change: 0 additions & 1 deletion src/Asset/Controller/UpdateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public function __construct(
operationId: 'updateAssetById',
description: 'Update assets by id',
summary: 'Update asset',
security: self::SECURITY_SCHEME,
tags: [Tags::Assets->name]
)]
#[IdParameter(type: 'asset')]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,22 @@
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Authorization\Attributes\Request;
namespace Pimcore\Bundle\StudioBackendBundle\Authorization\Attributes\Response;

use Attribute;
use OpenApi\Attributes\JsonContent;
use OpenApi\Attributes\RequestBody;
use Pimcore\Bundle\StudioBackendBundle\Authorization\Schema\Refresh;
use OpenApi\Attributes\Response;

/**
* @internal
*/
#[Attribute(Attribute::TARGET_METHOD)]
final class TokenRequestBody extends RequestBody
final class InvalidCredentials extends Response
{
public function __construct()
{
parent::__construct(
required: true,
content: new JsonContent(ref: Refresh::class)
response: 401,
description: 'Invalid credentials Response',
);
}
}
111 changes: 0 additions & 111 deletions src/Authorization/Controller/AuthorizationController.php

This file was deleted.

57 changes: 57 additions & 0 deletions src/Authorization/Controller/LoginController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Authorization\Controller;

use OpenApi\Attributes\JsonContent;
use OpenApi\Attributes\Post;
use Pimcore\Bundle\StudioBackendBundle\Authorization\Attributes\Request\CredentialsRequestBody;
use Pimcore\Bundle\StudioBackendBundle\Authorization\Attributes\Response\InvalidCredentials;
use Pimcore\Bundle\StudioBackendBundle\Authorization\Schema\LoginSuccess;
use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\SuccessResponse;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Config\Tags;
use Pimcore\Security\User\User;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\CurrentUser;

/**
* @internal
*/
final class LoginController extends AbstractApiController
{
#[Route('/login', name: 'pimcore_studio_api_login', methods: ['POST'])]
#[Post(
path: self::API_PATH . '/login',
operationId: 'login',
summary: 'Login with user credentials and get access token',
tags: [Tags::Authorization->name]
)]
#[CredentialsRequestBody]
#[SuccessResponse(
description: 'Login successful',
content: new JsonContent(ref: LoginSuccess::class)
)]
#[InvalidCredentials]
martineiber marked this conversation as resolved.
Show resolved Hide resolved
public function login(#[CurrentUser] User $user): JsonResponse
{
return $this->jsonResponse([
'username' => $user->getUserIdentifier(),
'roles' => $user->getRoles(),
]);
}
}
45 changes: 45 additions & 0 deletions src/Authorization/Controller/LogoutController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Authorization\Controller;

use OpenApi\Attributes\Post;
use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController;
use Pimcore\Bundle\StudioBackendBundle\Exception\UnreachableException;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\SuccessResponse;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Config\Tags;
use Symfony\Component\Routing\Attribute\Route;

/**
* @internal
*/
final class LogoutController extends AbstractApiController
{
#[Route('/logout', name: 'pimcore_studio_api_logout', methods: ['POST'])]
#[Post(
path: self::API_PATH . '/logout',
operationId: 'logout',
summary: 'Logout and invalidate current session for active user',
tags: [Tags::Authorization->name]
)]
#[SuccessResponse(
description: 'Logout successful',
)]
public function logout(): void
{
throw new UnreachableException('Should not be called. Handled by symfony.');
}
}
39 changes: 39 additions & 0 deletions src/Authorization/EventListener/LogoutListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Authorization\EventListener;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Security\Http\Event\LogoutEvent;

/**
* @internal
*/
final class LogoutListener implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
LogoutEvent::class => 'onLogout',
];
}

public function onLogout(LogoutEvent $event): void
{
$event->setResponse(new JsonResponse([]));
martineiber marked this conversation as resolved.
Show resolved Hide resolved
}
}
Loading
Loading