Skip to content

Commit 90295d7

Browse files
committed
CHANGES:
FIXED various problems ADDED annotations
1 parent 3f25791 commit 90295d7

File tree

1 file changed

+102
-20
lines changed

1 file changed

+102
-20
lines changed

sso-rest-auth-client.php

Lines changed: 102 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ public function __construct()
5454

5555
}
5656

57+
/**
58+
* Create Table which logs failed login attempts on plugin activation
59+
* @since 1.0
60+
* @action plugin activation
61+
* @access public
62+
*/
5763
public function create_failed_login_log_table()
5864
{
5965
global $wpdb;
@@ -73,6 +79,12 @@ public function create_failed_login_log_table()
7379
$wpdb->query($sql);
7480
}
7581

82+
/**
83+
* Delete Table which logs failed login attempts on plugin deactivation
84+
* @since 1.0
85+
* @action plugin deactivation
86+
* @access public
87+
*/
7688
public function delete_failed_login_log_table()
7789
{
7890
global $wpdb;
@@ -84,10 +96,15 @@ public function delete_failed_login_log_table()
8496
$wpdb->query($sql);
8597
}
8698

87-
99+
/**
100+
* Check if user (accessed via specific IP) has less than 4 login attempts or last lock is older than 20 Minutes old
101+
* @param $username
102+
* @return bool|WP_Error
103+
* @since 1.0
104+
* @action check_credentials
105+
*/
88106
public function check_login_attempts($username)
89107
{
90-
91108
$ip = $_SERVER['REMOTE_ADDR'];
92109
$hash = md5($username . $ip);
93110
global $wpdb;
@@ -105,6 +122,11 @@ public function check_login_attempts($username)
105122
}
106123
}
107124

125+
/**
126+
* Delete failed login attempts which are older than 20 Minutes
127+
* @since 1.0
128+
* @action check_credentials
129+
*/
108130
public function cleanup_old_failed_login_attempts()
109131
{
110132

@@ -118,6 +140,12 @@ public function cleanup_old_failed_login_attempts()
118140

119141
}
120142

143+
/**
144+
* Add a new failed login attempt
145+
* @param $username
146+
* @since 1.0
147+
* @action check_credentials
148+
*/
121149
public function add_failed_login_attempt($username)
122150
{
123151

@@ -143,28 +171,46 @@ public function add_failed_login_attempt($username)
143171

144172
}
145173

