dropbox-sdk 1.5.1 → 1.6

Sign up to get free protection for your applications and to get access to all the features.
data/web_file_browser.rb CHANGED
@@ -13,68 +13,63 @@
13
13
  require 'rubygems'
14
14
  require 'sinatra'
15
15
  require 'pp'
16
+ require 'securerandom'
16
17
  require './lib/dropbox_sdk'
17
18
 
18
19
  # Get your app's key and secret from https://www.dropbox.com/developers/
19
20
  APP_KEY = ''
20
21
  APP_SECRET = ''
21
- ACCESS_TYPE = :app_folder #The two valid values here are :app_folder and :dropbox
22
- #The default is :app_folder, but your application might be
23
- #set to have full :dropbox access. Check your app at
24
- #https://www.dropbox.com/developers/apps
25
22
 
26
23
  # -------------------------------------------------------------------
27
24
  # OAuth stuff
28
25
 
29
- get '/oauth-start' do
30
- # OAuth Step 1: Get a request token from Dropbox.
31
- db_session = DropboxSession.new(APP_KEY, APP_SECRET)
32
- begin
33
- db_session.get_request_token
34
- rescue DropboxError => e
35
- return html_page "Exception in OAuth step 1", "<p>#{h e}</p>"
36
- end
26
+ def get_web_auth()
27
+ return DropboxOAuth2Flow.new(APP_KEY, APP_SECRET, url('/dropbox-auth-finish'),
28
+ session, :dropbox_auth_csrf_token)
29
+ end
37
30
 
38
- session[:request_db_session] = db_session.serialize
31
+ get '/dropbox-auth-start' do
32
+ authorize_url = get_web_auth().start()
39
33
 
40
- # OAuth Step 2: Send the user to the Dropbox website so they can authorize
41
- # our app. After the user authorizes our app, Dropbox will redirect them
42
- # to our '/oauth-callback' endpoint.
43
- auth_url = db_session.get_authorize_url url('/oauth-callback')
44
- redirect auth_url
34
+ # Send the user to the Dropbox website so they can authorize our app. After the user
35
+ # authorizes our app, Dropbox will redirect them to our '/dropbox-auth-finish' endpoint.
36
+ redirect authorize_url
45
37
  end
46
38
 
47
- get '/oauth-callback' do
48
- # Finish OAuth Step 2
49
- ser = session[:request_db_session]
50
- unless ser
51
- return html_page "Error in OAuth step 2", "<p>Couldn't find OAuth state in session.</p>"
52
- end
53
- db_session = DropboxSession.deserialize(ser)
54
-
55
- # OAuth Step 3: Get an access token from Dropbox.
39
+ get '/dropbox-auth-finish' do
56
40
  begin
57
- db_session.get_access_token
41
+ access_token, user_id, url_state = get_web_auth.finish(params)
42
+ rescue DropboxOAuth2Flow::BadRequestError => e
43
+ return html_page "Error in OAuth 2 flow", "<p>Bad request to /dropbox-auth-finish: #{e}</p>"
44
+ rescue DropboxOAuth2Flow::BadStateError => e
45
+ return html_page "Error in OAuth 2 flow", "<p>Auth session expired: #{e}</p>"
46
+ rescue DropboxOAuth2Flow::CsrfError => e
47
+ logger.info("/dropbox-auth-finish: CSRF mismatch: #{e}")
48
+ return html_page "Error in OAuth 2 flow", "<p>CSRF mismatch</p>"
49
+ rescue DropboxOAuth2Flow::NotApprovedError => e
50
+ return html_page "Not Approved?", "<p>Why not, bro?</p>"
51
+ rescue DropboxOAuth2Flow::ProviderError => e
52
+ return html_page "Error in OAuth 2 flow", "Error redirect from Dropbox: #{e}"
58
53
  rescue DropboxError => e
59
- return html_page "Exception in OAuth step 3", "<p>#{h e}</p>"
54
+ logger.info "Error getting OAuth 2 access token: #{e}"
55
+ return html_page "Error in OAuth 2 flow", "<p>Error getting access token</p>"
60
56
  end
61
- session.delete(:request_db_session)
62
- session[:authorized_db_session] = db_session.serialize
57
+
58
+ # In this simple example, we store the authorized DropboxSession in the session.
59
+ # A real webapp might store it somewhere more persistent.
60
+ session[:access_token] = access_token
63
61
  redirect url('/')
