Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## CHANGE LOG

### v6.5.0

- 为 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

- gem 兼容性调整 。 [https://github.com/qiniu/ruby-sdk/pull/122](https://github.com/qiniu/ruby-sdk/pull/122)
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
3 changes: 1 addition & 2 deletions lib/qiniu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,11 @@ def upload_file opts = {}
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])
Expand Down
18 changes: 15 additions & 3 deletions lib/qiniu/auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ def calculate_deadline(expires_in, deadline = nil)
# 默认授权期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
Expand Down Expand Up @@ -169,7 +181,7 @@ def authorize_download_url(url, args = EMPTY_ARGS)
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)

### 生成下载授权凭证
Expand Down Expand Up @@ -219,7 +231,7 @@ def generate_acctoken(url, body = '')
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)

### 生成管理授权凭证
Expand All @@ -238,7 +250,7 @@ def generate_uptoken(put_policy)
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)

### 生成上传授权凭证
Expand Down
4 changes: 2 additions & 2 deletions lib/qiniu/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
module Qiniu
module Version
MAJOR = 6
MINOR = 4
PATCH = 2
MINOR = 5
PATCH = 0
# Returns a version string by joining <tt>MAJOR</tt>, <tt>MINOR</tt>, and <tt>PATCH</tt> with <tt>'.'</tt>
#
# Example
Expand Down
36 changes: 35 additions & 1 deletion spec/qiniu/auth_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,41 @@ module Auth
puts data.inspect
end
end
end
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 Storage
end # module Exception_Auth
end # module Qiniu
3 changes: 3 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
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)
Expand Down