Backend Integration Helpers

This page provides complete implementations of helper classes and functions required for backend-proxied deployments. These helpers handle backend API communication and transform FIDO2 server responses into formats required by the SDK.

Note: These are reference implementations. You may need to adapt them to your specific backend API contract, error handling requirements, and security policies.

iOS (Swift)

BackendApiClient

The BackendApiClient class handles communication with your backend services, including session cookie management.

import Foundation

class BackendApiClient {
    private let baseUrl: String
    private var sessionCookies: String?
    
    init(baseUrl: String) {
        self.baseUrl = baseUrl
    }
    
    func clearSessionCookies() {
        sessionCookies = nil
    }
    
    func registerBegin(username: String, displayName: String, rpId: String) async throws -> [String: Any] {
        let url = URL(string: "\(baseUrl)/fido/register")!
        var request = URLRequest(url: url)
        request.httpMethod = "POST"
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        
        let body: [String: Any] = [
            "operation": "register",
            "username": username,
            "displayName": displayName,
            "rpId": rpId
        ]
        request.httpBody = try JSONSerialization.data(withJSONObject: body)
        
        let (data, _) = try await URLSession.shared.data(for: request)
        
        guard let json = try JSONSerialization.jsonObject(with: data) as? [String: Any] else {
            throw NSError(domain: "BackendApiClient", code: -1, userInfo: [NSLocalizedDescriptionKey: "Invalid response"])
        }
        
        // Extract cookies from JSON response body
        extractAndStoreCookies(from: json)
        
        return json
    }
    
    func registerComplete(username: String, cardResponseData: [String: Any]) async throws -> [String: Any] {
        let url = URL(string: "\(baseUrl)/fido/register")!
        var request = URLRequest(url: url)
        request.httpMethod = "POST"
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        
        var body: [String: Any] = [
            "operation": "register",
            "username": username,
            "cardResponseData": cardResponseData
        ]
        
        if let cookies = sessionCookies {
            body["cookies"] = cookies
            request.setValue(cookies, forHTTPHeaderField: "Cookie")
        }
        
        request.httpBody = try JSONSerialization.data(withJSONObject: body)
        
        let (data, _) = try await URLSession.shared.data(for: request)
        
        guard let json = try JSONSerialization.jsonObject(with: data) as? [String: Any] else {
            throw NSError(domain: "BackendApiClient", code: -1, userInfo: [NSLocalizedDescriptionKey: "Invalid response"])
        }
        
        return json
    }
    
    func authenticateBegin(username: String, rpId: String) async throws -> [String: Any] {
        let url = URL(string: "\(baseUrl)/fido/authenticate")!
        var request = URLRequest(url: url)
        request.httpMethod = "POST"
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        
        let body: [String: Any] = [
            "operation": "authenticate",
            "username": username,
            "rpId": rpId
        ]
        request.httpBody = try JSONSerialization.data(withJSONObject: body)
        
        let (data, _) = try await URLSession.shared.data(for: request)
        
        guard let json = try JSONSerialization.jsonObject(with: data) as? [String: Any] else {
            throw NSError(domain: "BackendApiClient", code: -1, userInfo: [NSLocalizedDescriptionKey: "Invalid response"])
        }
        
        // Extract cookies from JSON response body
        extractAndStoreCookies(from: json)
        
        return json
    }
    
    func authenticateComplete(
        cardResponseData: [String: Any],
        sessionId: String?,
        username: String
    ) async throws -> [String: Any] {
        let url = URL(string: "\(baseUrl)/fido/authenticate")!
        var request = URLRequest(url: url)
        request.httpMethod = "POST"
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        
        var body: [String: Any] = [
            "operation": "authenticate",
            "username": username,
            "cardResponseData": cardResponseData
        ]
        
        if let sessionId = sessionId {
            body["sessionId"] = sessionId
        }
        
        if let cookies = sessionCookies {
            body["cookies"] = cookies
            request.setValue(cookies, forHTTPHeaderField: "Cookie")
        }
        
        request.httpBody = try JSONSerialization.data(withJSONObject: body)
        
        let (data, _) = try await URLSession.shared.data(for: request)
        
        guard let json = try JSONSerialization.jsonObject(with: data) as? [String: Any] else {
            throw NSError(domain: "BackendApiClient", code: -1, userInfo: [NSLocalizedDescriptionKey: "Invalid response"])
        }
        
        return json
    }
    
    private func extractAndStoreCookies(from response: [String: Any]) {
        if let cookies = response["cookies"] as? String, !cookies.isEmpty {
            sessionCookies = cookies
        }
    }
}

createOptionsResponseFromBackend

This helper function constructs an ArculusFidoOptionsAuthorizationResponse from the backend response for use with authenticateCardOnly().

Android (Java)

BackendApiClient

The BackendApiClient class handles communication with your backend services, including session cookie management and SSL certificate handling.

FidoOptionsHelper

The FidoOptionsHelper class provides a static method to transform backend responses into ArculusFidoOptionsAuthorizationResponse objects for use with authenticateCardOnly().

Desktop (Java)

BackendApiClient

The BackendApiClient class handles communication with your backend services, including session cookie management.

FidoOptionsHelper

The FidoOptionsHelper class provides a static method to transform backend responses into ArculusFidoOptionsAuthorizationResponse objects for use with authenticateCardOnly().

Last updated