From 6b709de6c8a632d12255d4f82f60e7f576cb2801 Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Thu, 6 Mar 2014 17:26:16 -0800 Subject: [PATCH] Allow dnsimple authentication via API tokens --- lib/fog/dnsimple/dns.rb | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/lib/fog/dnsimple/dns.rb b/lib/fog/dnsimple/dns.rb index a20cc8fff..cd53c7b70 100644 --- a/lib/fog/dnsimple/dns.rb +++ b/lib/fog/dnsimple/dns.rb @@ -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 @@ -42,6 +41,8 @@ module Fog 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 @@ -59,6 +60,8 @@ module Fog 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]) @@ -79,13 +82,26 @@ module Fog 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)