Skip to content

Commit c9d8ac6

Browse files
committed
CHANGES:
ADDED new max login feature ADDED german translation
1 parent 2940e09 commit c9d8ac6

File tree

4 files changed

+293
-44
lines changed

4 files changed

+293
-44
lines changed

languages/rw-sso-client-de_DE.mo

1.08 KB
Binary file not shown.

languages/rw-sso-client-de_DE.po

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
msgid ""
2+
msgstr ""
3+
"Project-Id-Version: rw sso REST Auth Client\n"
4+
"Report-Msgid-Bugs-To: \n"
5+
"POT-Creation-Date: 2022-02-22 15:16+0000\n"
6+
"PO-Revision-Date: 2022-02-22 16:02+0000\n"
7+
"Last-Translator: \n"
8+
"Language-Team: Deutsch\n"
9+
"Language: de_DE\n"
10+
"Plural-Forms: nplurals=2; plural=n != 1;\n"
11+
"MIME-Version: 1.0\n"
12+
"Content-Type: text/plain; charset=UTF-8\n"
13+
"Content-Transfer-Encoding: 8bit\n"
14+
"X-Generator: Loco https://localise.biz/\n"
15+
"X-Loco-Version: 2.5.8; wp-5.9\n"
16+
"X-Domain: rw-sso-client"
17+
18+
#. Description of the plugin
19+
msgid ""
20+
"Client Authentication tool to compare Wordpress login Data with a Remote "
21+
"Login Server"
22+
msgstr ""
23+
24+
#. Author of the plugin
25+
msgid "Daniel Reintanz"
26+
msgstr ""
27+
28+
#. URI of the plugin
29+
msgid "https://github.com/rpi-virtuell/rw-sso-rest-auth-client"
30+
msgstr ""
31+
32+
#: sso-rest-auth-client.php:224
33+
msgid "Invite User"
34+
msgstr "Benutzer einladen"
35+
36+
#: sso-rest-auth-client.php:201
37+
msgid "No Response from Remote Login Server!"
38+
msgstr "Keine Antwort vom Remote Login Server!"
39+
40+
#. Name of the plugin
41+
msgid "rw sso REST Auth Client"
42+
msgstr ""
43+
44+
#: sso-rest-auth-client.php:99
45+
#| msgid "The maximum amount of login attempts has been reached please wait "
46+
msgid ""
47+
"The maximum amount of login attempts has been reached please wait %d minutes"
48+
msgstr ""
49+
"Die maximale Anzahl der Anmeldeversuche wurde erreicht, bitte warten Sie %d "
50+
"Minuten"
51+
52+
#: sso-rest-auth-client.php:102
53+
msgid "The maximum amount of login attempts has been reached!"
54+
msgstr "Die maximale Anzahl an Anmeldeversuchen wurde erreicht!"
55+
56+
#: sso-rest-auth-client.php:212
57+
msgid "Username and Password are required!"
58+
msgstr "Benutzername und Passwort sind erforderlich!"
59+
60+
#: sso-rest-auth-client.php:198
61+
msgid "Username or password is invalid"
62+
msgstr "Benutzername oder Passwort ist ungültig"