64
- # In this simple example, we store the authorized DropboxSession in the web
65
- # session hash. A "real" webapp might store it somewhere more persistent.
62
+ end
63
+
64
+ get '/dropbox-unlink' do
65
+ session.delete(:access_token)
66
+ nil
66
67
  end
67
68
 
68
69
  # If we already have an authorized DropboxSession, returns a DropboxClient.
69
- def get_db_client
70
- if session[:authorized_db_session]
71
- db_session = DropboxSession.deserialize(session[:authorized_db_session])
72
- begin
73
- return DropboxClient.new(db_session, ACCESS_TYPE)
74
- rescue DropboxAuthError => e
75
- # The stored session didn't work. Fall through and start OAuth.
76
- session[:authorized_db_session].delete
77
- end
70
+ def get_dropbox_client
71
+ if session[:access_token]
72
+ return DropboxClient.new(session[:access_token])
78
73
  end
79
74
  end
80
75
 
@@ -83,34 +78,36 @@ end
83
78
 
84
79
  get '/' do
85
80
  # Get the DropboxClient object. Redirect to OAuth flow if necessary.
86
- db_client = get_db_client
87
- unless db_client
88
- redirect url("/oauth-start")
81
+ client = get_dropbox_client
82
+ unless client
83
+ redirect url("/dropbox-auth-start")
89
84
  end
90
85
 
91
86
  # Call DropboxClient.metadata
92
87
  path = params[:path] || '/'
93
88
  begin
94
- entry = db_client.metadata(path)
89
+ entry = client.metadata(path)
95
90
  rescue DropboxAuthError => e
96
- session.delete(:authorized_db_session) # An auth error means the db_session is probably bad
97
- return html_page "Dropbox auth error", "<p>#{h e}</p>"
91
+ session.delete(:access_token) # An auth error means the access token is probably bad
92
+ logger.info "Dropbox auth error: #{e}"
93
+ return html_page "Dropbox auth error"
98
94
  rescue DropboxError => e
99
95
  if e.http_response.code == '404'
100
- return html_page "Path not found: #{h path}", ""
96
+ return html_page "Path not found: #{h path}"
101
97
  else
102
- return html_page "Dropbox API error", "<pre>#{h e.http_response}</pre>"
98
+ logger.info "Dropbox API error: #{e}"
99
+ return html_page "Dropbox API error"
103
100
  end
104
101
  end
105
102
 
106
103
  if entry['is_dir']
107
- render_folder(db_client, entry)
104
+ render_folder(client, entry)
108
105
  else
109
- render_file(db_client, entry)
106
+ render_file(client, entry)
110
107
  end
111
108
  end
112
109
 
113
- def render_folder(db_client, entry)
110
+ def render_folder(client, entry)
114
111
  # Provide an upload form (so the user can add files to this folder)
115
112
  out = "<form action='/upload' method='post' enctype='multipart/form-data'>"
116
113
  out += "<label for='file'>Upload file:</label> <input name='file' type='file'/>"
@@ -128,7 +125,7 @@ def render_folder(db_client, entry)
128
125
  html_page "Folder: #{entry['path']}", out
129
126
  end
130
127
 
131
- def render_file(db_client, entry)
128
+ def render_file(client, entry)
132
129
  # Just dump out metadata hash
133
130
  html_page "File: #{entry['path']}", "<pre>#{h entry.pretty_inspect}</pre>"
134
131
  end
@@ -144,19 +141,21 @@ post '/upload' do
144
141
  end
145
142
 
146
143
  # Get the DropboxClient object.
147
- db_client = get_db_client
148
- unless db_client
144
+ client = get_dropbox_client
145
+ unless client
149
146
  return html_page "Upload error", "<p>Not linked with a Dropbox account.</p>"
150
147
  end
151
148
 
152
149
  # Call DropboxClient.put_file
153
150
  begin
154
- entry = db_client.put_file("#{params[:folder]}/#{name}", temp_file.read)
151
+ entry = client.put_file("#{params[:folder]}/#{name}", temp_file.read)
155
152
  rescue DropboxAuthError => e
