-
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support the
HTTP_REMOTE_USER
header (#208)
- Loading branch information
Showing
3 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Cachet\Http\Middleware; | ||
|
||
use Cachet\Cachet; | ||
use Cachet\Models\User; | ||
use Closure; | ||
use Illuminate\Http\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
class AuthenticateRemoteUser | ||
{ | ||
/** | ||
* Handle an incoming request. | ||
* | ||
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next | ||
*/ | ||
public function handle(Request $request, Closure $next): Response | ||
{ | ||
if ($remoteUser = $request->headers->get('REMOTE_USER')) { | ||
$userModel = Cachet::userModel(); | ||
/** @var User|null $user */ | ||
$user = $userModel::query()->where('email', $remoteUser)->first(); | ||
|
||
if ($user !== null) { | ||
auth()->login($user); | ||
} | ||
} | ||
|
||
return $next($request); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
tests/Feature/Http/Middleware/AuthenticateRemoteUserTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
use Cachet\Http\Middleware\AuthenticateRemoteUser; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\Auth; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Workbench\App\User; | ||
|
||
it('authenticates remote user if REMOTE_USER header is present', function () { | ||
$user = User::factory()->create(['email' => '[email protected]']); | ||
|
||
$request = Request::create('/test', 'GET', [], [], [], ['HTTP_REMOTE_USER' => '[email protected]']); | ||
|
||
$next = function ($request) { | ||
return new Response('OK'); | ||
}; | ||
|
||
$middleware = new AuthenticateRemoteUser(); | ||
|
||
$response = $middleware->handle($request, $next); | ||
|
||
expect(Auth::check())->toBeTrue() | ||
->and(Auth::user()->email)->toBe('[email protected]') | ||
->and($response->getContent())->toBe('OK'); | ||
}); | ||
|
||
it('does not authenticate remote user if REMOTE_USER header is not present', function () { | ||
$request = Request::create('/test'); | ||
|
||
$next = function ($request) { | ||
return new Response('OK'); | ||
}; | ||
|
||
$middleware = new AuthenticateRemoteUser(); | ||
|
||
$response = $middleware->handle($request, $next); | ||
|
||
expect(Auth::check())->toBeFalse() | ||
->and($response->getContent())->toBe('OK'); | ||
}); |