forked from stevenkword/coin-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoin-auth.php
284 lines (235 loc) · 10.3 KB
/
coin-auth.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<?php
/**
* Plugin Name: coin-auth
* Plugin URI: https://github.com/dustyfresh/coin-auth
* Description: coinhive crypto-miner captcha WordPress plugin. Forked from version 1.6 of https://github.com/ashmatadeen/no-captcha
*
* @package coin-auth
* Author: dustyfresh
* Author URI: https://twitter.com/dustyfresh
* Version: 1.0.1
*/
add_action( 'admin_menu', 'coin_auth_menu' );
add_action( 'admin_init', 'coin_auth_display_options' );
add_action( 'login_enqueue_scripts', 'coin_auth_login_form_script' );
add_action( 'login_form', 'coin_auth_render_login_captcha' );
add_filter( 'wp_authenticate_user', 'coin_auth_verify_login_captcha', 10, 2 );
// Specific support for WooCommerce login form!
// Using WooCommerce specific hooks because WooCommerce's login form does not use the expected wp_login_form().
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
add_action( 'woocommerce_login_form', 'coin_auth_render_login_captcha' );
add_action( 'wp_enqueue_scripts', 'coin_auth_login_form_script' );
}
function coin_auth_menu() {
add_options_page( 'Coin auth', 'Coin auth', 'manage_options', 'coinhive-options', 'coin_auth_options_page' );
}
function coin_auth_options_page() {
?>
<h2>Coinhive authentication for WordPress</h2>
<div class="wrap">
<div id="icon-options-general" class="icon32"></div>
<div id="poststuff">
<div id="post-body" class="metabox-holder columns-2">
<!-- main content -->
<div id="post-body-content">
<div class="meta-box-sortables ui-sortable">
<div class="postbox">
<div class="inside">
<form method="post" action="options.php">
<?php
settings_fields( 'coin_section' );
do_settings_sections( 'coinhive-options' );
submit_button();
?>
</form>
<form method="post" action="options.php">
<?php
settings_fields( 'exlude_ips_section' );
do_settings_sections( 'coinhive-exlude_ips-options' );
submit_button();
?>
</form>
</div>
</div>
<!-- .postbox -->
</div>
<!-- .meta-box-sortables .ui-sortable -->
</div>
<!-- post-body-content -->
<!-- sidebar -->
<div id="postbox-container-1" class="postbox-container">
<div class="meta-box-sortables">
<div class="postbox">
<center>
<h3>Monero conversions</h3>
<div class="inside">
<?php
$monero = json_decode( file_get_contents( 'https://min-api.cryptocompare.com/data/price?fsym=XMR&tsyms=BTC,USD,EUR', false ) );
$xmr_to_btc = $monero->BTC;
$xmr_to_usd = $monero->USD;
$xmr_to_eur = $monero->EUR;
echo "<p><b>BTC:</b> $xmr_to_btc</p>";
echo "<p><b>USD:</b> $xmr_to_usd</p>";
echo "<p><b>EUR:</b> $xmr_to_eur</p>";
?>
<!-- <a href="https://twitter.com/intent/tweet?screen_name=dustyfresh&ref_src=twsrc%5Etfw" class="twitter-mention-button" data-show-count="false">Tweet to @dustyfresh</a><script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script> !-->
</div>
<!-- .inside -->
</center>
</div>
<!-- .postbox -->
</div>
<!-- .meta-box-sortables -->
</div>
<!-- #postbox-container-1 .postbox-container -->
</div>
<!-- #post-body .metabox-holder .columns-2 -->
<br class="clear">
</div>
<!-- #poststuff -->
</div> <!-- .wrap -->
<?php
}
function coin_auth_display_options() {
// add coinhive section to settings menu
add_settings_section( 'coin_section', 'Coinhive auth Settings', 'coin_auth_display_coin_api_content', 'coinhive-options' );
// Coinhive auth key Secret-key section
add_settings_field( 'coin_auth_site_key', 'Site key', 'coin_auth_key_input', 'coinhive-options', 'coin_section' );
add_settings_field( 'coin_auth_secret_key', 'Secret Key', 'coin_auth_secret_key_input', 'coinhive-options', 'coin_section' );
register_setting( 'coin_section', 'coin_auth_site_key' );
register_setting( 'coin_section', 'coin_auth_secret_key' );
add_settings_field( 'coin_auth_hashcount', 'Hash count for proof of work (hashes goal should be a multiple of 256). The higher the number the longer the proof of work will take to recive access token.', 'coin_auth_hashcount_input', 'coinhive-options', 'coin_section' );
register_setting( 'coin_section', 'coin_auth_hashcount' );
if ( get_option( 'coin_auth_hashcount' ) === false ) {
update_option( 'coin_auth_hashcount', '256' ); }
// IP whitelist settings in options
add_settings_section( 'exlude_ips_section', 'Whitelist IP addresses', 'coin_auth_display_coin_exlude_ips_content', 'coinhive-exlude_ips-options' );
add_settings_field( 'coin_auth_exlude_ips', 'Whitelist', 'coin_auth_exlude_ips_input', 'coinhive-exlude_ips-options', 'exlude_ips_section' );
add_settings_field( 'coin_auth_exlude_ips_forwarded_for', 'Cloudflare', 'coin_auth_exlude_ips_forwarded_for_input', 'coinhive-exlude_ips-options', 'exlude_ips_section' );
register_setting( 'exlude_ips_section', 'coin_auth_exlude_ips' );
register_setting( 'exlude_ips_section', 'coin_auth_exlude_ips_forwarded_for' );
}
function coin_auth_hashcount_input() {
echo '<input type="text" name="coin_auth_hashcount" id="coin_auth_hashcount" value="' . esc_attr( get_option( 'coin_auth_hashcount' ) ) . '" />';
}
function coin_auth_display_coin_exlude_ips_content() {
echo '<p>You can whitelist specific IP addresses (separated by comma) from having to complete the proof-of-work captcha:</p>';
}
function coin_auth_exlude_ips_input() {
echo '<input size="60" type="text" name="coin_auth_exlude_ips" id="coin_auth_exlude_ips" value="' . esc_attr( get_option( 'coin_auth_exlude_ips' ) ) . '" />';
}
function coin_auth_display_coin_api_content() {
echo 'Please register with <a href="https://coinhive.com/" target="_blank">Coinhive</a> to obtain the API keys and enter them below.</p>';
}
function coin_auth_key_input() {
echo '<input type="text" name="coin_auth_site_key" id="captcha_site_key" value="' . esc_attr( get_option( 'coin_auth_site_key' ) ) . '" />';
}
function coin_auth_secret_key_input() {
echo '<input type="password" name="coin_auth_secret_key" id="captcha_secret_key" value="' . esc_attr( get_option( 'coin_auth_secret_key' ) ) . '" />';
}
function coin_auth_exlude_ips_forwarded_for_input() {
echo '<input type="checkbox" id="coin_auth_exlude_ips_forwarded_for" name="coin_auth_exlude_ips_forwarded_for" value="1"' . checked( 1, esc_attr( get_option( 'coin_auth_exlude_ips_forwarded_for' ) ), false ) . '/>';
}
function coin_auth_login_form_script() {
if ( ! coin_auth_is_ip_excluded() ) {
wp_register_script( 'coin_js', plugins_url( '/js/coinhive.js', __FILE__ ) );
wp_enqueue_script( 'coin_js' );
// wp_register_script( 'coin_auth_login', 'https://authedmine.com/lib/captcha.min.js' );
// wp_enqueue_script( 'coin_auth_login' );
}
}
function coin_auth_render_login_captcha() {
// Set your keys or work will automatically happen **without proof verification**
if ( ! coin_auth_api_keys_set() && ! coin_auth_is_ip_excluded() ) {
die( "<h4 style='color: red;'>coinhive-auth API keys are not set! You must remove the plugin before being able to login again. Reinstall the plugin and configure Coinhive API keys.</h4>" );
}
if ( coin_auth_api_keys_set() && ! coin_auth_is_ip_excluded() ) {
echo '
<script>
function hide(id){
var x = document.getElementById(id);
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
function callmemaybe(token) {
document.getElementById("loginform").innerHTML += "<input type=\"hidden\" name=\"captcha_token\" value=\""+ token + "\">";
hide("notice");
hide("coinhive-auth");
document.getElementById("alert").innerHTML += "<br/><font style=\'color: green;\'>Proof of work verification complete! You may now login to this WordPress.</font>";
}
</script>
<div class="coinhive-captcha"
name="coinhive-auth"
id="coinhive-auth"
data-hashes="' . esc_attr( get_option( 'coin_auth_hashcount' ) ) . ' "
data-key="' . esc_attr( get_option( 'coin_auth_site_key' ) ) . '"
data-whitelabel="true"
data-disable-elements="input[type=submit]"
data-callback="callmemaybe">
<em>Loading Captcha...<br>
If it does not load, please disable Adblock!</em>
</div>
<div id="alert"><font style="color: red;" id="notice">Verify captcha before logging in.</font><br/><a href="https://coinhive.com/info/captcha-help" target="_blank"><u>What is this?</u></a><br/>Disable adblock if you do not see "Verify me" box.</div>';
}//end if
}
function coin_auth_verify_login_captcha( $user, $password ) {
$post_data = [
'secret' => esc_attr( get_option( 'coin_auth_secret_key' ) ),
// <- Your secret key
'token' => $_POST['captcha_token'],
'hashes' => esc_attr( get_option( 'coin_auth_hashcount' ) ),
];
$post_context = stream_context_create(
[
'http' => [
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query( $post_data ),
],
]
);
$url = 'https://api.coinhive.com/token/verify';
$response = json_decode( file_get_contents( $url, false, $post_context ) );
if ( $response && $response->success ) {
// All good. Token verified!
return $user;
}
}
function coin_auth_api_keys_set() {
if ( get_option( 'coin_auth_secret_key' ) && get_option( 'coin_auth_site_key' ) ) {
return true;
} else {
return false;
}
}
function coin_auth_get_exlude_ips() {
$exlude_ips = esc_attr( get_option( 'coin_auth_exlude_ips' ) );
if ( $exlude_ips ) {
return array_map( 'trim', explode( ',', $exlude_ips ) );
} else {
return array();
}
}
function coin_auth_get_client_ip() {
$ipaddress = '';
if ( isset( $_SERVER['HTTP_CLIENT_IP'] ) ) {
$ipaddress = $_SERVER['HTTP_CLIENT_IP'];
} elseif ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
$ipaddress = $_SERVER['REMOTE_ADDR'];
} elseif ( get_option( 'coin_auth_exlude_ips_forwarded_for' ) === '1' && isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ipaddress = 'UNKNOWN';
}
return $ipaddress;
}
function coin_auth_is_ip_excluded() {
if ( coin_auth_get_client_ip() === 'UNKNOWN' ) {
return false;
} else {
return in_array( coin_auth_get_client_ip(), coin_auth_get_exlude_ips() );
}
}