146-
public function remote_logout(){
147-
wp_redirect(KONTO_SERVER.'/wp-login.php?action=logout&redirect_to='.home_url());
148-
die();
174+
/**
175+
* Logout the current user of the Konto server and get redirected back to the home_url
176+
* @since 1.0
177+
* @action wp_logout
178+
*/
179+
public function remote_logout()
180+
{
181+
wp_redirect(KONTO_SERVER . '/wp-login.php?action=logout&redirect_to=' . home_url());
182+
die();
149183
}
150-
public function remote_login(){
151-
$login_token = get_user_meta(get_current_user_id(),'rw_sso_login_token',true);
152-
if (!empty($login_token))
153-
{
184+
185+
/**
186+
* Set the login token if a login token is set in meta data of the current user
187+
* @since 1.0
188+
* @action wp_head
189+
* @action admin_head
190+
*/
191+
public function remote_login()
192+
{
193+
$login_token = get_user_meta(get_current_user_id(), 'rw_sso_login_token', true);
194+
if (!empty($login_token)) {
154195
?>
155-
<script src="<?php echo KONTO_SERVER . '?login_token='. $login_token ?>">
196+
<script src="<?php echo KONTO_SERVER . '?login_token=' . $login_token ?>">
156197
</script>
157198
<?php
158199
delete_user_meta(get_current_user_id(), 'rw_sso_login_token');
159200
}
160201
}
161202

203+
/**
204+
* Login the user via login token provided via url and check its validity via REST call to the Konto server
205+
* @since 1.0
206+
* @action login_head
207+
*/
162208
public function login_through_token()
163209
{
164210
if (is_user_logged_in()) {
165211
return;
166212
}
167-
if(isset($_GET['rw_sso_login_token'])) {
213+
if (isset($_GET['rw_sso_login_token'])) {
168214
$login_token = $_GET['rw_sso_login_token'];
169215
$url = KONTO_SERVER . '/wp-json/sso/v1/check_login_token';
170216
$response = wp_remote_post($url, array(
@@ -193,15 +239,23 @@ public function login_through_token()
193239
</script>
194240
<script>
195241
if (rw_sso_login_token) {
196-
location.href = '?rw_sso_login_token=' + rw_sso_login_token + '&redirect='+ encodeURI(location.href);
242+
location.href = '?rw_sso_login_token=' + rw_sso_login_token + '&redirect=' + encodeURI(location.href);
197243
}
198244
</script>
199245
<?php
200246

201247
}
202248
}
203249

204-
250+
/**
251+
* Central Method to handle the main Single Sign On logic
252+
* @param $user
253+
* @param $username
254+
* @param $password
255+
* @return WP_Error|WP_User
256+
* @since 1.0
257+
* @action authenticate
258+
*/
205259
public function check_credentials($user, $username, $password)
206260
{
207261
if (!empty($username) && !empty($password)) {
@@ -224,13 +278,13 @@ public function check_credentials($user, $username, $password)
224278
if (isset($response->success)) {
225279
if ($response->success) {
226280
if ($user = get_user_by('login', $username)) {
227-
update_user_meta($user->ID,'rw_sso_login_token',$response->profile->login_token);
281+
update_user_meta($user->ID, 'rw_sso_login_token', $response->profile->login_token);
228282
if (is_multisite() && !is_user_member_of_blog($user->ID, get_current_blog_id())) {
229283
add_user_to_blog(get_current_blog_id(), $user->ID, get_option('default_role'));
230284
}
231285
return $user;
232286
} elseif ($user = get_user_by('email', $username)) {
233-
update_user_meta($user->ID,'rw_sso_login_token',$response->profile->login_token);
287+
update_user_meta($user->ID, 'rw_sso_login_token', $response->profile->login_token);
234288
if (is_multisite() && !is_user_member_of_blog($user->ID, get_current_blog_id())) {
235289
add_user_to_blog(get_current_blog_id(), $user->ID, get_option('default_role'));
236290
}
@@ -244,18 +298,16 @@ public function check_credentials($user, $username, $password)
244298
'display_name' => $response->profile->display_name,
245299
'user_email' => $response->profile->user_email
246300
));
247-
update_user_meta($user_id,'rw_sso_login_token',$response->profile->login_token);
301+
update_user_meta($user_id, 'rw_sso_login_token', $response->profile->login_token);
248302
if (is_wp_error($user_id)) {
249303
return $user_id;
250304
} else {
251305
return get_user_by('id', $user_id);
252-
253306
}
254307
}
255308

256309
} else {
257310
$this->add_failed_login_attempt($username);
258-
259311
return new WP_Error('Wrong credentials', __('Username or password is invalid', 'rw-sso-client'));
260312
}
261313
} else {
@@ -272,19 +324,33 @@ public function check_credentials($user, $username, $password)
272324
}
273325
}
274326

327+
/**
328+
* Redirect Users to the invite users page if user_new.php is accessed
329+
* @action user_new_form_tag
330+
* @since 1.0
331+
*/
275332
function redir_new_user()
276333
{
277334
wp_redirect(home_url() . '/wp-admin/users.php?page=invite_user');
278335
}
279336

337+
/**
338+
* Remove and Add new menu User "creation" pages
339+
* @action admin_menu
340+
* @since 1.0
341+
*/
280342
function add_invite_user_user_page()
281343
{
282344
remove_submenu_page('users.php', 'user-new.php');
283345
add_users_page('invite_user', __('Invite User', 'rw-sso-client'), 'edit_users', 'invite_user', array($this, 'init_invite_user_page'), 1);
284346
}
285347

286-
public
287-
function get_users_via_ajax()
348+
/**
349+
* Provide a Json with User data html
350+
* @action wp_ajax_get_users_via_ajax
351+
* @since 1.0
352+
*/
353+
public function get_users_via_ajax()
288354
{
289355
$search_input = isset($_POST['search_input']) ? $_POST['search_input'] : '';
290356
$return = array('success' => false);
@@ -313,6 +379,11 @@ function get_users_via_ajax()
313379
die();
314380
}
315381

382+
/**
383+
* Creates a User which is provided via ajax and returns its id
384+
* @since 1.0
385+
* @action wp_ajax_invite_user_via_ajax
386+
*/
316387
public function invite_user_via_ajax()
317388
{
318389
$return = array('success' => false);
@@ -350,6 +421,11 @@ public function invite_user_via_ajax()
350421
die();
351422
}
352423

424+
/**
425+
* Provide HTML information for the construction of a new User Menu Page to invite Users of a Konto Server
426+
* @since 1.0
427+
* @action add_users_page
428+
*/
353429
function init_invite_user_page()
354430
{
355431

@@ -497,6 +573,12 @@ function remote_search() {
497573

498574
}
499575

576+
/**
577+
* Provide HTML to display a dropdown with all roles of the WordPress server
578+
* @since 1.0
579+
* @action init_invite_user_page
580+
* @return string
581+
*/
500582
private function prepare_role_html()
501583
{
502584
$return = '<label for="role">Rolle festlegen</label><select name="role" id="role">';

0 commit comments

Comments
 (0)