OathCodeInfo

public struct OathCodeInfo : Codable, Sendable

Contains OTP code information, including the actual code and validity details.

This class provides comprehensive information about a generated OTP code, including timing information for TOTP codes and counter information for HOTP codes.

Properties

  • The generated OTP code.

    Declaration

    Swift

    public let code: String
  • For TOTP, the time remaining in seconds before the code expires. For HOTP, this will be -1.

    Declaration

    Swift

    public let timeRemaining: Int
  • For HOTP, the current counter value after code generation. For TOTP, this will be -1.

    Declaration

    Swift

    public let counter: Int
  • For TOTP, a value from 0.0 to 1.0 indicating progress through the time window. For HOTP, this will be 0.0.

    Declaration

    Swift

    public let progress: Double
  • For TOTP, the total validity period in seconds. For HOTP, this will be 0.

    Declaration

    Swift

    public let totalPeriod: Int

Factory Methods

  • Creates an instance for a TOTP code.

    Declaration

    Swift

    public static func forTotp(
        code: String,
        timeRemaining: Int,
        totalPeriod: Int
    ) -> OathCodeInfo

    Parameters

    code

    The generated OTP code.

    timeRemaining

    The time remaining in seconds before the code expires.

    totalPeriod

    The total validity period in seconds.

    Return Value

    An OathCodeInfo instance configured for TOTP.

  • Creates an instance for a HOTP code.

    Declaration

    Swift

    public static func forHotp(
        code: String,
        counter: Int
    ) -> OathCodeInfo

    Parameters

    code

    The generated OTP code.

    counter

    The counter value after code generation.

    Return Value

    An OathCodeInfo instance configured for HOTP.

JSON Serialization

  • Converts this code info to a JSON string representation.

    This is a convenience method for cross-platform API consistency. You can also use Swift’s standard JSONEncoder directly:

    let encoder = JSONEncoder()
    let data = try encoder.encode(codeInfo)
    let jsonString = String(data: data, encoding: .utf8)
    

    Throws

    EncodingError if serialization fails.

    Declaration

    Swift

    public func toJson() throws -> String

    Return Value

    A JSON string representing this code info.

  • Creates an OathCodeInfo from a JSON string.

    This is a convenience method for cross-platform API consistency. You can also use Swift’s standard JSONDecoder directly:

    let decoder = JSONDecoder()
    let codeInfo = try decoder.decode(OathCodeInfo.self, from: jsonData)
    

    Throws

    DecodingError if the JSON is invalid.

    Declaration

    Swift

    public static func fromJson(_ jsonString: String) throws -> OathCodeInfo

    Parameters

    jsonString

    The JSON string to parse.

    Return Value

    An OathCodeInfo instance.