Skip to content
This repository was archived by the owner on Jul 24, 2023. It is now read-only.

Commit 3ef23ef

Browse files
committed
Merge pull request #1 from darthtaco/darthtaco-patch-1
Prevent re-authentication with every request.
2 parents 57a9232 + 1d8ec94 commit 3ef23ef

File tree

1 file changed

+38
-34
lines changed

1 file changed

+38
-34
lines changed

src/Middleware/WindowsAuthenticate.php

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -42,43 +42,47 @@ public function __construct(Guard $auth)
4242
*/
4343
public function handle(Request $request, Closure $next)
4444
{
45-
// Retrieve the SSO login attribute.
46-
$auth = $this->getWindowsAuthAttribute();
47-
48-
// Retrieve the SSO input key.
49-
$key = key($auth);
50-
51-
// Handle Windows Authentication.
52-
if ($account = $request->server($auth[$key])) {
53-
// Usernames may be prefixed with their domain,
54-
// we just need their account name.
55-
$username = explode('\\', $account);
56-
57-
if (count($username) === 2) {
58-
list($domain, $username) = $username;
59-
} else {
60-
$username = $username[key($username)];
61-
}
62-
63-
// Create a new user LDAP user query.
64-
$query = $this->newAdldapUserQuery();
65-
66-
// Filter the query by the username attribute
67-
$query->whereEquals($key, $username);
68-
69-
// Retrieve the first user result
70-
$user = $query->first();
71-
72-
if ($user instanceof User) {
73-
$model = $this->getModelFromAdldap($user, str_random());
74-
75-
if ($model instanceof Model && $this->auth->guest()) {
76-
// Double check user instance before logging them in.
77-
$this->auth->login($model);
45+
// If the user is already logged in, no need to reauthenticate
46+
if ( ! $this->auth->check() ) {
47+
48+
// Retrieve the SSO login attribute.
49+
$auth = $this->getWindowsAuthAttribute();
50+
51+
// Retrieve the SSO input key.
52+
$key = key($auth);
53+
54+
// Handle Windows Authentication.
55+
if ($account = $request->server($auth[$key])) {
56+
// Usernames may be prefixed with their domain,
57+
// we just need their account name.
58+
$username = explode('\\', $account);
59+
60+
if (count($username) === 2) {
61+
list($domain, $username) = $username;
62+
} else {
63+
$username = $username[key($username)];
64+
}
65+
66+
// Create a new user LDAP user query.
67+
$query = $this->newAdldapUserQuery();
68+
69+
// Filter the query by the username attribute
70+
$query->whereEquals($key, $username);
71+
72+
// Retrieve the first user result
73+
$user = $query->first();
74+
75+
if ($user instanceof User) {
76+
$model = $this->getModelFromAdldap($user, str_random());
77+
78+
if ($model instanceof Model && $this->auth->guest()) {
79+
// Double check user instance before logging them in.
80+
$this->auth->login($model);
81+
}
7882
}
7983
}
8084
}
81-
85+
8286
return $this->returnNextRequest($request, $next);
8387
}
8488

0 commit comments

Comments
 (0)