diff --git a/README.md b/README.md index 603dfa1..8bd50d1 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,22 @@ alias work_haste="HASTE_SERVER=https://something.com haste" After which you can use `work_haste` to send hastes to that server instead. +### Authentication + +If your haste installation requires http authentication, +add the following to your ~.bash_profile: + +```bash +export HASTE_USER="myusername" +export HASTE_PASS="mypassword" +``` + +if you are using SSL, you will need to supply your certs path + +```bash +export HASTE_SSL_CERTS="/System/Library/OpenSSL/certs" +``` + ## Use as a library You can also use `Haste` as a library to upload hastes: diff --git a/lib/haste/cli.rb b/lib/haste/cli.rb index f6611e4..213ca11 100644 --- a/lib/haste/cli.rb +++ b/lib/haste/cli.rb @@ -4,7 +4,11 @@ module Haste # Create a new uploader def initialize - @uploader = Uploader.new ENV['HASTE_SERVER'] + @uploader = Uploader.new( + ENV['HASTE_SERVER'], + ENV['HASTE_USER'], + ENV['HASTE_PASS'], + ENV['HASTE_SSL_CERTS']) end # And then handle the basic usage diff --git a/lib/haste/uploader.rb b/lib/haste/uploader.rb index b6ac235..72ad08c 100644 --- a/lib/haste/uploader.rb +++ b/lib/haste/uploader.rb @@ -8,12 +8,16 @@ module Haste class Uploader - attr_reader :server_url + attr_reader :server_url, :server_user, :server_pass, :ssl_certs - def initialize(server_url = nil) + def initialize(server_url = nil, server_user = nil, server_pass = nil, ssl_certs = nil) @server_url = server_url || Haste::DEFAULT_URL @server_url = @server_url.dup @server_url = @server_url.chop if @server_url.end_with?('/') + + @server_user = server_user + @server_pass = server_pass + @ssl_certs = ssl_certs end # Take in a path and return a key @@ -51,11 +55,31 @@ module Haste end def connection - @connection ||= Faraday.new(:url => server_url) do |c| - c.adapter Faraday.default_adapter + @connection ||= connection_set + end + + def connection_set + return connection_https if @ssl_certs + connection_http + end + + def connection_http + Faraday.new(:url => server_url) do |c| + connection_config(c) end end + def connection_https + Faraday.new(:url => server_url, :ssl => { :ca_path => @ssl_certs }) do |c| + connection_config(c) + end + end + + def connection_config(config) + config.basic_auth(@server_user, @server_pass) if @server_user + config.adapter Faraday.default_adapter + end + def fail_with(msg) raise Exception.new(msg) end