OathClient
public final class OathClient : @unchecked Sendable
Main public interface for OATH functionality. This client handles TOTP and HOTP credential management and code generation.
The OathClient provides a high-level interface for managing OATH credentials, including adding credentials from URIs, generating OTP codes, and managing credential storage with policy enforcement.
Example usage:
let client = try await OathClient.createClient { config in
config.storage = OathKeychainStorage()
config.enableCredentialCache = false
config.logger = LogManager.logger
}
let credential = try await client.addCredentialFromUri("otpauth://totp/Example:user@example.com?secret=JBSWY3DPEHPK3PXP")
let code = try await client.generateCode(credential.id)
-
createClient(configure:Asynchronous) Factory method to create and initialize an OathClient instance.
Throws
OathError.initializationFailedif client initialization fails.Example usage:
let client = try await OathClient.createClient { config in config.encryptionEnabled = false config.logger = LogManager.logger config.storage = OathKeychainStorage() }Declaration
Swift
public static func createClient( configure: (OathConfiguration) -> Void = { _ in } ) async throws -> OathClientParameters
configureA closure to configure the OathConfiguration.
Return Value
A fully initialized OathClient instance.
-
addCredentialFromUri(_:Asynchronous) Creates an OATH credential from a standard otpauth:// URI.
Throws
OathError.invalidUriif the URI is malformed.Throws
OathError.policyViolationif credential policies are not met.Throws
OathStorageErrorif storage operations fail.Supported URI formats:
otpauth://totp/Issuer:Account?secret=SECRET&issuer=Issuer&algorithm=SHA1&digits=6&period=30otpauth://hotp/Issuer:Account?secret=SECRET&issuer=Issuer&algorithm=SHA1&digits=6&counter=0mfauth://totp/Issuer:Account?secret=SECRET&issuer=Issuer&algorithm=SHA1&digits=6&period=30
Declaration
Swift
public func addCredentialFromUri(_ uri: String) async throws -> OathCredentialParameters
uriThe otpauth:// or mfauth:// URI string.
Return Value
The created and stored OathCredential.
-
saveCredential(_:Asynchronous) Saves or updates an OATH credential in storage.
Throws
OathError.policyViolationif credential policies are not met.Throws
OathStorageErrorif storage operations fail.Declaration
Swift
public func saveCredential(_ credential: OathCredential) async throws -> OathCredentialParameters
credentialThe credential to save or update.
Return Value
The saved credential with any policy updates applied.
-
getCredentials()AsynchronousRetrieves all stored OATH credentials.
Throws
OathStorageErrorif storage operations fail.Declaration
Swift
public func getCredentials() async throws -> [OathCredential]Return Value
An array of all stored OathCredential objects.
-
getCredential(_:Asynchronous) Retrieves a specific OATH credential by its ID.
Throws
OathStorageErrorif storage operations fail.Declaration
Swift
public func getCredential(_ credentialId: String) async throws -> OathCredential?Parameters
credentialIdThe unique identifier of the credential.
Return Value
The OathCredential if found, nil otherwise.
-
deleteCredential(_:Asynchronous) Deletes an OATH credential by its ID.
Throws
OathStorageErrorif storage operations fail.Declaration
Swift
public func deleteCredential(_ credentialId: String) async throws -> BoolParameters
credentialIdThe unique identifier of the credential to delete.
Return Value
true if the credential was deleted, false if it wasn’t found.
-
generateCode(_:Asynchronous) Generates a one-time password (OTP) for a given credential.
Throws
OathError.credentialNotFoundif the credential doesn’t exist.Throws
OathError.credentialLockedif the credential is locked.Throws
OathError.codeGenerationFailedif code generation fails.For HOTP credentials, this method will automatically increment the counter. For TOTP credentials, the code is valid for the current time period.
Declaration
Swift
public func generateCode(_ credentialId: String) async throws -> StringParameters
credentialIdThe unique identifier of the credential.
Return Value
The generated OTP code as a string.
-
generateCodeWithValidity(_:Asynchronous) Generates an OTP with additional information about its validity.
Throws
OathError.credentialNotFoundif the credential doesn’t exist.Throws
OathError.credentialLockedif the credential is locked.Throws
OathError.codeGenerationFailedif code generation fails.This method provides additional information such as:
- For TOTP: time remaining before expiration and progress through the time window
- For HOTP: the counter value used for generation
Declaration
Swift
public func generateCodeWithValidity(_ credentialId: String) async throws -> OathCodeInfoParameters
credentialIdThe unique identifier of the credential.
Return Value
OathCodeInfo containing the code and validity information.
-
close()AsynchronousClean up resources used by the OATH client. This method should be called when the client is no longer needed to ensure proper cleanup of resources and cached data.
Throws
OathError.cleanupFailedif cleanup operations fail.Declaration
Swift
public func close() async throws
View on GitHub