Skip to content

Instantly share code, notes, and snippets.

@alternatex
Created February 2, 2014 22:03
Show Gist options
  • Save alternatex/8775594 to your computer and use it in GitHub Desktop.
Save alternatex/8775594 to your computer and use it in GitHub Desktop.
Basic Auth // Apache // Helper
#!/bin/bash
# Apache2 Basic Auth Directory Protection Helper
# ===
# Note: `AllowOverride All` must be enabled for the given directory
# gather current directory
cwd=${PWD##*/}
# create directory to store password file
pwdirectory=/var/tmp/insecure/"$cwd"-pw
mkdir -p "$pwdirectory"
# create password file
htpasswd -c "$pwdirectory/.htpasswd" $cwd
# password file mustn't be placed into a directory accessible by public
echo "deny from all" > "$pwdirectory/.htaccess"
# basic auth
echo "AuthType Basic" > .htaccess
echo "AuthName \"Restricted Access\"" >> .htaccess
echo "AuthUserFile $pwdirectory/.htpasswd" >> .htaccess
echo "Require user $cwd" >> .htaccess
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment