VerifyAssertionResponse.swift 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. /// Represents the response from the verifyAssertion endpoint.
  16. /// See https: // developers.google.com/identity/toolkit/web/reference/relyingparty/verifyAssertion
  17. class VerifyAssertionResponse: AuthRPCResponse, AuthMFAResponse {
  18. required init() {}
  19. /// The unique ID identifies the IdP account.
  20. var federatedID: String?
  21. /// The IdP ID. For white listed IdPs it's a short domain name e.g. google.com, aol.com,
  22. /// live.net and yahoo.com.If the "providerId" param is set to OpenID OP identifier other than
  23. /// the white listed IdPs the OP identifier is returned.If the "identifier" param is federated
  24. /// ID in the createAuthUri request.The domain part of the federated ID is returned.
  25. var providerID: String?
  26. /// The RP local ID if it's already been mapped to the IdP account identified by the federated ID.
  27. var localID: String?
  28. /// The email returned by the IdP. NOTE: The federated login user may not own the email.
  29. var email: String?
  30. /// It's the identifier param in the createAuthUri request if the identifier is an email. It
  31. /// can be used to check whether the user input email is different from the asserted email.
  32. var inputEmail: String?
  33. /// The original email stored in the mapping storage. It's returned when the federated ID is
  34. /// associated to a different email.
  35. var originalEmail: String?
  36. /// The user approved request token for the OpenID OAuth extension.
  37. var oauthRequestToken: String?
  38. /// The scope for the OpenID OAuth extension.
  39. var oauthScope: String?
  40. /// The first name of the user.
  41. var firstName: String?
  42. /// The last name of the user.
  43. var lastName: String?
  44. /// The full name of the user.
  45. var fullName: String?
  46. /// The nickname of the user.
  47. var nickName: String?
  48. /// The display name of the user.
  49. var displayName: String?
  50. /// Either an authorization code suitable for performing an STS token exchange, or the
  51. /// access token from Secure Token Service, depending on whether `returnSecureToken` is set
  52. /// on the request.
  53. private(set) var idToken: String?
  54. /// The approximate expiration date of the access token.
  55. var approximateExpirationDate: Date?
  56. /// The refresh token from Secure Token Service.
  57. var refreshToken: String?
  58. /// The action code.
  59. var action: String?
  60. /// The language preference of the user.
  61. var language: String?
  62. /// The timezone of the user.
  63. var timeZone: String?
  64. /// The URI of the accessible profile picture.
  65. var photoURL: URL?
  66. /// The birth date of the IdP account.
  67. var dateOfBirth: String?
  68. /// The opaque value used by the client to maintain context info between the authentication
  69. /// request and the IDP callback.
  70. var context: String?
  71. /// When action is 'map', contains the idps which can be used for confirmation.
  72. var verifiedProvider: [String]?
  73. /// Whether the assertion is from a non-trusted IDP and need account linking confirmation.
  74. var needConfirmation: Bool = false
  75. /// It's true if the email is recycled.
  76. var emailRecycled: Bool = false
  77. /// The value is true if the IDP is also the email provider. It means the user owns the email.
  78. var emailVerified: Bool = false
  79. /// Flag indicating that the user signing in is a new user and not a returning user.
  80. var isNewUser: Bool = false
  81. /// Dictionary containing the additional IdP specific information.
  82. var profile: [String: Any]?
  83. /// The name of the user.
  84. var username: String?
  85. /// The ID token for the OpenID OAuth extension.
  86. var oauthIDToken: String?
  87. /// The approximate expiration date of the oauth access token.
  88. var oauthExpirationDate: Date?
  89. /// The access token for the OpenID OAuth extension.
  90. var oauthAccessToken: String?
  91. /// The secret for the OpenID OAuth extension.
  92. var oauthSecretToken: String?
  93. /// The pending ID Token string.
  94. var pendingToken: String?
  95. // MARK: - AuthMFAResponse
  96. private(set) var mfaPendingCredential: String?
  97. private(set) var mfaInfo: [AuthProtoMFAEnrollment]?
  98. func setFields(dictionary: [String: AnyHashable]) throws {
  99. federatedID = dictionary["federatedId"] as? String
  100. providerID = dictionary["providerId"] as? String
  101. localID = dictionary["localId"] as? String
  102. emailRecycled = dictionary["emailRecycled"] as? Bool ?? false
  103. emailVerified = dictionary["emailVerified"] as? Bool ?? false
  104. email = dictionary["email"] as? String
  105. inputEmail = dictionary["inputEmail"] as? String
  106. originalEmail = dictionary["originalEmail"] as? String
  107. oauthRequestToken = dictionary["oauthRequestToken"] as? String
  108. oauthScope = dictionary["oauthScope"] as? String
  109. firstName = dictionary["firstName"] as? String
  110. lastName = dictionary["lastName"] as? String
  111. fullName = dictionary["fullName"] as? String
  112. nickName = dictionary["nickName"] as? String
  113. displayName = dictionary["displayName"] as? String
  114. idToken = dictionary["idToken"] as? String
  115. if let expiresIn = dictionary["expiresIn"] as? String {
  116. approximateExpirationDate = Date(timeIntervalSinceNow: (expiresIn as NSString)
  117. .doubleValue)
  118. }
  119. refreshToken = dictionary["refreshToken"] as? String
  120. isNewUser = dictionary["isNewUser"] as? Bool ?? false
  121. if let rawUserInfo = dictionary["rawUserInfo"] as? String,
  122. let data = rawUserInfo.data(using: .utf8) {
  123. if let info = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves),
  124. let profile = info as? [String: Any] {
  125. self.profile = profile
  126. }
  127. } else if let profile = dictionary["rawUserInfo"] as? [String: Any] {
  128. self.profile = profile
  129. }
  130. username = dictionary["username"] as? String
  131. action = dictionary["action"] as? String
  132. language = dictionary["language"] as? String
  133. timeZone = dictionary["timeZone"] as? String
  134. photoURL = URL(string: dictionary["photoUrl"] as? String ?? "")
  135. dateOfBirth = dictionary["dateOfBirth"] as? String
  136. context = dictionary["context"] as? String
  137. needConfirmation = dictionary["needConfirmation"] as? Bool ?? false
  138. if let verifiedProvider = dictionary["verifiedProvider"] as? String,
  139. let data = verifiedProvider.data(using: .utf8) {
  140. if let decoded = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves),
  141. let provider = decoded as? [String] {
  142. self.verifiedProvider = provider
  143. }
  144. } else if let verifiedProvider = dictionary["verifiedProvider"] as? [String] {
  145. self.verifiedProvider = verifiedProvider
  146. }
  147. oauthIDToken = dictionary["oauthIdToken"] as? String
  148. if let oauthExpirationDate = dictionary["oauthExpireIn"] as? String {
  149. self
  150. .oauthExpirationDate = Date(timeIntervalSinceNow: (oauthExpirationDate as NSString)
  151. .doubleValue)
  152. }
  153. oauthAccessToken = dictionary["oauthAccessToken"] as? String
  154. oauthSecretToken = dictionary["oauthTokenSecret"] as? String
  155. pendingToken = dictionary["pendingToken"] as? String
  156. if let mfaInfoDicts = dictionary["mfaInfo"] as? [[String: AnyHashable]] {
  157. mfaInfo = mfaInfoDicts.map {
  158. AuthProtoMFAEnrollment(dictionary: $0)
  159. }
  160. }
  161. mfaPendingCredential = dictionary["mfaPendingCredential"] as? String
  162. }
  163. }