AppCheckAPITests.swift 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 FirebaseCore
  19. import FirebaseAppCheck
  20. final class AppCheckAPITests {
  21. func usage() {
  22. // MARK: - AppAttestProvider
  23. if #available(iOS 14.0, macOS 11.3, macCatalyst 14.5, tvOS 15.0, watchOS 9.0, *) {
  24. if let app = FirebaseApp.app(), let provider = AppAttestProvider(app: app) {
  25. provider.getToken { token, error in
  26. // ...
  27. }
  28. }
  29. }
  30. // MARK: - AppCheck
  31. // `AppCheckTokenDidChange` & associated notification keys
  32. _ = NotificationCenter.default
  33. .addObserver(
  34. forName: .AppCheckTokenDidChange,
  35. object: nil,
  36. queue: .main
  37. ) { notification in
  38. _ = notification.userInfo?[AppCheckTokenNotificationKey]
  39. _ = notification.userInfo?[AppCheckAppNameNotificationKey]
  40. }
  41. // Retrieving an AppCheck instance
  42. _ = AppCheck.appCheck()
  43. if let app = FirebaseApp.app() {
  44. _ = AppCheck.appCheck(app: app)
  45. }
  46. // Get token
  47. AppCheck.appCheck().token(forcingRefresh: false) { token, error in
  48. if let _ /* error */ = error {
  49. // ...
  50. } else if let _ /* token */ = token {
  51. // ...
  52. }
  53. }
  54. // Get token (async/await)
  55. #if compiler(>=5.5.2) && canImport(_Concurrency)
  56. if #available(iOS 13.0, macOS 11.15, macCatalyst 13.0, tvOS 13.0, watchOS 7.0, *) {
  57. // async/await is a Swift 5.5+ feature available on iOS 15+
  58. Task {
  59. do {
  60. try await AppCheck.appCheck().token(forcingRefresh: false)
  61. } catch {
  62. // ...
  63. }
  64. }
  65. }
  66. #endif // compiler(>=5.5.2) && canImport(_Concurrency)
  67. // Set `AppCheckProviderFactory`
  68. AppCheck.setAppCheckProviderFactory(DummyAppCheckProviderFactory())
  69. // Get & Set `isTokenAutoRefreshEnabled`
  70. _ = AppCheck.appCheck().isTokenAutoRefreshEnabled
  71. AppCheck.appCheck().isTokenAutoRefreshEnabled = false
  72. // MARK: - `AppCheckDebugProvider`
  73. // `AppCheckDebugProvider` initializer
  74. if let app = FirebaseApp.app(), let debugProvider = AppCheckDebugProvider(app: app) {
  75. // Get token
  76. debugProvider.getToken { token, error in
  77. if let _ /* error */ = error {
  78. // ...
  79. } else if let _ /* token */ = token {
  80. // ...
  81. }
  82. }
  83. // Get token (async/await)
  84. #if compiler(>=5.5.2) && canImport(_Concurrency)
  85. if #available(iOS 13.0, macOS 11.15, macCatalyst 13.0, tvOS 13.0, watchOS 7.0, *) {
  86. // async/await is a Swift 5.5+ feature available on iOS 15+
  87. Task {
  88. do {
  89. _ = try await debugProvider.getToken()
  90. } catch {
  91. // ...
  92. }
  93. }
  94. }
  95. #endif // compiler(>=5.5.2) && canImport(_Concurrency)
  96. _ = debugProvider.localDebugToken()
  97. _ = debugProvider.currentDebugToken()
  98. }
  99. // MARK: - AppCheckToken
  100. let token = AppCheckToken(token: "token", expirationDate: Date.distantFuture)
  101. _ = token.token
  102. _ = token.expirationDate
  103. // MARK: - AppCheckErrors
  104. AppCheck.appCheck().token(forcingRefresh: false) { _, error in
  105. if let error = error {
  106. switch error {
  107. case AppCheckErrorCode.unknown:
  108. break
  109. case AppCheckErrorCode.serverUnreachable:
  110. break
  111. case AppCheckErrorCode.invalidConfiguration:
  112. break
  113. case AppCheckErrorCode.keychain:
  114. break
  115. case AppCheckErrorCode.unsupported:
  116. break
  117. default:
  118. break
  119. }
  120. }
  121. // ...
  122. }
  123. // MARK: - AppCheckProvider
  124. // A protocol implemented by:
  125. // - `AppAttestDebugProvider`
  126. // - `AppCheckDebugProvider`
  127. // - `DeviceCheckProvider`
  128. // MARK: - AppCheckProviderFactory
  129. // A protocol implemented by:
  130. // - `AppCheckDebugProvider`
  131. // - `DeviceCheckProvider`
  132. // MARK: - DeviceCheckProvider
  133. // `DeviceCheckProvider` initializer
  134. #if !os(watchOS)
  135. if #available(iOS 11.0, macOS 10.15, macCatalyst 13.0, tvOS 11.0, *) {
  136. if let app = FirebaseApp.app(), let deviceCheckProvider = DeviceCheckProvider(app: app) {
  137. // Get token
  138. deviceCheckProvider.getToken { token, error in
  139. if let _ /* error */ = error {
  140. // ...
  141. } else if let _ /* token */ = token {
  142. // ...
  143. }
  144. }
  145. // Get token (async/await)
  146. #if compiler(>=5.5.2) && canImport(_Concurrency)
  147. if #available(iOS 13.0, macOS 11.15, macCatalyst 13.0, tvOS 13.0, watchOS 7.0, *) {
  148. // async/await is a Swift 5.5+ feature available on iOS 15+
  149. Task {
  150. do {
  151. _ = try await deviceCheckProvider.getToken()
  152. } catch AppCheckErrorCode.unsupported {
  153. // ...
  154. } catch {
  155. // ...
  156. }
  157. }
  158. }
  159. #endif // compiler(>=5.5.2) && canImport(_Concurrency)
  160. }
  161. }
  162. #endif // !os(watchOS)
  163. }
  164. }
  165. class DummyAppCheckProvider: NSObject, AppCheckProvider {
  166. func getToken(completion handler: @escaping (AppCheckToken?, Error?) -> Void) {
  167. handler(AppCheckToken(token: "token", expirationDate: .distantFuture), nil)
  168. }
  169. }
  170. class DummyAppCheckProviderFactory: NSObject, AppCheckProviderFactory {
  171. func createProvider(with app: FirebaseApp) -> AppCheckProvider? {
  172. return DummyAppCheckProvider()
  173. }
  174. }