Added SharedSecretBasicAuthenticationMiddleware

This commit is contained in:
2024-09-17 22:55:24 +02:00
parent 363cd7fc05
commit 90cd7765b2

View File

@@ -0,0 +1,41 @@
<?php
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
namespace CloudObjects\SDK\Laravel;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
use CloudObjects\SDK\Helpers\SharedSecretAuthentication;
use Illuminate\Support\Facades\App;
class SharedSecretBasicAuthenticationMiddleware {
private $authenticationHelper;
public function __construct(SharedSecretAuthentication $authenticationHelper)
{
$this->authenticationHelper = $authenticationHelper;
}
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
if ($this->authenticationHelper->verify(
$request->getUser(), $request->getPassword()
) == SharedSecretAuthentication::RESULT_OK) {
// Credentials have been validated
return $next($request);
}
// Fail with 401 if not
App::abort(401);
}
}