Skip to content

Commit

Permalink
EZP-32215: Made ezpublish_rest.session_authenticator dependency (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
webhdx authored Dec 9, 2020
1 parent 47f541f commit 0e0731e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/bundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,10 @@ services:
class: EzSystems\EzPlatformRest\Server\Controller\SessionController
parent: ezpublish_rest.controller.base
arguments:
- "@ezpublish_rest.session_authenticator"
- "%ezpublish_rest.csrf_token_intention%"
- '@eZ\Publish\API\Repository\PermissionResolver'
- '@ezpublish.api.service.user'
- "@?ezpublish_rest.session_authenticator"
- "@?ezpublish_rest.security.csrf.token_manager"
tags: [controller.service_arguments]

Expand Down
30 changes: 22 additions & 8 deletions src/lib/Server/Controller/SessionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

class SessionController extends Controller
{
/** @var \eZ\Publish\Core\MVC\Symfony\Security\Authentication\AuthenticatorInterface */
/** @var \eZ\Publish\Core\MVC\Symfony\Security\Authentication\AuthenticatorInterface|null */
private $authenticator;

/** @var \EzSystems\EzPlatformRest\Server\Security\CsrfTokenManager */
Expand All @@ -43,10 +43,10 @@ class SessionController extends Controller
private $csrfTokenStorage;

public function __construct(
AuthenticatorInterface $authenticator,
$tokenIntention,
PermissionResolver $permissionResolver,
UserService $userService,
?AuthenticatorInterface $authenticator = null,
CsrfTokenManager $csrfTokenManager = null,
TokenStorageInterface $csrfTokenStorage = null
) {
Expand Down Expand Up @@ -83,7 +83,7 @@ public function createSessionAction(Request $request)
$this->checkCsrfToken($request);
}

$token = $this->authenticator->authenticate($request);
$token = $this->getAuthenticator()->authenticate($request);
$csrfToken = $this->getCsrfToken();

return new Values\UserSession(
Expand All @@ -97,10 +97,10 @@ public function createSessionAction(Request $request)
// Already logged in with another user, this will be converted to HTTP status 409
return new Values\Conflict();
} catch (AuthenticationException $e) {
$this->authenticator->logout($request);
$this->getAuthenticator()->logout($request);
throw new UnauthorizedException('Invalid login or password', $request->getPathInfo());
} catch (AccessDeniedException $e) {
$this->authenticator->logout($request);
$this->getAuthenticator()->logout($request);
throw new UnauthorizedException($e->getMessage(), $request->getPathInfo());
}
}
Expand All @@ -119,7 +119,7 @@ public function refreshSessionAction($sessionId, Request $request)
$session = $request->getSession();

if ($session === null || !$session->isStarted() || $session->getId() != $sessionId || !$this->hasStoredCsrfToken()) {
$response = $this->authenticator->logout($request);
$response = $this->getAuthenticator()->logout($request);
$response->setStatusCode(404);

return $response;
Expand Down Expand Up @@ -153,15 +153,15 @@ public function deleteSessionAction($sessionId, Request $request)
/** @var $session \Symfony\Component\HttpFoundation\Session\Session */
$session = $request->getSession();
if (!$session->isStarted() || $session->getId() != $sessionId || !$this->hasStoredCsrfToken()) {
$response = $this->authenticator->logout($request);
$response = $this->getAuthenticator()->logout($request);
$response->setStatusCode(404);

return $response;
}

$this->checkCsrfToken($request);

return new Values\DeletedUserSession($this->authenticator->logout($request));
return new Values\DeletedUserSession($this->getAuthenticator()->logout($request));
}

/**
Expand Down Expand Up @@ -223,4 +223,18 @@ private function getCsrfToken()

return $this->csrfTokenManager->getToken($this->csrfTokenIntention)->getValue();
}

private function getAuthenticator(): ?AuthenticatorInterface
{
if (null === $this->authenticator) {
throw new \RuntimeException(
sprintf(
"No %s instance injected. Ensure 'ezpublish_rest_session' is configured under your firewall",
AuthenticatorInterface::class
)
);
}

return $this->authenticator;
}
}

0 comments on commit 0e0731e

Please sign in to comment.