Blog | G5 Cyber Security

Offline User Authentication

TL;DR

Authenticating users on devices without constant internet access (like iPads) requires a local authentication system combined with secure syncing when online. This guide covers using a combination of device biometrics/passcodes, locally stored credentials, and a robust API for synchronisation.

1. Choose an Authentication Method

Select how users will initially log in on the iPad:

For this guide, we’ll assume using Device Biometrics as the primary method.

2. Local Credential Storage

You need a secure way to store user credentials on the iPad. Never store passwords in plain text! Use Apple’s Keychain:

import KeychainSwift
let keychain = KeychainSwift()
// Store username
keychain.set("username", forKey: "user_username")
// Retrieve username
if let username = keychain.get("user_username") {
  print(username)
}

Consider using a library like KeychainSwift to simplify Keychain interactions.

3. Implement Local Authentication

Use Apple’s LocalAuthentication framework:

  1. Import Framework: Add LocalAuthentication to your project.
  2. Context Creation: Create a LAContext object.
  3. Evaluate Policy: Use evaluatePolicy() to prompt the user for authentication (biometrics or passcode).
import LocalAuthentication
let context = LAContext()
var error: NSError?
if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics) {
  context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: "Authenticate to access the app") {
    (success, error) in
    if success {
      // Authentication successful!
      print("Authenticated successfully.")
    } else {
      // Authentication failed
      print(error?.localizedDescription ?? "Authentication Failed")
    }
  }
}

4. API for Synchronisation

Design a secure API endpoint to handle user authentication and data synchronisation when the device is online.

5. Offline Data Handling

The iPad needs to function even when offline:

6. Background Sync

Periodically attempt to sync data in the background:

7. Security Considerations

Exit mobile version