From da78e2ace46dc08169cb699212f41615a169690d Mon Sep 17 00:00:00 2001 From: Greg Helton Date: Thu, 11 Feb 2016 23:56:15 -0500 Subject: [PATCH 1/3] Remove request signing and protocol, update API URLs to new battle.net api host. Now requires an API key. See https://dev.battle.net/docs/read/community_apis/migration --- lib/battlenet/battlenet.rb | 43 ++++++++++---------------------------- 1 file changed, 11 insertions(+), 32 deletions(-) diff --git a/lib/battlenet/battlenet.rb b/lib/battlenet/battlenet.rb index 3d7546c..fe50adc 100644 --- a/lib/battlenet/battlenet.rb +++ b/lib/battlenet/battlenet.rb @@ -83,25 +83,21 @@ class << self # Creates a new instance of the Battlenet API. # # @param region [Symbol] the region to perform API calls against. - # @param public [String|nil] the public key to use when signing requests - # @param private [String|nil] the private key to use when signing requests - def initialize(region = :us, public = nil, private = nil) - @public = public - @private = private - - @proto = @public && @private ? "https://" : "http://" - @endpoint = '/api/wow' + def initialize(region = :us, apikey = nil) + @apikey = apikey + @proto = "https://" + @endpoint = '/wow' @domain = case region when :us - 'us.battle.net' + 'us.api.battle.net' when :eu - 'eu.battle.net' + 'eu.api.battle.net' when :kr - 'kr.battle.net' + 'kr.api.battle.net' when :tw - 'tw.battle.net' + 'tw.api.battle.net' when :cn - 'battlenet.com.cn' + 'battlenet.api.com.cn' else raise "Invalid region: #{region.to_s}" end @@ -146,13 +142,8 @@ def make_request(verb, path, params = {}) options = {} headers = {} - if @public && @private - now = Time.now - signed = sign_request verb, path, now - headers.merge!({ - "Authorization" => "BNET #{@public}:#{signed}", - "Date" => now.httpdate - }) + if @apikey + params.merge!({"apikey" => @apikey}) end options[:headers] = headers unless headers.empty? @@ -173,16 +164,4 @@ def process_response(response) end response end - - # Signs an HTTP request. - # - # @param verb [Symbol] the HTTP verb for the request being signed - # @param path [String] the path for the rquest being signed - # @param time [Time] the time to use when signing the request - # @return [String] value to be used as the final portion of the `Authorization` HTTP header - # @see Battlenet::Authentication - def sign_request(verb, path, time) - auth = Battlenet::Authentication.new @private - auth.sign verb, fullpath(path), time - end end From b1d4f16fcd5957365c71d93e4d9bc8c012ce05f2 Mon Sep 17 00:00:00 2001 From: Greg Helton Date: Thu, 11 Feb 2016 23:59:30 -0500 Subject: [PATCH 2/3] Fix CN url, add SEA region --- lib/battlenet/battlenet.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/battlenet/battlenet.rb b/lib/battlenet/battlenet.rb index fe50adc..a84061d 100644 --- a/lib/battlenet/battlenet.rb +++ b/lib/battlenet/battlenet.rb @@ -97,7 +97,9 @@ def initialize(region = :us, apikey = nil) when :tw 'tw.api.battle.net' when :cn - 'battlenet.api.com.cn' + 'api.battlenet.com.cn' + when :sea + 'sea.api.battle.net' else raise "Invalid region: #{region.to_s}" end From b0b429e18b75fbf8b1c3763d342fe95453d580d3 Mon Sep 17 00:00:00 2001 From: Greg Helton Date: Fri, 12 Feb 2016 15:47:22 -0500 Subject: [PATCH 3/3] Update docs and remove authentication.rb --- lib/battlenet/authentication.rb | 21 --------------------- lib/battlenet/battlenet.rb | 10 ++++------ 2 files changed, 4 insertions(+), 27 deletions(-) delete mode 100644 lib/battlenet/authentication.rb diff --git a/lib/battlenet/authentication.rb b/lib/battlenet/authentication.rb deleted file mode 100644 index 258e015..0000000 --- a/lib/battlenet/authentication.rb +++ /dev/null @@ -1,21 +0,0 @@ -require 'time' -require 'base64' -require 'openssl' - -class Battlenet - class Authentication - def initialize(private) - @private = private - end - - def sign(verb, path, time) - string = string_to_sign(verb, path, time) - signature = OpenSSL::HMAC.digest 'sha1', @private, string - Base64.encode64 signature - end - - def string_to_sign(verb, path, time) - "#{verb.to_s.upcase}\n#{time.httpdate}\n#{path}\n" - end - end -end diff --git a/lib/battlenet/battlenet.rb b/lib/battlenet/battlenet.rb index a84061d..d6d17f0 100644 --- a/lib/battlenet/battlenet.rb +++ b/lib/battlenet/battlenet.rb @@ -1,5 +1,4 @@ require 'httparty' -require 'battlenet/authentication' require 'battlenet/exceptions/api_exception' require 'battlenet/modules/character' require 'battlenet/modules/guild' @@ -25,7 +24,7 @@ # # @example Return basic information about a character named Cyaga from the US realm Nazjatar # -# api = Battlenet.new :us +# api = Battlenet.new :us, 'your_apikey' # char = api.character 'Nazjatar', 'Cyaga' # char['level'] # # => 85 @@ -83,6 +82,7 @@ class << self # Creates a new instance of the Battlenet API. # # @param region [Symbol] the region to perform API calls against. + # @param apikey [String] API key used to authenticate the request. def initialize(region = :us, apikey = nil) @apikey = apikey @proto = "https://" @@ -108,8 +108,7 @@ def initialize(region = :us, apikey = nil) self.class.base_uri @base_uri end - # Signs and performs an HTTP GET request. The request is only signed if a public and private - # key were provided during object instantiation. + # Performs an HTTP GET request. Uses API key if set. # # @param path (see #make_request) # @param params (see #make_request) @@ -129,8 +128,7 @@ def fullpath(path) "#{@endpoint}#{path}" end - # Signs and performs an HTTP request. The request is only signed if a public and private - # key were provided during object instantiation. + # Performs an HTTP request. Uses API key if set. # # @param verb [Symbol] the HTTP verb to perform # @param path [String] the path to GET