qiniu-rs 2.1.1 → 2.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- qiniu-rs (2.1.1)
4
+ qiniu-rs (2.1.2)
5
5
  json (~> 1.7.3)
6
6
  mime-types (~> 1.19)
7
7
  rest-client (~> 1.6.7)
data/lib/qiniu/rs.rb CHANGED
@@ -12,6 +12,9 @@ module Qiniu
12
12
  autoload :IO, 'qiniu/rs/io'
13
13
  autoload :RS, 'qiniu/rs/rs'
14
14
  autoload :Image, 'qiniu/rs/image'
15
+ autoload :AccessToken, 'qiniu/tokens/access_token'
16
+ autoload :QboxToken, 'qiniu/tokens/qbox_token'
17
+ autoload :UploadToken, 'qiniu/tokens/upload_token'
15
18
 
16
19
  class << self
17
20
 
@@ -115,8 +118,19 @@ module Qiniu
115
118
  Image.preivew_url(url, spec)
116
119
  end
117
120
 
118
- def generate_upload_token(scope, expires_in, callback_url = nil, return_url = nil)
119
- Utils.generate_upload_token(scope, expires_in, callback_url, return_url)
121
+ #def generate_upload_token(scope, expires_in, callback_url = nil, return_url = nil)
122
+ # Utils.generate_upload_token(scope, expires_in, callback_url, return_url)
123
+ #end
124
+
125
+ def generate_upload_token(opts = {})
126
+ token_obj = UploadToken.new(opts)
127
+ token_obj.access_key = Config.settings[:access_key]
128
+ token_obj.secret_key = Config.settings[:secret_key]
129
+ #token_obj.scope = opts[:scope]
130
+ #token_obj.expires_in = opts[:expires_in]
131
+ #token_obj.callback_url = opts[:callback_url]
132
+ #token_obj.return_url = opts[:return_url]
133
+ token_obj.generate_token
120
134
  end
121
135
 
122
136
  end
@@ -151,6 +151,7 @@ module Qiniu
151
151
  %Q(#{access_key}:#{encoded_digest})
152
152
  end
153
153
 
154
+ =begin
154
155
  def generate_upload_token(scope, expires_in, callback_url = nil, return_url = nil)
155
156
  access_key = Config.settings[:access_key]
156
157
  secret_key = Config.settings[:secret_key]
@@ -163,6 +164,7 @@ module Qiniu
163
164
  encoded_digest = urlsafe_base64_encode(hmac.digest)
164
165
  %Q(#{access_key}:#{encoded_digest}:#{signature})
165
166
  end
167
+ =end
166
168
 
167
169
  end
168
170
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Qiniu
4
4
  module RS
5
- VERSION = "2.1.1"
5
+ VERSION = "2.1.2"
6
6
  end
7
7
  end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'hmac-sha1'
4
+ require 'qiniu/rs/config'
5
+ require 'qiniu/rs/utils'
6
+
7
+ module Qiniu
8
+ module RS
9
+ class AccessToken
10
+
11
+ include Utils
12
+
13
+ attr_accessor :access_key, :secret_key
14
+
15
+ def generate_encoded_digest(signature)
16
+ hmac = HMAC::SHA1.new(@secret_key)
17
+ hmac.update(signature)
18
+ urlsafe_base64_encode(hmac.digest)
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,37 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'json'
4
+ require 'qiniu/tokens/access_token'
5
+
6
+ module Qiniu
7
+ module RS
8
+ class QboxToken < AccessToken
9
+
10
+ attr_accessor :url, :params
11
+
12
+ def initialize(opts = {})
13
+ @url = opts[:url]
14
+ @params = opts[:params]
15
+ end
16
+
17
+ def generate_signature
18
+ uri = URI.parse(@url)
19
+ signature = uri.path
20
+ query_string = uri.query
21
+ signature += '?' + query_string if !query_string.nil? && !query_string.empty?
22
+ signature += "\n";
23
+ if @params.is_a?(Hash)
24
+ total_param = @params.map { |key, value| key.to_s+"="+value.to_s }
25
+ signature += total_param.join("&")
26
+ end
27
+ signature
28
+ end
29
+
30
+ def generate_token
31
+ encoded_digest = generate_encoded_digest(signature)
32
+ %Q(#{@access_key}:#{encoded_digest})
33
+ end
34
+
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,37 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'json'
4
+ require 'qiniu/tokens/access_token'
5
+ require 'qiniu/rs/utils'
6
+
7
+ module Qiniu
8
+ module RS
9
+ class UploadToken < AccessToken
10
+
11
+ include Utils
12
+
13
+ attr_accessor :scope, :expires_in, :callback_url, :return_url
14
+
15
+ def initialize(opts = {})
16
+ @scope = opts[:scope]
17
+ @expires_in = opts[:expires_in]
18
+ @callback_url = opts[:callback_url]
19
+ @return_url = opts[:return_url]
20
+ end
21
+
22
+ def generate_signature
23
+ params = {:scope => @scope, :deadline => Time.now.to_i + @expires_in}
24
+ params[:callbackUrl] = @callback_url if !@callback_url.nil? && !@calback_url.empty?
25
+ params[:returnUrl] = @return_url if !@return_url.nil? && !@return_url.empty?
26
+ urlsafe_base64_encode(params.to_json)
27
+ end
28
+
29
+ def generate_token
30
+ signature = generate_signature
31
+ encoded_digest = generate_encoded_digest(signature)
32
+ %Q(#{@access_key}:#{encoded_digest}:#{signature})
33
+ end
34
+
35
+ end
36
+ end
37
+ end
@@ -151,7 +151,7 @@ module Qiniu
151
151
 
152
152
  context ".generate_upload_token" do
153
153
  it "should works" do
154
- data = Qiniu::RS.generate_upload_token('test_bucket', 3600)
154
+ data = Qiniu::RS.generate_upload_token({:scope => 'test_bucket', :expires_in => 3600})
155
155
  data.should_not be_empty
156
156
  puts data.inspect
157
157
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: qiniu-rs
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.1
4
+ version: 2.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-02 00:00:00.000000000 Z
12
+ date: 2012-07-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -149,6 +149,9 @@ files:
149
149
  - lib/qiniu/rs/rs.rb
150
150
  - lib/qiniu/rs/utils.rb
151
151
  - lib/qiniu/rs/version.rb
152
+ - lib/qiniu/tokens/access_token.rb
153
+ - lib/qiniu/tokens/qbox_token.rb
154
+ - lib/qiniu/tokens/upload_token.rb
152
155
  - qiniu-rs.gemspec
153
156
  - spec/qiniu/rs/auth_spec.rb
154
157
  - spec/qiniu/rs/image_logo_for_test.png
@@ -173,7 +176,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
173
176
  version: '0'
174
177
  segments:
175
178
  - 0
176
- hash: 1345358308427521600
179
+ hash: -2135467260921136706
177
180
  required_rubygems_version: !ruby/object:Gem::Requirement
178
181
  none: false
179
182
  requirements:
@@ -182,7 +185,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
182
185
  version: '0'
183
186
  segments:
184
187
  - 0
185
- hash: 1345358308427521600
188
+ hash: -2135467260921136706
186
189
  requirements: []
187
190
  rubyforge_project:
188
191
  rubygems_version: 1.8.24