Token Management
Relevant source files
The following files were used as context for generating this wiki page:
Introduction
Token Management in the Social-Connect project facilitates secure storage and retrieval of authentication credentials used for network requests. The system relies on the iOS Keychain services to persist sensitive data and integrates directly with the networking layer to ensure every outgoing request to the Social-Connect API is properly authorized.
This module acts as the foundation for user-specific operations, such as following or unfollowing users, by providing the necessary Bearer tokens for the APIClient. The lifecycle of a token influences the entire application flow, from initial launch in the AppDelegate to deep link processing in the DeepLinkHandler.
Architecture and Components
Keychain Integration
The core of token persistence is handled by the KeychainManager. It uses the Apple Security framework to interface with the system keychain, ensuring that the authToken is stored securely and is not accessible to other applications.
- Class:
KeychainManager - Key Identifier:
authToken - Security Class:
kSecClassGenericPassword
The following diagram illustrates how the KeychainManager interacts with the system-level Security framework to retrieve tokens:
Sources: KeychainManager.swift:4-30
Authorization Flow in Networking
The APIClient serves as the consumer of the managed tokens. During the construction of any POST request, the client queries the KeychainManager for an existing token. If found, it injects the token into the HTTP headers using the Bearer authentication scheme.
| Component | Responsibility |
|---|---|
KeychainManager.shared.getAuthToken() | Retrieves the raw string token from secure storage. |
URLRequest.addValue(_:forHTTPHeaderField:) | Injects "Bearer [token]" into the "Authorization" header. |
APIClient.post(endpoint:completion:) | Coordinates the request lifecycle and handles the response. |
Sources: APIClient.swift:15-21, KeychainManager.swift:9-25
The sequence below demonstrates the token retrieval during a typical service call:
Sources: UserService.swift:9-15, APIClient.swift:7-40
Data Flow and Usage
Service Layer Integration
Tokens are implicitly required for high-level operations defined in UserService. Methods such as followUser and unfollowUser trigger the APIClient, which in turn relies on the Token Management system to authorize the request.
func followUser(userId: String, completion: ((Bool, Error?) -> Void)? = nil) {
let endpoint = "users/\(userId)/follow"
apiClient.post(endpoint: endpoint) { success, error in
// Logic proceeds only if token-authorized request succeeds
}
}
Sources: UserService.swift:7-17
Impact on Application Entry Points
The availability of a valid token indirectly affects how components like DeepLinkHandler function. When a deep link (e.g., socialconnect://follow?userId=123) is received by the AppDelegate, the DeepLinkHandler invokes the UserService. If the Token Management system does not have a valid token, the subsequent API calls initiated by the deep link will fail authorization.
Implementation Details
Security Constraints
The KeychainManager utilizes specific search attributes to ensure data integrity:
kSecMatchLimitOne: Ensures only the most recent/relevant token is returned.kSecReturnData: Requests the actual data payload from the keychain.kSecAttrAccount: Sets the lookup key specifically to "authToken".
Sources: KeychainManager.swift:10-15
API Configuration
All token-authorized requests are directed to the base URL: https://api.socialconnect.com/v1/. The Token Management system ensures that the Authorization header is correctly paired with the Content-Type: application/json header for all outgoing traffic.
Sources: APIClient.swift:4-16
Conclusion
Token Management in Social-Connect is a centralized system that abstracts the complexities of secure storage and HTTP authorization. By utilizing the KeychainManager as a singleton source of truth, the application ensures that user sessions are maintained securely across service calls, deep link navigations, and app restarts.