mirror of
https://github.com/seejohnrun/haste-client.git
synced 2025-12-16 00:35:56 +00:00
Basic Authentication (#28)
* adding basic http authentication * updating ruby style * SSL support, variable name corrections
This commit is contained in:
16
README.md
16
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.
|
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
|
## Use as a library
|
||||||
|
|
||||||
You can also use `Haste` as a library to upload hastes:
|
You can also use `Haste` as a library to upload hastes:
|
||||||
|
|||||||
@@ -4,7 +4,11 @@ module Haste
|
|||||||
|
|
||||||
# Create a new uploader
|
# Create a new uploader
|
||||||
def initialize
|
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
|
end
|
||||||
|
|
||||||
# And then handle the basic usage
|
# And then handle the basic usage
|
||||||
|
|||||||
@@ -8,12 +8,16 @@ module Haste
|
|||||||
|
|
||||||
class Uploader
|
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 || Haste::DEFAULT_URL
|
||||||
@server_url = @server_url.dup
|
@server_url = @server_url.dup
|
||||||
@server_url = @server_url.chop if @server_url.end_with?('/')
|
@server_url = @server_url.chop if @server_url.end_with?('/')
|
||||||
|
|
||||||
|
@server_user = server_user
|
||||||
|
@server_pass = server_pass
|
||||||
|
@ssl_certs = ssl_certs
|
||||||
end
|
end
|
||||||
|
|
||||||
# Take in a path and return a key
|
# Take in a path and return a key
|
||||||
@@ -51,9 +55,29 @@ module Haste
|
|||||||
end
|
end
|
||||||
|
|
||||||
def connection
|
def connection
|
||||||
@connection ||= Faraday.new(:url => server_url) do |c|
|
@connection ||= connection_set
|
||||||
c.adapter Faraday.default_adapter
|
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
def fail_with(msg)
|
def fail_with(msg)
|
||||||
|
|||||||
Reference in New Issue
Block a user