Skip to content

Commit e26f441

Browse files
Brad Rogersrogebrd
authored andcommitted
Add Support for Short-Lived Tokens
Add token_access_type to oauth flow - Legacy - default - the current long lived token flow - Online - requests only a short-lived access token - Offline - requests both a short-lived access token and a refresh token Add refresh check to each API call Add refresh check on client creation NOTE: Non-Legacy token_access_types currently require beta access, please reach out to Dropbox support if this is something you are interested in
1 parent dabb96c commit e26f441

File tree

19 files changed

+1032
-37
lines changed

19 files changed

+1032
-37
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Visual studio stuff
44
*.suo
55
*.user
6+
.vs/
67

78
#ignore build contents
89
[dD]ebug/

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

Lines changed: 146 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,21 @@ public class DropboxApiTests
3333
/// </summary>
3434
public static string UserAccessToken;
3535

36+
/// <summary>
37+
/// The user refresh token.
38+
/// </summary>
39+
public static string UserRefreshToken;
40+
41+
/// <summary>
42+
/// The app key
43+
/// </summary>
44+
public static string AppKey;
45+
46+
/// <summary>
47+
/// The app secret
48+
/// </summary>
49+
public static string AppSecret;
50+
3651
/// <summary>
3752
/// The Dropbox client.
3853
/// </summary>
@@ -53,16 +68,18 @@ public class DropboxApiTests
5368
[ClassInitialize]
5469
public static void Initialize(TestContext context)
5570
{
71+
72+
AppKey = context.Properties["appKey"].ToString();
73+
AppSecret = context.Properties["appSecret"].ToString();
74+
75+
UserRefreshToken = context.Properties["userRefreshToken"].ToString();
5676
UserAccessToken = context.Properties["userAccessToken"].ToString();
5777
Client = new DropboxClient(UserAccessToken);
5878

5979
var teamToken = context.Properties["teamAccessToken"].ToString();
6080
TeamClient = new DropboxTeamClient(teamToken);
61-
62-
var appKey = context.Properties["appKey"].ToString();
63-
var appSecret = context.Properties["appSecret"].ToString();
64-
65-
AppClient = new DropboxAppClient(appKey, appSecret);
81+
82+
AppClient = new DropboxAppClient(AppKey, AppSecret);
6683
}
6784

6885
[TestInitialize]
@@ -91,6 +108,130 @@ public void Cleanup()
91108
}
92109
}
93110

111+
/// <summary>
112+
/// Tests creating a client with only refresh token and
113+
/// ensuring the client refreshed the token before making a call
114+
/// </summary>
115+
/// <returns>The <see cref="Task" /></returns>
116+
[TestMethod]
117+
public async Task TestRefreshClient()
118+
{
119+
var client = new DropboxClient(UserRefreshToken, AppKey, AppSecret);
120+
var result = await client.Users.GetCurrentAccountAsync();
121+
Assert.IsNotNull(result.Email);
122+
}
123+
124+
/// <summary>
125+
/// Test get authorization url
126+
/// </summary>
127+
/// <returns>The <see cref="Task"/></returns>
128+
[TestMethod]
129+
public async Task TestGetAuthorizationUri()
130+
{
131+
string clientId = "myclientid";
132+
string[] redirectUris = new[] {"", "http://127.0.0.1:52475/"};
133+
string[] states = new[] {"", "state"};
134+
bool[] forceReapproves = new[] {false, true};
135+
bool[] disableSignups = new[] {false, true};
136+
string[] requireRoles = new[] {"", "role"};
137+
bool[] forceReauthentications = new[] {false, true};
138+
TokenAccessType[] tokenAccessTypes = new[]
139+
{TokenAccessType.Legacy, TokenAccessType.Offline, TokenAccessType.Online};
140+
foreach (string redirectUri in redirectUris)
141+
{
142+
foreach (var state in states)
143+
{
144+
foreach (var forceReapprove in forceReapproves)
145+
{
146+
foreach (var disableSignup in disableSignups)
147+
{
148+
foreach (var requireRole in requireRoles)
149+
{
150+
foreach (var forceReauthentication in forceReauthentications)
151+
{
152+
foreach (var tokenAccessType in tokenAccessTypes)
153+
{
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)
190+
{
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="));
224+
}
225+
}
226+
}
227+
}
228+
}
229+
}
230+
}
231+
}
232+
}
233+
234+
94235
/// <summary>
95236
/// Test get metadata.
96237
/// </summary>

