boxr 0.25.0 → 0.26.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9f91a21daf6cd40a57ba2b240f980ecdd9b0bc53
4
- data.tar.gz: 5a44c1b4e0750902636de3d96444863be26f6f4e
3
+ metadata.gz: 80bb5e98b518eb34aaaddc369a739111cb2bbcd1
4
+ data.tar.gz: 852abc3f680cf0e7e379c67e1fad79096c871d3a
5
5
  SHA512:
6
- metadata.gz: 19377c61c9d9f3b36d7319d0048c291629af9b03099820b8ec732236fd24856186ce540c65a47c24830cc2ecffba313738f017e2b1e8b0a8ccda7d699e226b6d
7
- data.tar.gz: cbbe61b6758e677b23494b971f7cdf7e84f48dee81dd4c8bf6aba1258157ba0b4056b35365b3a787b24a37eadc0f7ffc7e601007570bca321498bcc27e320696
6
+ metadata.gz: eb43cff1678d67aaa57366a99bed71eed95babcaa3d608404c3362385d26e30e3d69d5a528f11e1ec752b1a3f83ce168c3da4c984f8bdbc65f9e44064540de61
7
+ data.tar.gz: 24926c43ccc5d4992168b231c97f68780732e5d502be9590f359107edf7c6cadf37171f76b4b9153397c42f8cfa1a6de026abd16067f6e90c1ae23ac8f5bcc15
data/README.md CHANGED
@@ -97,22 +97,28 @@ client = Boxr::Client.new #using ENV['BOX_DEVELOPER_TOKEN']
97
97
 
98
98
  folder = client.folder_from_path('/some/directory/structure')
99
99
  file = client.upload_file('test.txt', folder)
100
- file = client.create_shared_link_for_file(file, access: :open)
101
- puts "Shared Link: #{file.shared_link.url}"
100
+ updated_file = client.create_shared_link_for_file(file, access: :open)
101
+ puts "Shared Link: #{updated_file.shared_link.url}"
102
102
  ```
103
103
 
104
104
  ### Methods
105
- #### [OAuth](https://developers.box.com/docs/#oauth-2)
105
+ #### [OAuth & JWT](https://developers.box.com/docs/#oauth-2)
106
106
  ```ruby
107
107
  #NOTE: these are all module methods
108
108
 
109
- Boxr::oauth_url(state, host: "app.box.com", response_type: "code", scope: nil, folder_id: nil, box_client_id: ENV['BOX_CLIENT_ID'])
109
+ #OAuth methods
110
+ Boxr::oauth_url(state, host: "app.box.com", response_type: "code", scope: nil, folder_id: nil, client_id: ENV['BOX_CLIENT_ID'])
110
111
 
111
- Boxr::get_tokens(code, grant_type: "authorization_code", username: nil, box_client_id: ENV['BOX_CLIENT_ID'], box_client_secret: ENV['BOX_CLIENT_SECRET'])
112
+ Boxr::get_tokens(code=nil, grant_type: "authorization_code", assertion: nil, scope: nil, username: nil, client_id: ENV['BOX_CLIENT_ID'], client_secret: ENV['BOX_CLIENT_SECRET'])
112
113
 
113
- Boxr::refresh_tokens(refresh_token, box_client_id: ENV['BOX_CLIENT_ID'], box_client_secret: ENV['BOX_CLIENT_SECRET'])
114
+ Boxr::refresh_tokens(refresh_token, client_id: ENV['BOX_CLIENT_ID'], client_secret: ENV['BOX_CLIENT_SECRET'])
114
115
 
115
- Boxr::revoke_tokens(token, box_client_id: ENV['BOX_CLIENT_ID'], box_client_secret: ENV['BOX_CLIENT_SECRET'])
116
+ Boxr::revoke_tokens(token, client_id: ENV['BOX_CLIENT_ID'], client_secret: ENV['BOX_CLIENT_SECRET'])
117
+
118
+ #JWT methods
119
+ Boxr::get_enterprise_token(private_key: ENV['JWT_PRIVATE_KEY'], private_key_password: ENV['JWT_PRIVATE_KEY_PASSWORD'], enterprise_id: ENV['BOX_ENTERPRISE_ID'], client_id: ENV['BOX_CLIENT_ID'])
120
+
121
+ Boxr::get_user_token(user_id, private_key: ENV['JWT_PRIVATE_KEY'], private_key_password: ENV['JWT_PRIVATE_KEY_PASSWORD'], client_id: ENV['BOX_CLIENT_ID'])
116
122
  ```
117
123
  #### [Folders](https://developers.box.com/docs/#folders)
118
124
  ```ruby
@@ -24,12 +24,16 @@ module Boxr
24
24
  auth_post(uri, body)
25
25
  end
26
26
 
