Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created July 3, 2023 02:26
Show Gist options
  • Save code-boxx/e3011d79f9140e0944a355739d335214 to your computer and use it in GitHub Desktop.
Save code-boxx/e3011d79f9140e0944a355739d335214 to your computer and use it in GitHub Desktop.
Javascript AJAX With HTTP Auth

JAVASCRIPT AJAX WITH HTTP AUTH

https://code-boxx.com/http-basic-authentication-ajax/

NOTES

  1. This example is based on an Apache Web Server.
  2. Run unpack.bat (Windows) unpack.sh (Linux/Mac). This will:
    • Create a protected folder.
    • Move .htaccess, .htpasswd, secret.html inside.
  3. Edit protected\.htaccess, set AuthUserFile to the correct file path.
  4. Access http://YOURSITE.com/main.html.

ADDING MORE USERS TO HTPASSWD

  1. htpasswd is usually placed inside apache\bin folder.
  2. htpasswd -c YOUR\PROJECT\FOLDER\.htpasswd USER.
  3. Just enter the password.
  4. If you need to add more users, run htpasswd again, without -c option - htpasswd FOLDER\.htpasswd USER2.
  5. Alternatively, just use any "online htpasswd generator".

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

AuthType Basic
AuthName "Password Required"
AuthUserFile D:\http\protected\.htpasswd
Require valid-user
USER:$apr1$i.0uy9VI$RIL6cpq41G1BEEzsdtY/y0
<!DOCTYPE html>
<html>
<head>
<title>AJAX Authenticate Demo</title>
</head>
<body>
<!-- (A) "LOGIN FORM" -->
<form onsubmit="return load()">
<input type="text" id="user-name" placeholder="Name" value="USER" required>
<input type="password" id="user-pass" placeholder="Password" value="PASS" required>
<input type="submit" value="Load!">
</form>
<!-- (B) CONTAINER TO LOAD CONTENTS VIA AJAX -->
<div id="load-here"></div>
<!-- (C) AJAX HTTP AUTHENTICATION -->
<script>
function load () {
// OPTIONAL BACKWARD COMPATIBILITY - POLYFILL FOR BASE64 ENCODE
// if (window.btoa === undefined) { LOAD POLYFILL }
// https://github.com/MaxArt2501/base64-js
// (C1) AUTHORIZATION USER + PASSWORD
var name = document.getElementById("user-name").value,
pass = document.getElementById("user-pass").value,
token = "Basic " + window.btoa(name + ":" + pass);
// (C2) AJAX CALL
var xhr = new XMLHttpRequest();
xhr.open("GET", "protected/secret.html");
xhr.setRequestHeader("Authorization", token);
xhr.onload = function(){
if (this.status==200) {
document.getElementById("load-here").innerHTML = this.response;
} else {
alert("HTTP ERROR " + this.status);
}
};
xhr.send();
return false;
}
</script>
</body>
</html>
<p>uvuvwevwevwe onyetenyevwe ugwemubwem ossas</p>
<p>Wow. Very secret. Such protection. Much covert.</p>
md protected
move htaccess.txt protected/.htaccess
move htpasswd.txt protected/.htpasswd
move secret.html protected
mkdir -m 777 protected
mv ./htaccess.txt ./protected/.htaccess
mv ./htpasswd.txt ./protected/.htpasswd
mv ./secret.html ./protected
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment