-
Notifications
You must be signed in to change notification settings - Fork 451
/
DropboxOauth2Helper.cs
891 lines (811 loc) · 44.6 KB
/
DropboxOauth2Helper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
//-----------------------------------------------------------------------------
// <copyright file="DropboxOauth2Helper.cs" company="Dropbox Inc">
// Copyright (c) Dropbox Inc. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------------
namespace Dropbox.Api
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
/// <summary>
/// Used by <see cref="DropboxOAuth2Helper.GetAuthorizeUri" /> to specify which OAuth 2.0 flow to use.
/// </summary>
public enum OAuthResponseType
{
/// <summary>
/// This represents the OAuth 2.0 token or implicit grant flow. The server will return the bearer token via
/// the <c>redirectUri</c> callback, rather than requiring your app to make a second call to a server.
/// This is useful for pure client-side apps, such as mobile apps or JavaScript-based apps.
/// </summary>
Token,
/// <summary>
/// This represents the OAuth 2.0 code flow. The server will return a code via the <c>redirectUri</c>
/// callback which should be converted into a bearer token using the <see cref="DropboxOAuth2Helper.ProcessCodeFlowAsync"/>
/// method. This is the recommended flow for apps that are running on a server.
/// </summary>
Code,
}
/// <summary>
/// Used by <see cref="DropboxOAuth2Helper.GetAuthorizeUri" /> to specify which type of OAuth token to request.
/// </summary>
public enum TokenAccessType
{
/// <summary>
/// Creates one long-lived token with no expiration
/// </summary>
Legacy,
/// <summary>
/// Create one short-lived token with an expiration with a refresh token
/// </summary>
Offline,
/// <summary>
/// Create one short-lived token with an expiration
/// </summary>
Online,
}
/// <summary>
/// Which scopes that have already been granted to include when requesting scopes.
/// </summary>
public enum IncludeGrantedScopes
{
/// <summary>
/// Default, requests only the scopes passed in to the oauth request
/// </summary>
None,
/// <summary>
/// Token should include all previously granted user scopes as well as requested scopes
/// </summary>
User,
/// <summary>
/// Token should include all previously granted team scopes as well as requested scopes
/// </summary>
Team,
}
/// <summary>
/// Contains methods that make authorizing with Dropbox easier.
/// </summary>
/// <example>
/// <para>
/// This shows an example of how to use the token flow. This is part of a Windows Console or WPF app.
/// </para>
/// <para>
/// The <c>GetAccessToken()</c> method calls <see cref="DropboxOAuth2Helper.GetAuthorizeUri" /> to create the URI with response type
/// set to <see cref="OAuthResponseType.Token"/> for token flow.
/// </para>
/// <para>
/// <see cref="Guid.NewGuid"/> is called to generate a random string to use as the state argument, this value can also be used to
/// store application context and prevent cross-site request forgery.
/// </para>
/// <para>
/// A <see cref="HttpListener"/> is created to listen to the <c>RedirectUri</c> which will later receive redirect callback from
/// the server. <see cref="System.Diagnostics.Process.Start"/> is called to launch a native browser and navigate user to the authorize
/// URI. The <c>RedirectUri</c> needs to be registered at <a href="https://www.dropbox.com/developers/apps">App Console</a>. It's
/// common to use value like <c>http://127.0.0.1:{some_avaialble_port}</c>.
/// </para>
/// <para>
/// After user successfully authorizes the request, <c>HandleOAuth2Redirect</c> receives the redirect callback which contains state
/// and access token as URL fragment. Since the server cannot receive URL fragment directly, it calls <c>RespondPageWithJSRedirect</c>
/// to respond with a HTML page which runs JS code and sends URL fragment as query string parameter to a separate <c>JSRedirect</c> endpoint.
/// </para>
/// <para>
/// <c>HandleJSRedirect</c> is called to handle redirect from JS code and processes OAuth response from query string.
/// This returns an <see cref="OAuth2Response"/> containing the access token that will be passed to the <see cref="DropboxClient"/> constructor.
/// </para>
/// <code>
/// private async Task HandleOAuth2Redirect(HttpListener http)
/// {
/// var context = await http.GetContextAsync();
///
/// // We only care about request to RedirectUri endpoint.
/// while (context.Request.Url.AbsolutePath != RedirectUri.AbsolutePath)
/// {
/// context = await http.GetContextAsync();
/// }
///
/// // Respond with a HTML page which runs JS to send URl fragment.
/// RespondPageWithJSRedirect();
/// }
///
///
/// private async Task<OAuth2Response> HandleJSRedirect(HttpListener http)
/// {
/// var context = await http.GetContextAsync();
///
/// // We only care about request to TokenRedirectUri endpoint.
/// while (context.Request.Url.AbsolutePath != JSRedirectUri.AbsolutePath)
/// {
/// context = await http.GetContextAsync();
/// }
///
/// var redirectUri = new Uri(context.Request.QueryString["url_with_fragment"]);
///
/// var result = DropboxOAuth2Helper.ParseTokenFragment(redirectUri);
///
/// return result;
/// }
///
/// private async Task GetAccessToken() {
/// var state = Guid.NewGuid().ToString("N");
/// var authorizeUri = DropboxOAuth2Helper.GetAuthorizeUri(OAuthResponseType.Token, ApiKey, new Uri(RedirectUri), state: state);
///
/// var http = new HttpListener();
/// http.Prefixes.Add(RedirectUri);
/// http.Start();
///
/// System.Diagnostics.Process.Start(authorizeUri.ToString());
///
/// // Handle OAuth redirect and send URL fragment to local server using JS.
/// await HandleOAuth2Redirect(http);
///
/// // Handle redirect from JS and process OAuth response.
/// var result = await HandleJSRedirect(http);
///
/// if (result.State != state)
/// {
/// // The state in the response doesn't match the state in the request.
/// return null;
/// }
///
/// Settings.Default.AccessToken = result.AccessToken;
/// }
/// </code>
/// <para>
/// This shows an example of how to use the code flow. This is part of a controller class on an ASP.Net MVC website.
/// </para>
/// <para>
/// The <c>Connect()</c> method calls <see cref="DropboxOAuth2Helper.GetAuthorizeUri" /> to create the URI that the browser component
/// navigate to; the response type is set to <see cref="OAuthResponseType.Code"/> to create a URI for the code flow.
/// </para>
/// <para>
/// <see cref="Guid.NewGuid"/> is called to generate a random string to use as the state argument, this value is stored
/// on a field in the web app's user database associated with the current user, this helps prevent cross-site request forgery.
/// </para>
/// <para>
/// The <c>AuthAsync</c> method handles the route represented by the <c>RedirectUri</c>. The ASP.Net infrastructure has already
/// parsed the query string and extracted the <c>code</c> and <c>state</c> arguments. After validating that the <c>state</c>
/// matches the value stored in the user record in the <c>Connect</c> method, authorization is completed by calling
/// <see cref="DropboxOAuth2Helper.ProcessCodeFlowAsync"/>. This returns an <see cref="OAuth2Response"/> containing the access token
/// that will be passed to the <see cref="DropboxClient"/> constructor.
/// </para>
/// <code>
/// // GET: /Home/Connect
/// public ActionResult Connect()
/// {
/// var user = this.store.CurrentUser();
/// user.ConnectState = Guid.NewGuid().ToString("N");
/// this.store.SaveChanges();
///
/// var redirect = DropboxOAuth2Helper.GetAuthorizeUri(OauthResponseType.Code, AppKey, RedirectUri, user.ConnectState);
/// return Redirect(redirect.ToString());
/// }
///
/// // GET: /Home/Auth
/// public async Task<ActionResult> AuthAsync(string code, string state)
/// {
/// var user = this.store.CurrentUser();
///
/// if (user.ConnectState != state)
/// {
/// this.Flash("There was an error connecting to Dropbox.");
/// return this.RedirectToAction("Index");
/// }
///
/// OAuth2Response response = await DropboxOAuth2Helper.ProcessCodeFlowAsync(code, AppKey, AppSecret, RedirectUri);
///
/// user.DropboxAccessToken = response.AccessToken;
/// await this.store.SaveChangesAsync();
///
/// this.Flash("This account has been connected to Dropbox.");
/// return this.RedirectToAction("Profile");
/// }
/// </code>
/// </example>
public static class DropboxOAuth2Helper
{
/// <summary>
/// Length of Code Verifier for PKCE to be used in OAuth Flow.
/// </summary>
private static readonly int PKCEVerifierLength = 128;
/// <summary>
/// Gets the URI used to start the OAuth2.0 authorization flow.
/// </summary>
/// <param name="oauthResponseType">The grant type requested, either <c>Token</c> or <c>Code</c>.</param>
/// <param name="clientId">The apps key, found in the
/// <a href="https://www.dropbox.com/developers/apps">App Console</a>.</param>
/// <param name="redirectUri">Where to redirect the user after authorization has completed. This must be the exact URI
/// registered in the <a href="https://www.dropbox.com/developers/apps">App Console</a>; even <c>localhost</c>
/// must be listed if it is used for testing. A redirect URI is required for a token flow, but optional for code.
/// If the redirect URI is omitted, the code will be presented directly to the user and they will be invited to enter
/// the information in your app.</param>
/// <param name="state">Up to 500 bytes of arbitrary data that will be passed back to <paramref name="redirectUri"/>.
/// This parameter should be used to protect against cross-site request forgery (CSRF).</param>
/// <param name="forceReapprove">Whether or not to force the user to approve the app again if they've already done so.
/// If <c>false</c> (default), a user who has already approved the application may be automatically redirected to
/// <paramref name="redirectUri"/>If <c>true</c>, the user will not be automatically redirected and will have to approve
/// the app again.</param>
/// <param name="disableSignup">When <c>true</c> (default is <c>false</c>) users will not be able to sign up for a
/// Dropbox account via the authorization page. Instead, the authorization page will show a link to the Dropbox
/// iOS app in the App Store. This is only intended for use when necessary for compliance with App Store policies.</param>
/// <param name="requireRole">If this parameter is specified, the user will be asked to authorize with a particular
/// type of Dropbox account, either work for a team account or personal for a personal account. Your app should still
/// verify the type of Dropbox account after authorization since the user could modify or remove the require_role
/// parameter.</param>
/// <param name="forceReauthentication"> If <c>true</c>, users will be signed out if they are currently signed in.
/// This will make sure the user is brought to a page where they can create a new account or sign in to another account.
/// This should only be used when there is a definite reason to believe that the user needs to sign in to a new or
/// different account.</param>
/// <param name="tokenAccessType">Determines the type of token to request. See <see cref="TokenAccessType" />
/// for information on specific types available. If none is specified, this will use the legacy type.</param>
/// <param name="scopeList">list of scopes to request in base oauth flow. If left blank, will default to all scopes for app.</param>
/// <param name="includeGrantedScopes">which scopes to include from previous grants. Note: if this user has never linked the app, include_granted_scopes must be None.</param>
/// <param name="codeChallenge">If using PKCE, please us the PKCEOAuthFlow object.</param>
/// <returns>The uri of a web page which must be displayed to the user in order to authorize the app.</returns>
public static Uri GetAuthorizeUri(OAuthResponseType oauthResponseType, string clientId, string redirectUri = null, string state = null, bool forceReapprove = false, bool disableSignup = false, string requireRole = null, bool forceReauthentication = false, TokenAccessType tokenAccessType = TokenAccessType.Legacy, string[] scopeList = null, IncludeGrantedScopes includeGrantedScopes = IncludeGrantedScopes.None, string codeChallenge = null)
{
var uri = string.IsNullOrEmpty(redirectUri) ? null : new Uri(redirectUri);
return GetAuthorizeUri(oauthResponseType, clientId, uri, state, forceReapprove, disableSignup, requireRole, forceReauthentication, tokenAccessType, scopeList, includeGrantedScopes, codeChallenge);
}
/// <summary>
/// Gets the URI used to start the OAuth2.0 authorization flow.
/// </summary>
/// <param name="oauthResponseType">The grant type requested, either <c>Token</c> or <c>Code</c>.</param>
/// <param name="clientId">The apps key, found in the
/// <a href="https://www.dropbox.com/developers/apps">App Console</a>.</param>
/// <param name="redirectUri">Where to redirect the user after authorization has completed. This must be the exact URI
/// registered in the <a href="https://www.dropbox.com/developers/apps">App Console</a>; even <c>localhost</c>
/// must be listed if it is used for testing. A redirect URI is required for a token flow, but optional for code.
/// If the redirect URI is omitted, the code will be presented directly to the user and they will be invited to enter
/// the information in your app.</param>
/// <param name="state">Up to 500 bytes of arbitrary data that will be passed back to <paramref name="redirectUri"/>.
/// This parameter should be used to protect against cross-site request forgery (CSRF).</param>
/// <param name="forceReapprove">Whether or not to force the user to approve the app again if they've already done so.
/// If <c>false</c> (default), a user who has already approved the application may be automatically redirected to
/// <paramref name="redirectUri"/>If <c>true</c>, the user will not be automatically redirected and will have to approve
/// the app again.</param>
/// <param name="disableSignup">When <c>true</c> (default is <c>false</c>) users will not be able to sign up for a
/// Dropbox account via the authorization page. Instead, the authorization page will show a link to the Dropbox
/// iOS app in the App Store. This is only intended for use when necessary for compliance with App Store policies.</param>
/// <param name="requireRole">If this parameter is specified, the user will be asked to authorize with a particular
/// type of Dropbox account, either work for a team account or personal for a personal account. Your app should still
/// verify the type of Dropbox account after authorization since the user could modify or remove the require_role
/// parameter.</param>
/// <param name="forceReauthentication"> If <c>true</c>, users will be signed out if they are currently signed in.
/// This will make sure the user is brought to a page where they can create a new account or sign in to another account.
/// This should only be used when there is a definite reason to believe that the user needs to sign in to a new or
/// different account.</param>
/// <param name="tokenAccessType">Determines the type of token to request. See <see cref="TokenAccessType" />
/// for information on specific types available. If none is specified, this will use the legacy type.</param>
/// <param name="scopeList">list of scopes to request in base oauth flow. If left blank, will default to all scopes for app.</param>
/// <param name="includeGrantedScopes">which scopes to include from previous grants. Note: if this user has never linked the app, include_granted_scopes must be None.</param>
/// <param name="codeChallenge">If using PKCE, please us the PKCEOAuthFlow object.</param>
/// <returns>The uri of a web page which must be displayed to the user in order to authorize the app.</returns>
public static Uri GetAuthorizeUri(OAuthResponseType oauthResponseType, string clientId, Uri redirectUri = null, string state = null, bool forceReapprove = false, bool disableSignup = false, string requireRole = null, bool forceReauthentication = false, TokenAccessType tokenAccessType = TokenAccessType.Legacy, string[] scopeList = null, IncludeGrantedScopes includeGrantedScopes = IncludeGrantedScopes.None, string codeChallenge = null)
{
if (string.IsNullOrWhiteSpace(clientId))
{
throw new ArgumentNullException("clientId");
}
if (redirectUri == null && oauthResponseType != OAuthResponseType.Code)
{
throw new ArgumentNullException("redirectUri");
}
var queryBuilder = new StringBuilder();
queryBuilder.Append("response_type=");
switch (oauthResponseType)
{
case OAuthResponseType.Token:
queryBuilder.Append("token");
break;
case OAuthResponseType.Code:
queryBuilder.Append("code");
break;
default:
throw new ArgumentOutOfRangeException("oauthResponseType");
}
queryBuilder.Append("&client_id=").Append(Uri.EscapeDataString(clientId));
if (redirectUri != null)
{
queryBuilder.Append("&redirect_uri=").Append(Uri.EscapeDataString(redirectUri.ToString()));
}
if (!string.IsNullOrWhiteSpace(state))
{
queryBuilder.Append("&state=").Append(Uri.EscapeDataString(state));
}
if (forceReapprove)
{
queryBuilder.Append("&force_reapprove=true");
}
if (disableSignup)
{
queryBuilder.Append("&disable_signup=true");
}
if (!string.IsNullOrWhiteSpace(requireRole))
{
queryBuilder.Append("&require_role=").Append(requireRole);
}
if (forceReauthentication)
{
queryBuilder.Append("&force_reauthentication=true");
}
queryBuilder.Append("&token_access_type=").Append(tokenAccessType.ToString().ToLower());
if (scopeList != null)
{
queryBuilder.Append("&scope=").Append(string.Join(" ", scopeList));
}
if (includeGrantedScopes != IncludeGrantedScopes.None)
{
queryBuilder.Append("&include_granted_scopes=").Append(includeGrantedScopes.ToString().ToLower());
}
if (codeChallenge != null)
{
queryBuilder.Append("&code_challenge_method=S256&code_challenge=").Append(codeChallenge);
}
var uriBuilder = new UriBuilder("https://www.dropbox.com/oauth2/authorize")
{
Query = queryBuilder.ToString(),
};
return uriBuilder.Uri;
}
/// <summary>
/// Gets the URI used to start the OAuth2.0 authorization flow which doesn't require a redirect URL.
/// </summary>
/// <param name="clientId">The apps key, found in the
/// <a href="https://www.dropbox.com/developers/apps">App Console</a>.</param>
/// <param name="disableSignup">When <c>true</c> (default is <c>false</c>) users will not be able to sign up for a
/// Dropbox account via the authorization page. Instead, the authorization page will show a link to the Dropbox
/// iOS app in the App Store. This is only intended for use when necessary for compliance with App Store policies.</param>
/// <returns>The uri of a web page which must be displayed to the user in order to authorize the app.</returns>
public static Uri GetAuthorizeUri(string clientId, bool disableSignup = false)
{
return GetAuthorizeUri(OAuthResponseType.Code, clientId, (Uri)null, disableSignup: disableSignup);
}
/// <summary>
/// Generates a PKCE code verifier.
/// </summary>
/// <returns>The PKCE code verifier.</returns>
public static string GeneratePKCECodeVerifier()
{
var bytes = new byte[PKCEVerifierLength];
RandomNumberGenerator.Create().GetBytes(bytes);
return Convert.ToBase64String(bytes)
.TrimEnd('=')
.Replace('+', '-')
.Replace('/', '_')
.Substring(0, 128);
}
/// <summary>
/// Hashes a PKCE code verifier to generate a code challenge.
/// </summary>
/// <param name="codeVerifier">The PKCE code verifier.</param>
/// <returns>The PKCE code challenge.</returns>
public static string GeneratePKCECodeChallenge(string codeVerifier)
{
using (var sha256 = SHA256.Create())
{
var challengeBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(codeVerifier));
return Convert.ToBase64String(challengeBytes)
.TrimEnd('=')
.Replace('+', '-')
.Replace('/', '_');
}
}
/// <summary>
/// Parses the token fragment. When using the OAuth 2.0 token or implicit grant flow, the
/// user will be redirected to a URI with a fragment containing the authorization token.
/// </summary>
/// <param name="redirectedUri">The redirected URI.</param>
/// <returns>The authorization response, containing the access token and uid of the authorized user.</returns>
public static OAuth2Response ParseTokenFragment(Uri redirectedUri)
{
if (redirectedUri == null)
{
throw new ArgumentNullException("redirectedUri");
}
var fragment = redirectedUri.Fragment;
if (string.IsNullOrWhiteSpace(fragment))
{
throw new ArgumentException("The supplied uri doesn't contain a fragment", "redirectedUri");
}
fragment = fragment.TrimStart('#');
string accessToken = null;
string uid = null;
string state = null;
string tokenType = null;
foreach (var pair in fragment.Split('&'))
{
var elements = pair.Split('=');
if (elements.Length != 2)
{
continue;
}
switch (elements[0])
{
case "access_token":
accessToken = Uri.UnescapeDataString(elements[1]);
break;
case "uid":
uid = Uri.UnescapeDataString(elements[1]);
break;
case "state":
state = Uri.UnescapeDataString(elements[1]);
break;
case "token_type":
tokenType = Uri.UnescapeDataString(elements[1]);
break;
}
}
return new OAuth2Response(accessToken, uid, state, tokenType);
}
/// <summary>
/// Processes the second half of the OAuth 2.0 code flow.
/// </summary>
/// <param name="code">The code acquired in the query parameters of the redirect from the initial authorize url.</param>
/// <param name="appKey">The application key, found in the
/// <a href="https://www.dropbox.com/developers/apps">App Console</a>.</param>
/// <param name="appSecret">The application secret, found in the
/// <a href="https://www.dropbox.com/developers/apps">App Console</a> This is optional if using PKCE.</param>
/// <param name="redirectUri">The redirect URI that was provided in the initial authorize URI,
/// this is only used to validate that it matches the original request, it is not used to redirect
/// again.</param>
/// <param name="client">An optional http client instance used to make requests.</param>
/// <param name="codeVerifier">The code verifier for PKCE flow. If using PKCE, please us the PKCEOauthFlow object.</param>
/// <returns>The authorization response, containing the access token and uid of the authorized user.</returns>
public static async Task<OAuth2Response> ProcessCodeFlowAsync(string code, string appKey, string appSecret = null, string redirectUri = null, HttpClient client = null, string codeVerifier = null)
{
if (string.IsNullOrEmpty(code))
{
throw new ArgumentNullException("code");
}
else if (string.IsNullOrEmpty(appKey))
{
throw new ArgumentNullException("appKey");
}
else if (string.IsNullOrEmpty(appSecret) && string.IsNullOrEmpty(codeVerifier))
{
throw new ArgumentNullException("appSecret or codeVerifier");
}
var httpClient = client ?? new HttpClient();
try
{
var parameters = new Dictionary<string, string>
{
{ "code", code },
{ "grant_type", "authorization_code" },
{ "client_id", appKey },
};
if (!string.IsNullOrEmpty(appSecret))
{
parameters["client_secret"] = appSecret;
}
if (!string.IsNullOrEmpty(codeVerifier))
{
parameters["code_verifier"] = codeVerifier;
}
if (!string.IsNullOrEmpty(redirectUri))
{
parameters["redirect_uri"] = redirectUri;
}
var content = new FormUrlEncodedContent(parameters);
var response = await httpClient.PostAsync("https://api.dropbox.com/oauth2/token", content).ConfigureAwait(false);
var raw = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
JObject json = JObject.Parse(raw);
if (response.StatusCode != HttpStatusCode.OK)
{
throw new OAuth2Exception(json["error"].ToString(), json.Value<string>("error_description"));
}
string refreshToken = null;
if (json.Value<string>("refresh_token") != null)
{
refreshToken = json["refresh_token"].ToString();
}
int expiresIn = -1;
if (json.Value<string>("expires_in") != null)
{
expiresIn = json["expires_in"].ToObject<int>();
}
string[] scopeList = null;
if (json.Value<string>("scope") != null)
{
scopeList = json["scope"].ToString().Split(' ');
}
if (expiresIn == -1)
{
return new OAuth2Response(
json["access_token"].ToString(),
json["uid"].ToString(),
null,
json["token_type"].ToString());
}
return new OAuth2Response(
json["access_token"].ToString(),
refreshToken,
json["uid"].ToString(),
null,
json["token_type"].ToString(),
expiresIn,
scopeList);
}
finally
{
if (client == null)
{
httpClient.Dispose();
}
}
}
/// <summary>
/// Processes the second half of the OAuth 2.0 code flow.
/// </summary>
/// <param name="responseUri">The URI to which the user was redirected after the initial authorization request.</param>
/// <param name="appKey">The application key, found in the
/// <a href="https://www.dropbox.com/developers/apps">App Console</a>.</param>
/// <param name="appSecret">The application secret, found in the
/// <a href="https://www.dropbox.com/developers/apps">App Console</a>.</param>
/// <param name="redirectUri">The redirect URI that was provided in the initial authorize URI,
/// this is only used to validate that it matches the original request, it is not used to redirect
/// again.</param>
/// <param name="state">The state parameter (if any) that matches that used in the initial authorize URI.</param>
/// <param name="client">An optional http client instance used to make requests.</param>
/// <param name="codeVerifier">The code verifier for PKCE flow. If using PKCE, please us the PKCEOauthFlow object.</param>
/// <returns>The authorization response, containing the access token and uid of the authorized user.</returns>
public static Task<OAuth2Response> ProcessCodeFlowAsync(Uri responseUri, string appKey, string appSecret, string redirectUri = null, string state = null, HttpClient client = null, string codeVerifier = null)
{
if (responseUri == null)
{
throw new ArgumentNullException("responseUri");
}
else if (string.IsNullOrEmpty(appKey))
{
throw new ArgumentNullException("appKey");
}
else if (string.IsNullOrEmpty(appSecret) && string.IsNullOrEmpty(codeVerifier))
{
throw new ArgumentNullException("appSecret or codeVerifier");
}
var query = responseUri.Query;
if (string.IsNullOrEmpty(query))
{
throw new ArgumentException("The redirect uri is missing expected query arguments.", "responseUri");
}
query = query.TrimStart('?');
string code = null;
foreach (var pair in query.Split('&'))
{
var elements = pair.Split('=');
if (elements.Length != 2)
{
continue;
}
switch (elements[0])
{
case "code":
code = Uri.UnescapeDataString(elements[1]);
break;
case "state":
if (state == null)
{
throw new ArgumentNullException("state", "The state argument is expected.");
}
else if (state != Uri.UnescapeDataString(elements[1]))
{
throw new ArgumentException("The state in the responseUri does not match the provided value.", "responseUri");
}
break;
}
}
if (string.IsNullOrEmpty(code))
{
throw new ArgumentException("The responseUri is missing a code value in the query component.", "responseUri");
}
return ProcessCodeFlowAsync(code, appKey, appSecret, redirectUri, client, codeVerifier);
}
}
/// <summary>
/// Object used to execute OAuth through PKCE
/// Use this object to maintain code verifier and challenge using S256 method.
/// </summary>
public class PKCEOAuthFlow
{
/// <summary>
/// Initializes a new instance of the <see cref="PKCEOAuthFlow"/> class.
/// Default constructor that also generates code verifier and code challenge to be used in PKCE flow.
/// </summary>
public PKCEOAuthFlow()
{
this.CodeVerifier = DropboxOAuth2Helper.GeneratePKCECodeVerifier();
this.CodeChallenge = DropboxOAuth2Helper.GeneratePKCECodeChallenge(this.CodeVerifier);
}
private string CodeVerifier { get; set; }
private string CodeChallenge { get; set; }
/// <summary>
/// Gets the URI used to start the OAuth2.0 authorization flow. Passes in codeChallenge generated in this class.
/// </summary>
/// <param name="oauthResponseType">The grant type requested, either <c>Token</c> or <c>Code</c>.</param>
/// <param name="clientId">The apps key, found in the
/// <a href="https://www.dropbox.com/developers/apps">App Console</a>.</param>
/// <param name="redirectUri">Where to redirect the user after authorization has completed. This must be the exact URI
/// registered in the <a href="https://www.dropbox.com/developers/apps">App Console</a>; even <c>localhost</c>
/// must be listed if it is used for testing. A redirect URI is required for a token flow, but optional for code.
/// If the redirect URI is omitted, the code will be presented directly to the user and they will be invited to enter
/// the information in your app.</param>
/// <param name="state">Up to 500 bytes of arbitrary data that will be passed back to <paramref name="redirectUri"/>.
/// This parameter should be used to protect against cross-site request forgery (CSRF).</param>
/// <param name="forceReapprove">Whether or not to force the user to approve the app again if they've already done so.
/// If <c>false</c> (default), a user who has already approved the application may be automatically redirected to
/// <paramref name="redirectUri"/>If <c>true</c>, the user will not be automatically redirected and will have to approve
/// the app again.</param>
/// <param name="disableSignup">When <c>true</c> (default is <c>false</c>) users will not be able to sign up for a
/// Dropbox account via the authorization page. Instead, the authorization page will show a link to the Dropbox
/// iOS app in the App Store. This is only intended for use when necessary for compliance with App Store policies.</param>
/// <param name="requireRole">If this parameter is specified, the user will be asked to authorize with a particular
/// type of Dropbox account, either work for a team account or personal for a personal account. Your app should still
/// verify the type of Dropbox account after authorization since the user could modify or remove the require_role
/// parameter.</param>
/// <param name="forceReauthentication"> If <c>true</c>, users will be signed out if they are currently signed in.
/// This will make sure the user is brought to a page where they can create a new account or sign in to another account.
/// This should only be used when there is a definite reason to believe that the user needs to sign in to a new or
/// different account.</param>
/// <param name="tokenAccessType">Determines the type of token to request. See <see cref="TokenAccessType" />
/// for information on specific types available. If none is specified, this will use the legacy type.</param>
/// <param name="scopeList">list of scopes to request in base oauth flow. If left blank, will default to all scopes for app.</param>
/// <param name="includeGrantedScopes">which scopes to include from previous grants. Note: if this user has never linked the app, include_granted_scopes must be None.</param>
/// <returns>The uri of a web page which must be displayed to the user in order to authorize the app.</returns>
public Uri GetAuthorizeUri(OAuthResponseType oauthResponseType, string clientId, string redirectUri = null, string state = null, bool forceReapprove = false, bool disableSignup = false, string requireRole = null, bool forceReauthentication = false, TokenAccessType tokenAccessType = TokenAccessType.Legacy, string[] scopeList = null, IncludeGrantedScopes includeGrantedScopes = IncludeGrantedScopes.None)
{
return DropboxOAuth2Helper.GetAuthorizeUri(oauthResponseType, clientId, redirectUri, state, forceReapprove, disableSignup, requireRole, forceReauthentication, tokenAccessType, scopeList, includeGrantedScopes, this.CodeChallenge);
}
/// <summary>
/// Processes the second half of the OAuth 2.0 code flow. Uses the codeVerifier created in this class to execute the second half.
/// </summary>
/// <param name="code">The code acquired in the query parameters of the redirect from the initial authorize url.</param>
/// <param name="appKey">The application key, found in the
/// <a href="https://www.dropbox.com/developers/apps">App Console</a>.</param>
/// <param name="redirectUri">The redirect URI that was provided in the initial authorize URI,
/// this is only used to validate that it matches the original request, it is not used to redirect
/// again.</param>
/// <param name="client">An optional http client instance used to make requests.</param>
/// <returns>The authorization response, containing the access token and uid of the authorized user.</returns>
public Task<OAuth2Response> ProcessCodeFlowAsync(string code, string appKey, string redirectUri = null, HttpClient client = null)
{
return DropboxOAuth2Helper.ProcessCodeFlowAsync(code, appKey, null, redirectUri, client, this.CodeVerifier);
}
/// <summary>
/// Processes the second half of the OAuth 2.0 code flow. Uses the codeVerifier created in this class to execute second half.
/// </summary>
/// <param name="responseUri">The URI to which the user was redirected after the initial authorization request.</param>
/// <param name="appKey">The application key, found in the
/// <a href="https://www.dropbox.com/developers/apps">App Console</a>.</param>
/// <param name="redirectUri">The redirect URI that was provided in the initial authorize URI,
/// this is only used to validate that it matches the original request, it is not used to redirect
/// again.</param>
/// <param name="state">The state parameter (if any) that matches that used in the initial authorize URI.</param>
/// <param name="client">An optional http client instance used to make requests.</param>
/// <returns>The authorization response, containing the access token and uid of the authorized user.</returns>
public Task<OAuth2Response> ProcessCodeFlowAsync(Uri responseUri, string appKey, string redirectUri = null, string state = null, HttpClient client = null)
{
return DropboxOAuth2Helper.ProcessCodeFlowAsync(responseUri, appKey, null, redirectUri, state, client, this.CodeVerifier);
}
}
/// <summary>
/// Contains the parameters passed in a successful authorization response.
/// </summary>
public sealed class OAuth2Response
{
/// <summary>
/// Initializes a new instance of the <see cref="OAuth2Response"/> class.
/// </summary>
/// <param name="accessToken">The access token.</param>
/// <param name="refreshToken">The refresh token.</param>
/// <param name="uid">The uid.</param>
/// <param name="state">The state.</param>
/// <param name="tokenType">The token type.</param>
/// <param name="expiresIn">The duration until token expiry in seconds.</param>
/// <param name="scopeList">The scope list.</param>
internal OAuth2Response(string accessToken, string refreshToken, string uid, string state, string tokenType, int expiresIn, string[] scopeList)
{
if (string.IsNullOrEmpty(accessToken) || uid == null)
{
throw new ArgumentException("Invalid OAuth 2.0 response, missing access_token and/or uid.");
}
this.AccessToken = accessToken;
this.Uid = uid;
this.State = state;
this.TokenType = tokenType;
this.RefreshToken = refreshToken;
this.ExpiresAt = DateTime.Now.AddSeconds(expiresIn);
this.ScopeList = scopeList;
}
/// <summary>
/// Initializes a new instance of the <see cref="OAuth2Response"/> class.
/// </summary>
/// <param name="accessToken">The access_token.</param>
/// <param name="uid">The uid.</param>
/// <param name="state">The state.</param>
/// <param name="tokenType">The token_type.</param>
internal OAuth2Response(string accessToken, string uid, string state, string tokenType)
{
if (string.IsNullOrEmpty(accessToken) || uid == null)
{
throw new ArgumentException("Invalid OAuth 2.0 response, missing access_token and/or uid.");
}
this.AccessToken = accessToken;
this.Uid = uid;
this.State = state;
this.TokenType = tokenType;
this.RefreshToken = null;
this.ExpiresAt = null;
this.ScopeList = null;
}
/// <summary>
/// Gets the access token, a token which can be used to make calls to the Dropbox API.
/// </summary>
/// <remarks>
/// Pass this as the <c>oauth2AccessToken</c> argument when creating an instance
/// of <see cref="DropboxClient"/>.</remarks>
/// <value>
/// A token which can be used to make calls to the Dropbox API.
/// </value>
public string AccessToken { get; private set; }
/// <summary>
/// Gets the Dropbox user ID of the authorized user.
/// </summary>
/// <value>
/// The Dropbox user ID of the authorized user.
/// </value>
public string Uid { get; private set; }
/// <summary>
/// Gets the state content, if any, originally passed to the authorize URI.
/// </summary>
/// <value>
/// The state content, if any, originally passed to the authorize URI.
/// </value>
public string State { get; private set; }
/// <summary>
/// Gets the type of the token, which will always be <c>bearer</c> if set.
/// </summary>
/// <value>
/// This will always be <c>bearer</c> if set.
/// </value>
public string TokenType { get; private set; }
/// <summary>
/// Gets the refresh token, if offline or online access type was selected.
/// </summary>
public string RefreshToken { get; private set; }
/// <summary>
/// Gets the time of expiration of the access token, if the token will expire.
/// This is only filled if offline or online access type was selected.
/// </summary>
public DateTime? ExpiresAt { get; private set; }
/// <summary>
/// Gets list of scopes this oauth2 request granted the user.
/// </summary>
public string[] ScopeList { get; private set; }
}
/// <summary>
/// Exception when error occurs during oauth2 flow.
/// </summary>
public sealed class OAuth2Exception : Exception
{
/// <summary>
/// Initializes a new instance of the <see cref="OAuth2Exception"/> class.
/// </summary>
/// <param name="message">The message.</param>
/// <param name="errorDescription">The error description.</param>
public OAuth2Exception(string message, string errorDescription = null)
: base(message)
{
this.ErrorDescription = errorDescription;
}
/// <summary>
/// Gets the error description.
/// </summary>
public string ErrorDescription { get; private set; }
}
}