AppCheckAPITests.swift 5.8 KB

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