diff --git a/CHANGELOG.md b/CHANGELOG.md index 129033b..13a2149 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ## CHANGE LOG +### v6.5.0 + +- 为 Qiniu::Auth 添加一个异常处理逻辑,在 Access Key 和 Secret Key 未正常设置(nil 值)的情况下给出正确提示。[https://github.com/qiniu/ruby-sdk/pull/124](https://github.com/qiniu/ruby-sdk/pull/124) + ### v6.4.2 - gem 兼容性调整 。 [https://github.com/qiniu/ruby-sdk/pull/122](https://github.com/qiniu/ruby-sdk/pull/122) diff --git a/Gemfile.lock b/Gemfile.lock index 1397204..fba0c60 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - qiniu (6.4.1) + qiniu (6.5.0) json (~> 1.8) mime-types (~> 1.19) rest-client (~> 1.7.3) diff --git a/lib/qiniu/auth.rb b/lib/qiniu/auth.rb index 210c6fa..f261701 100755 --- a/lib/qiniu/auth.rb +++ b/lib/qiniu/auth.rb @@ -25,6 +25,18 @@ module Qiniu # 默认授权期1小时 return Time.now.to_i + DEFAULT_AUTH_SECONDS end # calculate_deadline + + def calculate_hmac_sha1_digest(sk, str) + begin + sign = HMAC::SHA1.new(sk).update(str).digest + rescue RuntimeError => e + raise RuntimeError, "Please set Qiniu's access_key and secret_key before authorize any tokens." + rescue + raise + else + return sign + end + end end # class << self class PutPolicy @@ -169,7 +181,7 @@ module Qiniu end ### 生成数字签名 - sign = HMAC::SHA1.new(secret_key).update(download_url).digest + sign = calculate_hmac_sha1_digest(secret_key, download_url) encoded_sign = Utils.urlsafe_base64_encode(sign) ### 生成下载授权凭证 @@ -219,7 +231,7 @@ module Qiniu end ### 生成数字签名 - sign = HMAC::SHA1.new(secret_key).update(signing_str).digest + sign = calculate_hmac_sha1_digest(secret_key, signing_str) encoded_sign = Utils.urlsafe_base64_encode(sign) ### 生成管理授权凭证 @@ -238,7 +250,7 @@ module Qiniu encoded_put_policy = Utils.urlsafe_base64_encode(put_policy.to_json) ### 生成数字签名 - sign = HMAC::SHA1.new(secret_key).update(encoded_put_policy).digest + sign = calculate_hmac_sha1_digest(secret_key, encoded_put_policy) encoded_sign = Utils.urlsafe_base64_encode(sign) ### 生成上传授权凭证 diff --git a/lib/qiniu/version.rb b/lib/qiniu/version.rb index 3a5f348..58d7d89 100755 --- a/lib/qiniu/version.rb +++ b/lib/qiniu/version.rb @@ -3,8 +3,8 @@ module Qiniu module Version MAJOR = 6 - MINOR = 4 - PATCH = 2 + MINOR = 5 + PATCH = 0 # Returns a version string by joining MAJOR, MINOR, and PATCH with '.' # # Example diff --git a/spec/qiniu/auth_spec.rb b/spec/qiniu/auth_spec.rb index 5648846..cf4fa88 100755 --- a/spec/qiniu/auth_spec.rb +++ b/spec/qiniu/auth_spec.rb @@ -68,7 +68,41 @@ module Qiniu puts data.inspect end end - end - end # module Storage + end # module Auth + + module Exception_Auth + describe Exception_Auth, :not_set_ak_sk => true do + ### 测试未设置 ak/sk 的异常抛出情况 + context ".not_set_ak_sk" do + it "should works" do + puts Qiniu::Config.instance_variable_get("@settings").inspect + + begin + uptoken = Qiniu::Auth.generate_uptoken({}) + rescue => e + e.message.should == "Please set Qiniu's access_key and secret_key before authorize any tokens." + else + fail "Not raise any exception." + end + + begin + download_url = Qiniu::Auth.authorize_download_url("http://test.qiniudn.com/a_private_file") + rescue => e + e.message.should == "Please set Qiniu's access_key and secret_key before authorize any tokens." + else + fail "Not raise any exception." + end + + begin + acctoken = Qiniu::Auth.generate_acctoken("http://rsf.qbox.me/list") + rescue => e + e.message.should == "Please set Qiniu's access_key and secret_key before authorize any tokens." + else + fail "Not raise any exception." + end + end + end + end + end # module Exception_Auth end # module Qiniu diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index b5ec3ee..54eefd7 100755 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -8,6 +8,9 @@ RSpec.configure do |config| config.before :all do Qiniu.establish_connection! :access_key => ENV["QINIU_ACCESS_KEY"], :secret_key => ENV["QINIU_SECRET_KEY"] end + config.before :each, :not_set_ak_sk => true do + Qiniu.establish_connection! :access_key => nil, :secret_key => nil + end end def make_unique_bucket (bucket)