AppCheckAPITests.swift 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 FirebaseAppCheck
  19. import FirebaseCore
  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 #available(iOS 13.0, macOS 10.15, macCatalyst 13.0, tvOS 13.0, watchOS 7.0, *) {
  56. // async/await is a Swift Concurrency feature available on iOS 13+ and macOS 10.15+
  57. Task {
  58. do {
  59. try await AppCheck.appCheck().token(forcingRefresh: false)
  60. } catch {
  61. // ...
  62. }
  63. }
  64. }
  65. // Set `AppCheckProviderFactory`
  66. AppCheck.setAppCheckProviderFactory(DummyAppCheckProviderFactory())
  67. // Get & Set `isTokenAutoRefreshEnabled`
  68. _ = AppCheck.appCheck().isTokenAutoRefreshEnabled
  69. AppCheck.appCheck().isTokenAutoRefreshEnabled = false
  70. // MARK: - `AppCheckDebugProvider`
  71. // `AppCheckDebugProvider` initializer
  72. if let app = FirebaseApp.app(), let debugProvider = AppCheckDebugProvider(app: app) {
  73. // Get token
  74. debugProvider.getToken { token, error in
  75. if let _ /* error */ = error {
  76. // ...
  77. } else if let _ /* token */ = token {
  78. // ...
  79. }
  80. }
  81. // Get token (async/await)
  82. if #available(iOS 13.0, macOS 10.15, macCatalyst 13.0, tvOS 13.0, watchOS 7.0, *) {
  83. // async/await is a Swift Concurrency feature available on iOS 13+ and macOS 10.15+
  84. Task {
  85. do {
  86. _ = try await debugProvider.getToken()
  87. } catch {
  88. // ...
  89. }
  90. }
  91. }
  92. _ = debugProvider.localDebugToken()
  93. _ = debugProvider.currentDebugToken()
  94. }
  95. // MARK: - AppCheckToken
  96. let token = AppCheckToken(token: "token", expirationDate: Date.distantFuture)
  97. _ = token.token
  98. _ = token.expirationDate
  99. // MARK: - AppCheckErrors
  100. AppCheck.appCheck().token(forcingRefresh: false) { _, error in
  101. if let error = error {
  102. switch error {
  103. case AppCheckErrorCode.unknown:
  104. break
  105. case AppCheckErrorCode.serverUnreachable:
  106. break
  107. case AppCheckErrorCode.invalidConfiguration:
  108. break
  109. case AppCheckErrorCode.keychain:
  110. break
  111. case AppCheckErrorCode.unsupported:
  112. break
  113. default:
  114. break
  115. }
  116. }
  117. // ...
  118. }
  119. // MARK: - AppCheckProvider
  120. // A protocol implemented by:
  121. // - `AppAttestDebugProvider`
  122. // - `AppCheckDebugProvider`
  123. // - `DeviceCheckProvider`
  124. // MARK: - AppCheckProviderFactory
  125. // A protocol implemented by:
  126. // - `AppCheckDebugProvider`
  127. // - `DeviceCheckProvider`
  128. // MARK: - DeviceCheckProvider
  129. // `DeviceCheckProvider` initializer
  130. #if !os(watchOS)
  131. if #available(iOS 11.0, macOS 10.15, macCatalyst 13.0, tvOS 11.0, *) {
  132. if let app = FirebaseApp.app(), let deviceCheckProvider = DeviceCheckProvider(app: app) {
  133. // Get token
  134. deviceCheckProvider.getToken { token, error in
  135. if let _ /* error */ = error {
  136. // ...
  137. } else if let _ /* token */ = token {
  138. // ...
  139. }
  140. }
  141. // Get token (async/await)
  142. if #available(iOS 13.0, macOS 10.15, macCatalyst 13.0, tvOS 13.0, watchOS 7.0, *) {
  143. // async/await is a Swift Concurrency feature available on iOS 13+ and macOS 10.15+
  144. Task {
  145. do {
  146. _ = try await deviceCheckProvider.getToken()
  147. } catch AppCheckErrorCode.unsupported {
  148. // ...
  149. } catch {
  150. // ...
  151. }
  152. }
  153. }
  154. }
  155. }
  156. #endif // !os(watchOS)
  157. }
  158. }
  159. class DummyAppCheckProvider: NSObject, AppCheckProvider {
  160. func getToken(completion handler: @escaping (AppCheckToken?, Error?) -> Void) {
  161. handler(AppCheckToken(token: "token", expirationDate: .distantFuture), nil)
  162. }
  163. }
  164. class DummyAppCheckProviderFactory: NSObject, AppCheckProviderFactory {
  165. func createProvider(with app: FirebaseApp) -> AppCheckProvider? {
  166. return DummyAppCheckProvider()
  167. }
  168. }