16
16
* Handles the endpoints related to authorizing this app with the Dropbox API
17
17
* via OAuth 2.
18
18
*/
19
- public class DropboxAuth
20
- {
19
+ public class DropboxAuth {
21
20
private final Common common ;
22
21
23
- public DropboxAuth (Common common )
24
- {
22
+ public DropboxAuth (Common common ) {
25
23
this .common = common ;
26
24
}
27
25
@@ -31,14 +29,18 @@ public DropboxAuth(Common common)
31
29
// Start the process of getting a Dropbox API access token for the user's Dropbox account.
32
30
33
31
public void doStart (HttpServletRequest request , HttpServletResponse response )
34
- throws IOException , ServletException
35
- {
32
+ throws IOException , ServletException {
36
33
if (!common .checkPost (request , response )) return ;
37
34
User user = common .requireLoggedInUser (request , response );
38
35
if (user == null ) return ;
39
36
40
37
// Start the authorization process with Dropbox.
41
- String authorizeUrl = getWebAuth (request ).start ();
38
+ DbxWebAuth .Request authRequest = DbxWebAuth .newRequestBuilder ()
39
+ // After we redirect the user to the Dropbox website for authorization,
40
+ // Dropbox will redirect them back here.
41
+ .withRedirectUri (getRedirectUri (request ), getSessionStore (request ))
42
+ .build ();
43
+ String authorizeUrl = getWebAuth (request ).authorize (authRequest );
42
44
43
45
// Redirect the user to the Dropbox website so they can approve our application.
44
46
// The Dropbox website will send them back to /dropbox-auth-finish when they're done.
@@ -55,8 +57,7 @@ public void doStart(HttpServletRequest request, HttpServletResponse response)
55
57
// an HTTP POST.
56
58
57
59
public void doFinish (HttpServletRequest request , HttpServletResponse response )
58
- throws IOException , ServletException
59
- {
60
+ throws IOException , ServletException {
60
61
if (!common .checkGet (request , response )) return ;
61
62
62
63
User user = common .requireLoggedInUser (request , response );
@@ -67,33 +68,31 @@ public void doFinish(HttpServletRequest request, HttpServletResponse response)
67
68
68
69
DbxAuthFinish authFinish ;
69
70
try {
70
- authFinish = getWebAuth (request ).finish (request .getParameterMap ());
71
- }
72
- catch (DbxWebAuth .BadRequestException e ) {
71
+ authFinish = getWebAuth (request ).finishFromRedirect (
72
+ getRedirectUri (request ),
73
+ getSessionStore (request ),
74
+ request .getParameterMap ()
75
+ );
76
+ } catch (DbxWebAuth .BadRequestException e ) {
73
77
common .log .println ("On /dropbox-auth-finish: Bad request: " + e .getMessage ());
74
78
response .sendError (400 );
75
79
return ;
76
- }
77
- catch (DbxWebAuth .BadStateException e ) {
80
+ } catch (DbxWebAuth .BadStateException e ) {
78
81
// Send them back to the start of the auth flow.
79
82
response .sendRedirect (common .getUrl (request , "/dropbox-auth-start" ));
80
83
return ;
81
- }
82
- catch (DbxWebAuth .CsrfException e ) {
84
+ } catch (DbxWebAuth .CsrfException e ) {
83
85
common .log .println ("On /dropbox-auth-finish: CSRF mismatch: " + e .getMessage ());
84
86
response .sendError (403 );
85
87
return ;
86
- }
87
- catch (DbxWebAuth .NotApprovedException e ) {
88
+ } catch (DbxWebAuth .NotApprovedException e ) {
88
89
common .page (response , 200 , "Not approved?" , "Why not, bro?" );
89
90
return ;
90
- }
91
- catch (DbxWebAuth .ProviderException e ) {
91
+ } catch (DbxWebAuth .ProviderException e ) {
92
92
common .log .println ("On /dropbox-auth-finish: Auth failed: " + e .getMessage ());
93
93
response .sendError (503 , "Error communicating with Dropbox." );
94
94
return ;
95
- }
96
- catch (DbxException e ) {
95
+ } catch (DbxException e ) {
97
96
common .log .println ("On /dropbox-auth-finish: Error getting token: " + e );
98
97
response .sendError (503 , "Error communicating with Dropbox." );
99
98
return ;
@@ -112,8 +111,7 @@ public void doFinish(HttpServletRequest request, HttpServletResponse response)
112
111
// -------------------------------------------------------------------------------------------
113
112
114
113
public void doUnlink (HttpServletRequest request , HttpServletResponse response )
115
- throws IOException , ServletException
116
- {
114
+ throws IOException , ServletException {
117
115
if (!common .checkPost (request , response )) return ;
118
116
User user = common .requireLoggedInUser (request , response );
119
117
if (user == null ) return ;
@@ -127,17 +125,18 @@ public void doUnlink(HttpServletRequest request, HttpServletResponse response)
127
125
128
126
// -------------------------------------------------------------------------------------------
129
127
130
- private DbxWebAuth getWebAuth (final HttpServletRequest request )
131
- {
132
- // After we redirect the user to the Dropbox website for authorization,
133
- // Dropbox will redirect them back here.
134
- String redirectUrl = common .getUrl (request , "/dropbox-auth-finish" );
135
-
128
+ private DbxSessionStore getSessionStore (final HttpServletRequest request ) {
136
129
// Select a spot in the session for DbxWebAuth to store the CSRF token.
137
130
HttpSession session = request .getSession (true );
138
131
String sessionKey = "dropbox-auth-csrf-token" ;
139
- DbxSessionStore csrfTokenStore = new DbxStandardSessionStore (session , sessionKey );
132
+ return new DbxStandardSessionStore (session , sessionKey );
133
+ }
134
+
135
+ private DbxWebAuth getWebAuth (final HttpServletRequest request ) {
136
+ return new DbxWebAuth (common .getRequestConfig (request ), common .dbxAppInfo );
137
+ }
140
138
141
- return new DbxWebAuth (common .getRequestConfig (request ), common .dbxAppInfo , redirectUrl , csrfTokenStore );
139
+ private String getRedirectUri (final HttpServletRequest request ) {
140
+ return common .getUrl (request , "/dropbox-auth-finish" );
142
141
}
143
142
}
0 commit comments