-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhhdev-bedrock-basic-auth.php
More file actions
executable file
·72 lines (58 loc) · 2.36 KB
/
Copy pathhhdev-bedrock-basic-auth.php
File metadata and controls
executable file
·72 lines (58 loc) · 2.36 KB
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
<?php
/*
Plugin Name: HHdev Bedrock Basic Authentication
Plugin URI: https://haha.nl/wordpress-plug-in-op-maat/
Description: Basic auth for the Bedrock WordPress framework. Optional authenticate whole site or just login. Works with default wp-login.php or with WPS Hide Login plug-in changed url.
Author: herbert hoekstra - haha.nl - hrbrt.dev
Version: 1.0.2
Author URI: https://haha.nl
*/
// needed on some servers to make basic auth work
/*
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
*/
/*
// use HHdev Bedrock authentication
Config::define('HTTP_AUTH_USER_NAME', 'username' );
Config::define('HTTP_AUTH_USER_PASS', 'password' );
Config::define('HTTP_AUTH_ENABLED', true ); // use true or false
//Config::define('HTTP_AUTH_LEVEL', 'login' ); // use 'site' or 'login'
*/
function hhdev_requireAuth($user, $pass) {
header('Cache-Control: no-cache, must-revalidate, max-age=0');
$has_supplied_credentials = !(empty($_SERVER['PHP_AUTH_USER']) && empty($_SERVER['PHP_AUTH_PW']));
$is_not_authenticated = (
!$has_supplied_credentials ||
$_SERVER['PHP_AUTH_USER'] != $user ||
$_SERVER['PHP_AUTH_PW'] != $pass
);
if ($is_not_authenticated) {
header('HTTP/1.1 401 Authorization Required');
header('WWW-Authenticate: Basic realm="Access denied"');
wp_die(
'Access denied.',
'Authorization Required',
array('response' => 401)
);
}
}
function hhdev_initAuth() {
if (defined('HTTP_AUTH_ENABLED') && HTTP_AUTH_ENABLED && php_sapi_name() !== "cli") {
$admin_slug = '/wp/wp-login.php'; // default login
// check to use other login slug via WPS Hide Login
if(file_exists(WP_PLUGIN_DIR . '/wps-hide-login/wps-hide-login.php')) $admin_slug = '/' . get_option( 'whl_page' ).'/';
if (defined('HTTP_AUTH_USER_NAME') && defined('HTTP_AUTH_USER_PASS')) {
// whole website
if (strcasecmp(HTTP_AUTH_LEVEL, 'site') === 0) {
hhdev_requireAuth(HTTP_AUTH_USER_NAME, HTTP_AUTH_USER_PASS);
// login only
} elseif (strcasecmp(HTTP_AUTH_LEVEL, 'login') === 0 && trim(strtok($_SERVER['REQUEST_URI'], '?')) == $admin_slug) {
hhdev_requireAuth(HTTP_AUTH_USER_NAME, HTTP_AUTH_USER_PASS);
}
}
}
}
hhdev_initAuth();