languages/rw-sso-client.pot

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#, fuzzy
2+
msgid ""
3+
msgstr ""
4+
"Project-Id-Version: rw sso REST Auth Client\n"
5+
"Report-Msgid-Bugs-To: \n"
6+
"POT-Creation-Date: 2022-02-22 16:00+0000\n"
7+
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
8+
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
9+
"Language-Team: \n"
10+
"Language: \n"
11+
"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n"
12+
"MIME-Version: 1.0\n"
13+
"Content-Type: text/plain; charset=UTF-8\n"
14+
"Content-Transfer-Encoding: 8bit\n"
15+
"X-Generator: Loco https://localise.biz/\n"
16+
"X-Loco-Version: 2.5.8; wp-5.9\n"
17+
"X-Domain: rw-sso-client"
18+
19+
#. Description of the plugin
20+
msgid ""
21+
"Client Authentication tool to compare Wordpress login Data with a Remote "
22+
"Login Server"
23+
msgstr ""
24+
25+
#. Author of the plugin
26+
msgid "Daniel Reintanz"
27+
msgstr ""
28+
29+
#. URI of the plugin
30+
msgid "https://github.com/rpi-virtuell/rw-sso-rest-auth-client"
31+
msgstr ""
32+
33+
#: sso-rest-auth-client.php:224
34+
msgid "Invite User"
35+
msgstr ""
36+
37+
#: sso-rest-auth-client.php:201
38+
msgid "No Response from Remote Login Server!"
39+
msgstr ""
40+
41+
#. Name of the plugin
42+
msgid "rw sso REST Auth Client"
43+
msgstr ""
44+
45+
#: sso-rest-auth-client.php:99
46+
#, php-format
47+
msgid ""
48+
"The maximum amount of login attempts has been reached please wait %d minutes"
49+
msgstr ""
50+
51+
#: sso-rest-auth-client.php:102
52+
msgid "The maximum amount of login attempts has been reached!"
53+
msgstr ""
54+
55+
#: sso-rest-auth-client.php:212
56+
msgid "Username and Password are required!"
57+
msgstr ""
58+
59+
#: sso-rest-auth-client.php:198
60+
msgid "Username or password is invalid"
61+
msgstr ""

sso-rest-auth-client.php

Lines changed: 170 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* Description: Client Authentication tool to compare Wordpress login Data with a Remote Login Server
66
* Author: Daniel Reintanz
77
* Version: 1.0.0
8+
* Domain Path: /languages
9+
* Text Domain: rw-sso-client
810
* Licence: GPLv3
911
* GitHub Plugin URI: https://github.com/rpi-virtuell/rw-sso-rest-auth-client
1012
* GitHub Branch: master
@@ -24,78 +26,202 @@ class SsoRestAuthClient
2426
*/
2527
public function __construct()
2628
{
27-
add_filter('authenticate', array($this, 'check_credentials'), 10, 3);
28-
add_action('admin_menu', array($this, 'add_invite_user_user_page'),999);
29+
if (!defined('KONTO_SERVER')) {
30+
if (getenv('KONTO_SERVER'))
31+
define('KONTO_SERVER', getenv('KONTO_SERVER'));
32+
else
33+
// .htaccess Eintrag fehlt: SetEnv KONTO_SERVER "https://my-wordpress-website.com"
34+
wp_die('Environmental Var KONTO_SERVER is not defined');
35+
}
36+
add_filter('authenticate', array($this, 'check_credentials'), 999, 3);
37+
add_action('admin_menu', array($this, 'add_invite_user_user_page'), 999);
2938
add_action('user_new_form_tag', array($this, 'redir_new_user'), 999);
3039
add_action('wp_ajax_search_user', 'ajax_search_user');
3140
add_action('wp_ajax_get_users_via_ajax', array($this, 'get_users_via_ajax'));
3241
add_action('wp_ajax_invite_user_via_ajax', array($this, 'invite_user_via_ajax'));
42+
register_activation_hook(__FILE__, array($this, 'create_failed_login_log_table'));
43+
register_deactivation_hook(__FILE__, array($this, 'delete_failed_login_log_table'));
44+
add_filter('lostpassword_url', function () {
45+
return KONTO_SERVER . '/wp-login.php?action=lostpassword';
46+
});
47+
add_filter('register_url', function () {
48+
return KONTO_SERVER . '/wp-login.php?action=register';
49+
});
50+
51+
}
52+
53+
public function create_failed_login_log_table()
54+
{
55+
global $wpdb;
56+
57+
$table_name = $wpdb->prefix . 'failed_login_log';
58+
$charset_collate = $wpdb->get_charset_collate();
59+
60+
$sql = "CREATE TABLE IF NOT EXISTS $table_name (
61+
`hash` char(32) NOT NULL DEFAULT '' ,
62+
`last_login` bigint(20) NULL ,
63+
`ip` varchar(30) NULL DEFAULT '' ,
64+
`username` varchar(30) NULL DEFAULT '' ,
65+
INDEX (`hash`)
66+
) $charset_collate;";
67+
68+
69+
$wpdb->query($sql);
70+
}
71+
72+
public function delete_failed_login_log_table()
73+
{
74+
global $wpdb;
75+
76+
$table_name = $wpdb->prefix . 'failed_login_log';
77+
78+
$sql = "DROP TABLE IF EXISTS `$table_name`;";
79+
80+
$wpdb->query($sql);
81+
}
82+
83+
84+
public function check_login_attempts($username)
85+
{
86+
87+
$ip = $_SERVER['REMOTE_ADDR'];
88+
$hash = md5($username . $ip);
89+
global $wpdb;
90+
$versuche = $wpdb->get_var("SELECT count(*) FROM {$wpdb->prefix}failed_login_log WHERE hash = '{$hash}' and last_login > UNIX_TIMESTAMP()-(60*20)");
91+
92+
BugFu::log($versuche);
93+
94+
if (intval($versuche) > 3) {
95+
$lastlogin = $wpdb->get_var("SELECT last_login FROM {$wpdb->prefix}failed_login_log WHERE hash = '{$hash}' ORDER BY last_login DESC LIMIT 1");
96+
$lastlogin -= time() - 1200;
97+
$lastlogin = intval($lastlogin / 60);
98+
99+
return new WP_Error('max_invalid_logins', sprintf(__("The maximum amount of login attempts has been reached please wait %d minutes", 'rw-sso-client'), $lastlogin));
100+
}elseif ( 5 < $wpdb->get_var("SELECT count(*) FROM {$wpdb->prefix}failed_login_log WHERE ip = '$ip' and last_login > UNIX_TIMESTAMP()-(60*20)"))
101+
{
102+
return new WP_Error('max_invalid_logins', __("The maximum amount of login attempts has been reached!", 'rw-sso-client'));
103+
}
104+
else {
105+
return true;
106+
}
107+
}
108+
109+
public function cleanup_old_failed_login_attempts()
110+
{
111+
112+
global $wpdb;
113+
114+
$table_name = $wpdb->prefix . 'failed_login_log';
115+
116+
$sql = "DELETE FROM `$table_name` WHERE last_login < UNIX_TIMESTAMP()-(60*20);";
117+
118+
$wpdb->query($sql);
119+
120+
}
121+
122+
public function add_failed_login_attempt($username)
123+
{
124+
125+
$ip = $_SERVER['REMOTE_ADDR'];
126+
$hash = md5($username . $ip);
127+
global $wpdb;
128+
129+
$result = $wpdb->insert(
130+
$wpdb->prefix . 'failed_login_log',
131+
array(
132+
'hash' => $hash,
133+
'ip' => $ip,
134+
'username' => $username,
135+
'last_login' => time(),
136+
),
137+
array(
138+
'%s',
139+
'%s',
140+
'%s',
141+
'%d',
142+
)
143+
);
33144

34145
}
35146

