Skip to content

Commit 155b8c9

Browse files
authored
Merge pull request #114 from microsoftgraph/po/UpdateTokenPath
Update Token Path To UserProfile
2 parents b0c7628 + 89158d0 commit 155b8c9

File tree

5 files changed

+56
-23
lines changed

5 files changed

+56
-23
lines changed
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
// ------------------------------------------------------------------------------
22
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.
33
// ------------------------------------------------------------------------------
4+
45
namespace Microsoft.Graph.PowerShell.Authentication
56
{
7+
using System;
8+
using System.IO;
69
public static class Constants
710
{
811
public const string GraphAuthConfigId = "GraphAuthConfigId";
912
public const string SDKHeaderValue = "Graph-powershell-{0}-{1}.{2}.{3}";
1013
internal const string UserParameterSet = "UserParameterSet";
1114
internal const string AppParameterSet = "AppParameterSet";
12-
internal static readonly int MaxDeviceCodeTimeOut = 120; // 2 mins timeout.
15+
internal const int MaxDeviceCodeTimeOut = 120; // 2 mins timeout.
16+
internal const string UserCacheFileName = "userTokenCache.bin3";
17+
internal const string AppCacheFileName = "appTokenCache.bin3";
18+
internal static readonly string TokenCacheDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".graph");
1319
}
1420
}

src/Authentication/Authentication/Helpers/AuthenticationHelpers.cs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,16 @@ namespace Microsoft.Graph.PowerShell.Authentication.Helpers
55
{
66
using Microsoft.Graph.Auth;
77
using Microsoft.Graph.PowerShell.Authentication.Models;
8+
using Microsoft.Graph.PowerShell.Authentication.TokenCache;
89
using Microsoft.Identity.Client;
910
using System;
1011
using System.IO;
1112
using System.Linq;
12-
using System.Security.Cryptography;
1313
using System.Security.Cryptography.X509Certificates;
1414

1515
internal static class AuthenticationHelpers
1616
{
1717
private static readonly object FileLock = new object();
18-
private static readonly string UserCacheFileName = "userTokenCache.bin3";
19-
private static readonly string AppCacheFileName = "appTokenCache.bin3";
20-
21-
/// <summary>
22-
/// Path to the token cache.
23-
/// </summary>
24-
internal static readonly string CacheFilePath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
2518

2619
internal static IAuthenticationProvider GetAuthProvider(AuthConfig authConfig)
2720
{
@@ -32,7 +25,7 @@ internal static IAuthenticationProvider GetAuthProvider(AuthConfig authConfig)
3225
.WithTenantId(authConfig.TenantId)
3326
.Build();
3427

35-
ConfigureTokenCache(publicClientApp.UserTokenCache, Path.Combine(CacheFilePath, UserCacheFileName));
28+
ConfigureTokenCache(publicClientApp.UserTokenCache, Constants.UserCacheFileName);
3629
return new DeviceCodeProvider(publicClientApp, authConfig.Scopes, async (result) => {
3730
await Console.Out.WriteLineAsync(result.Message);
3831
});
@@ -45,7 +38,7 @@ internal static IAuthenticationProvider GetAuthProvider(AuthConfig authConfig)
4538
.WithCertificate(string.IsNullOrEmpty(authConfig.CertificateThumbprint) ? GetCertificateByName(authConfig.CertificateName) : GetCertificateByThumbprint(authConfig.CertificateThumbprint))
4639
.Build();
4740

48-
ConfigureTokenCache(confidentialClientApp.AppTokenCache, Path.Combine(CacheFilePath, AppCacheFileName));
41+
ConfigureTokenCache(confidentialClientApp.AppTokenCache, Constants.AppCacheFileName);
4942
return new ClientCredentialProvider(confidentialClientApp);
5043
}
5144
}
@@ -55,19 +48,24 @@ internal static void Logout(AuthConfig authConfig)
5548
lock (FileLock)
5649
{
5750
if (authConfig.AuthType == AuthenticationType.Delegated)
58-
File.Delete(Path.Combine(CacheFilePath, UserCacheFileName));
51+
File.Delete(Path.Combine(Constants.TokenCacheDirectory, Constants.UserCacheFileName));
5952
else
60-
File.Delete(Path.Combine(CacheFilePath, AppCacheFileName));
53+
File.Delete(Path.Combine(Constants.TokenCacheDirectory, Constants.AppCacheFileName));
6154
}
6255
}
6356