156
- session.delete(:authorized_db_session) # An auth error means the db_session is probably bad
157
- return html_page "Dropbox auth error", "<p>#{h e}</p>"
153
+ session.delete(:access_token) # An auth error means the access token is probably bad
154
+ logger.info "Dropbox auth error: #{e}"
155
+ return html_page "Dropbox auth error"
158
156
  rescue DropboxError => e
159
- return html_page "Dropbox API error", "<p>#{h e}</p>"
157
+ logger.info "Dropbox API error: #{e}"
158
+ return html_page "Dropbox API error"
160
159
  end
161
160
 
162
161
  html_page "Upload complete", "<pre>#{h entry.pretty_inspect}</pre>"
@@ -164,13 +163,18 @@ end
164
163
 
165
164
  # -------------------------------------------------------------------
166
165
 
167
- def html_page(title, body)
166
+ def html_page(title, body='')
168
167
  "<html>" +
169
168
  "<head><title>#{h title}</title></head>" +
170
169
  "<body><h1>#{h title}</h1>#{body}</body>" +
171
170
  "</html>"
172
171
  end
173
172
 
173
+ # Rack will issue a warning if no session secret key is set. A real web app would not have
174
+ # a hard-coded secret in the code but would load it from a config file.
175
+ use Rack::Session::Cookie, :secret => 'dummy_secret'
176
+
177
+ set :port, 5000
174
178
  enable :sessions
175
179
 
176
180
  helpers do
metadata CHANGED
@@ -1,40 +1,45 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: dropbox-sdk
3
- version: !ruby/object:Gem::Version
4
- version: 1.5.1
3
+ version: !ruby/object:Gem::Version
4
+ hash: 3
5
5
  prerelease:
6
+ segments:
7
+ - 1
8
+ - 6
9
+ version: "1.6"
6
10
  platform: ruby
7
- authors:
11
+ authors:
8
12
  - Dropbox, Inc.
9
13
  autorequire:
10
14
  bindir: bin
11
15
  cert_chain: []
12
- date: 2012-09-06 00:00:00.000000000 Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
16
+
17
+ date: 2013-07-08 00:00:00 Z
18
+ dependencies:
19
+ - !ruby/object:Gem::Dependency
15
20
  name: json
16
- requirement: !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
- version: '0'
22
- type: :runtime
23
21
  prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
22
+ requirement: &id001 !ruby/object:Gem::Requirement
25
23
  none: false
26
- requirements:
27
- - - ! '>='
28
- - !ruby/object:Gem::Version
29
- version: '0'
30
- description: ! " A library that provides a plain function-call interface to
31
- the\n Dropbox API web endpoints.\n"
32
- email:
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ hash: 3
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :runtime
32
+ version_requirements: *id001
33
+ description: " A library that provides a plain function-call interface to the\n Dropbox API web endpoints.\n"
34
+ email:
33
35
  - support-api@dropbox.com
34
36
  executables: []
37
+
35
38
  extensions: []
39
+
36
40
  extra_rdoc_files: []
37
- files:
41
+
42
+ files:
38
43
  - CHANGELOG
39
44
  - LICENSE
40
45
  - README
@@ -44,28 +49,37 @@ files:
44
49
  - lib/dropbox_sdk.rb
45
50
  - lib/trusted-certs.crt
46
51
  homepage: http://www.dropbox.com/developers/
47
- licenses:
52
+ licenses:
48
53
  - MIT
49
54
  post_install_message:
50
55
  rdoc_options: []
51
- require_paths:
56
+
57
+ require_paths:
52
58
  - lib
53
- required_ruby_version: !ruby/object:Gem::Requirement
59
+ required_ruby_version: !ruby/object:Gem::Requirement
54
60
  none: false
55
- requirements:
56
- - - ! '>='
57
- - !ruby/object:Gem::Version
58
- version: '0'
59
- required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ hash: 3
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
69
  none: false
61
- requirements:
62
- - - ! '>='
63
- - !ruby/object:Gem::Version
64
- version: '0'
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ hash: 3
74
+ segments:
75
+ - 0
76
+ version: "0"
65
77
  requirements: []
78
+
66
79
  rubyforge_project:
67
- rubygems_version: 1.8.24
80
+ rubygems_version: 1.8.25
68
81
  signing_key:
69
82
  specification_version: 3
70
83
  summary: Dropbox REST API Client.
71
84
  test_files: []
85
+