27
- def self.get_enterprise_token(private_key, scope: nil, enterprise_id: ENV['BOX_ENTERPRISE_ID'], client_id: ENV['BOX_CLIENT_ID'])
28
- jwt_auth_post(private_key, scope, client_id, enterprise_id, "enterprise")
27
+ def self.get_enterprise_token(private_key: ENV['JWT_PRIVATE_KEY'], private_key_password: ENV['JWT_PRIVATE_KEY_PASSWORD'],
28
+ enterprise_id: ENV['BOX_ENTERPRISE_ID'], client_id: ENV['BOX_CLIENT_ID'])
29
+ unlocked_private_key = unlock_key(private_key, private_key_password)
30
+ jwt_auth_post(unlocked_private_key, client_id, enterprise_id, "enterprise")
29
31
  end
30
32
 
31
- def self.get_user_token(private_key, user_id, scope: nil, client_id: ENV['BOX_CLIENT_ID'])
32
- jwt_auth_post(private_key, scope, client_id, user_id, "user")
33
+ def self.get_user_token(user_id, private_key: ENV['JWT_PRIVATE_KEY'], private_key_password: ENV['JWT_PRIVATE_KEY_PASSWORD'],
34
+ client_id: ENV['BOX_CLIENT_ID'])
35
+ unlocked_private_key = unlock_key(private_key, private_key_password)
36
+ jwt_auth_post(unlocked_private_key, client_id, user_id, "user")
33
37
  end
34
38
 
35
39
  def self.refresh_tokens(refresh_token, client_id: ENV['BOX_CLIENT_ID'], client_secret: ENV['BOX_CLIENT_SECRET'])
@@ -54,7 +58,8 @@ module Boxr
54
58
 
55
59
  private
56
60
 
57
- def self.jwt_auth_post(private_key, scope, iss, sub, box_sub_type)
61
+
62
+ def self.jwt_auth_post(private_key, iss, sub, box_sub_type)
58
63
  payload = {
59
64
  iss: iss,
60
65
  sub: sub,
@@ -65,7 +70,7 @@ module Boxr
65
70
  }
66
71
  assertion = JWT.encode(payload, private_key, "RS256")
67
72
 
68
- get_token(grant_type: JWT_GRANT_TYPE, assertion: assertion, scope: scope)
73
+ get_token(grant_type: JWT_GRANT_TYPE, assertion: assertion)
69
74
  end
70
75
 
71
76
  def self.auth_post(uri, body)
@@ -81,4 +86,12 @@ module Boxr
81
86
  end
82
87
  end
83
88
 
89
+ def self.unlock_key(private_key, private_key_password)
90
+ if private_key.is_a?(OpenSSL::PKey::RSA)
91
+ private_key
92
+ else
93
+ OpenSSL::PKey::RSA.new(private_key, private_key_password)
94
+ end
95
+ end
96
+
84
97
  end
@@ -53,14 +53,26 @@ module Boxr
53
53
  VALID_COLLABORATION_ROLES = ['editor','viewer','previewer','uploader','previewer uploader','viewer uploader','co-owner','owner']
54
54
 
55
55
 
56
- def initialize(access_token=ENV['BOX_DEVELOPER_TOKEN'], refresh_token: nil, client_id: ENV['BOX_CLIENT_ID'], client_secret: ENV['BOX_CLIENT_SECRET'],
57
- identifier: nil, as_user: nil, &token_refresh_listener)
56
+ def initialize( access_token=ENV['BOX_DEVELOPER_TOKEN'],
57
+ refresh_token: nil,
58
+ client_id: ENV['BOX_CLIENT_ID'],
59
+ client_secret: ENV['BOX_CLIENT_SECRET'],
60
+ enterprise_id: ENV['BOX_ENTERPRISE_ID'],
61
+ jwt_private_key: ENV['JWT_PRIVATE_KEY'],
62
+ jwt_private_key_password: ENV['JWT_PRIVATE_KEY_PASSWORD'],
63
+ identifier: nil,
64
+ as_user: nil,
65
+ &token_refresh_listener)
66
+
58
67
  @access_token = access_token
59
68
  raise BoxrError.new(boxr_message: "Access token cannot be nil") if @access_token.nil?
60
69
 
61
70
  @refresh_token = refresh_token
62
71
  @client_id = client_id
63
72
  @client_secret = client_secret
73
+ @enterprise_id = enterprise_id
74
+ @jwt_private_key = jwt_private_key
75
+ @jwt_private_key_password = jwt_private_key_password
64
76
  @identifier = identifier
65
77
  @as_user_id = ensure_id(as_user)
66
78
  @token_refresh_listener = token_refresh_listener
@@ -179,21 +191,34 @@ module Boxr
179
191
 
180
192
  def standard_headers()
