-
Notifications
You must be signed in to change notification settings - Fork 1
/
securepass.class.php
231 lines (189 loc) · 5.59 KB
/
securepass.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
/**
* SecurePass main controller.
*
* @class WPSecurePassController
* @author =undo= <[email protected]>
* @copyright Copyright (C) 2014 wpXtreme Inc. All Rights Reserved.
* @date 2014-10-14
* @version 1.0.0
* @since 0.2.0
*
*/
final class WPSecurePassController {
/**
* An instance of WPSecurePassOptions class.
*
* @var WPSecurePassOptions $_options
*/
private $_options = null;
/**
* Return a singleton instance of WPSecurePassController class
*
* @return WPSecurePassController
*/
public static function init()
{
static $instance = null;
if( is_null( $instance ) ) {
$instance = new self();
}
return $instance;
}
/**
* Create an instance of WPSecurePassController class
*
* @return WPSecurePassController
*/
public function __construct()
{
// Get options instance
$this->_options = WPSecurePassOptions::init();
// If master enable
if( $this->_options->enable() ) {
// Create the right callback for selected protocol
$callback = array(
'radius' => array( $this, 'radius_authenticate' ),
'restful' => array( $this, 'restful_authenticate' ),
);
// Get selected protocol
$protocol = $this->_options->protocol();
// Stability
if( !isset( $callback[ $protocol ] ) ) {
return;
}
// Filter the user to authenticate.
add_filter( 'authenticate', $callback[ $protocol ], 10, 3 );
}
}
/**
* Filter the user to authenticate.
*
* If a non-null value is passed, the filter will effectively short-circuit
* authentication, returning an error instead.
*
* This filter is used to authenticate an User by SecurePass RESTfull API.
*
* @since WP 2.8.0
*
* @param null|WP_User $user User to authenticate.
* @param string $username User login.
* @param string $password User password
*/
public function restful_authenticate( $user, $username, $password )
{
// Get instance of RESTFul API class
$restful = WPSecurePassRestful::init();
// Check API
$continue = $restful->ping();
// Stability
if( false === $continue ) {
return $user;
}
// Authentocate ?
if( false === $this->canAuthenticateUser( $username ) ) {
return $user;
}
// Authenticate via RESTful
$user = $restful->auth( $username, $password );
if( $user === false ) {
$user = new WP_Error( 'denied', __( "<strong>ERROR</strong>: Invalid username and password." ) );
remove_action( 'authenticate', 'wp_authenticate_username_password', 20 );
}
return $user;
}
/**
* Filter the user to authenticate.
*
* If a non-null value is passed, the filter will effectively short-circuit
* authentication, returning an error instead.
*
* This filter is used to authenticate an User by SecurePass RADIUS.
*
* @since WP 2.8.0
*
* @param null|WP_User $user User to authenticate.
* @param string $username User login.
* @param string $password User password
*/
public function radius_authenticate( $user, $username, $password )
{
/*
* Set the default SecurePass radius server
* We point to the Swiss datacenter here by default,
* change radius_host to radius2.secure-pass.net to point to Milan
* don't forget to change the secret accordingly
*/
$radius_host = $this->_options->radiusHost();
$radius_secret = $this->_options->radiusSecret();
// Stability
if( empty( $radius_host ) || empty( $radius_secret ) ) {
return $user;
}
// Authentocate ?
if( false === $this->canAuthenticateUser( $username ) ) {
return $user;
}
// Get info
$user = get_user_by( 'login', $username );
$radius = new Radius( $radius_host, $radius_secret );
// Check the password via RADIUS
if( !$radius->AccessRequest( $username, $password ) ) {
$user = new WP_Error( 'denied', __( "<strong>ERROR</strong>: Invalid username and password." ) );
remove_action( 'authenticate', 'wp_authenticate_username_password', 20 );
}
return $user;
}
/**
* Return TRUE if an user must be authenticate by SecurePass.
* If no roles or users id are set in options this method return FALSE.
*
* @param string $username The user login.
*
* @return bool
*/
public function canAuthenticateUser( $username )
{
// Get restrict roles
$roles = $this->_options->listUserRoles();
// Get restrict users id
$users_id = $this->_options->listUsers();
// If no options restrict found return FALSE
if( empty( $roles ) && empty( $users_id ) ) {
return false;
}
// Prepare found
$found = false;
// Get user
$user = get_user_by( 'login', $username );
// Stability
if( !is_object( $user ) ) {
return false;
}
// Check for roles
if( !empty( $roles ) ) {
foreach( $roles as $role ) {
if( in_array( $role, $user->roles ) ) {
$found = true;
break;
}
}
}
// Find this user in roles
if( true === $found ) {
// White or Black list ?
return $this->_options->isWhiteList();
}
// Search in the users id
if( !empty( $users_id ) ) {
$found = in_array( $user->ID, $users_id );
}
// Find in users id
if( true === $found ) {
// White or Black list ?
return $this->_options->isWhiteList();
}
// If the user is not found, then return the right permission in accordion with White or Black list
return ! $this->_options->isWhiteList();
}
}