64-
private static void ConfigureTokenCache(ITokenCache tokenCache, string tokenCachePath)
57+
private static void ConfigureTokenCache(ITokenCache tokenCache, string tokenCacheFile)
6558
{
59+
if (!Directory.Exists(Constants.TokenCacheDirectory))
60+
Directory.CreateDirectory(Constants.TokenCacheDirectory);
61+
62+
string tokenCacheFilePath = Path.Combine(Constants.TokenCacheDirectory, tokenCacheFile);
63+
6664
tokenCache.SetBeforeAccess((TokenCacheNotificationArgs args) => {
6765
lock (FileLock)
6866
{
69-
args.TokenCache.DeserializeMsalV3(File.Exists(tokenCachePath)
70-
? TokenCryptoHelpers.DecryptToken(File.ReadAllBytes(tokenCachePath))
67+
args.TokenCache.DeserializeMsalV3(File.Exists(tokenCacheFilePath)
68+
? TokenCryptographer.DecryptToken(File.ReadAllBytes(tokenCacheFilePath))
7169
: null,
7270
shouldClearExistingCache: true);
7371
}
@@ -78,7 +76,7 @@ private static void ConfigureTokenCache(ITokenCache tokenCache, string tokenCach
7876
{
7977
if (args.HasStateChanged)
8078
{
81-
File.WriteAllBytes(tokenCachePath, TokenCryptoHelpers.EncryptToken(args.TokenCache.SerializeMsalV3()));
79+
File.WriteAllBytes(tokenCacheFilePath, TokenCryptographer.EncryptToken(args.TokenCache.SerializeMsalV3()));
8280
}
8381
}
8482
});

src/Authentication/Authentication/Properties/launchSettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"profiles": {
33
"Graph.Authentication": {
44
"commandName": "Executable",
5-
"executablePath": "C:\\Program Files\\PowerShell\\6\\pwsh.exe",
5+
"executablePath": "C:\\Program Files\\PowerShell\\7-preview\\pwsh.exe",
66
"commandLineArgs": "-NoProfile -NoExit"
77
}
88
}

src/Authentication/Authentication/Helpers/TokenCryptoHelpers.cs renamed to src/Authentication/Authentication/TokenCache/TokenCryptographer.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
// ------------------------------------------------------------------------------
22
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.
33
// ------------------------------------------------------------------------------
4-
namespace Microsoft.Graph.PowerShell.Authentication.Helpers
4+
namespace Microsoft.Graph.PowerShell.Authentication.TokenCache
55
{
66
using System;
7-
using System.Security.Cryptography;
87

98
/// <summary>
109
/// Helper class to handle token encryption and decryption.
1110
/// </summary>
12-
internal static class TokenCryptoHelpers
11+
internal static class TokenCryptographer
1312
{
1413
/// <summary>
1514
/// Encrypts the passed buffer based on the host platform.
@@ -19,7 +18,7 @@ internal static class TokenCryptoHelpers
1918
public static byte[] EncryptToken(byte[] buffer)
2019
{
2120
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
22-
return ProtectedData.Protect(buffer, null, DataProtectionScope.CurrentUser);
21+
return WindowsTokenCache.EncryptToken(buffer);
2322
return buffer;
2423
}
2524

@@ -31,7 +30,7 @@ public static byte[] EncryptToken(byte[] buffer)
3130
public static byte[] DecryptToken(byte[] buffer)
3231
{
3332
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
34-
return ProtectedData.Unprotect(buffer, null, DataProtectionScope.CurrentUser);
33+
return WindowsTokenCache.DecryptToken(buffer);
3534
return buffer;
3635
}
3736
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// ------------------------------------------------------------------------------
2+
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.
3+
// ------------------------------------------------------------------------------
4+
5+
namespace Microsoft.Graph.PowerShell.Authentication.TokenCache
6+
{
7+
using System.Security.Cryptography;
8+
internal static class WindowsTokenCache
9+
{
10+
/// <summary>
11+
/// Encrypts the passed buffer using Windows DPAPI.
12+
/// </summary>
13+
/// <param name="buffer">A <see cref="byte[]"/> to encrypt.</param>
14+
/// <returns>An encrypted <see cref="byte[]"/>.</returns>
15+
public static byte[] EncryptToken(byte[] buffer)
16+
{
17+
return ProtectedData.Protect(buffer, null, DataProtectionScope.CurrentUser);
18+
}
19+
20+
/// <summary>
21+
/// Decrypts the passed buffer Windows DPAPI.
22+
/// </summary>
23+
/// <param name="buffer">A <see cref="byte[]"/> to decrypt.</param>
24+
/// <returns>An decrypted <see cref="byte[]"/>.</returns>
25+
public static byte[] DecryptToken(byte[] buffer)
26+
{
27+
return ProtectedData.Unprotect(buffer, null, DataProtectionScope.CurrentUser);
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)