diff --git a/README.md b/README.md new file mode 100644 index 0000000..7d50862 --- /dev/null +++ b/README.md @@ -0,0 +1,6 @@ +#ILAB Basic Auth + +This is a simple plugin that allows you to turn on basic auth for stuff like +staging sites or dev sites that are publicly accessible. This shouldn't be +used for any kind of security other than to prevent random people or +robots from indexing your site. \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..a5914a8 --- /dev/null +++ b/composer.json @@ -0,0 +1,15 @@ +{ + "name" : "ilab/ilab-auth", + "description": "Plugin for providing basic auth protection for public stating/dev sites.", + "type": "wordpress-plugin", + "license": "LGPL", + "keywords": ["basic-auth","wordpress"], + "authors": [ + { + "name": "Jon Gilkison", + "email": "jon@interfacelab.com" + } + ], + "require" : { + } +} diff --git a/ilab-auth.php b/ilab-auth.php new file mode 100644 index 0000000..595faed --- /dev/null +++ b/ilab-auth.php @@ -0,0 +1,98 @@ +hosts[] = trim($host); + } + } + + public function checkAuth() { + if (count($this->hosts)>0) { + if (!in_array($_SERVER['HTTP_HOST'],$this->hosts)) + return; + } + + if (is_user_logged_in()) + return; + + nocache_headers(); + + $usr = isset($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : ''; + $pwd = isset($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : ''; + if (empty($usr) && empty($pwd) && isset($_SERVER['HTTP_AUTHORIZATION']) && $_SERVER['HTTP_AUTHORIZATION']) { + list($type, $auth) = explode(' ', $_SERVER['HTTP_AUTHORIZATION']); + if (strtolower($type) === 'basic') { + list($usr, $pwd) = explode(':', base64_decode($auth)); + } + } + + $is_authenticated = wp_authenticate($usr, $pwd); + if (!is_wp_error($is_authenticated)) + return; + + $message = get_option('ilab-basic-auth-message','Please enter your username and password.'); + $error = get_option('ilab-basic-auth-error','You need to supply a username or password to view this site.'); + + header('WWW-Authenticate: Basic realm="'.$message.'"'); + wp_die($error, 'Authorization Required', ['response' => 401]); + } + + public function displaySettings() { + ?> +
+

Basic Auth

+
+ + +

Settings

+

You can enable basic auth on a per host basis by specifying the exact domains in the Hosts settings. This is useful if you want to enable basic auth for dev or staging, but not production.

+

If you don't define any hosts, basic auth will be required on every domain.

+ + + + + + + + + + + + + +
Hosts
Authentication Message
Failure Message
+ + +
+
+