OAuthCredential.swift 4.6 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) open 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. init(withProviderID providerID: String,
  39. idToken: String? = nil,
  40. rawNonce: String? = nil,
  41. accessToken: String? = nil,
  42. secret: String? = nil,
  43. fullName: PersonNameComponents? = nil,
  44. pendingToken: String? = nil) {
  45. self.idToken = idToken
  46. self.rawNonce = rawNonce
  47. self.accessToken = accessToken
  48. self.pendingToken = pendingToken
  49. self.secret = secret
  50. self.fullName = fullName
  51. OAuthResponseURLString = nil
  52. sessionID = nil
  53. super.init(provider: providerID)
  54. }
  55. init(withProviderID providerID: String,
  56. sessionID: String,
  57. OAuthResponseURLString: String) {
  58. self.sessionID = sessionID
  59. self.OAuthResponseURLString = OAuthResponseURLString
  60. accessToken = nil
  61. pendingToken = nil
  62. secret = nil
  63. idToken = nil
  64. rawNonce = nil
  65. fullName = nil
  66. super.init(provider: providerID)
  67. }
  68. convenience init?(withVerifyAssertionResponse response: VerifyAssertionResponse) {
  69. guard Self.nonEmptyString(response.oauthIDToken) ||
  70. Self.nonEmptyString(response.oauthAccessToken) ||
  71. Self.nonEmptyString(response.oauthSecretToken) else {
  72. return nil
  73. }
  74. self.init(withProviderID: response.providerID ?? OAuthProvider.id,
  75. idToken: response.oauthIDToken,
  76. rawNonce: nil,
  77. accessToken: response.oauthAccessToken,
  78. secret: response.oauthSecretToken,
  79. pendingToken: response.pendingToken)
  80. }
  81. override func prepare(_ request: VerifyAssertionRequest) {
  82. request.providerIDToken = idToken
  83. request.providerRawNonce = rawNonce
  84. request.providerAccessToken = accessToken
  85. request.requestURI = OAuthResponseURLString
  86. request.sessionID = sessionID
  87. request.providerOAuthTokenSecret = secret
  88. request.fullName = fullName
  89. request.pendingToken = pendingToken
  90. }
  91. // MARK: Secure Coding
  92. public static var supportsSecureCoding: Bool = true
  93. public func encode(with coder: NSCoder) {
  94. coder.encode(idToken, forKey: "IDToken")
  95. coder.encode(rawNonce, forKey: "rawNonce")
  96. coder.encode(accessToken, forKey: "accessToken")
  97. coder.encode(pendingToken, forKey: "pendingToken")
  98. coder.encode(secret, forKey: "secret")
  99. coder.encode(fullName, forKey: "fullName")
  100. }
  101. public required init?(coder: NSCoder) {
  102. idToken = coder.decodeObject(of: NSString.self, forKey: "IDToken") as? String
  103. rawNonce = coder.decodeObject(of: NSString.self, forKey: "rawNonce") as? String
  104. accessToken = coder.decodeObject(of: NSString.self, forKey: "accessToken") as? String
  105. pendingToken = coder.decodeObject(of: NSString.self, forKey: "pendingToken") as? String
  106. secret = coder.decodeObject(of: NSString.self, forKey: "secret") as? String
  107. fullName = coder.decodeObject(of: NSPersonNameComponents.self, forKey: "fullName")
  108. 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. }