Skip to content

Commit 0a84a0c

Browse files
committed
Added ability to set guards on WindowsAuthenticate middleware
Closes #182
1 parent 9613f32 commit 0a84a0c

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/Middleware/WindowsAuthenticate.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace LdapRecord\Laravel\Middleware;
44

55
use Closure;
6+
use Illuminate\Support\Arr;
67
use Illuminate\Contracts\Auth\Factory as Auth;
78
use Illuminate\Database\Eloquent\Model as Eloquent;
89
use LdapRecord\Laravel\Auth\DatabaseUserProvider;
@@ -14,6 +15,13 @@
1415

1516
class WindowsAuthenticate
1617
{
18+
/**
19+
* The guards to use for SSO authentication.
20+
*
21+
* @var null|array
22+
*/
23+
public static $guards;
24+
1725
/**
1826
* The server key to use for fetching user SSO information.
1927
*
@@ -66,6 +74,16 @@ public function __construct(Auth $auth)
6674
$this->auth = $auth;
6775
}
6876

77+
/**
78+
* Set the guards to use for authentication.
79+
*
80+
* @param string|array $guards
81+
*/
82+
public static function guards($guards)
83+
{
84+
static::$guards = Arr::wrap($guards);
85+
}
86+
6987
/**
7088
* Define the server key to use for fetching user SSO information.
7189
*
@@ -131,7 +149,7 @@ public static function rememberAuthenticatedUsers()
131149
*/
132150
public function handle($request, Closure $next, ...$guards)
133151
{
134-
$this->authenticate($request, $guards);
152+
$this->authenticate($request, static::$guards ?? $guards);
135153

136154
return $next($request);
137155
}

tests/WindowsAuthMiddlewareTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ protected function setUp(): void
2323
parent::setUp();
2424

2525
// Reset all middleware options.
26+
WindowsAuthenticate::$guards = null;
2627
WindowsAuthenticate::$serverKey = 'AUTH_USER';
2728
WindowsAuthenticate::$username = 'samaccountname';
2829
WindowsAuthenticate::$domainVerification = true;
@@ -364,6 +365,20 @@ public function test_users_are_logged_out_if_enabled_and_user_is_not_authenticat
364365
$this->assertFalse(auth()->check());
365366
});
366367
}
368+
369+
public function test_guards_can_be_set_manually()
370+
{
371+
WindowsAuthenticate::guards('invalid');
372+
373+
$middleware = new WindowsAuthenticate(app('auth'));
374+
$request = tap(new Request, function ($request) {
375+
$request->server->set('AUTH_USER', 'Local\SteveBauman');
376+
});
377+
378+
$this->expectException(\InvalidArgumentException::class);
379+
380+
$middleware->handle($request, function () {});
381+
}
367382
}
368383

369384
class WindowsAuthRuleStub extends Rule

0 commit comments

Comments
 (0)