Session Management
Relevant source files
The following files were used as context for generating this wiki page:
The Session Management system in Social-Connect handles the lifecycle of user authentication and authorized communication with the backend services. It ensures that sensitive credentials, specifically the authentication token, are securely stored and retrieved to sign network requests.
The system acts as a bridge between secure local storage and the networking layer, allowing features like user interactions and deep-link processing to operate within an authenticated context.
Authentication Architecture
The architecture relies on a centralized KeychainManager to interface with the iOS Security framework and an APIClient that automatically injects credentials into outgoing requests.
Core Components
| Component | Responsibility |
|---|---|
KeychainManager | Manages secure storage and retrieval of the authToken. |
APIClient | Handles HTTP communication and injects the Bearer token into headers. |
UserService | Provides high-level user actions (follow/unfollow) that require an active session. |
DeepLinkHandler | Processes external URLs and triggers session-dependent actions. |
Sources: KeychainManager.swift, APIClient.swift, UserService.swift, DeepLinkHandler.swift
Data Flow for Authorized Requests
When a service requests a protected resource, the system follows a specific flow to retrieve the token and authorize the request.
The APIClient checks for a token via KeychainManager.shared.getAuthToken() before every POST request. If found, it is added as a Bearer token in the Authorization header.
Sources: APIClient.swift:16-20, KeychainManager.swift:10-24
Token Security and Storage
Authentication tokens are stored using the iOS Keychain to ensure they persist across app restarts and remain encrypted.
Keychain Implementation
The KeychainManager uses the kSecClassGenericPassword class to store the token under the account name "authToken".
func getAuthToken() -> String? {
let query: [String: Any] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrAccount as String: "authToken",
kSecReturnData as String: true,
kSecMatchLimit as String: kSecMatchLimitOne
]
// ...
}
Sources: KeychainManager.swift:11-16
Integration with Application Lifecycle
Session-based actions can be triggered directly from the application lifecycle, such as when the app is opened via a deep link.
Deep Link Authorization
The AppDelegate intercepts incoming URLs and passes them to the DeepLinkHandler. If a deep link requires a user action (like following a user), the UserService is invoked, which in turn utilizes the current session via APIClient.
API Session Headers
All requests routed through the APIClient include standard headers to maintain session consistency with the backend.
| Header | Value | Condition |
|---|---|---|
Content-Type | application/json | Always |
Authorization | Bearer {token} | If getAuthToken() returns data |
Sources: APIClient.swift:14-20
Conclusion
Session Management in Social-Connect provides a secure, abstracted way to handle user authentication. By centralizing token retrieval in the KeychainManager and automating header injection in the APIClient, the application ensures that all user-centric services, such as UserService and DeepLinkHandler, can interact with the backend securely without manually managing credentials at the call site.