Table of Contents

Class OperationBase<T>

Namespace
Yubico.YubiKey.Otp.Operations
Assembly
Yubico.YubiKey.dll

Base class for OTP configuration operations.

public abstract class OperationBase<T> where T : OperationBase<T>

Type Parameters

T

The child class type.

Inheritance
object
OperationBase<T>
Derived

Remarks

The reference to the child type allows builder methods and properties to return a reference to the calling class to allow chaining.

Constructors

OperationBase(IYubiKeyConnection, IOtpSession, Slot?)

Constructs as OperationBase<T> instance.

protected OperationBase(IYubiKeyConnection yubiKey, IOtpSession session, Slot? slot = null)

Parameters

yubiKey IYubiKeyConnection

The connection to the YubiKey session.

session IOtpSession

Reference to IOtpSession instance.

slot Slot?

Optional parameter specifying the slot to configure. Important: Inheriting classes that configure an OTP slot must supply this.

Remarks

This generic abstract class is meant to be inherited from by a class that specifies itself as the templated type. This is so that this base class can allow the child class to call methods to configure settings in OtpSettings<T> and get back a reference to the calling class. This allows us to use method-chaining to implement the fluent builder pattern.

Most, but not all, classes that inherit from OperationBase<T> will be configuring an OTP slot. Those classes must call this constructor with a valid value. Classes that do not configure a slot should either not specify this parameter, or call it with null.

Properties

Connection

Reference to the IYubiKeyConnection for the YubiKey being configured.

protected IYubiKeyConnection Connection { get; }

Property Value

IYubiKeyConnection

CurrentAccessCode

The six-byte access code currently set to protect the OTP slot.

protected Span<byte> CurrentAccessCode { get; set; }

Property Value

Span<byte>

NewAccessCode

The six-byte access code to set for the OTP slot after applying the configuration.

protected Span<byte> NewAccessCode { get; set; }

Property Value

Span<byte>

OtpSlot

The OTP Slot to configure.

protected Slot? OtpSlot { get; set; }

Property Value

Slot?

Session

A reference to the IOtpSession object that created the operation.

protected IOtpSession Session { get; set; }

Property Value

IOtpSession

Version

The firmware version on the YubiKey this task is associated with.

public FirmwareVersion Version { get; }

Property Value

FirmwareVersion

Methods

Execute()

Commit the settings and perform the operation.

public void Execute()

ExecuteOperation()

Execute the operation here.

protected abstract void ExecuteOperation()

Remarks

This method is called after pre-launch code has run. Everything that could be validated should have been before this method is called.

The only validation could that should be in this method are things that could not be checked in the PreLaunchOperation() method. For example, if an operation must be completed in multiple steps, and subsequent steps depend on the success of previous steps, then it must be in this method by necessity.

PreLaunchOperation()

Validate all settings and choices here.

protected virtual void PreLaunchOperation()

Remarks

All possible validation should be done here. The point of this method is to simplify the ExecuteOperation() method that each operation must implement.

Conflicting choices that could not be checked by the OtpSettings<T> methods should be checked here.

Many of the operation classes use nullable fields (bool?) for choices. This allows the PreLaunchOperation() implementation to verify that a choice has been made. In the ExecuteOperation() method, the field has already been validated, and an exception thrown if it was not set, so null-forgiving operators are used when accessing those fields in ExecuteOperation().

SetNewAccessCode(SlotAccessCode)

Set the new access code the YubiKey slot will be programmed with.

public T SetNewAccessCode(SlotAccessCode code)

Parameters

code SlotAccessCode

The SlotAccessCode to use.

Returns

T

The ConfigureStaticPassword instance

Remarks

There are two access code methods - UseCurrentAccessCode(SlotAccessCode) and SetNewAccessCode(SlotAccessCode). If the YubiKey slot is currently protected by an access code, it must be specified when you call UseCurrentAccessCode(SlotAccessCode). If you wish to retain the same access code, it must also be specified to SetNewAccessCode(SlotAccessCode). If you do not specify it as the new access code, then the slot will have its protection removed and have no access code.

Exceptions

ArgumentException

Thrown if the access code is longer than MaxAccessCodeLength.

UseCurrentAccessCode(SlotAccessCode)

Set the current access code the YubiKey slot is programmed with.

public T UseCurrentAccessCode(SlotAccessCode code)

Parameters

code SlotAccessCode

The SlotAccessCode to use.

Returns

T

The ConfigureStaticPassword instance

Remarks

There are two access code methods - UseCurrentAccessCode(SlotAccessCode) and SetNewAccessCode(SlotAccessCode). If the YubiKey slot is currently protected by an access code, it must be specified when you call UseCurrentAccessCode(SlotAccessCode). If you wish to retain the same access code, it must also be specified to SetNewAccessCode(SlotAccessCode). If you do not specify it as the new access code, then the slot will have its protection removed and have no access code.

Exceptions

ArgumentException

Thrown if the access code is longer than MaxAccessCodeLength.