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

Commit 1b63ca6

Browse files
committed
Porting fixes and additions to master
1 parent aceddc0 commit 1b63ca6

File tree

4 files changed

+426
-53
lines changed

4 files changed

+426
-53
lines changed

src/AdldapAuthUserProvider.php

Lines changed: 3 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Adldap\Laravel;
44

55
use Adldap\Laravel\Facades\Adldap;
6+
use Adldap\Laravel\Traits\ImportsUsers;
67
use Adldap\Models\User;
78
use Illuminate\Auth\EloquentUserProvider;
89
use Illuminate\Contracts\Auth\Authenticatable;
@@ -12,6 +13,8 @@
1213

1314
class AdldapAuthUserProvider extends EloquentUserProvider
1415
{
16+
use ImportsUsers;
17+
1518
/**
1619
* {@inheritdoc}
1720
*/
@@ -345,16 +348,6 @@ protected function getAdldap($provider = null)
345348
return $ad->getManager()->get($provider);
346349
}
347350

348-
/**
349-
* Returns the username attribute for discovering LDAP users.
350-
*
351-
* @return array
352-
*/
353-
protected function getUsernameAttribute()
354-
{
355-
return Config::get('adldap_auth.username_attribute', ['username' => $this->getSchema()->accountName()]);
356-
}
357-
358351
/**
359352
* Returns the password key to retrieve the
360353
* password from the user input array.
@@ -366,49 +359,6 @@ protected function getPasswordKey()
366359
return Config::get('adldap_auth.password_key', 'password');
367360
}
368361

369-
/**
370-
* Retrieves the Adldap login attribute for authenticating users.
371-
*
372-
* @return string
373-
*/
374-
protected function getLoginAttribute()
375-
{
376-
return Config::get('adldap_auth.login_attribute', $this->getSchema()->accountName());
377-
}
378-
379-
/**
380-
* Retrieves the Adldap bind user to model config option for binding
381-
* the Adldap user model instance to the laravel model.
382-
*
383-
* @return bool
384-
*/
385-
protected function getBindUserToModel()
386-
{
387-
return Config::get('adldap_auth.bind_user_to_model', false);
388-
}
389-
390-
/**
391-
* Retrieves the Adldap sync attributes for filling the
392-
* Laravel user model with active directory fields.
393-
*
394-
* @return array
395-
*/
396-
protected function getSyncAttributes()
397-
{
398-
return Config::get('adldap_auth.sync_attributes', ['name' => $this->getSchema()->commonName()]);
399-
}
400-
401-
/**
402-
* Retrieves the Aldldap select attributes when performing
403-
* queries for authentication and binding for users.
404-
*
405-
* @return array
406-
*/
407-
protected function getSelectAttributes()
408-
{
409-
return Config::get('adldap_auth.select_attributes', []);
410-
}
411-
412362
/**
413363
* Retrieves the Adldap login fallback option for falling back
414364
* to the local database if AD authentication fails.

src/Config/auth.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,20 @@
3434

3535
'username_attribute' => ['username' => 'samaccountname'],
3636

37+
/*
38+
|--------------------------------------------------------------------------
39+
| Limitation Filter
40+
|--------------------------------------------------------------------------
41+
|
42+
| The limitation filter allows you to enter a raw filter to only allow
43+
| specific users / groups / ous to authenticate.
44+
|
45+
| This should be a standard LDAP filter.
46+
|
47+
*/
48+
49+
'limitation_filter' => '',
50+
3751
/*
3852
|--------------------------------------------------------------------------
3953
| Login Fallback
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
3+
namespace Adldap\Laravel\Middleware;
4+
5+
use Adldap\Laravel\Traits\ImportsUsers;
6+
use Adldap\Models\User;
7+
use Adldap\Schemas\ActiveDirectory;
8+
use Closure;
9+
use Illuminate\Contracts\Auth\Guard;
10+
use Illuminate\Database\Eloquent\Model;
11+
use Illuminate\Http\Request;
12+
use Illuminate\Support\Facades\Config;
13+
14+
class WindowsAuthenticate
15+
{
16+
use ImportsUsers;
17+
18+
/**
19+
* The authenticator implementation.
20+
*
21+
* @var \Illuminate\Contracts\Auth\Guard
22+
*/
23+
protected $auth;
24+
25+
/**
26+
* Create a new filter instance.
27+
*
28+
* @param Guard $auth
29+
*/
30+
public function __construct(Guard $auth)
31+
{
32+
$this->auth = $auth;
33+
}
34+
35+
/**
36+
* Handle an incoming request.
37+
*
38+
* @param Request $request
39+
* @param Closure $next
40+
*
41+
* @return mixed
42+
*/
43+
public function handle(Request $request, Closure $next)
44+
{
45+
// If the user is already logged in, we don't need to reauthenticate.
46+
if (!$this->auth->check()) {
47+
// Retrieve the SSO login attribute.
48+
$auth = $this->getWindowsAuthAttribute();
49+
50+
// Retrieve the SSO input key.
51+
$key = key($auth);
52+
53+
// Handle Windows Authentication.
54+
if ($account = $request->server($auth[$key])) {
55+
// Usernames may be prefixed with their domain,
56+
// we just need their account name.
57+
$username = explode('\\', $account);
58+
59+
if (count($username) === 2) {
60+
list($domain, $username) = $username;
61+
} else {
62+
$username = $username[key($username)];
63+
}
64+
65+
// Create a new user LDAP user query.
66+
$query = $this->newAdldapUserQuery();
67+
68+
// Filter the query by the username attribute
69+
$query->whereEquals($key, $username);
70+
71+
// Retrieve the first user result
72+
$user = $query->first();
73+
74+
if ($user instanceof User) {
75+
$model = $this->getModelFromAdldap($user, str_random());
76+
77+
if ($model instanceof Model) {
78+
// Double check user instance before logging them in.
79+
$this->auth->login($model);
80+
}
81+
}
82+
}
83+
}
84+
85+
return $this->returnNextRequest($request, $next);
86+
}
87+
88+
/**
89+
* Returns the next request.
90+
*
91+
* This method exists for override ability.
92+
*
93+
* @param Request $request
94+
* @param Closure $next
95+
*
96+
* @return mixed
97+
*/
98+
public function returnNextRequest(Request $request, Closure $next)
99+
{
100+
return $next($request);
101+
}
102+
103+
/**
104+
* Returns a new auth model instance.
105+
*
106+
* @return \Illuminate\Database\Eloquent\Model
107+
*/
108+
public function createModel()
109+
{
110+
$model = $this->auth->getProvider()->getModel();
111+
112+
return new $model();
113+
}
114+
115+
/**
116+
* Returns the windows authentication attribute.
117+
*
118+
* @return string
119+
*/
120+
protected function getWindowsAuthAttribute()
121+
{
122+
return Config::get('adldap_auth.windows_auth_attribute', [ActiveDirectory::ACCOUNT_NAME => 'AUTH_USER']);
123+
}
124+
}

0 commit comments

Comments
 (0)