From 1b0491a2c94eb4da1daa2c131fc57ffc29eb929f Mon Sep 17 00:00:00 2001 From: Liang Tao Date: Wed, 13 May 2015 21:23:32 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E5=85=A5=E5=8F=A3?= =?UTF-8?q?=E5=87=BD=E6=95=B0=20upload=5Ffile=20=EF=BC=8C=E4=BB=A5?= =?UTF-8?q?=E6=AD=A3=E7=A1=AE=E5=9C=B0=E8=B0=83=E7=94=A8=E5=88=86=E7=89=87?= =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E5=8A=9F=E8=83=BD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/qiniu.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/qiniu.rb b/lib/qiniu.rb index 302125a..231086b 100755 --- a/lib/qiniu.rb +++ b/lib/qiniu.rb @@ -64,12 +64,11 @@ module Qiniu opts[:enable_resumable_upload] = true unless opts.has_key?(:enable_resumable_upload) if opts[:enable_resumable_upload] && File::size(source_file) > Config.settings[:block_size] - code, data, raw_headers = Storage.upload_with_token(opts[:uptoken], + code, data, raw_headers = Storage.resumable_upload_with_token(opts[:uptoken], opts[:file], opts[:bucket], opts[:key], opts[:mime_type], - opts[:note], opts[:customer], opts[:callback_params], opts[:rotate]) From 44dcf0b6226a8af279b91c119f5ea871adbe9d31 Mon Sep 17 00:00:00 2001 From: Liang Tao Date: Tue, 23 Jun 2015 22:28:37 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20Qiniu::Auth=20?= =?UTF-8?q?=EF=BC=8C=E5=A2=9E=E5=8A=A0=E4=B8=80=E4=B8=AA=E5=B0=86=E5=BA=95?= =?UTF-8?q?=E5=B1=82=E5=BC=82=E5=B8=B8=E8=BD=AC=E6=8D=A2=E6=88=90=E6=9B=B4?= =?UTF-8?q?=E5=8F=8B=E5=A5=BD=E6=8F=90=E7=A4=BA=E7=9A=84=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 4 ++++ Gemfile.lock | 2 +- lib/qiniu/auth.rb | 18 +++++++++++++++--- lib/qiniu/version.rb | 4 ++-- spec/qiniu/auth_spec.rb | 38 ++++++++++++++++++++++++++++++++++++-- spec/spec_helper.rb | 3 +++ 6 files changed, 61 insertions(+), 8 deletions(-) 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) From 99698187979b187008c558ff191fd6dbb2405e7c Mon Sep 17 00:00:00 2001 From: Liang Tao Date: Tue, 23 Jun 2015 22:35:54 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=20CHANGELOG=20=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=20PR=20=E5=8F=B7=E7=A0=81=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 13a2149..d277998 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ### 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) +- 为 Qiniu::Auth 添加一个异常处理逻辑,在 Access Key 和 Secret Key 未正常设置(nil 值)的情况下给出正确提示。[https://github.com/qiniu/ruby-sdk/pull/126](https://github.com/qiniu/ruby-sdk/pull/126) ### v6.4.2