mirror of
https://github.com/wahyd4/ruby-sdk.git
synced 2026-08-09 21:06:19 +10:00
修改 Qiniu::Auth ,增加一个将底层异常转换成更友好提示的异常。
This commit is contained in:
@@ -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)
|
||||
|
||||
+1
-1
@@ -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)
|
||||
|
||||
+15
-3
@@ -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)
|
||||
|
||||
### 生成上传授权凭证
|
||||
|
||||
@@ -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
|
||||
|
||||
+36
-2
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user