Skip to content

Commit bbed058

Browse files
Normalize empty OAuth tokens (#400)
1 parent 5e15433 commit bbed058

4 files changed

Lines changed: 123 additions & 14 deletions

File tree

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
//-----------------------------------------------------------------------------
2+
// <copyright file="DropboxClientTokenTests.cs" company="Dropbox Inc">
3+
// Copyright (c) Dropbox Inc. All rights reserved.
4+
// </copyright>
5+
//-----------------------------------------------------------------------------
6+
7+
namespace Dropbox.Api.Unit.Tests
8+
{
9+
using System;
10+
using Microsoft.VisualStudio.TestTools.UnitTesting;
11+
12+
/// <summary>
13+
/// Tests token validation and normalization for Dropbox clients.
14+
/// </summary>
15+
[TestClass]
16+
public class DropboxClientTokenTests
17+
{
18+
/// <summary>
19+
/// Verifies an empty access token is rejected by the user client.
20+
/// </summary>
21+
[TestMethod]
22+
public void TestDropboxClientRejectsEmptyAccessToken()
23+
{
24+
Assert.ThrowsExactly<ArgumentException>(() => CreateDropboxClientWithAccessToken(string.Empty));
25+
}
26+
27+
/// <summary>
28+
/// Verifies an empty refresh token is rejected by the user client.
29+
/// </summary>
30+
[TestMethod]
31+
public void TestDropboxClientRejectsEmptyRefreshToken()
32+
{
33+
Assert.ThrowsExactly<ArgumentException>(() => CreateDropboxClientWithRefreshToken(string.Empty));
34+
}
35+
36+
/// <summary>
37+
/// Verifies an empty access token is rejected by the team client.
38+
/// </summary>
39+
[TestMethod]
40+
public void TestDropboxTeamClientRejectsEmptyAccessToken()
41+
{
42+
Assert.ThrowsExactly<ArgumentException>(() => CreateDropboxTeamClientWithAccessToken(string.Empty));
43+
}
44+
45+
/// <summary>
46+
/// Verifies an empty refresh token is rejected by the team client.
47+
/// </summary>
48+
[TestMethod]
49+
public void TestDropboxTeamClientRejectsEmptyRefreshToken()
50+
{
51+
Assert.ThrowsExactly<ArgumentException>(() => CreateDropboxTeamClientWithRefreshToken(string.Empty));
52+
}
53+
54+
/// <summary>
55+
/// Verifies empty token strings are normalized to missing tokens.
56+
/// </summary>
57+
[TestMethod]
58+
public void TestRequestHandlerOptionsNormalizeEmptyTokens()
59+
{
60+
var emptyAccessTokenOptions = new DropboxRequestHandlerOptions(
61+
new DropboxClientConfig(),
62+
string.Empty,
63+
"refresh-token",
64+
null,
65+
"app-key",
66+
null);
67+
var emptyRefreshTokenOptions = new DropboxRequestHandlerOptions(
68+
new DropboxClientConfig(),
69+
"access-token",
70+
string.Empty,
71+
null,
72+
"app-key",
73+
null);
74+
75+
Assert.IsNull(emptyAccessTokenOptions.OAuth2AccessToken);
76+
Assert.AreEqual("refresh-token", emptyAccessTokenOptions.OAuth2RefreshToken);
77+
Assert.AreEqual("access-token", emptyRefreshTokenOptions.OAuth2AccessToken);
78+
Assert.IsNull(emptyRefreshTokenOptions.OAuth2RefreshToken);
79+
}
80+
81+
private static void CreateDropboxClientWithAccessToken(string token)
82+
{
83+
using (var client = new DropboxClient(token))
84+
{
85+
}
86+
}
87+
88+
private static void CreateDropboxClientWithRefreshToken(string token)
89+
{
90+
using (var client = new DropboxClient(token, "app-key"))
91+
{
92+
}
93+
}
94+
95+
private static void CreateDropboxTeamClientWithAccessToken(string token)
96+
{
97+
using (var client = new DropboxTeamClient(token))
98+
{
99+
}
100+
}
101+
102+
private static void CreateDropboxTeamClientWithRefreshToken(string token)
103+
{
104+
using (var client = new DropboxTeamClient(token, "app-key"))
105+
{
106+
}
107+
}
108+
}
109+
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ public DropboxClient(string oauth2AccessToken, string oauth2RefreshToken, DateTi
128128
public DropboxClient(string oauth2AccessToken, string oauth2RefreshToken, DateTime oauth2AccessTokenExpiresAt, string appKey, DropboxClientConfig config)
129129
: this(new DropboxRequestHandlerOptions(config, oauth2AccessToken, oauth2RefreshToken, oauth2AccessTokenExpiresAt, appKey, null))
130130
{
131-
if (oauth2AccessToken == null && oauth2RefreshToken == null)
131+
if (string.IsNullOrEmpty(oauth2AccessToken) && string.IsNullOrEmpty(oauth2RefreshToken))
132132
{
133-
throw new ArgumentException("Cannot pass in both null access and refresh token");
133+
throw new ArgumentException("Access token and refresh token cannot both be null or empty.");
134134
}
135135
}
136136

@@ -158,9 +158,9 @@ public DropboxClient(string oauth2AccessToken, string oauth2RefreshToken, DateTi
158158
public DropboxClient(string oauth2AccessToken, string oauth2RefreshToken, DateTime oauth2AccessTokenExpiresAt, string appKey, string appSecret, DropboxClientConfig config)
159159
: this(new DropboxRequestHandlerOptions(config, oauth2AccessToken, oauth2RefreshToken, oauth2AccessTokenExpiresAt, appKey, appSecret))
160160
{
161-
if (oauth2AccessToken == null && oauth2RefreshToken == null)
161+
if (string.IsNullOrEmpty(oauth2AccessToken) && string.IsNullOrEmpty(oauth2RefreshToken))
162162
{
163-
throw new ArgumentException("Cannot pass in both null access and refresh token");
163+
throw new ArgumentException("Access token and refresh token cannot both be null or empty.");
164164
}
165165
}
166166

@@ -175,9 +175,9 @@ public DropboxClient(string oauth2AccessToken, string oauth2RefreshToken, DateTi
175175
public DropboxClient(string oauth2AccessToken, string oauth2RefreshToken, string appKey, string appSecret, DropboxClientConfig config)
176176
: this(new DropboxRequestHandlerOptions(config, oauth2AccessToken, oauth2RefreshToken, null, appKey, appSecret))
177177
{
178-
if (oauth2AccessToken == null && oauth2RefreshToken == null)
178+
if (string.IsNullOrEmpty(oauth2AccessToken) && string.IsNullOrEmpty(oauth2RefreshToken))
179179
{
180-
throw new ArgumentException("Cannot pass in both null access and refresh token");
180+
throw new ArgumentException("Access token and refresh token cannot both be null or empty.");
181181
}
182182
}
183183

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,8 +1164,8 @@ public DropboxRequestHandlerOptions(
11641164
this.LongPollHttpClient = longPollHttpClient;
11651165
this.DisposeUploadStream = disposeUploadStream;
11661166
this.AutoContentHash = autoContentHash;
1167-
this.OAuth2AccessToken = oauth2AccessToken;
1168-
this.OAuth2RefreshToken = oauth2RefreshToken;
1167+
this.OAuth2AccessToken = string.IsNullOrEmpty(oauth2AccessToken) ? null : oauth2AccessToken;
1168+
this.OAuth2RefreshToken = string.IsNullOrEmpty(oauth2RefreshToken) ? null : oauth2RefreshToken;
11691169
this.OAuth2AccessTokenExpiresAt = oauth2AccessTokenExpiresAt;
11701170
this.AppKey = appKey;
11711171
this.AppSecret = appSecret;

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,9 @@ public DropboxTeamClient(string oauth2AccessToken, string oauth2RefreshToken, Da
132132
public DropboxTeamClient(string oauth2AccessToken, string oauth2RefreshToken, DateTime oauth2AccessTokenExpiresAt, string appKey, DropboxClientConfig config)
133133
: this(new DropboxRequestHandlerOptions(config, oauth2AccessToken, oauth2RefreshToken, oauth2AccessTokenExpiresAt, appKey, null))
134134
{
135-
if (oauth2AccessToken == null && oauth2RefreshToken == null)
135+
if (string.IsNullOrEmpty(oauth2AccessToken) && string.IsNullOrEmpty(oauth2RefreshToken))
136136
{
137-
throw new ArgumentException("Cannot pass in both null access and refresh token");
137+
throw new ArgumentException("Access token and refresh token cannot both be null or empty.");
138138
}
139139
}
140140

@@ -162,9 +162,9 @@ public DropboxTeamClient(string oauth2AccessToken, string oauth2RefreshToken, Da
162162
public DropboxTeamClient(string oauth2AccessToken, string oauth2RefreshToken, DateTime oauth2AccessTokenExpiresAt, string appKey, string appSecret, DropboxClientConfig config)
163163
: this(new DropboxRequestHandlerOptions(config, oauth2AccessToken, oauth2RefreshToken, oauth2AccessTokenExpiresAt, appKey, appSecret))
164164
{
165-
if (oauth2AccessToken == null && oauth2RefreshToken == null)
165+
if (string.IsNullOrEmpty(oauth2AccessToken) && string.IsNullOrEmpty(oauth2RefreshToken))
166166
{
167-
throw new ArgumentException("Cannot pass in both null access and refresh token");
167+
throw new ArgumentException("Access token and refresh token cannot both be null or empty.");
168168
}
169169
}
170170

@@ -179,9 +179,9 @@ public DropboxTeamClient(string oauth2AccessToken, string oauth2RefreshToken, Da
179179
public DropboxTeamClient(string oauth2AccessToken, string oauth2RefreshToken, string appKey, string appSecret, DropboxClientConfig config)
180180
: this(new DropboxRequestHandlerOptions(config, oauth2AccessToken, oauth2RefreshToken, null, appKey, appSecret))
181181
{
182-
if (oauth2AccessToken == null && oauth2RefreshToken == null)
182+
if (string.IsNullOrEmpty(oauth2AccessToken) && string.IsNullOrEmpty(oauth2RefreshToken))
183183
{
184-
throw new ArgumentException("Cannot pass in both null access and refresh token");
184+
throw new ArgumentException("Access token and refresh token cannot both be null or empty.");
185185
}
186186
}
187187

0 commit comments

Comments
 (0)