1
0
mirror of https://github.com/seejohnrun/haste-client.git synced 2025-12-12 14:55:56 +00:00

Basic Authentication (#28)

* adding basic http authentication

* updating ruby style

* SSL support, variable name corrections
This commit is contained in:
Nick Otter
2022-02-14 10:25:49 -07:00
committed by GitHub
parent fb77a2e9f5
commit 6e9801e547
3 changed files with 49 additions and 5 deletions

View File

@@ -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:

View File

@@ -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

View File

@@ -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