Skip to content

Commit b6ab148

Browse files
committed
Added refresh token support to PKCE flow.
1 parent dd772d9 commit b6ab148

File tree

4 files changed

+34
-30
lines changed

4 files changed

+34
-30
lines changed

Modules/IntelliTect.PSDropbin/IntelliTect.PSDropBin/DropboxDriveInfo.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,38 @@ public class DropboxDriveInfo : PSDriveInfo
1010
{
1111
private const string DropboxCredentialNameBase = "DropboxUserToken";
1212

13-
public static string GetDropboxCredentialName(string driveName)
13+
private static string GetDropboxCredentialName(string driveName)
1414
{
1515
return $"{DropboxCredentialNameBase}-{driveName}";
1616
}
1717

18+
public static string GetDropboxAccessTokenName(string driveName)
19+
{
20+
return $"{GetDropboxCredentialName(driveName)}-AccessToken";
21+
}
22+
23+
public static string GetDropboxRefreshTokenName(string driveName)
24+
{
25+
return $"{GetDropboxCredentialName(driveName)}-RefreshToken";
26+
}
27+
1828
public DropboxDriveInfo(PSDriveInfo driveInfo)
1929
: base(driveInfo)
2030
{
21-
string userToken;
31+
string accessToken;
2232
if (driveInfo.Credential?.Password == null)
2333
{
24-
string credentialName = GetDropboxCredentialName(driveInfo.Name);
25-
userToken = CredentialManager.ReadCredential(credentialName);
34+
string credentialName = GetDropboxAccessTokenName(driveInfo.Name);
35+
accessToken = CredentialManager.ReadCredential(credentialName);
2636
}
2737
else
2838
{
29-
userToken = driveInfo.Credential.GetNetworkCredential().Password;
39+
accessToken = driveInfo.Credential.GetNetworkCredential().Password;
3040
}
3141

32-
Client = new DropboxClient(userToken);
42+
string refreshToken = CredentialManager.ReadCredential(GetDropboxRefreshTokenName(driveInfo.Name));
43+
44+
Client = new DropboxClient(accessToken, refreshToken, Settings.Default.AccessTokenExpiration, Settings.Default.ApiKey);
3345
}
3446

3547
public DropboxClient Client { get; private set; }

Modules/IntelliTect.PSDropbin/IntelliTect.PSDropBin/PKCEHelper.cs

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -81,26 +81,11 @@ private async Task<Uri> HandleJSRedirect(HttpListener http)
8181
/// <returns>A valid access token if successful otherwise null.</returns>
8282
public async Task<string> GetOAuthTokensAsync(string[] scopeList, IncludeGrantedScopes includeGrantedScopes, string driveName)
8383
{
84-
bool accessExpiringSoon = false;
85-
DateTime expireDate;
8684
Settings.Default.Upgrade();
8785

88-
try
89-
{
90-
expireDate = Settings.Default.AccessTokenExpiration;
91-
if (expireDate <= DateTime.Now)
92-
{
93-
accessExpiringSoon = true;
94-
}
95-
}
96-
catch (Exception)
97-
{
98-
accessExpiringSoon = true;
99-
}
100-
101-
string credentialName = DropboxDriveInfo.GetDropboxCredentialName(driveName);
86+
string accessTokencredentialName = DropboxDriveInfo.GetDropboxAccessTokenName(driveName);
10287

103-
if (string.IsNullOrEmpty(CredentialManager.ReadCredential(credentialName)) || accessExpiringSoon)
88+
if (string.IsNullOrEmpty(CredentialManager.ReadCredential(accessTokencredentialName)))
10489
{
10590
string apiKey = GetApiKey();
10691

@@ -156,9 +141,13 @@ public async Task<string> GetOAuthTokensAsync(string[] scopeList, IncludeGranted
156141
Console.WriteLine("OAuth token acquire complete");
157142

158143
CredentialManager.WriteCredential(
159-
DropboxDriveInfo.GetDropboxCredentialName(driveName),
144+
DropboxDriveInfo.GetDropboxAccessTokenName(driveName),
160145
result.AccessToken
161146
);
147+
CredentialManager.WriteCredential(
148+
DropboxDriveInfo.GetDropboxRefreshTokenName(driveName),
149+
result.RefreshToken
150+
);
162151
UpdateSettings(result);
163152
}
164153
catch (Exception e)
@@ -169,7 +158,7 @@ public async Task<string> GetOAuthTokensAsync(string[] scopeList, IncludeGranted
169158
}
170159
}
171160

172-
return CredentialManager.ReadCredential(credentialName);
161+
return CredentialManager.ReadCredential(accessTokencredentialName);
173162
}
174163

175164
private static void UpdateSettings(OAuth2Response result)

Modules/IntelliTect.PSDropbin/IntelliTect.PSDropBin/RemoveDropboxCredential.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@ protected override void ProcessRecord()
1616
{
1717
try
1818
{
19-
string credentialName = DropboxDriveInfo.GetDropboxCredentialName(Name);
20-
bool result = CredentialManager.DeleteCredential(credentialName);
21-
WriteObject( result
22-
? "Credential removed. You may wish to also revoke access in your Dropbox user profile."
23-
: "No credential found." );
19+
string accessTokenName = DropboxDriveInfo.GetDropboxAccessTokenName(Name);
20+
bool accessTokenResult = CredentialManager.DeleteCredential(accessTokenName);
21+
string refreshTokenName = DropboxDriveInfo.GetDropboxRefreshTokenName(Name);
22+
bool refreshTokenResult = CredentialManager.DeleteCredential(refreshTokenName);
23+
WriteObject(accessTokenResult && refreshTokenResult
24+
? "Credentials removed. You may wish to also revoke access in your Dropbox user profile."
25+
: "No credential found.");
26+
Settings.Default.Reset();
2427
}
2528
catch ( Exception e )
2629
{
0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)