forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleHostUserInterfaceSecurity.cs
More file actions
139 lines (124 loc) · 4.83 KB
/
Copy pathConsoleHostUserInterfaceSecurity.cs
File metadata and controls
139 lines (124 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/********************************************************************++
Copyright (c) Microsoft Corporation. All rights reserved.
--********************************************************************/
using System;
using System.Security;
using System.Management.Automation;
using System.Management.Automation.Internal;
using Microsoft.Win32;
using System.Globalization;
namespace Microsoft.PowerShell
{
/// <summary>
///
/// ConsoleHostUserInterface implements console-mode user interface for powershell
///
/// </summary>
internal partial
class ConsoleHostUserInterface : System.Management.Automation.Host.PSHostUserInterface
{
/// <summary>
/// Prompt for credentials.
///
/// In future, when we have Credential object from the security team,
/// this function will be modified to prompt using secure-path
/// if so configured.
///
/// </summary>
/// <param name="userName"> name of the user whose creds are to be prompted for. If set to null or empty string, the function will prompt for user name first. </param>
///
/// <param name="targetName"> name of the target for which creds are being collected </param>
///
/// <param name="message"> message to be displayed. </param>
///
/// <param name="caption"> caption for the message. </param>
///
/// <returns> PSCredential object</returns>
///
public override PSCredential PromptForCredential(
string caption,
string message,
string userName,
string targetName)
{
return PromptForCredential(caption,
message,
userName,
targetName,
PSCredentialTypes.Default,
PSCredentialUIOptions.Default);
}
/// <summary>
/// Prompt for credentials.
/// </summary>
/// <param name="userName"> name of the user whose creds are to be prompted for. If set to null or empty string, the function will prompt for user name first. </param>
///
/// <param name="targetName"> name of the target for which creds are being collected </param>
///
/// <param name="message"> message to be displayed. </param>
///
/// <param name="caption"> caption for the message. </param>
///
/// <param name="allowedCredentialTypes"> what type of creds can be supplied by the user </param>
///
/// <param name="options"> options that control the cred gathering UI behavior </param>
///
/// <returns> PSCredential object, or null if input was cancelled (or if reading from stdin and stdin at EOF)</returns>
///
public override PSCredential PromptForCredential(
string caption,
string message,
string userName,
string targetName,
PSCredentialTypes allowedCredentialTypes,
PSCredentialUIOptions options)
{
PSCredential cred = null;
SecureString password = null;
string userPrompt = null;
string passwordPrompt = null;
if (!string.IsNullOrEmpty(caption))
{
// Should be a skin lookup
WriteLineToConsole();
WriteToConsole(PromptColor, RawUI.BackgroundColor, WrapToCurrentWindowWidth(caption));
WriteLineToConsole();
}
if (!string.IsNullOrEmpty(message))
{
WriteLineToConsole(WrapToCurrentWindowWidth(message));
}
if (string.IsNullOrEmpty(userName))
{
userPrompt = ConsoleHostUserInterfaceSecurityResources.PromptForCredential_User;
//
// need to prompt for user name first
//
do
{
WriteToConsole(userPrompt, true);
userName = ReadLine();
if (userName == null)
{
return null;
}
}
while (userName.Length == 0);
}
passwordPrompt = StringUtil.Format(ConsoleHostUserInterfaceSecurityResources.PromptForCredential_Password, userName
);
//
// now, prompt for the password
//
WriteToConsole(passwordPrompt, true);
password = ReadLineAsSecureString();
if (password == null)
{
return null;
}
WriteLineToConsole();
cred = new PSCredential(userName, password);
return cred;
}
}
}