Skip to content

Commit 6d5c51c

Browse files
gautamdshethGautam Sheth
andauthored
Add AzureCloudShell detection and improve user prompts for device login (#4997)
Co-authored-by: Gautam Sheth <gautam.sheth@staffbase.com>
1 parent 00a12a9 commit 6d5c51c

File tree

5 files changed

+64
-20
lines changed

5 files changed

+64
-20
lines changed

src/Commands/AzureAD/RegisterAzureADApp.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -659,15 +659,22 @@ private void StartConsentFlow(string loginEndPoint, AzureADApp azureApp, string
659659
{
660660
using (var authManager = AuthenticationManager.CreateWithDeviceLogin(azureApp.AppId, Tenant, (deviceCodeResult) =>
661661
{
662-
try
662+
if (PSUtility.IsAzureCloudShell())
663663
{
664-
ClipboardService.SetText(deviceCodeResult.UserCode);
664+
Host.UI.WriteWarningLine($"\n\nTo sign in, use a web browser to open the page {deviceCodeResult.VerificationUrl} and enter the code {deviceCodeResult.UserCode} to authenticate.");
665665
}
666-
catch
666+
else
667667
{
668+
try
669+
{
670+
ClipboardService.SetText(deviceCodeResult.UserCode);
671+
}
672+
catch
673+
{
674+
}
675+
Host.UI.WriteWarningLine($"\n\nPlease login.\n\nWe opened a browser and navigated to {deviceCodeResult.VerificationUrl}\n\nEnter code: {deviceCodeResult.UserCode} (we copied this code to your clipboard)\n\nNOTICE: close the browser tab after you authenticated successfully to continue the process.");
676+
BrowserHelper.OpenBrowserForInteractiveLogin(deviceCodeResult.VerificationUrl, BrowserHelper.FindFreeLocalhostRedirectUri(), cancellationTokenSource);
668677
}
669-
Host.UI.WriteWarningLine($"\n\nPlease login.\n\nWe opened a browser and navigated to {deviceCodeResult.VerificationUrl}\n\nEnter code: {deviceCodeResult.UserCode} (we copied this code to your clipboard)\n\nNOTICE: close the browser tab after you authenticated successfully to continue the process.");
670-
BrowserHelper.OpenBrowserForInteractiveLogin(deviceCodeResult.VerificationUrl, BrowserHelper.FindFreeLocalhostRedirectUri(), cancellationTokenSource);
671678
return Task.FromResult(0);
672679
}, AzureEnvironment))
673680
{

src/Commands/AzureAD/RegisterEntraIDAppForInteractiveLogin.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -500,15 +500,22 @@ private void StartConsentFlow(string loginEndPoint, AzureADApp azureApp, string
500500
{
501501
using (var authManager = AuthenticationManager.CreateWithDeviceLogin(azureApp.AppId, Tenant, (deviceCodeResult) =>
502502
{
503-
try
503+
if (PSUtility.IsAzureCloudShell())
504504
{
505-
ClipboardService.SetText(deviceCodeResult.UserCode);
505+
Host.UI.WriteWarningLine($"\n\nTo sign in, use a web browser to open the page {deviceCodeResult.VerificationUrl} and enter the code {deviceCodeResult.UserCode} to authenticate.");
506506
}
507-
catch
507+
else
508508
{
509+
try
510+
{
511+
ClipboardService.SetText(deviceCodeResult.UserCode);
512+
}
513+
catch
514+
{
515+
}
516+
Host.UI.WriteWarningLine($"\n\nPlease login.\n\nWe opened a browser and navigated to {deviceCodeResult.VerificationUrl}\n\nEnter code: {deviceCodeResult.UserCode} (we copied this code to your clipboard)\n\nNOTICE: close the browser tab after you authenticated successfully to continue the process.");
517+
BrowserHelper.OpenBrowserForInteractiveLogin(deviceCodeResult.VerificationUrl, BrowserHelper.FindFreeLocalhostRedirectUri(), cancellationTokenSource);
509518
}
510-
Host.UI.WriteWarningLine($"\n\nPlease login.\n\nWe opened a browser and navigated to {deviceCodeResult.VerificationUrl}\n\nEnter code: {deviceCodeResult.UserCode} (we copied this code to your clipboard)\n\nNOTICE: close the browser tab after you authenticated successfully to continue the process.");
511-
BrowserHelper.OpenBrowserForInteractiveLogin(deviceCodeResult.VerificationUrl, BrowserHelper.FindFreeLocalhostRedirectUri(), cancellationTokenSource);
512519
return Task.FromResult(0);
513520
}, AzureEnvironment))
514521
{

src/Commands/Base/PnPConnection.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -280,15 +280,22 @@ internal static PnPConnection CreateWithDeviceLogin(string clientId, string url,
280280
{
281281
authManager = Framework.AuthenticationManager.CreateWithDeviceLogin(clientId, tenantId, (deviceCodeResult) =>
282282
{
283-
try
283+
if (PSUtility.IsAzureCloudShell())
284284
{
285-
ClipboardService.SetText(deviceCodeResult.UserCode);
285+
messageWriter.LogWarning($"\n\nTo sign in, use a web browser to open the page {deviceCodeResult.VerificationUrl} and enter the code {deviceCodeResult.UserCode} to authenticate.\n\n");
286286
}
287-
catch
287+
else
288288
{
289+
try
290+
{
291+
ClipboardService.SetText(deviceCodeResult.UserCode);
292+
}
293+
catch
294+
{
295+
}
296+
messageWriter.LogWarning($"\n\nCode {deviceCodeResult.UserCode} has been copied to your clipboard and a new tab in the browser has been opened. Please paste this code in there and proceed.\n\n");
297+
BrowserHelper.OpenBrowserForInteractiveLogin(deviceCodeResult.VerificationUrl, BrowserHelper.FindFreeLocalhostRedirectUri(), cancellationTokenSource);
289298
}
290-
messageWriter.LogWarning($"\n\nCode {deviceCodeResult.UserCode} has been copied to your clipboard and a new tab in the browser has been opened. Please paste this code in there and proceed.\n\n");
291-
BrowserHelper.OpenBrowserForInteractiveLogin(deviceCodeResult.VerificationUrl, BrowserHelper.FindFreeLocalhostRedirectUri(), cancellationTokenSource);
292299

293300
return Task.FromResult(0);
294301
}, azureEnvironment, tokenCacheCallback: async (tokenCache) =>

src/Commands/Utilities/AzureAuthHelper.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,24 @@ internal static string AuthenticateDeviceLogin(CancellationTokenSource cancellat
3232
{
3333
try
3434
{
35-
using (var authManager = PnP.Framework.AuthenticationManager.CreateWithDeviceLogin(CLIENTID, (result) =>
35+
using (var authManager = AuthenticationManager.CreateWithDeviceLogin(CLIENTID, (deviceCodeResult) =>
3636
{
37-
38-
ClipboardService.SetText(result.UserCode);
39-
messageWriter.LogWarning($"Please login.\n\nWe opened a browser and navigated to {result.VerificationUrl}\n\nEnter code: {result.UserCode} (we copied this code to your clipboard)\n\nNOTICE: close the browser tab after you authenticated successfully to continue the process.");
40-
BrowserHelper.OpenBrowserForInteractiveLogin(result.VerificationUrl, BrowserHelper.FindFreeLocalhostRedirectUri(), cancellationTokenSource);
37+
if (PSUtility.IsAzureCloudShell())
38+
{
39+
messageWriter.LogWarning($"\n\nTo sign in, use a web browser to open the page {deviceCodeResult.VerificationUrl} and enter the code {deviceCodeResult.UserCode} to authenticate.");
40+
}
41+
else
42+
{
43+
try
44+
{
45+
ClipboardService.SetText(deviceCodeResult.UserCode);
46+
}
47+
catch
48+
{
49+
}
50+
messageWriter.LogWarning($"Please login.\n\nWe opened a browser and navigated to {deviceCodeResult.VerificationUrl}\n\nEnter code: {deviceCodeResult.UserCode} (we copied this code to your clipboard)\n\nNOTICE: close the browser tab after you authenticated successfully to continue the process.");
51+
BrowserHelper.OpenBrowserForInteractiveLogin(deviceCodeResult.VerificationUrl, BrowserHelper.FindFreeLocalhostRedirectUri(), cancellationTokenSource);
52+
}
4153

4254
return Task.FromResult(0);
4355
}, azureEnvironment))

src/Commands/Utilities/PSUtility.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,16 @@ public static bool IsUserLocalAdmin()
5050
return true;
5151
}
5252
#pragma warning restore CA1416 // Validate platform compatibility
53+
54+
public static bool IsAzureCloudShell()
55+
{
56+
string psDistChannel = Environment.GetEnvironmentVariable("POWERSHELL_DISTRIBUTION_CHANNEL");
57+
if (string.IsNullOrWhiteSpace(psDistChannel))
58+
{
59+
return false;
60+
}
61+
62+
return psDistChannel == "CloudShell";
63+
}
5364
}
5465
}

0 commit comments

Comments
 (0)