Skip to content

Commit 93d8d7b

Browse files
committed
refactor(otp): Simplify command invocation and error handling in OtpErrorTransform
1 parent 8f24ae9 commit 93d8d7b

File tree

1 file changed

+6
-21
lines changed

1 file changed

+6
-21
lines changed

Yubico.YubiKey/src/Yubico/YubiKey/Pipelines/OtpErrorTransform.cs

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,15 @@ public ResponseApdu Invoke(CommandApdu command, Type commandType, Type responseT
4141
if (commandType == typeof(ReadStatusCommand) ||
4242
responseType != typeof(ReadStatusResponse))
4343
{
44-
return InvokeCommand(command, commandType, responseType);
44+
return _nextTransform.Invoke(command, commandType, responseType);
4545
}
4646

4747
// Otherwise we assume this to be a command that applies a config (and therefore looks for a status response).
4848
// In order to detect failures, we grab the status structure before applying said command so that we have a
4949
// sequence number to compare to.
5050
var beforeStatus = GetCurrentStatus();
51-
var responseApdu = InvokeCommand(command, commandType, responseType, out var afterStatus);
51+
var responseApdu = _nextTransform.Invoke(command, commandType, responseType);
52+
var afterStatus = ReadStatus(responseApdu);
5253

5354
try
5455
{
@@ -62,7 +63,7 @@ public ResponseApdu Invoke(CommandApdu command, Type commandType, Type responseT
6263
{
6364
_logger.LogWarning(e, "Handling keyboard connection exception. Translating to APDU response.");
6465

65-
return CreateFailedApdu([]);
66+
return CreateFailedApdu();
6667
}
6768
}
6869

@@ -83,20 +84,6 @@ internal static bool IsValidSequenceProgression(OtpStatus beforeStatus, OtpStatu
8384
return normalIncrement || validReset;
8485
}
8586

86-
private ResponseApdu InvokeCommand(
87-
CommandApdu commandApdu,
88-
Type commandType,
89-
Type responseType,
90-
out OtpStatus afterStatus)
91-
{
92-
var responseApdu = _nextTransform.Invoke(commandApdu, commandType, responseType);
93-
afterStatus = ReadStatus(responseApdu);
94-
return responseApdu;
95-
}
96-
97-
private ResponseApdu InvokeCommand(CommandApdu commandApdu, Type commandType, Type responseType) =>
98-
_nextTransform.Invoke(commandApdu, commandType, responseType);
99-
10087
private OtpStatus GetCurrentStatus()
10188
{
10289
var command = new ReadStatusCommand();
@@ -105,9 +92,7 @@ private OtpStatus GetCurrentStatus()
10592
typeof(ReadStatusCommand),
10693
typeof(ReadStatusResponse));
10794

108-
return command
109-
.CreateResponseForApdu(responseApdu)
110-
.GetData();
95+
return ReadStatus(responseApdu);
11196
}
11297

11398
private static OtpStatus ReadStatus(ResponseApdu responseApdu)
@@ -117,6 +102,6 @@ private static OtpStatus ReadStatus(ResponseApdu responseApdu)
117102
return afterStatus;
118103
}
119104

120-
private static ResponseApdu CreateFailedApdu(byte[] data) => new(data, SWConstants.WarningNvmUnchanged);
105+
private static ResponseApdu CreateFailedApdu(byte[]? data = null) => new(data ?? [], SWConstants.WarningNvmUnchanged);
121106
}
122107
}

0 commit comments

Comments
 (0)