147+
36148
public function check_credentials($user, $username, $password)
37149
{
38150
if (!empty($username) && !empty($password)) {
39-
$url = getenv("KONTO_SERVER") . '/wp-json/sso/v1/check_credentials';
40-
$response = wp_remote_post($url, array(
41-
'method' => 'POST',
42-
'body' => array(
43-
'username' => $username,
44-
'password' => $password,
45-
'origin_url' => home_url()
46-
)));
47151

48-
$response = json_decode(wp_remote_retrieve_body($response));
49-
if (!is_wp_error($response)) {
50-
if ($response->success) {
51-
if ($user = get_user_by('login', $username)) {
52-
if (is_multisite() && !is_user_member_of_blog($user->ID, get_current_blog_id())) {
53-
add_user_to_blog(get_current_blog_id(), $user->ID, get_option('default_role'));
54-
}
55-
return $user;
56-
} elseif ($user = get_user_by('email', $username)) {
57-
if (is_multisite() && !is_user_member_of_blog($user->ID, get_current_blog_id())) {
58-
add_user_to_blog(get_current_blog_id(), $user->ID, get_option('default_role'));
59-
}
60-
return $user;
61-
} else {
62-
$user_id = wp_insert_user(array(
63-
'user_login' => $response->profile->user_login,
64-
'first_name' => $response->profile->first_name,
65-
'last_name' => $response->profile->last_name,
66-
'user_pass' => wp_generate_password(8),
67-
'display_name' => $response->profile->display_name,
68-
'user_email' => $response->profile->user_email
69-
));
70-
if (is_wp_error($user_id)) {
71-
return $user_id->get_error_message();
152+
$this->cleanup_old_failed_login_attempts();
153+
if (!is_wp_error($attempts = $this->check_login_attempts($username))) {
154+
if (is_a($user, 'WP_User')) {
155+
return $user;
156+
}
157+
$url = KONTO_SERVER . '/wp-json/sso/v1/check_credentials';
158+
$response = wp_remote_post($url, array(
159+
'method' => 'POST',
160+
'body' => array(
161+
'username' => $username,
162+
'password' => $password,
163+
'origin_url' => home_url()
164+
)));
165+
$response = json_decode(wp_remote_retrieve_body($response));
166+
if (!is_wp_error($response)) {
167+
if (isset($response->success)) {
168+
if ($response->success) {
169+
if ($user = get_user_by('login', $username)) {
170+
if (is_multisite() && !is_user_member_of_blog($user->ID, get_current_blog_id())) {
171+
add_user_to_blog(get_current_blog_id(), $user->ID, get_option('default_role'));
172+
}
173+
return $user;
174+
} elseif ($user = get_user_by('email', $username)) {
175+
if (is_multisite() && !is_user_member_of_blog($user->ID, get_current_blog_id())) {
176+
add_user_to_blog(get_current_blog_id(), $user->ID, get_option('default_role'));
177+
}
178+
return $user;
179+
} else {
180+
$user_id = wp_insert_user(array(
181+
'user_login' => $response->profile->user_login,
182+
'first_name' => $response->profile->first_name,
183+
'last_name' => $response->profile->last_name,
184+
'user_pass' => wp_generate_password(8),
185+
'display_name' => $response->profile->display_name,
186+
'user_email' => $response->profile->user_email
187+
));
188+
if (is_wp_error($user_id)) {
189+
return $user_id;
190+
} else {
191+
return get_user_by('id', $user_id);
192+
193+
}
194+
}
72195
} else {
73-
return get_user_by('id', $user_id);
196+
$this->add_failed_login_attempt($username);
74197

198+
return new WP_Error('Wrong credentials', __('Username or password is invalid', 'rw-sso-client'));
75199
}
200+
} else {
201+
return new WP_Error('NoResponse', __('No Response from Remote Login Server!', 'rw-sso-client'));
76202
}
77203
} else {
78-
return new WP_Error('NoResponse', 'No Response from Remote Login Server!');
204+
return $response;
79205
}
80-
81206
} else {
82-
return $response->get_error_message();
83-
}
84207

208+
BugFu::log($attempts);
209+
return $attempts;
210+
}
85211
} else {
86-
return new WP_Error('Missing Parameters', 'Required Parameters are missing!');
212+
return new WP_Error('Missing Parameters', __('Username and Password are required!', 'rw-sso-client'));
87213
}
88214
}
89215

90216
function redir_new_user()
91217
{
92-
wp_redirect(home_url().'/wp-admin/users.php?page=invite_user');
218+
wp_redirect(home_url() . '/wp-admin/users.php?page=invite_user');
93219
}
94220

95221
function add_invite_user_user_page()
96222
{
97-
remove_submenu_page('users.php','user-new.php');
98-
add_users_page('invite_user', 'Nutzer einladen', 'edit_users', 'invite_user', array($this, 'init_invite_user_page'), 1);
223+
remove_submenu_page('users.php', 'user-new.php');
224+
add_users_page('invite_user', __('Invite User', 'rw-sso-client'), 'edit_users', 'invite_user', array($this, 'init_invite_user_page'), 1);
99225
}
100226

101227
public
@@ -293,7 +419,7 @@ function remote_search() {
293419
$('#user_invite_form').hide();
294420
$('#results').html($('#selected_user').val() + ' wurde erfolgreich hinzugefügt!');
295421
}
296-
if($('#results') && data.success === false){
422+
if ($('#results') && data.success === false) {
297423
$('#results').html($('#selected_user').val() + ' konnte nicht hinzugefügt werden!');
298424
}
299425
},

0 commit comments

Comments
 (0)