OAuthCredential.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  16. @objc(FIROAuthCredential) public class OAuthCredential: AuthCredential, NSSecureCoding {
  17. /** @property IDToken
  18. @brief The ID Token associated with this credential.
  19. */
  20. @objc(IDToken) public let idToken: String?
  21. /** @property accessToken
  22. @brief The access token associated with this credential.
  23. */
  24. @objc public let accessToken: String?
  25. /** @property secret
  26. @brief The secret associated with this credential. This will be nil for OAuth 2.0 providers.
  27. @detail OAuthCredential already exposes a providerId getter. This will help the developer
  28. determine whether an access token/secret pair is needed.
  29. */
  30. @objc public let secret: String?
  31. // internal
  32. let OAuthResponseURLString: String?
  33. let sessionID: String?
  34. let pendingToken: String?
  35. let fullName: PersonNameComponents?
  36. // private
  37. let rawNonce: String?
  38. // TODO: Remove public objc
  39. @objc public init(withProviderID providerID: String,
  40. idToken: String? = nil,
  41. rawNonce: String? = nil,
  42. accessToken: String? = nil,
  43. secret: String? = nil,
  44. fullName: PersonNameComponents? = nil,
  45. pendingToken: String? = nil) {
  46. self.idToken = idToken
  47. self.rawNonce = rawNonce
  48. self.accessToken = accessToken
  49. self.pendingToken = pendingToken
  50. self.secret = secret
  51. self.fullName = fullName
  52. OAuthResponseURLString = nil
  53. sessionID = nil
  54. super.init(provider: providerID)
  55. }
  56. @objc public init(withProviderID providerID: String,
  57. sessionID: String,
  58. OAuthResponseURLString: String) {
  59. self.sessionID = sessionID
  60. self.OAuthResponseURLString = OAuthResponseURLString
  61. accessToken = nil
  62. pendingToken = nil
  63. secret = nil
  64. idToken = nil
  65. rawNonce = nil
  66. fullName = nil
  67. super.init(provider: providerID)
  68. }
  69. convenience init?(withVerifyAssertionResponse response: VerifyAssertionResponse) {
  70. guard Self.nonEmptyString(response.oauthIDToken) ||
  71. Self.nonEmptyString(response.oauthAccessToken) ||
  72. Self.nonEmptyString(response.oauthSecretToken) else {
  73. return nil
  74. }
  75. self.init(withProviderID: response.providerID ?? OAuthProvider.id,
  76. idToken: response.oauthIDToken,
  77. rawNonce: nil,
  78. accessToken: response.oauthAccessToken,
  79. secret: response.oauthSecretToken,
  80. pendingToken: response.pendingToken)
  81. }
  82. override func prepare(_ request: VerifyAssertionRequest) {
  83. request.providerIDToken = idToken
  84. request.providerRawNonce = rawNonce
  85. request.providerAccessToken = accessToken
  86. request.requestURI = OAuthResponseURLString
  87. request.sessionID = sessionID
  88. request.providerOAuthTokenSecret = secret
  89. request.fullName = fullName
  90. request.pendingToken = pendingToken
  91. }
  92. // MARK: Secure Coding
  93. public static var supportsSecureCoding: Bool = true
  94. public func encode(with coder: NSCoder) {
  95. coder.encode(idToken, forKey: "IDToken")
  96. coder.encode(rawNonce, forKey: "rawNonce")
  97. coder.encode(accessToken, forKey: "accessToken")
  98. coder.encode(pendingToken, forKey: "pendingToken")
  99. coder.encode(secret, forKey: "secret")
  100. coder.encode(fullName, forKey: "fullName")
  101. }
  102. public required init?(coder: NSCoder) {
  103. idToken = coder.decodeObject(of: NSString.self, forKey: "IDToken") as? String
  104. rawNonce = coder.decodeObject(of: NSString.self, forKey: "rawNonce") as? String
  105. accessToken = coder.decodeObject(of: NSString.self, forKey: "accessToken") as? String
  106. pendingToken = coder.decodeObject(of: NSString.self, forKey: "pendingToken") as? String
  107. secret = coder.decodeObject(of: NSString.self, forKey: "secret") as? String
  108. fullName = coder.decodeObject(forKey: "fullName") as? PersonNameComponents
  109. OAuthResponseURLString = nil
  110. sessionID = nil
  111. super.init(provider: OAuthProvider.id)
  112. }
  113. private static func nonEmptyString(_ string: String?) -> Bool {
  114. guard let string else {
  115. return false
  116. }
  117. return string.count > 0
  118. }
  119. }