Skip to content

Commit 427593a

Browse files
authored
Authentication Bug Fixes (#934)
* Fallback to process scope when on WSL. * Ensure directory exists before opening a file. * Default to process scope on WSL.
1 parent ca08854 commit 427593a

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/Authentication/Authentication/Cmdlets/ConnectMgGraph.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ namespace Microsoft.Graph.PowerShell.Authentication.Cmdlets
1212
using System.Linq;
1313
using System.Management.Automation;
1414
using System.Net;
15+
using System.Runtime.InteropServices;
1516
using System.Security.Cryptography.X509Certificates;
1617
using System.Threading;
1718
using System.Threading.Tasks;
1819

1920
using Microsoft.Graph.Authentication.Core;
2021
using Microsoft.Graph.PowerShell.Authentication.Common;
22+
using Microsoft.Graph.PowerShell.Authentication.Extensions;
2123
using Microsoft.Graph.PowerShell.Authentication.Helpers;
2224
using Microsoft.Graph.PowerShell.Authentication.Interfaces;
2325
using Microsoft.Graph.PowerShell.Authentication.Models;
@@ -189,8 +191,16 @@ private async Task ProcessRecordAsync()
189191
authContext.AuthType = AuthenticationType.Delegated;
190192
string[] processedScopes = ProcessScopes(Scopes);
191193
authContext.Scopes = processedScopes.Length == 0 ? new[] { "User.Read" } : processedScopes;
192-
// Default to CurrentUser but allow the customer to change this via `ContextScope` param.
193-
authContext.ContextScope = this.IsParameterBound(nameof(ContextScope)) ? ContextScope : ContextScope.CurrentUser;
194+
if (RuntimeInformation.OSDescription.ContainsValue("WSL", StringComparison.InvariantCulture))
195+
{
196+
// Use process scope when on WSL. WSL does not have secret service that the we use to cache tokens on Linux, see https://github.com/microsoft/WSL/issues/4254.
197+
authContext.ContextScope = ContextScope.Process;
198+
}
199+
else
200+
{
201+
// Default to CurrentUser but allow the customer to change this via `ContextScope` param.
202+
authContext.ContextScope = this.IsParameterBound(nameof(ContextScope)) ? ContextScope : ContextScope.CurrentUser;
203+
}
194204
authContext.AuthProviderType = UseDeviceAuthentication ? AuthProviderType.DeviceCodeProvider : AuthProviderType.InteractiveAuthenticationProvider;
195205
if (!string.IsNullOrWhiteSpace(ClientId))
196206
{

src/Authentication/Authentication/Common/DiskDataStore.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ public string[] GetDirectories(string startDirectory, string filePattern, Search
213213
/// <returns>A <see cref="FileStream"/> to the specified path with shared read.</returns>
214214
public Stream OpenForSharedRead(string path)
215215
{
216+
Directory.CreateDirectory(Path.GetDirectoryName(path));
216217
return File.Open(path, FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read);
217218
}
218219

@@ -223,8 +224,7 @@ public Stream OpenForSharedRead(string path)
223224
/// <returns>A <see cref="FileStream"/> to the specified path with exclusive write.</returns>
224225
public Stream OpenForExclusiveWrite(string path)
225226
{
226-
string directory = Path.GetDirectoryName(path);
227-
Directory.CreateDirectory(directory);
227+
Directory.CreateDirectory(Path.GetDirectoryName(path));
228228
return File.Open(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
229229
}
230230
}

0 commit comments

Comments
 (0)