Skip to content

sentryco/BioAuth

Repository files navigation

codebeat badge Tests

BioAuth 🧬

Biometric authentication kit

Description

BioAuth is a Swift library that provides a simple and easy-to-use interface for biometric authentication on iOS devices. It supports both Face ID and Touch ID, and provides a unified API for both methods.

BioAuth provides a static context for LAContext and a method to initiate the biometric authentication process. It also includes utility methods to determine if biometric authentication is available and accessible on the device.

The library uses the modern Result type for better error handling and includes descriptive error messages for better debugging. It also provides a way to retrieve available BioAuth types, including touch, face, and none.

BioAuth is designed to be easy to integrate into any project that requires biometric authentication. It is also designed with security in mind, and includes features such as the ability to invalidate the context when the app is closed or goes into the background, and a timer to invalidate the context after a period of inactivity.

Features

  • Provides a way to retrieve available BioAuth types, including touch, face, and none
  • Includes descriptive error messages for better debugging
  • Uses the modern Result type for better error handling

Installation via Swift Package Manager (SPM)

You can add BioAuth to your project via Swift Package Manager, which is integrated into the Swift build system as of Swift 5.9. To add BioAuth to your project, modify your Package.swift file to include the following dependency:

.package(url: "https://github.com/sentryco/BioAuth", branch: "main")

This will configure your project to use the main branch of the BioAuth library from the specified GitHub repository.

Examples:

BioAuthType.type // .face

// Initialize BioAuth and handle the result using the `Result` type
BioAuth.initBioAuth { [weak self] result in
   switch result {
   case .success(let desc): notifyUser(desc) // If the result is successful, notify the user with the description
   case .failure(let error): notifyUser("Error", error.localizedDescription) // If the result is a failure, notify the user with the error message
   }
}

// Define a function to notify the user with an alert
func notifyUser(_ msg: String, err: String?) {
   let alert = UIAlertController(title: msg, message: err, preferredStyle: .alert) // Create a new alert controller with the given title, message, and style
   let cancelAction = UIAlertAction(title: "OK", style: .cancel, handler: nil) // Create a new cancel action with the given title and style, and set the handler to `nil`
   alert.addAction(cancelAction) // Add the cancel action to the alert controller
   self.present(alert, animated: true, completion: nil) // Present the alert controller with animation and no completion handler
}

Example (mac)

BioAuth.initBioAuth { [weak self] result in
   switch result {
   case .success(let desc): self?.notifyUser(desc.msg)
   // - Fixme: ⚠️️ might need to cast error as BioAuthError to get correct localizedDescription
   case .failure(let error): self?.notifyUser("Error", err: error.localizedDescription)
   }
}
func notifyUser(_ msg: String, err: String? = nil) {
   let alert = NSAlert() // Create a new `NSAlert` instance
   alert.messageText = "Biometric authentication" // Set the message text of the alert to "Biometric authentication"
   alert.informativeText = msg // Set the informative text of the alert to the given message
   alert.alertStyle = .warning // Set the alert style to `.warning`
   alert.addButton(withTitle: "OK") // Add an "OK" button to the alert
   alert.addButton(withTitle: "Cancel") // Add a "Cancel" button to the alert
   let res = alert.runModal() // Run the alert modally and get the result
   if res == .alertFirstButtonReturn { // If the user clicked the "OK" button, print "ok"
      Swift.print("ok")
   } else if res == .alertSecondButtonReturn { // If the user clicked the "Cancel" button, print "cancel"
      Swift.print("cancel")
   }
}

Resources:

License:

This software is licensed under the MIT License. Please ensure to adhere to the licensing terms when using, modifying, or distributing this code.

Todo:

About

Biometric authentication kit

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages