Table of Contents

Class YubiKeyResponse

Namespace
Yubico.YubiKey
Assembly
Yubico.YubiKey.dll

Base class for all YubiKey responses.

public class YubiKeyResponse : IYubiKeyResponse
Inheritance
object
YubiKeyResponse
Implements
Derived

Remarks

This base class is primarily responsible for mapping YubiKey specific status words to more generic constructs like ResponseStatus and exceptions.

This class can also be overridden to customize error handling if a certain application or command requires special casing.

If the subtype needs to change the mappings associated with an existing status code, it should override StatusCodeMap. For example:

public class MyResponse : YubiKeyResponse
{
    // MyResponse has custom definitions for what certain StatusWord
    // values mean. It only has to override this map, providing
    // the new values. It calls the base method so that any unknown
    // values are passed through, potentially being "understood"
    // by types MyResponse inherited from.
    protected override ResponseStatusPair StatusCodeMap =>
        StatusWord switch
        {
            // Add new maps or override existing ones here
            _ => base.StatusCodeMap,
        };

    public MyResponse(ResponseApdu responseApdu) : base(responseApdu)
    {
    }
}

StatusCodeMap can also be overridden if the subtype introduces a new status code. This typically happens when the ResponseApdu.Data is actually an encoded message which contains its own status code.

Constructors

YubiKeyResponse(ResponseApdu)

Initializes a new instance of the YubiKeyResponse class.

public YubiKeyResponse(ResponseApdu responseApdu)

Parameters

responseApdu ResponseApdu

The ResponseApdu from the YubiKey.

Exceptions

ArgumentNullException

responseApdu

Properties

ResponseApdu

The APDU returned by the YubiKey.

protected ResponseApdu ResponseApdu { get; set; }

Property Value

ResponseApdu

Status

An application independent status.

public ResponseStatus Status { get; }

Property Value

ResponseStatus

ResponseStatus.Success, ResponseStatus.Failed, etc.

Remarks

The Status property communicates many common error conditions. For example there is no data to return, or the command required a touch interaction. These errors are best checked and handled before calling methods that use the data returned by the YubiKey such as GetData(). (this is known as the Tester-Doer pattern).

StatusCodeMap

Retrieves the details describing the processing state.

protected virtual YubiKeyResponse.ResponseStatusPair StatusCodeMap { get; }

Property Value

YubiKeyResponse.ResponseStatusPair

The ResponseStatus and a descriptive message, as a YubiKeyResponse.ResponseStatusPair.

Remarks

Implementers of subtypes can override this member to change or add mappings.

StatusMessage

A short textual description of the status.

public string StatusMessage { get; }

Property Value

string

Remarks

This intended to displayed to the end-user, when an unhandled error occurs. Programs should use Status for normal flow control.

StatusWord

The application specific status word.

public short StatusWord { get; }

Property Value

short

0x9000, 0x6A82, etc.

Remarks

This is the two-byte response code of the response APDU. It is also known as the "Status Word", made up of SW1 and SW2. For example, the response code for Success is 9000, which is SW1=90 and SW2=00.

Methods

ToString()

public override string ToString()

Returns

string

See Also