Skip to content

Commit

Permalink
Merge pull request #2739 from chrisroberts/feature/dnsimple-tokenauth
Browse files Browse the repository at this point in the history
Allow dnsimple authentication via API tokens
  • Loading branch information
geemus committed Mar 7, 2014
2 parents 06e595c + 6b709de commit 58da7e2
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions lib/fog/dnsimple/dns.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ module Fog
module DNS
class DNSimple < Fog::Service

requires :dnsimple_email, :dnsimple_password
recognizes :dnsimple_url, :host, :path, :port, :scheme, :persistent
recognizes :dnsimple_email, :dnsimple_password, :dnsimple_token, :dnsimple_domain, :dnsimple_url, :host, :path, :port, :scheme, :persistent

model_path 'fog/dnsimple/models/dns'
model :record
Expand Down Expand Up @@ -42,6 +41,8 @@ def self.reset
def initialize(options={})
@dnsimple_email = options[:dnsimple_email]
@dnsimple_password = options[:dnsimple_password]
@dnsimple_token = options[:dnsimple_token]
@dnsimple_domain = options[:dnsimple_domain]
end

def data
Expand All @@ -59,6 +60,8 @@ class Real
def initialize(options={})
@dnsimple_email = options[:dnsimple_email]
@dnsimple_password = options[:dnsimple_password]
@dnsimple_token = options[:dnsimple_token]
@dnsimple_domain = options[:dnsimple_domain]
@connection_options = options[:connection_options] || {}
if options[:dnsimple_url]
uri = URI.parse(options[:dnsimple_url])
Expand All @@ -79,13 +82,26 @@ def reload

def request(params)
params[:headers] ||= {}
key = "#{@dnsimple_email}:#{@dnsimple_password}"
params[:headers].merge!({ "Authorization" => "Basic " + Base64.encode64(key).gsub("\n",''),
"Accept" => "application/json",
"Content-Type" => "application/json" })

if(@dnsimple_password)
key = "#{@dnsimple_email}:#{@dnsimple_password}"
params[:headers].merge!("Authorization" => "Basic " + Base64.encode64(key).gsub("\n",''))
elsif(@dnsimple_token)
if(@dnsimple_domain)
params[:headers].merge!("X-DNSimple-Domain-Token" => @dnsimple_token)
else
params[:headers].merge!("X-DNSimple-Token" => "#{@dnsimple_email}:#{@dnsimple_token}")
end
else
raise ArgumentError.new("Insufficient credentials to properly authenticate!")
end
params[:headers].merge!(
"Accept" => "application/json",
"Content-Type" => "application/json"
)

version = params.delete(:version) || 'v1'
params[:path] = "/#{version}#{params[:path]}"
params[:path] = File.join('/', version, params[:path])

response = @connection.request(params)

Expand Down

0 comments on commit 58da7e2

Please sign in to comment.