dropbox-sdk-dotnet/Dropbox.Api.Tests/dropbox.runsettings

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
<TestRunParameters>
44
<Parameter name="userAccessToken" value="" />
55
<Parameter name="teamAccessToken" value="" />
6+
<Parameter name="userRefreshToken" value="" />
67
<Parameter name="appKey" value="" />
78
<Parameter name="appSecret" value="" />
89
</TestRunParameters>
9-
</RunSettings>
10+
</RunSettings>

dropbox-sdk-dotnet/Dropbox.Api.sln

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Microsoft Visual Studio Solution File, Format Version 12.00
2-
# Visual Studio 15
3-
VisualStudioVersion = 15.0.28307.168
2+
# Visual Studio Version 16
3+
VisualStudioVersion = 16.0.30011.22
44
MinimumVisualStudioVersion = 10.0.40219.1
55
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleBlogDemo", "Examples\SimpleBlogDemo\SimpleBlogDemo.csproj", "{8772EB1E-3019-4FB3-9059-67681693E873}"
66
EndProject
@@ -24,6 +24,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleBusinessDashboard", "
2424
EndProject
2525
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dropbox.Api.Tests", "Dropbox.Api.Tests\Dropbox.Api.Tests.csproj", "{0FDF769B-43F8-4907-98D5-A413BD9DE62D}"
2626
EndProject
27+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OauthTest", "Examples\OauthBasic\OauthTest.csproj", "{AC53CD8C-710C-4019-8B62-F819F0D6EF38}"
28+
EndProject
2729
Global
2830
GlobalSection(SharedMSBuildProjectFiles) = preSolution
2931
Examples\UniversalDemo\UniversalDemo\UniversalDemo.Shared\UniversalDemo.Shared.projitems*{51d9899c-31d6-4c22-b894-e9498cb90577}*SharedItemsImports = 4
@@ -54,13 +56,19 @@ Global
5456
{33A16535-A722-4679-BD9F-A78576988519}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
5557
{33A16535-A722-4679-BD9F-A78576988519}.Debug|Any CPU.Build.0 = Debug|Any CPU
5658
{33A16535-A722-4679-BD9F-A78576988519}.Debug|ARM.ActiveCfg = Debug|Any CPU
59+
{33A16535-A722-4679-BD9F-A78576988519}.Debug|ARM.Build.0 = Debug|Any CPU
5760
{33A16535-A722-4679-BD9F-A78576988519}.Debug|x64.ActiveCfg = Debug|Any CPU
61+
{33A16535-A722-4679-BD9F-A78576988519}.Debug|x64.Build.0 = Debug|Any CPU
5862
{33A16535-A722-4679-BD9F-A78576988519}.Debug|x86.ActiveCfg = Debug|Any CPU
63+
{33A16535-A722-4679-BD9F-A78576988519}.Debug|x86.Build.0 = Debug|Any CPU
5964
{33A16535-A722-4679-BD9F-A78576988519}.Release|Any CPU.ActiveCfg = Release|Any CPU
6065
{33A16535-A722-4679-BD9F-A78576988519}.Release|Any CPU.Build.0 = Release|Any CPU
6166
{33A16535-A722-4679-BD9F-A78576988519}.Release|ARM.ActiveCfg = Release|Any CPU
67+
{33A16535-A722-4679-BD9F-A78576988519}.Release|ARM.Build.0 = Release|Any CPU
6268
{33A16535-A722-4679-BD9F-A78576988519}.Release|x64.ActiveCfg = Release|Any CPU
69+
{33A16535-A722-4679-BD9F-A78576988519}.Release|x64.Build.0 = Release|Any CPU
6370
{33A16535-A722-4679-BD9F-A78576988519}.Release|x86.ActiveCfg = Release|Any CPU
71+
{33A16535-A722-4679-BD9F-A78576988519}.Release|x86.Build.0 = Release|Any CPU
6472
{680E8F6F-AA67-4912-AE20-F89E482DC2CD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
6573
{680E8F6F-AA67-4912-AE20-F89E482DC2CD}.Debug|Any CPU.Build.0 = Debug|Any CPU
6674
{680E8F6F-AA67-4912-AE20-F89E482DC2CD}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
@@ -171,6 +179,22 @@ Global
171179
{0FDF769B-43F8-4907-98D5-A413BD9DE62D}.Release|ARM.ActiveCfg = Release|Any CPU
172180
{0FDF769B-43F8-4907-98D5-A413BD9DE62D}.Release|x64.ActiveCfg = Release|Any CPU
173181
{0FDF769B-43F8-4907-98D5-A413BD9DE62D}.Release|x86.ActiveCfg = Release|Any CPU
182+
{AC53CD8C-710C-4019-8B62-F819F0D6EF38}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
183+
{AC53CD8C-710C-4019-8B62-F819F0D6EF38}.Debug|Any CPU.Build.0 = Debug|Any CPU
184+
{AC53CD8C-710C-4019-8B62-F819F0D6EF38}.Debug|ARM.ActiveCfg = Debug|Any CPU
185+
{AC53CD8C-710C-4019-8B62-F819F0D6EF38}.Debug|ARM.Build.0 = Debug|Any CPU
186+
{AC53CD8C-710C-4019-8B62-F819F0D6EF38}.Debug|x64.ActiveCfg = Debug|Any CPU
187+
{AC53CD8C-710C-4019-8B62-F819F0D6EF38}.Debug|x64.Build.0 = Debug|Any CPU
188+
{AC53CD8C-710C-4019-8B62-F819F0D6EF38}.Debug|x86.ActiveCfg = Debug|Any CPU
189+
{AC53CD8C-710C-4019-8B62-F819F0D6EF38}.Debug|x86.Build.0 = Debug|Any CPU
190+
{AC53CD8C-710C-4019-8B62-F819F0D6EF38}.Release|Any CPU.ActiveCfg = Release|Any CPU
191+
{AC53CD8C-710C-4019-8B62-F819F0D6EF38}.Release|Any CPU.Build.0 = Release|Any CPU
192+
{AC53CD8C-710C-4019-8B62-F819F0D6EF38}.Release|ARM.ActiveCfg = Release|Any CPU
193+
{AC53CD8C-710C-4019-8B62-F819F0D6EF38}.Release|ARM.Build.0 = Release|Any CPU
194+
{AC53CD8C-710C-4019-8B62-F819F0D6EF38}.Release|x64.ActiveCfg = Release|Any CPU
195+
{AC53CD8C-710C-4019-8B62-F819F0D6EF38}.Release|x64.Build.0 = Release|Any CPU
196+
{AC53CD8C-710C-4019-8B62-F819F0D6EF38}.Release|x86.ActiveCfg = Release|Any CPU
197+
{AC53CD8C-710C-4019-8B62-F819F0D6EF38}.Release|x86.Build.0 = Release|Any CPU
174198
EndGlobalSection
175199
GlobalSection(SolutionProperties) = preSolution
176200
HideSolutionNode = FALSE

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.7413")]
21+
[assembly: AssemblyFileVersion("4.0.7452")]
2222

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public DropboxAppClient(string appKey, string appSecret)
3232
/// <param name="appSecret">The Dropbox app secret (e.g. consumer secret in OAuth).</param>
3333
/// <param name="config">The <see cref="DropboxClientConfig"/>.</param>
3434
public DropboxAppClient(string appKey, string appSecret, DropboxClientConfig config)
35-
: this(new DropboxRequestHandlerOptions(config, GetBasicAuthHeader(appKey, appSecret)))
35+
: this(new DropboxRequestHandlerOptions(config, GetBasicAuthHeader(appKey, appSecret), null, null, null, null))
3636
{
3737
}
3838

0 commit comments

Comments
 (0)