181
193
  headers = {"Authorization" => "Bearer #{@access_token}"}
182
- headers['As-User'] = "#{@as_user_id}" unless @as_user_id.nil?
194
+ if @jwt_private_key.nil?
195
+ headers['As-User'] = "#{@as_user_id}" unless @as_user_id.nil?
196
+ end
183
197
  headers
184
198
  end
185
199
 
186
200
  def with_auto_token_refresh
187
- return yield unless @refresh_token
201
+ return yield unless @refresh_token or @jwt_secret_key
188
202
 
189
203
  res = yield
190
204
  if res.status == 401
191
205
  auth_header = res.header['WWW-Authenticate'][0]
192
206
  if auth_header && auth_header.include?('invalid_token')
193
- new_tokens = Boxr::refresh_tokens(@refresh_token, client_id: client_id, client_secret: client_secret)
194
- @access_token = new_tokens.access_token
195
- @refresh_token = new_tokens.refresh_token
196
- @token_refresh_listener.call(@access_token, @refresh_token, @identifier) if @token_refresh_listener
207
+ if @refresh_token
208
+ new_tokens = Boxr::refresh_tokens(@refresh_token, client_id: client_id, client_secret: client_secret)
209
+ @access_token = new_tokens.access_token
210
+ @refresh_token = new_tokens.refresh_token
211
+ @token_refresh_listener.call(@access_token, @refresh_token, @identifier) if @token_refresh_listener
212
+ else
213
+ if @as_user_id
214
+ new_token = Boxr::get_user_token(@as_user_id, private_key: @jwt_private_key, private_key_password: @jwt_private_key_password, client_id: @client_id)
215
+ @access_token = new_token.access_token
216
+ else
217
+ new_token = Boxr::get_enterprise_token(private_key: @jwt_private_key, private_key_password: @jwt_private_key_password, enterprise_id: @enterprise_id, client_id: @client_id)
218
+ @access_token = new_token.access_token
219
+ end
220
+ end
221
+
197
222
  res = yield
198
223
  end
199
224
  end
@@ -12,7 +12,7 @@ module Boxr
12
12
  events = []
13
13
  loop do
14
14
  event_response = get_enterprise_events(created_after, created_before, stream_position, event_type, limit)
15
- event_response.events.each{|event| events << event}
15
+ events.concat(event_response.events)
16
16
  stream_position = event_response.next_stream_position
17
17
 
18
18
  break if event_response.events.empty?
@@ -18,6 +18,10 @@ module Boxr
18
18
  def update_metadata(file, updates, scope: :global, template: :properties)
19
19
  file_id = ensure_id(file)
20
20
  uri = "#{METADATA_URI}/#{file_id}/metadata/#{scope}/#{template}"
21
+
22
+ #in the event just one update is specified ensure that it is packaged inside an array
23
+ updates = [updates] unless updates.is_a? Array
24
+
21
25
  metadata, response = put(uri, updates, content_type: "application/json-patch+json")
22
26
  metadata
23
27
  end
@@ -1,3 +1,3 @@
1
1
  module Boxr
2
- VERSION = "0.25.0"
2
+ VERSION = "0.26.0"
3
3
  end
@@ -169,7 +169,7 @@ describe Boxr::Client do
169
169
 
170
170
  puts "download file"
171
171
  file = BOX_CLIENT.download_file(test_file)
172
- f = open("./spec/test_files/#{DOWNLOADED_TEST_FILE_NAME}", 'w+')
172
+ f = File.open("./spec/test_files/#{DOWNLOADED_TEST_FILE_NAME}", 'w+')
173
173
  f.write(file)
174
174
  f.close
175
175
  expect(FileUtils.identical?("./spec/test_files/#{TEST_FILE_NAME}","./spec/test_files/#{DOWNLOADED_TEST_FILE_NAME}")).to eq(true)
@@ -469,8 +469,10 @@ describe Boxr::Client do
469
469
  expect(metadata.a).to eq("hello")
470
470
 
471
471
  puts "update metadata"
472
- metadata = BOX_CLIENT.update_metadata(test_file, [{op: :replace, path: "/b", value: "there"}])
472
+ metadata = BOX_CLIENT.update_metadata(test_file, {op: :replace, path: "/b", value: "there"})
473
473
  expect(metadata.b).to eq("there")
474
+ metadata = BOX_CLIENT.update_metadata(test_file, [{op: :replace, path: "/b", value: "friend"}])
475
+ expect(metadata.b).to eq("friend")
474
476
 
475
477
  puts "get metadata"
476
478
  metadata = BOX_CLIENT.metadata(test_file)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: boxr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.25.0
4
+ version: 0.26.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chad Burnette
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-17 00:00:00.000000000 Z
11
+ date: 2015-05-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler