ActionCodeURL.swift 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. import Foundation
  15. /** @class FIRActionCodeURL
  16. @brief This class will allow developers to easily extract information about out of band links.
  17. */
  18. @objc(FIRActionCodeURL) open class ActionCodeURL: NSObject {
  19. /** @property APIKey
  20. @brief Returns the API key from the link. nil, if not provided.
  21. */
  22. @objc(APIKey) public let apiKey: String?
  23. /** @property operation
  24. @brief Returns the mode of oob action. The property will be of `FIRActionCodeOperation` type.
  25. It will return `FIRActionCodeOperationUnknown` if no oob action is provided.
  26. */
  27. @objc public let operation: ActionCodeOperation
  28. /** @property code
  29. @brief Returns the email action code from the link. nil, if not provided.
  30. */
  31. @objc public let code: String?
  32. /** @property continueURL
  33. @brief Returns the continue URL from the link. nil, if not provided.
  34. */
  35. @objc public let continueURL: URL?
  36. /** @property languageCode
  37. @brief Returns the language code from the link. nil, if not provided.
  38. */
  39. @objc public let languageCode: String?
  40. /** @fn actionCodeURLWithLink:
  41. @brief Construct an `ActionCodeURL` from an out of band link (e.g. email link).
  42. @param link The oob link string used to construct the action code URL.
  43. @return The `ActionCodeURL` object constructed based on the oob link provided.
  44. */
  45. @objc(actionCodeURLWithLink:) public init?(link: String) {
  46. var queryItems = ActionCodeURL.parseURL(link)
  47. if queryItems.count == 0 {
  48. let urlComponents = URLComponents(string: link)
  49. if let query = urlComponents?.query {
  50. queryItems = ActionCodeURL.parseURL(query)
  51. }
  52. }
  53. guard queryItems.count > 0 else {
  54. return nil
  55. }
  56. apiKey = queryItems["apiKey"]
  57. operation = ActionCodeInfo.actionCodeOperation(forRequestType: queryItems["mode"])
  58. code = queryItems["oobCode"]
  59. if let continueURL = queryItems["continueUrl"] {
  60. self.continueURL = URL(string: continueURL)
  61. } else {
  62. continueURL = nil
  63. }
  64. languageCode = queryItems["languageCode"]
  65. }
  66. class func parseURL(_ urlString: String) -> [String: String] {
  67. guard let linkURL = URLComponents(string: urlString)?.query else {
  68. return [:]
  69. }
  70. var queryItems: [String: String] = [:]
  71. let urlComponents = linkURL.components(separatedBy: "&")
  72. for component in urlComponents {
  73. let splitArray = component.split(separator: "=")
  74. if let queryItemKey = String(splitArray[0]).removingPercentEncoding,
  75. let queryItemValue = String(splitArray[1]).removingPercentEncoding {
  76. queryItems[queryItemKey] = queryItemValue
  77. }
  78. }
  79. return queryItems
  80. }
  81. }