Skip to content

Instantly share code, notes, and snippets.

@real34
Created April 5, 2012 12:33
Show Gist options
  • Save real34/2310728 to your computer and use it in GitHub Desktop.
Save real34/2310728 to your computer and use it in GitHub Desktop.
Capistrano : Http Authentification recipe - Generates htaccess/htpasswd Apache protection for your project
##
# HTTP Authentification deployment Recipe
# => Add htaccess password protection when deploying, with the correct htpassword file for the passed users
##
_cset (:http_auth_users) {[
["admin", "password"]
]}
_cset (:http_auth_path) { "" } # Path of the directory to add http auth, from the release root
namespace :httpAuth do
desc <<-DESC
Generates / updates .htaccess and .htpasswd files with the credentials passed in parameter
Inspired from: https://gist.github.com/805879
DESC
task :protect do
htpasswdFile = "#{current_release}#{http_auth_path}/.htpasswd"
htpasswdContent = http_auth_users.inject("") { |content, user|
content = content + user[0] + ":" + user[1].crypt("httpauth") + "\n"
}
put htpasswdContent, htpasswdFile, :mode => 0644
[
'',
'AuthType Basic',
'AuthName "Restricted"',
"AuthUserFile \"#{htpasswdFile}\"",
'Require valid-user'
].each { |line| run "echo '#{line}' >> #{current_release}#{http_auth_path}/.htaccess" }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment