Skip to content

Commit

Permalink
Added command feedback using WriteObject on 'Yubikey' and 'Fido' cmdl… (
Browse files Browse the repository at this point in the history
#83)

* Added command feedback using WriteWarning/WriteObject/WriteInformation on 'Yubikey' and 'Fido' cmdlets.
* Minor changes to language including punctuation.

---------

Co-authored-by: Oscar Virot <[email protected]>
  • Loading branch information
JMarkstrom and virot authored Dec 25, 2024
1 parent 8415e41 commit 748d4c4
Show file tree
Hide file tree
Showing 47 changed files with 173 additions and 156 deletions.
10 changes: 5 additions & 5 deletions Module/Cmdlets/Fido/ConnectYubikeyFIDO2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected override void BeginProcessing()
{
if (YubiKeyModule._yubikey is null)
{
WriteDebug("No YubiKey selected, calling Connect-Yubikey");
WriteDebug("No YubiKey selected, calling Connect-Yubikey...");
var myPowersShellInstance = PowerShell.Create(RunspaceMode.CurrentRunspace).AddCommand("Connect-Yubikey");
myPowersShellInstance.Invoke();
WriteDebug($"Successfully connected");
Expand All @@ -75,8 +75,8 @@ protected override void BeginProcessing()
// Check if Connect-YubikeyFIDO2 was called without a PIN (only possible with Yubikey that doesnt have a PIN configured)
if (this.MyInvocation.BoundParameters.ContainsKey("PIN") == false)
{
WriteWarning("FIDO2 has no PIN, please set PIN before continuing.");
WriteDebug("FIDO2 has no PIN, invokating Set-YubikeyFIDO2 -SetPIN");
WriteWarning("FIDO2 has no PIN, please set PIN before continuing:");
WriteDebug("FIDO2 has no PIN, invokating Set-YubikeyFIDO2 -SetPIN...");
var myPowersShellInstance = PowerShell.Create(RunspaceMode.CurrentRunspace).AddCommand("Set-YubikeyFIDO2").AddParameter("SetPIN");
myPowersShellInstance.Invoke();
}
Expand All @@ -98,12 +98,12 @@ protected override void ProcessRecord()
{
if (fido2Session.AuthenticatorInfo.GetOptionValue(AuthenticatorOptions.clientPin) == OptionValue.False)
{
WriteObject("Client PIN is not set");
WriteWarning("Client PIN is not set.");
return;
}
else if (fido2Session.AuthenticatorInfo.ForcePinChange == true)
{
WriteWarning("YubiKey requires PIN change to continue, see Set-YubikeyFIDO2 -SetPIN ");
WriteWarning("YubiKey requires PIN change to continue, see Set-YubikeyFIDO2 -SetPIN.");
return;
}
if (this.MyInvocation.BoundParameters["PIN"] is not null)
Expand Down
4 changes: 2 additions & 2 deletions Module/Cmdlets/Fido/GetYubikeyFIDO2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ protected override void BeginProcessing()
// If no FIDO2 PIN exists, we need to connect to the FIDO2 application
if (YubiKeyModule._fido2PIN is null)
{
WriteDebug("No FIDO2 session has been authenticated, calling Connect-YubikeyFIDO2");
WriteDebug("No FIDO2 session has been authenticated, calling Connect-YubikeyFIDO2...");
var myPowersShellInstance = PowerShell.Create(RunspaceMode.CurrentRunspace).AddCommand("Connect-YubikeyFIDO2").Invoke();
if (YubiKeyModule._fido2PIN is null)
{
throw new Exception("Connect-YubikeyFIDO2 failed to connect FIDO2 application.");
throw new Exception("Connect-YubikeyFIDO2 failed to the FIDO2 applet!");
}
}

Expand Down
33 changes: 22 additions & 11 deletions Module/Cmdlets/Fido/GetYubikeyFIDO2Credentials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ protected override void BeginProcessing()
// If no FIDO2 PIN exists, we need to connect to the FIDO2 application
if (YubiKeyModule._fido2PIN is null)
{
WriteDebug("No FIDO2 session has been authenticated, calling Connect-YubikeyFIDO2");
WriteDebug("No FIDO2 session has been authenticated, calling Connect-YubikeyFIDO2...");
var myPowersShellInstance = PowerShell.Create(RunspaceMode.CurrentRunspace).AddCommand("Connect-YubikeyFIDO2").Invoke();
if (YubiKeyModule._fido2PIN is null)
{
throw new Exception("Connect-YubikeyFIDO2 failed to connect FIDO2 application.");
throw new Exception("Connect-YubikeyFIDO2 failed to connect to the FIDO2 applet!");
}
}

Expand All @@ -38,21 +38,32 @@ protected override void ProcessRecord()
fido2Session.KeyCollector = YubiKeyModule._KeyCollector.YKKeyCollectorDelegate;

var RelyingParties = fido2Session.EnumerateRelyingParties();
foreach (RelyingParty RelyingParty in RelyingParties)

if (!RelyingParties.Any()) // Check if there are no relying parties
{
WriteWarning("No credentials found on the YubiKey.");
return;
}
else
{
var relayCredentials = fido2Session.EnumerateCredentialsForRelyingParty(RelyingParty);
foreach (CredentialUserInfo user in relayCredentials)
foreach (RelyingParty RelyingParty in RelyingParties)
{
Credentials credentials = new Credentials
var relayCredentials = fido2Session.EnumerateCredentialsForRelyingParty(RelyingParty);

foreach (CredentialUserInfo user in relayCredentials)
{
Site = RelyingParty.Id,
Name = user.User.Name,
DisplayName = user.User.DisplayName,
};
WriteObject(credentials);
Credentials credentials = new Credentials
{
Site = RelyingParty.Id,
Name = user.User.Name,
DisplayName = user.User.DisplayName,
};
WriteObject(credentials);
}
}
}
}
}

}
}
5 changes: 3 additions & 2 deletions Module/Cmdlets/Fido/ResetYubikeyFIDO2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ protected override void BeginProcessing()

if (YubiKeyModule._yubikey is null)
{
WriteDebug("No YubiKey selected, calling Connect-Yubikey");
WriteDebug("No YubiKey selected, calling Connect-Yubikey...");
var myPowersShellInstance = PowerShell.Create(RunspaceMode.CurrentRunspace).AddCommand("Connect-Yubikey");
myPowersShellInstance.Invoke();
WriteDebug($"Successfully connected");
WriteDebug($"Successfully connected.");
}
}

Expand Down Expand Up @@ -102,6 +102,7 @@ protected override void ProcessRecord()
}

YubiKeyModule._fido2PIN = null;
WriteObject("YubiKey FIDO applet successfully reset.");
}
}
}
Expand Down
18 changes: 11 additions & 7 deletions Module/Cmdlets/Fido/SetYubikeyFIDO2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,22 @@ protected override void BeginProcessing()
{
if (YubiKeyModule._yubikey is null)
{
WriteDebug("No Yubikey selected, calling Connect-Yubikey");
WriteDebug("No Yubikey selected, calling Connect-Yubikey...");
var myPowersShellInstance = PowerShell.Create(RunspaceMode.CurrentRunspace).AddCommand("Connect-Yubikey");
myPowersShellInstance.Invoke();
WriteDebug($"Successfully connected");
WriteDebug($"Successfully connected.");
}
}
else
{
// If no FIDO2 PIN exists, we need to connect to the FIDO2 application
if (YubiKeyModule._fido2PIN is null)
{
WriteDebug("No FIDO2 session has been authenticated, calling Connect-YubikeyFIDO2");
WriteDebug("No FIDO2 session has been authenticated, calling Connect-YubikeyFIDO2...");
var myPowersShellInstance = PowerShell.Create(RunspaceMode.CurrentRunspace).AddCommand("Connect-YubikeyFIDO2").Invoke();
if (YubiKeyModule._fido2PIN is null)
{
throw new Exception("Connect-YubikeyFIDO2 failed to connect FIDO2 application.");
throw new Exception("Connect-YubikeyFIDO2 failed to connect to the FIDO2 applet!");
}
}
}
Expand All @@ -128,14 +128,15 @@ protected override void ProcessRecord()
case "Set PIN minimum length":
if (fido2Session.AuthenticatorInfo.GetOptionValue(AuthenticatorOptions.setMinPINLength) == OptionValue.True)
{
// Code to increase min PIN length here.
// Code to set minimum PIN length here.

if (!fido2Session.TrySetPinConfig(MinimumPINLength, null, null))
{
throw new Exception("Failed to change the minimum PIN length.");
}
// Do it once more to force PIN change.
fido2Session.TrySetPinConfig(null, null, null);
WriteObject("Minimum PIN length set.");
}
else
{
Expand All @@ -151,6 +152,7 @@ protected override void ProcessRecord()
// Use TrySetPinConfig to enable Force PIN Change.
bool? forceChangePin = true;
if (!fido2Session.TrySetPinConfig(null, null, forceChangePin))
WriteObject("Force PIN change set.");
{
// Throw an exception if applying the setting fails.
throw new InvalidOperationException("Failed to enforce PIN change.");
Expand All @@ -175,12 +177,12 @@ protected override void ProcessRecord()
{
if (fido2Session.AuthenticatorInfo.GetOptionValue(AuthenticatorOptions.clientPin) == OptionValue.False)
{
WriteDebug("No FIDO2 PIN set, setting new PIN");
WriteDebug("No FIDO PIN set, setting new PIN...");
fido2Session.SetPin();
}
else
{
WriteDebug("FIDO2 PIN set, changing PIN");
WriteDebug("FIDO2 PIN set, changing PIN...");
fido2Session.ChangePin();
}
}
Expand All @@ -194,6 +196,8 @@ protected override void ProcessRecord()
YubiKeyModule._fido2PINNew = null;
}
YubiKeyModule._fido2PIN = (SecureString)this.MyInvocation.BoundParameters["NewPIN"];
WriteObject("FIDO PIN updated.");

break;
case "Send MinimumPIN to RelyingParty":
var rpidList = new List<string>(1);
Expand Down
9 changes: 5 additions & 4 deletions Module/Cmdlets/Fido/SetYubikeyFIDO2PIN.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public object GetDynamicParameters()
}

