-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathPSCredentialInfo.cs
More file actions
149 lines (130 loc) · 4.98 KB
/
Copy pathPSCredentialInfo.cs
File metadata and controls
149 lines (130 loc) · 4.98 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
139
140
141
142
143
144
145
146
147
148
149
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Management.Automation;
using System.Net;
using System.Security;
using System.Collections;
namespace Microsoft.PowerShell.PSResourceGet.UtilClasses
{
/// <summary>
/// This class contains information for a repository's authentication credential.
/// </summary>
public sealed class PSCredentialInfo
{
#region Constructor
/// <summary>
/// Initializes a new instance of the PSCredentialInfo class with
/// vaultName and secretName of type string, and
/// (optionally) credential of type PSCredential.
/// </summary>
/// <param name="vaultName"></param>
/// <param name="secretName"></param>
/// <param name="credential"></param>
public PSCredentialInfo(string vaultName, string secretName, PSCredential credential = null)
{
VaultName = vaultName;
SecretName = secretName;
Credential = credential;
}
/// <summary>
/// Initializes a new instance of the PSCredentialInfo class with
/// containing vaultName and secretName of type string, and
/// (optionally) credential of type PSCredential from a Hashtable.
/// </summary>
/// <param name="hashtable"></param>
public PSCredentialInfo(Hashtable hashtable)
{
if (!(hashtable.ContainsKey("VaultName") && (hashtable.ContainsKey("SecretName") || hashtable.ContainsKey("Name"))))
{
throw new ArgumentException("Credential Information must contain the keys 'VaultName' and 'SecretName'!");
}
VaultName = hashtable["VaultName"] as string;
if (hashtable.ContainsKey("SecretName"))
SecretName = hashtable["SecretName"] as string;
else
SecretName = hashtable["Name"] as string;
if (hashtable.ContainsKey("Credential") && hashtable["Credential"] is PSCredential psCred)
{
Credential = psCred;
}
}
/// <summary>
/// Initializes a new instance of the PSCredentialInfo class with
/// vaultName and secretName of type string, and
/// (optionally) credential of type PSCredential from a PSObject.
/// </summary>
/// <param name="psObject"></param>
public PSCredentialInfo(PSObject psObject)
{
if (psObject == null)
{
throw new ArgumentNullException(nameof(psObject));
}
VaultName = (string) psObject.Properties[PSCredentialInfo.VaultNameAttribute]?.Value;
SecretName = (string) psObject.Properties[PSCredentialInfo.SecretNameAttribute]?.Value;
if (String.IsNullOrEmpty(SecretName))
SecretName = (string) psObject.Properties["Name"]?.Value;
var credentialAttr = psObject.Properties[PSCredentialInfo.CredentialAttribute]?.Value;
if (credentialAttr is string credStr)
{
Credential = new PSCredential("PSGetUser", new NetworkCredential("", credStr).SecurePassword);
}
else if ((credentialAttr as PSObject)?.BaseObject is SecureString credSS)
{
Credential = new PSCredential("PSGetUser", credSS);
}
else if (credentialAttr is PSCredential psCred)
{
Credential = psCred;
}
}
#endregion
#region Members
private string _vaultName;
/// <summary>
/// the Name of the SecretManagement Vault
/// </summary>
public string VaultName {
get
{
return _vaultName;
}
private set
{
if (string.IsNullOrEmpty(value))
{
throw new ArgumentException($"Invalid CredentialInfo, {PSCredentialInfo.VaultNameAttribute} must be a non-empty string");
}
_vaultName = value;
}
}
private string _secretName;
/// <summary>
/// the Name of the Secret
/// </summary>
public string SecretName {
get
{
return _secretName;
}
private set
{
if (string.IsNullOrEmpty(value))
{
throw new ArgumentException($"Invalid CredentialInfo, {PSCredentialInfo.SecretNameAttribute} must be a non-empty string");
}
_secretName = value;
}
}
/// <summary>
/// optional Credential object to save in a SecretManagement Vault
/// for authenticating to repositories
/// </summary>
public PSCredential Credential { get; private set; }
internal static readonly string VaultNameAttribute = nameof(VaultName);
internal static readonly string SecretNameAttribute = nameof(SecretName);
internal static readonly string CredentialAttribute = nameof(Credential);
#endregion
}
}