SecureTokenService.swift 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // Copyright 2023 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import Foundation
  15. private let kFiveMinutes = 5 * 60.0
  16. /// A class represents a credential that proves the identity of the app.
  17. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  18. @objc(FIRSecureTokenService) // objc Needed for decoding old versions
  19. class SecureTokenService: NSObject, NSSecureCoding {
  20. /// The configuration for making requests to server.
  21. var requestConfiguration: AuthRequestConfiguration?
  22. /// The cached access token.
  23. ///
  24. /// This method is specifically for providing the access token to internal clients during
  25. /// deserialization and sign-in events, and should not be used to retrieve the access token by
  26. /// anyone else.
  27. var accessToken: String
  28. /// The refresh token for the user, or `nil` if the user has yet completed sign-in flow.
  29. ///
  30. /// This property needs to be set manually after the instance is decoded from archive.
  31. var refreshToken: String?
  32. /// The expiration date of the cached access token.
  33. var accessTokenExpirationDate: Date?
  34. /// Creates a `SecureTokenService` with access and refresh tokens.
  35. /// - Parameter requestConfiguration: The configuration for making requests to server.
  36. /// - Parameter accessToken: The STS access token.
  37. /// - Parameter accessTokenExpirationDate: The approximate expiration date of the access token.
  38. /// - Parameter refreshToken: The STS refresh token.
  39. init(withRequestConfiguration requestConfiguration: AuthRequestConfiguration?,
  40. accessToken: String,
  41. accessTokenExpirationDate: Date?,
  42. refreshToken: String) {
  43. self.requestConfiguration = requestConfiguration
  44. self.accessToken = accessToken
  45. self.refreshToken = refreshToken
  46. self.accessTokenExpirationDate = accessTokenExpirationDate
  47. taskQueue = AuthSerialTaskQueue()
  48. }
  49. /// Fetch a fresh ephemeral access token for the ID associated with this instance. The token
  50. /// received in the callback should be considered short lived and not cached.
  51. ///
  52. /// Invoked asyncronously on the auth global work queue in the future.
  53. /// - Parameter forceRefresh: Forces the token to be refreshed.
  54. /// - Parameter callback: Callback block that will be called to return either the token or an
  55. /// error.
  56. func fetchAccessToken(forcingRefresh forceRefresh: Bool,
  57. callback: @escaping (String?, Error?, Bool) -> Void) {
  58. taskQueue.enqueueTask { complete in
  59. if !forceRefresh, self.hasValidAccessToken() {
  60. complete()
  61. callback(self.accessToken, nil, false)
  62. } else {
  63. AuthLog.logDebug(code: "I-AUT000017", message: "Fetching new token from backend.")
  64. Task {
  65. do {
  66. let (token, tokenUpdated) = try await self.requestAccessToken(retryIfExpired: true)
  67. complete()
  68. callback(token, nil, tokenUpdated)
  69. } catch {
  70. complete()
  71. callback(nil, error, false)
  72. }
  73. }
  74. }
  75. }
  76. }
  77. private let taskQueue: AuthSerialTaskQueue
  78. // MARK: NSSecureCoding
  79. // Secure coding keys
  80. private let kAPIKeyCodingKey = "APIKey"
  81. private static let kRefreshTokenKey = "refreshToken"
  82. private static let kAccessTokenKey = "accessToken"
  83. private static let kAccessTokenExpirationDateKey = "accessTokenExpirationDate"
  84. static var supportsSecureCoding: Bool {
  85. true
  86. }
  87. required convenience init?(coder: NSCoder) {
  88. guard let refreshToken = coder.decodeObject(of: [NSString.self],
  89. forKey: Self.kRefreshTokenKey) as? String,
  90. let accessToken = coder.decodeObject(of: [NSString.self],
  91. forKey: Self.kAccessTokenKey) as? String else {
  92. return nil
  93. }
  94. let accessTokenExpirationDate = coder.decodeObject(
  95. of: [NSDate.self], forKey: Self.kAccessTokenExpirationDateKey
  96. ) as? Date
  97. // requestConfiguration is filled in after User is set by Auth.protectedDataInitialization.
  98. self.init(withRequestConfiguration: nil,
  99. accessToken: accessToken,
  100. accessTokenExpirationDate: accessTokenExpirationDate,
  101. refreshToken: refreshToken)
  102. }
  103. func encode(with coder: NSCoder) {
  104. // The API key is encoded even it is not used in decoding to be compatible with previous
  105. // versions of the library.
  106. coder.encode(requestConfiguration?.apiKey, forKey: kAPIKeyCodingKey)
  107. // Authorization code is not encoded because it is not long-lived.
  108. coder.encode(refreshToken, forKey: SecureTokenService.kRefreshTokenKey)
  109. coder.encode(accessToken, forKey: SecureTokenService.kAccessTokenKey)
  110. coder.encode(
  111. accessTokenExpirationDate,
  112. forKey: SecureTokenService.kAccessTokenExpirationDateKey
  113. )
  114. }
  115. // MARK: Private methods
  116. /// Makes a request to STS for an access token.
  117. ///
  118. /// This handles both the case that the token has not been granted yet and that it just
  119. /// needs to be refreshed. The caller is responsible for making sure that this is occurring in
  120. /// a `_taskQueue` task.
  121. ///
  122. /// Because this method is guaranteed to only be called from tasks enqueued in
  123. /// `_taskQueue`, we do not need any @synchronized guards around access to _accessToken/etc.
  124. /// since only one of those tasks is ever running at a time, and those tasks are the only
  125. /// access to and mutation of these instance variables.
  126. /// - Returns: Token and Bool indicating if update occurred.
  127. private func requestAccessToken(retryIfExpired: Bool) async throws -> (String?, Bool) {
  128. // TODO: This was a crash in ObjC SDK, should it callback with an error?
  129. guard let refreshToken, let requestConfiguration else {
  130. fatalError("refreshToken and requestConfiguration should not be nil")
  131. }
  132. let request = SecureTokenRequest.refreshRequest(refreshToken: refreshToken,
  133. requestConfiguration: requestConfiguration)
  134. let response = try await AuthBackend.call(with: request)
  135. var tokenUpdated = false
  136. if let newAccessToken = response.accessToken,
  137. newAccessToken.count > 0,
  138. newAccessToken != accessToken {
  139. let tokenResult = AuthTokenResult.tokenResult(token: newAccessToken)
  140. // There is an edge case where the request for a new access token may be made right
  141. // before the app goes inactive, resulting in the callback being invoked much later
  142. // with an expired access token. This does not fully solve the issue, as if the
  143. // callback is invoked less than an hour after the request is made, a token is not
  144. // re-requested here but the approximateExpirationDate will still be off since that
  145. // is computed at the time the token is received.
  146. if retryIfExpired,
  147. let expirationDate = tokenResult?.expirationDate,
  148. expirationDate.timeIntervalSinceNow <= kFiveMinutes {
  149. // We only retry once, to avoid an infinite loop in the case that an end-user has
  150. // their local time skewed by over an hour.
  151. return try await requestAccessToken(retryIfExpired: false)
  152. }
  153. accessToken = newAccessToken
  154. accessTokenExpirationDate = response.approximateExpirationDate
  155. tokenUpdated = true
  156. AuthLog.logDebug(
  157. code: "I-AUT000017",
  158. message: "Updated access token. Estimated expiration date: " +
  159. "\(String(describing: accessTokenExpirationDate)), current date: \(Date())"
  160. )
  161. }
  162. if let newRefreshToken = response.refreshToken,
  163. newRefreshToken != self.refreshToken {
  164. self.refreshToken = newRefreshToken
  165. tokenUpdated = true
  166. }
  167. return (response.accessToken, tokenUpdated)
  168. }
  169. private func hasValidAccessToken() -> Bool {
  170. if let accessTokenExpirationDate,
  171. accessTokenExpirationDate.timeIntervalSinceNow > kFiveMinutes {
  172. AuthLog.logDebug(code: "I-AUT000017",
  173. message: "Has valid access token. Estimated expiration date:" +
  174. "\(accessTokenExpirationDate), current date: \(Date())")
  175. return true
  176. }
  177. AuthLog.logDebug(code: "I-AUT000017",
  178. message: "Does not have valid access token. Estimated expiration date:" +
  179. "\(String(describing: accessTokenExpirationDate)), current date: \(Date())")
  180. return false
  181. }
  182. }