newPIN = new Collection<Attribute>() {
new ParameterAttribute() { Mandatory = true, HelpMessage = "New PIN code to set for the FIDO2 module.", ParameterSetName = "Set PIN", ValueFromPipeline = false},
new ParameterAttribute() { Mandatory = true, HelpMessage = "New PIN code to set for the FIDO applet.", ParameterSetName = "Set PIN", ValueFromPipeline = false},
new ValidateYubikeyPIN(minPinLength, 63)
};
}
Expand All @@ -53,7 +53,7 @@ public object GetDynamicParameters()
new ValidateYubikeyPIN(4, 63)
};
newPIN = new Collection<Attribute>() {
new ParameterAttribute() { Mandatory = true, HelpMessage = "New PIN code to set for the FIDO2 module.", ParameterSetName = "Set PIN", ValueFromPipeline = false},
new ParameterAttribute() { Mandatory = true, HelpMessage = "New PIN code to set for the FIDO applet.", ParameterSetName = "Set PIN", ValueFromPipeline = false},
new ValidateYubikeyPIN(4, 63)
};
}
Expand Down Expand Up @@ -90,14 +90,15 @@ protected override void ProcessRecord()
{
if (fido2Session.AuthenticatorInfo.GetOptionValue(AuthenticatorOptions.clientPin) == OptionValue.False)
{
WriteDebug("No FIDO2 PIN set, setting new PIN");
WriteDebug("No FIDO2 PIN set, setting new PIN...");
fido2Session.SetPin();
}
else
{
WriteDebug("FIDO2 PIN set, changing PIN");
WriteDebug("FIDO2 PIN set, changing PIN...");
fido2Session.ChangePin();
}
WriteObject("FIDO PIN updated.");
}
catch (Exception e)
{
Expand Down
4 changes: 2 additions & 2 deletions Module/Cmdlets/OATH/ConnectYubikeyOATH.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ protected override void BeginProcessing()
{
if (YubiKeyModule._yubikey is null)
{
WriteDebug("No YubiKey selected, calling Connect-Yubikey");
WriteDebug("No YubiKey selected, calling Connect-Yubikey...");
var myPowersShellInstance = PowerShell.Create(RunspaceMode.CurrentRunspace).AddCommand("Connect-Yubikey");
myPowersShellInstance.Invoke();
WriteDebug($"Successfully connected");
WriteDebug($"Successfully connected.");
}
}
protected override void ProcessRecord()
Expand Down
4 changes: 2 additions & 2 deletions Module/Cmdlets/OATH/GetYubikeyOATH.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ protected override void BeginProcessing()
{
if (YubiKeyModule._yubikey is null)
{
WriteDebug("No YubiKey selected, calling Connect-Yubikey");
WriteDebug("No YubiKey selected, calling Connect-Yubikey...");
var myPowersShellInstance = PowerShell.Create(RunspaceMode.CurrentRunspace).AddCommand("Connect-Yubikey");
myPowersShellInstance.Invoke();
WriteDebug($"Successfully connected");
WriteDebug($"Successfully connected.");
}
}

Expand Down
4 changes: 2 additions & 2 deletions Module/Cmdlets/OATH/GetYubikeyOATHAccount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ protected override void BeginProcessing()
{
if (YubiKeyModule._yubikey is null)
{
WriteDebug("No YubiKey selected, calling Connect-Yubikey");
WriteDebug("No YubiKey selected, calling Connect-Yubikey...");
var myPowersShellInstance = PowerShell.Create(RunspaceMode.CurrentRunspace).AddCommand("Connect-Yubikey");
myPowersShellInstance.Invoke();
WriteDebug($"Successfully connected");
WriteDebug($"Successfully connected.");
}
}

Expand Down
4 changes: 2 additions & 2 deletions Module/Cmdlets/OATH/NewYubikeyOATHAccount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ protected override void BeginProcessing()
{
if (YubiKeyModule._yubikey is null)
{
WriteDebug("No YubiKey selected, calling Connect-Yubikey");
WriteDebug("No YubiKey selected, calling Connect-Yubikey...");
var myPowersShellInstance = PowerShell.Create(RunspaceMode.CurrentRunspace).AddCommand("Connect-Yubikey");
myPowersShellInstance.Invoke();
WriteDebug($"Successfully connected");
WriteDebug($"Successfully connected.");
}
}

Expand Down
4 changes: 2 additions & 2 deletions Module/Cmdlets/OATH/ProtectYubikeyOATH.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ protected override void BeginProcessing()
{
if (YubiKeyModule._yubikey is null)
{
WriteDebug("No YubiKey selected, calling Connect-Yubikey");
WriteDebug("No YubiKey selected, calling Connect-Yubikey...");
var myPowersShellInstance = PowerShell.Create(RunspaceMode.CurrentRunspace).AddCommand("Connect-Yubikey");
myPowersShellInstance.Invoke();
WriteDebug($"Successfully connected");
WriteDebug($"Successfully connected.");
}
}

Expand Down
4 changes: 2 additions & 2 deletions Module/Cmdlets/OATH/RemoveYubikeyOATHAccount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ protected override void BeginProcessing()
{
if (YubiKeyModule._yubikey is null)
{
WriteDebug("No YubiKey selected, calling Connect-Yubikey");
WriteDebug("No YubiKey selected, calling Connect-Yubikey...");
var myPowersShellInstance = PowerShell.Create(RunspaceMode.CurrentRunspace).AddCommand("Connect-Yubikey");
myPowersShellInstance.Invoke();
WriteDebug($"Successfully connected");
WriteDebug($"Successfully connected.");
}
}

Expand Down
4 changes: 2 additions & 2 deletions Module/Cmdlets/OATH/RenameYubikeyOATHAccount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ protected override void BeginProcessing()
{
if (YubiKeyModule._yubikey is null)
{
WriteDebug("No YubiKey selected, calling Connect-Yubikey");
WriteDebug("No YubiKey selected, calling Connect-Yubikey...");
var myPowersShellInstance = PowerShell.Create(RunspaceMode.CurrentRunspace).AddCommand("Connect-Yubikey");
myPowersShellInstance.Invoke();
WriteDebug($"Successfully connected");
WriteDebug($"Successfully connected.");
};
}

Expand Down
4 changes: 2 additions & 2 deletions Module/Cmdlets/OATH/RequestYubikeyOATHCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ protected override void BeginProcessing()
{
if (YubiKeyModule._yubikey is null)
{
WriteDebug("No YubiKey selected, calling Connect-Yubikey");
WriteDebug("No YubiKey selected, calling Connect-Yubikey...");
var myPowersShellInstance = PowerShell.Create(RunspaceMode.CurrentRunspace).AddCommand("Connect-Yubikey");
myPowersShellInstance.Invoke();
WriteDebug($"Successfully connected");
WriteDebug($"Successfully connected.");
}
}

Expand Down
4 changes: 2 additions & 2 deletions Module/Cmdlets/OATH/ResetYubikeyOATH.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ protected override void BeginProcessing()
{
if (YubiKeyModule._yubikey is null)
{
WriteDebug("No YubiKey selected, calling Connect-Yubikey");
WriteDebug("No YubiKey selected, calling Connect-Yubikey...");
var myPowersShellInstance = PowerShell.Create(RunspaceMode.CurrentRunspace).AddCommand("Connect-Yubikey");
myPowersShellInstance.Invoke();
WriteDebug($"Successfully connected");
WriteDebug($"Successfully connected.");
}
}

Expand Down
4 changes: 2 additions & 2 deletions Module/Cmdlets/OATH/UnprotectYubikeyOATH.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ protected override void BeginProcessing()
{
if (YubiKeyModule._yubikey is null)
{
WriteDebug("No YubiKey selected, calling Connect-Yubikey");
WriteDebug("No YubiKey selected, calling Connect-Yubikey...");
var myPowersShellInstance = PowerShell.Create(RunspaceMode.CurrentRunspace).AddCommand("Connect-Yubikey");
myPowersShellInstance.Invoke();
WriteDebug($"Successfully connected");
WriteDebug($"Successfully connected.");
}
}

Expand Down
4 changes: 2 additions & 2 deletions Module/Cmdlets/OTP/GetYubikeyOTP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ protected override void BeginProcessing()
{
if (YubiKeyModule._yubikey is null)
{
WriteDebug("No YubiKey selected, calling Connect-Yubikey");
WriteDebug("No YubiKey selected, calling Connect-Yubikey...");
try
{
var myPowersShellInstance = PowerShell.Create(RunspaceMode.CurrentRunspace).AddCommand("Connect-Yubikey");
myPowersShellInstance.Invoke();
WriteDebug($"Successfully connected");
WriteDebug($"Successfully connected.");
}
catch (Exception e)
{
Expand Down
Loading

0 comments on commit 748d4c4

Please sign in to comment.