Security Architecture
Relevant source files
The following files were used as context for generating this wiki page:
The Security Architecture of the Social-Connect project focuses on secure credential storage, authenticated network communication, and safe handling of external entry points. The system ensures that sensitive user data, specifically authentication tokens, are handled using platform-specific secure storage mechanisms rather than insecure alternatives like UserDefaults.
The architecture follows a layered approach where the KeychainManager acts as the low-level security provider, the APIClient serves as the secure transport layer, and higher-level services like UserService and DeepLinkHandler utilize these components to perform authorized actions.
Secure Token Management
The application utilizes the iOS Keychain Services via the KeychainManager class to store and retrieve authentication tokens. This ensures that sensitive credentials are encrypted and persisted securely across application launches.
Keychain Implementation details
The implementation uses a generic password class (kSecClassGenericPassword) to store the "authToken". This involves querying the Keychain with specific attributes to retrieve data as a UTF-8 encoded string.
The diagram illustrates the flow of retrieving a secure token from the system keychain.
| Component | Responsibility | Relevant Logic |
|---|---|---|
KeychainManager | Singleton manager for sensitive data | getAuthToken() |
kSecClass | Defines the type of item to find | kSecClassGenericPassword |
kSecAttrAccount | Unique identifier for the token | "authToken" |
Sources: KeychainManager.swift:4-30
Authenticated Communication
All outbound requests to the backend API are managed by the APIClient. Security is enforced by automatically attaching a Bearer token to the Authorization header of every POST request if a token exists in the Keychain.
Authorization Flow
The APIClient interacts with the KeychainManager before initiating a URLSessionDataTask. If a token is successfully retrieved, it is appended to the request headers to authorize the user for the specific action.
Sequence of events showing how authentication tokens are injected into network requests.
Sources: APIClient.swift:7-25, UserService.swift:10-14
External Entry Point Security
The application handles external URLs through the DeepLinkHandler. This system parses incoming URLs to execute internal logic, such as following users or navigating to profiles.
Deep Link Validation
The AppDelegate routes all incoming URLs through the application(_:open:options:) method to the DeepLinkHandler. The handler validates the URL components, specifically the host and query items, before invoking business logic in the UserService.
Flow of an external deep link being processed and validated before execution.
| Host | Action | Security Context |
|---|---|---|
profile | Navigates to user profile | Requires valid userId parameter |
follow | Executes a follow request | Requires userId and valid Auth Token in APIClient |
Sources: AppDelegate.swift:14-17, DeepLinkHandler.swift:6-38
Network Security Configuration
The system uses a centralized base URL and standardizes header management to reduce the risk of sensitive data leakage.
// From APIClient.swift
private let baseURL = "https://api.socialconnect.com/v1/"
// Header Injection
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
if let token = KeychainManager.shared.getAuthToken() {
request.addValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
}
Sources: APIClient.swift:5-18
Summary
The Security Architecture of Social-Connect relies on the iOS Keychain for secret persistence and a centralized APIClient for header-based authentication. By decoupling the credential storage (KeychainManager) from the business logic (UserService), the application maintains a clean separation of concerns while ensuring all network mutations are authorized via Bearer tokens. External interactions via deep links are strictly routed through a handler that validates the request structure before interacting with sensitive services.