Skip to content

Commit

Permalink
Allow "GET" API calls and "Restart Station" button.
Browse files Browse the repository at this point in the history
  • Loading branch information
BusterNeece committed Aug 28, 2021
1 parent 5a2f1a4 commit 888e110
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/Middleware/Auth/ApiAuth.php
Expand Up @@ -51,24 +51,29 @@ protected function getApiUser(ServerRequestInterface $request): ?Entity\User
}

// Fallback to session login if available.
$csrfKey = $request->getHeaderLine('X-API-CSRF');
if (empty($csrfKey) && !$this->environment->isTesting()) {
return null;
}

$auth = new Auth(
userRepo: $this->userRepo,
session: $request->getAttribute(ServerRequest::ATTR_SESSION),
environment: $this->environment,
);

if ($auth->isLoggedIn()) {
$user = $auth->getLoggedInUser();
if ('GET' === $request->getMethod()) {
return $user;
}

$csrfKey = $request->getHeaderLine('X-API-CSRF');
if (empty($csrfKey) && !$this->environment->isTesting()) {
return null;
}

$csrf = $request->getAttribute(ServerRequest::ATTR_SESSION_CSRF);

if ($csrf instanceof Csrf) {
try {
$csrf->verify($csrfKey, self::API_CSRF_NAMESPACE);
return $auth->getLoggedInUser();
return $user;
} catch (CsrfValidationException) {
}
}
Expand Down
1 change: 1 addition & 0 deletions src/View.php
Expand Up @@ -127,6 +127,7 @@ public function setRequest(?ServerRequestInterface $request): void
'auth' => $request->getAttribute(ServerRequest::ATTR_AUTH),
'acl' => $request->getAttribute(ServerRequest::ATTR_ACL),
'customization' => $request->getAttribute(ServerRequest::ATTR_CUSTOMIZATION),
'csrf' => $request->getAttribute(ServerRequest::ATTR_SESSION_CSRF),
'flash' => $request->getAttribute(ServerRequest::ATTR_SESSION_FLASH),
'user' => $request->getAttribute(ServerRequest::ATTR_USER),
]
Expand Down
5 changes: 5 additions & 0 deletions templates/stations/sidebar.js.phtml
Expand Up @@ -24,6 +24,11 @@ $(function () {

$.ajax({
type: 'POST',
headers: {
"X-API-CSRF": <?=$this->escapeJs(
$csrf->generate(\App\Middleware\Auth\ApiAuth::API_CSRF_NAMESPACE)
) ?>
},
url: btn.attr('href'),
success: function (data) {
// Only restart if the user isn't on a form page
Expand Down

0 comments on commit 888e110

Please sign in to comment.