AppCheckAPITests.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //
  2. // Copyright 2021 Google LLC
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. // MARK: This file is used to evaluate the experience of using Firebase APIs in Swift.
  17. import Foundation
  18. import AppCheckCore
  19. import FirebaseCore
  20. final class AppCheckAPITests {
  21. func usage() {
  22. let app = FirebaseApp.app()!
  23. let projectID = app.options.projectID!
  24. let resourceName = "projects/\(projectID)/\(app.options.googleAppID)"
  25. // MARK: - AppAttestProvider
  26. #if TARGET_OS_IOS
  27. if #available(iOS 14.0, *) {
  28. // TODO(andrewheard): Add `requestHooks` in API tests.
  29. if let provider = AppCheckCoreAppAttestProvider(
  30. storageID: app.name,
  31. resourceName: resourceName,
  32. apiKey: app.options.apiKey,
  33. keychainAccessGroup: nil,
  34. requestHooks: nil
  35. ) {
  36. provider.getToken { token, error in
  37. // ...
  38. }
  39. }
  40. }
  41. #endif // TARGET_OS_IOS
  42. // MARK: - AppCheck
  43. guard let app = FirebaseApp.app() else { return }
  44. // Retrieving an AppCheck instance
  45. let appCheck = AppCheckCore(
  46. serviceName: app.name,
  47. resourceName: resourceName,
  48. appCheckProvider: DummyAppCheckProvider(),
  49. settings: DummyAppCheckSettings(),
  50. tokenDelegate: DummyAppCheckTokenDelegate(),
  51. keychainAccessGroup: app.options.appGroupID
  52. )
  53. // Get token
  54. appCheck.token(forcingRefresh: false) { token, error in
  55. if let _ /* error */ = error {
  56. // ...
  57. } else if let _ /* token */ = token {
  58. // ...
  59. }
  60. }
  61. // Get token (async/await)
  62. if #available(iOS 13.0, macOS 10.15, macCatalyst 13.0, tvOS 13.0, watchOS 7.0, *) {
  63. // async/await is only available on iOS 13+
  64. Task {
  65. do {
  66. try await appCheck.token(forcingRefresh: false)
  67. } catch {
  68. // ...
  69. }
  70. }
  71. }
  72. // MARK: - `AppCheckDebugProvider`
  73. // `AppCheckDebugProvider` initializer
  74. // TODO(andrewheard): Add `requestHooks` in API tests.
  75. let debugProvider = AppCheckCoreDebugProvider(
  76. serviceName: app.name,
  77. resourceName: resourceName,
  78. apiKey: app.options.apiKey,
  79. requestHooks: nil
  80. )
  81. // Get token
  82. debugProvider.getToken { token, error in
  83. if let _ /* error */ = error {
  84. // ...
  85. } else if let _ /* token */ = token {
  86. // ...
  87. }
  88. }
  89. // Get token (async/await)
  90. if #available(iOS 13.0, macOS 10.15, macCatalyst 13.0, tvOS 13.0, watchOS 7.0, *) {
  91. // async/await is only available on iOS 13+
  92. Task {
  93. do {
  94. _ = try await debugProvider.getToken()
  95. } catch {
  96. // ...
  97. }
  98. }
  99. }
  100. _ = debugProvider.localDebugToken()
  101. _ = debugProvider.currentDebugToken()
  102. // MARK: - AppCheckToken
  103. let token = AppCheckCoreToken(token: "token", expirationDate: Date.distantFuture)
  104. _ = token.token
  105. _ = token.expirationDate
  106. // MARK: - AppCheckErrors
  107. appCheck.token(forcingRefresh: false) { _, error in
  108. if let error = error {
  109. switch error {
  110. case AppCheckCoreErrorCode.unknown:
  111. break
  112. case AppCheckCoreErrorCode.serverUnreachable:
  113. break
  114. case AppCheckCoreErrorCode.invalidConfiguration:
  115. break
  116. case AppCheckCoreErrorCode.keychain:
  117. break
  118. case AppCheckCoreErrorCode.unsupported:
  119. break
  120. default:
  121. break
  122. }
  123. }
  124. // ...
  125. }
  126. // MARK: - AppCheckProvider
  127. // A protocol implemented by:
  128. // - `AppAttestDebugProvider`
  129. // - `AppCheckDebugProvider`
  130. // - `DeviceCheckProvider`
  131. // MARK: - DeviceCheckProvider
  132. // `DeviceCheckProvider` initializer
  133. #if !os(watchOS)
  134. if #available(iOS 11.0, macOS 10.15, macCatalyst 13.0, tvOS 11.0, *) {
  135. // TODO(andrewheard): Add `requestHooks` in API tests.
  136. let deviceCheckProvider = AppCheckCoreDeviceCheckProvider(
  137. serviceName: app.name,
  138. resourceName: resourceName,
  139. apiKey: app.options.apiKey,
  140. requestHooks: nil
  141. )
  142. // Get token
  143. deviceCheckProvider.getToken { token, error in
  144. if let _ /* error */ = error {
  145. // ...
  146. } else if let _ /* token */ = token {
  147. // ...
  148. }
  149. }
  150. // Get token (async/await)
  151. if #available(iOS 13.0, tvOS 13.0, *) {
  152. // async/await is only available on iOS 13+
  153. Task {
  154. do {
  155. _ = try await deviceCheckProvider.getToken()
  156. } catch AppCheckCoreErrorCode.unsupported {
  157. // ...
  158. } catch {
  159. // ...
  160. }
  161. }
  162. }
  163. }
  164. #endif // !os(watchOS)
  165. // MARK: - AppCheckCoreLogger
  166. // Set the log level for App Check Core
  167. AppCheckCoreLogger.logLevel = .debug
  168. }
  169. }
  170. class DummyAppCheckProvider: NSObject, AppCheckCoreProvider {
  171. func getToken(completion handler: @escaping (AppCheckCoreToken?, Error?) -> Void) {
  172. handler(AppCheckCoreToken(token: "token", expirationDate: .distantFuture), nil)
  173. }
  174. }
  175. class DummyAppCheckSettings: NSObject, AppCheckCoreSettingsProtocol {
  176. var isTokenAutoRefreshEnabled: Bool = true
  177. }
  178. class DummyAppCheckTokenDelegate: NSObject, AppCheckCoreTokenDelegate {
  179. func tokenDidUpdate(_ token: AppCheckCoreToken, serviceName: String) {}
  180. }