Skip to content

Commit e266c1e

Browse files
rogebrdBrad Rogers
andauthored
Add Support for Short-Lived Tokens (#119)
- Add option for requesting specific scopes and including already granted scopes in oauth flow - Add support for requesting specific scopes on token refresh - Expose refresh method publicly to dropbox client - Update tests to include downscoping - Update oauth example to support different scope examples Co-authored-by: Brad Rogers <[email protected]>
1 parent e26f441 commit e266c1e

File tree

7 files changed

+269
-121
lines changed

7 files changed

+269
-121
lines changed

dropbox-sdk-dotnet/Dropbox.Api.Tests/DropboxApiTests.cs

Lines changed: 99 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ public async Task TestGetAuthorizationUri()
135135
bool[] disableSignups = new[] {false, true};
136136
string[] requireRoles = new[] {"", "role"};
137137
bool[] forceReauthentications = new[] {false, true};
138+
List<String[]> scopes = new List<String[]>();
139+
scopes.Add(null);
140+
scopes.Add(new String[]{ "files.metadata.read", "files.content.read" });
141+
IncludeGrantedScopes[] includeGrantedScopes = new[] {IncludeGrantedScopes.None, IncludeGrantedScopes.User, IncludeGrantedScopes.Team};
142+
138143
TokenAccessType[] tokenAccessTypes = new[]
139144
{TokenAccessType.Legacy, TokenAccessType.Offline, TokenAccessType.Online};
140145
foreach (string redirectUri in redirectUris)
@@ -151,76 +156,101 @@ public async Task TestGetAuthorizationUri()
151156
{
152157
foreach (var tokenAccessType in tokenAccessTypes)
153158
{
154-
var authUri = DropboxOAuth2Helper.GetAuthorizeUri(OAuthResponseType.Code,
155-
clientId, redirectUri, state, forceReapprove, disableSignup,
156-
requireRole, forceReauthentication, tokenAccessType).ToString();
157-
158-
Assert.IsTrue(authUri.StartsWith("https://www.dropbox.com/oauth2/authorize"));
159-
Assert.IsTrue(authUri.Contains("response_type=code"));
160-
Assert.IsTrue(authUri.Contains("client_id=" + clientId));
161-
162-
if (String.IsNullOrWhiteSpace(state))
163-
{
164-
Assert.IsFalse(authUri.Contains("&state="));
165-
}
166-
else
167-
{
168-
Assert.IsTrue(authUri.Contains("&state=" + state));
169-
}
170-
171-
if (String.IsNullOrWhiteSpace(redirectUri))
172-
{
173-
Assert.IsFalse(authUri.Contains("&redirect_uri="));
174-
}
175-
else
176-
{
177-
Assert.IsTrue(authUri.Contains("&redirect_uri=" + Uri.EscapeDataString(redirectUri)));
178-
}
179-
180-
if (forceReapprove)
181-
{
182-
Assert.IsTrue(authUri.Contains("&force_reapprove=true"));
183-
}
184-
else
185-
{
186-
Assert.IsFalse(authUri.Contains("&force_reapprove="));
187-
}
188-
189-
if (disableSignup)
159+
foreach (var scope in scopes)
190160
{
191-
Assert.IsTrue(authUri.Contains("&disable_signup=true"));
192-
}
193-
else
194-
{
195-
Assert.IsFalse(authUri.Contains("&disable_signup="));
196-
}
197-
198-
if (String.IsNullOrWhiteSpace(requireRole))
199-
{
200-
Assert.IsFalse(authUri.Contains("&require_role="));
201-
}
202-
else
203-
{
204-
Assert.IsTrue(authUri.Contains("&require_role=" + requireRole));
205-
}
206-
207-
if (forceReauthentication)
208-
{
209-
Assert.IsTrue(authUri.Contains("&force_reauthentication=true"));
210-
}
211-
else
212-
{
213-
Assert.IsFalse(authUri.Contains("&force_reauthentication="));
214-
}
215-
216-
if (tokenAccessType != TokenAccessType.Legacy)
217-
{
218-
Assert.IsTrue(authUri.Contains("&token_access_type=" +
219-
tokenAccessType.ToString().ToLower()));
220-
}
221-
else
222-
{
223-
Assert.IsFalse(authUri.Contains("&token_access_type="));
161+
foreach (var includeGrantedScope in includeGrantedScopes)
162+
{
163+
var authUri = DropboxOAuth2Helper.GetAuthorizeUri(OAuthResponseType.Code,
164+
clientId, redirectUri, state, forceReapprove, disableSignup,
165+
requireRole, forceReauthentication, tokenAccessType, scope, includeGrantedScope).ToString();
166+
167+
Assert.IsTrue(authUri.StartsWith("https://www.dropbox.com/oauth2/authorize"));
168+
Assert.IsTrue(authUri.Contains("response_type=code"));
169+
Assert.IsTrue(authUri.Contains("client_id=" + clientId));
170+
171+
if (String.IsNullOrWhiteSpace(state))
172+
{
173+
Assert.IsFalse(authUri.Contains("&state="));
174+
}
175+
else
176+
{
177+
Assert.IsTrue(authUri.Contains("&state=" + state));
178+
}
179+
180+
if (String.IsNullOrWhiteSpace(redirectUri))
181+
{
182+
Assert.IsFalse(authUri.Contains("&redirect_uri="));
183+
}
184+
else
185+
{
186+
Assert.IsTrue(authUri.Contains("&redirect_uri=" + Uri.EscapeDataString(redirectUri)));
187+
}
188+
189+
if (forceReapprove)
190+
{
191+
Assert.IsTrue(authUri.Contains("&force_reapprove=true"));
192+
}
193+
else
194+
{
195+
Assert.IsFalse(authUri.Contains("&force_reapprove="));
196+
}
197+
198+
if (disableSignup)
199+
{
200+
Assert.IsTrue(authUri.Contains("&disable_signup=true"));
201+
}
202+
else
203+
{
204+
Assert.IsFalse(authUri.Contains("&disable_signup="));
205+
}
206+
207+
if (String.IsNullOrWhiteSpace(requireRole))
208+
{
209+
Assert.IsFalse(authUri.Contains("&require_role="));
210+
}
211+
else
212+
{
213+
Assert.IsTrue(authUri.Contains("&require_role=" + requireRole));
214+
}
215+
216+
if (forceReauthentication)
217+
{
218+
Assert.IsTrue(authUri.Contains("&force_reauthentication=true"));
219+
}
220+
else
221+
{
222+
Assert.IsFalse(authUri.Contains("&force_reauthentication="));
223+
}
224+
225+
if (tokenAccessType != TokenAccessType.Legacy)
226+
{
227+
Assert.IsTrue(authUri.Contains("&token_access_type=" +
228+
tokenAccessType.ToString().ToLower()));
229+
}
230+
else
231+
{
232+
Assert.IsFalse(authUri.Contains("&token_access_type="));
233+
}
234+
235+
if (scope != null)
236+
{
237+
Assert.IsTrue(authUri.Contains("&scope=" + String.Join(" ", scope)));
238+
}
239+
else
240+
{
241+
Assert.IsFalse(authUri.Contains("&scope="));
242+
}
243+
244+
if (includeGrantedScope != IncludeGrantedScopes.None)
245+
{
246+
Assert.IsTrue(authUri.Contains("&include_granted_scopes=" +
247+
includeGrantedScope.ToString().ToLower()));
248+
}
249+
else
250+
{
251+
Assert.IsFalse(authUri.Contains("&include_granted_scopes="));
252+
}
253+
}
224254
}
225255
}
226256
}

dropbox-sdk-dotnet/Dropbox.Api/AppProperties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
[assembly: AssemblyCulture("")]
1919

2020
[assembly: AssemblyVersion("4.0.0")]
21-
[assembly: AssemblyFileVersion("4.0.7452")]
21+
[assembly: AssemblyFileVersion("4.0.7454")]
2222

2323
#if DEBUG
2424
[assembly: InternalsVisibleTo("Dropbox.Api.Tests")]

dropbox-sdk-dotnet/Dropbox.Api/DropboxClient.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
namespace Dropbox.Api
88
{
99
using System;
10-
10+
using System.Threading.Tasks;
1111
using Dropbox.Api.Common;
1212

1313
/// <summary>
@@ -186,6 +186,16 @@ public DropboxClient WithPathRoot(PathRoot pathRoot)
186186
return new DropboxClient(this.requestHandler.WithPathRoot(pathRoot));
187187
}
188188

189+
/// <summary>
190+
/// Refreshes access token regardless of if existing token is expired
191+
/// </summary>
192+
/// <param name="scopeList">subset of scopes to refresh token with, or null to refresh with all scopes</param>
193+
/// <returns>true if token is successfully refreshed, false otherwise</returns>
194+
public Task<bool> RefreshAccessToken(string[] scopeList)
195+
{
196+
return this.requestHandler.RefreshAccessToken(scopeList);
197+
}
198+
189199
/// <summary>
190200
/// Initializes a new instance of the <see cref="T:Dropbox.Api.DropboxClient"/> class.
191201
/// </summary>

0 commit comments

Comments
 (0)