OAuthCredential.swift 4.5 KB

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