SettingsViewController.swift 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. @testable import FirebaseAuth
  15. import FirebaseCore
  16. import UIKit
  17. /// Namespace for performable actions on a Auth Settings view
  18. enum SettingsAction: String {
  19. case toggleIdentityTokenAPI = "Identity Toolkit"
  20. case toggleSecureTokenAPI = "Secure Token"
  21. case toggleActiveApp = "Active App"
  22. case toggleAccessGroup = "Current Access Group"
  23. case setAuthLanugage = "Auth Language"
  24. case useAppLanguage = "Use App Language"
  25. case togglePhoneAppVerification = "Disable App Verification (Phone)"
  26. }
  27. class SettingsViewController: UIViewController, DataSourceProviderDelegate {
  28. var dataSourceProvider: DataSourceProvider<AuthSettings>!
  29. var tableView: UITableView { view as! UITableView }
  30. private var _settings: AuthSettings?
  31. var settings: AuthSettings? {
  32. get { AppManager.shared.auth().settings }
  33. set { _settings = newValue }
  34. }
  35. // MARK: - UIViewController Life Cycle
  36. override func loadView() {
  37. view = UITableView(frame: .zero, style: .insetGrouped)
  38. }
  39. override func viewDidLoad() {
  40. super.viewDidLoad()
  41. configureNavigationBar()
  42. }
  43. override func viewWillAppear(_ animated: Bool) {
  44. super.viewWillAppear(animated)
  45. configureDataSourceProvider()
  46. }
  47. // MARK: - DataSourceProviderDelegate
  48. func didSelectRowAt(_ indexPath: IndexPath, on tableView: UITableView) {
  49. let item = dataSourceProvider.item(at: indexPath)
  50. guard let actionName = item.detailTitle,
  51. let action = SettingsAction(rawValue: actionName) else {
  52. // The row tapped has no affiliated action.
  53. return
  54. }
  55. let auth = AppManager.shared.auth()
  56. switch action {
  57. case .toggleSecureTokenAPI:
  58. toggleSecureTokenAPI()
  59. case .toggleIdentityTokenAPI:
  60. toggleIdentityTokenAPI()
  61. case .toggleActiveApp:
  62. AppManager.shared.toggle()
  63. case .toggleAccessGroup:
  64. toggleAccessGroup()
  65. case .setAuthLanugage:
  66. setAuthLanguage()
  67. case .useAppLanguage:
  68. auth.useAppLanguage()
  69. case .togglePhoneAppVerification:
  70. guard let settings = auth.settings else {
  71. fatalError("Unset auth.settings")
  72. }
  73. settings.isAppVerificationDisabledForTesting = !settings.isAppVerificationDisabledForTesting
  74. }
  75. updateUI()
  76. }
  77. // MARK: - Firebase 🔥
  78. private func toggleIdentityTokenAPI() {
  79. if IdentityToolkitRequest.host == "www.googleapis.com" {
  80. IdentityToolkitRequest.setHost("staging-www.sandbox.googleapis.com")
  81. } else {
  82. IdentityToolkitRequest.setHost("www.googleapis.com")
  83. }
  84. }
  85. private func toggleSecureTokenAPI() {
  86. if SecureTokenRequest.host == "securetoken.googleapis.com" {
  87. SecureTokenRequest.setHost("staging-securetoken.sandbox.googleapis.com")
  88. } else {
  89. SecureTokenRequest.setHost("securetoken.googleapis.com")
  90. }
  91. }
  92. private func toggleAccessGroup() {
  93. if AppManager.shared.auth().userAccessGroup == nil {
  94. guard let bundleDictionary = Bundle.main.infoDictionary,
  95. let group = bundleDictionary["AppIdentifierPrefix"] as? String else {
  96. fatalError("Configure AppIdentifierPrefix in the plist")
  97. }
  98. AppManager.shared.auth().userAccessGroup = group + "com.google.firebase.auth.keychainGroup1"
  99. } else {
  100. AppManager.shared.auth().userAccessGroup = nil
  101. }
  102. }
  103. private func setAuthLanguage() {
  104. let prompt = UIAlertController(title: nil, message: "Enter Language Code For Auth:",
  105. preferredStyle: .alert)
  106. prompt.addTextField()
  107. let okAction = UIAlertAction(title: "OK", style: .default) { action in
  108. AppManager.shared.auth().languageCode = prompt.textFields?[0].text ?? ""
  109. self.updateUI()
  110. }
  111. prompt.addAction(okAction)
  112. present(prompt, animated: true)
  113. }
  114. // MARK: - Private Helpers
  115. private func configureNavigationBar() {
  116. navigationItem.title = "Settings"
  117. guard let navigationBar = navigationController?.navigationBar else { return }
  118. navigationBar.prefersLargeTitles = true
  119. navigationBar.titleTextAttributes = [.foregroundColor: UIColor.systemOrange]
  120. navigationBar.largeTitleTextAttributes = [.foregroundColor: UIColor.systemOrange]
  121. }
  122. private func configureDataSourceProvider() {
  123. dataSourceProvider = DataSourceProvider(
  124. dataSource: settings?.sections,
  125. emptyStateView: SignedOutView(),
  126. tableView: tableView
  127. )
  128. dataSourceProvider.delegate = self
  129. }
  130. private func updateUI() {
  131. configureDataSourceProvider()
  132. animateUpdates(for: tableView)
  133. }
  134. private func animateUpdates(for tableView: UITableView) {
  135. UIView.transition(with: tableView, duration: 0.2,
  136. options: .transitionCrossDissolve,
  137. animations: { tableView.reloadData() })
  138. }
  139. }
  140. // MARK: - Extending a `AuthSettings` to conform to `DataSourceProvidable`
  141. extension AuthSettings: DataSourceProvidable {
  142. private var versionSection: Section {
  143. let items = [Item(title: FirebaseVersion(), detailTitle: "FirebaseAuth")]
  144. return Section(headerDescription: "Versions", items: items)
  145. }
  146. private var apiHostSection: Section {
  147. let items = [Item(title: IdentityToolkitRequest.host, detailTitle: "Identity Toolkit"),
  148. Item(title: SecureTokenRequest.host, detailTitle: "Secure Token")]
  149. return Section(headerDescription: "API Hosts", items: items)
  150. }
  151. private var appsSection: Section {
  152. let items = [Item(title: AppManager.shared.app.options.projectID, detailTitle: "Active App")]
  153. return Section(headerDescription: "Firebase Apps", items: items)
  154. }
  155. private var keychainSection: Section {
  156. let items = [Item(title: AppManager.shared.auth().userAccessGroup ?? "[none]",
  157. detailTitle: "Current Access Group")]
  158. return Section(headerDescription: "Keychain Access Groups", items: items)
  159. }
  160. // TODO: Add ability to click and clear both of these fields.
  161. private var phoneAuthSection: Section {
  162. var tokenString = "No Token"
  163. var credentialString = "No Credential"
  164. if let token = AppManager.shared.auth().tokenManager.token {
  165. let tokenType = token.type == .prod ? "Production" : "Sandbox"
  166. tokenString = "token: \(token.string): type: \(tokenType)"
  167. }
  168. if let credential = AppManager.shared.auth().appCredentialManager.credential {
  169. // TODO: Maybe use truncatedString like ObjC sample
  170. credentialString = "\(credential.receipt)/\(credential.secret ?? "nil")"
  171. }
  172. let items = [Item(title: tokenString, detailTitle: "APNs Token"),
  173. Item(title: credentialString, detailTitle: "App Credential")]
  174. return Section(headerDescription: "Phone Auth - TODO toggle off", items: items)
  175. }
  176. private var languageSection: Section {
  177. let languageCode = AppManager.shared.auth().languageCode
  178. let items = [Item(title: languageCode ?? "[none]", detailTitle: "Auth Language"),
  179. Item(title: "Click to Use App Language", detailTitle: "Use App Language")]
  180. return Section(headerDescription: "Language", items: items)
  181. }
  182. private var disableSection: Section {
  183. guard let settings = AppManager.shared.auth().settings else {
  184. fatalError("Missing auth settings")
  185. }
  186. let disabling = settings.isAppVerificationDisabledForTesting ? "YES" : "NO"
  187. let items = [Item(title: disabling, detailTitle: "Disable App Verification (Phone)")]
  188. return Section(headerDescription: "Auth Settings", items: items)
  189. }
  190. var sections: [Section] {
  191. [
  192. versionSection,
  193. apiHostSection,
  194. appsSection,
  195. keychainSection,
  196. phoneAuthSection,
  197. languageSection,
  198. disableSection,
  199. ]
  200. }
  201. }