Skip to content

Commit aed010e

Browse files
committed
Reimplement #10068 in auth-swift after rebase (#11005)
1 parent e25ecfa commit aed010e

File tree

6 files changed

+107
-1942
lines changed

6 files changed

+107
-1942
lines changed

FirebaseAuth/Sources/Swift/AuthProvider/OAuthCredential.swift

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import Foundation
3737
@objc public let OAuthResponseURLString: String?
3838
@objc public let sessionID: String?
3939
@objc public let pendingToken: String?
40+
let fullName: PersonNameComponents?
4041
// private
4142
@objc public let rawNonce: String?
4243

@@ -46,12 +47,14 @@ import Foundation
4647
rawNonce: String? = nil,
4748
accessToken: String? = nil,
4849
secret: String? = nil,
50+
fullName: PersonNameComponents? = nil,
4951
pendingToken: String? = nil) {
5052
self.idToken = idToken
5153
self.rawNonce = rawNonce
5254
self.accessToken = accessToken
5355
self.pendingToken = pendingToken
5456
self.secret = secret
57+
self.fullName = fullName
5558
OAuthResponseURLString = nil
5659
sessionID = nil
5760
super.init(provider: providerID)
@@ -67,6 +70,7 @@ import Foundation
6770
secret = nil
6871
idToken = nil
6972
rawNonce = nil
73+
fullName = nil
7074
super.init(provider: providerID)
7175
}
7276

@@ -99,19 +103,21 @@ import Foundation
99103
public static var supportsSecureCoding: Bool = true
100104

101105
public func encode(with coder: NSCoder) {
102-
coder.encode(idToken)
103-
coder.encode(rawNonce)
104-
coder.encode(accessToken)
105-
coder.encode(pendingToken)
106-
coder.encode(secret)
106+
coder.encode(idToken, forKey: "IDToken")
107+
coder.encode(rawNonce, forKey: "rawNonce")
108+
coder.encode(accessToken, forKey: "accessToken")
109+
coder.encode(pendingToken, forKey: "pendingToken")
110+
coder.encode(secret, forKey: "secret")
111+
coder.encode(fullName, forKey: "fullName")
107112
}
108113

109114
public required init?(coder: NSCoder) {
110-
idToken = coder.decodeObject(forKey: "idToken") as? String
115+
idToken = coder.decodeObject(forKey: "IDToken") as? String
111116
rawNonce = coder.decodeObject(forKey: "rawNonce") as? String
112117
accessToken = coder.decodeObject(forKey: "accessToken") as? String
113118
pendingToken = coder.decodeObject(forKey: "pendingToken") as? String
114119
secret = coder.decodeObject(forKey: "secret") as? String
120+
fullName = coder.decodeObject(forKey: "fullName") as? PersonNameComponents
115121
OAuthResponseURLString = nil
116122
sessionID = nil
117123
super.init(provider: OAuthProvider.id)

FirebaseAuth/Sources/Swift/AuthProvider/OAuthProvider.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,28 @@ import CommonCrypto
240240
}
241241
}
242242

243+
/** @fn appleCredentialWithIDToken:rawNonce:fullName:
244+
* @brief Creates an `AuthCredential` for the Sign in with Apple OAuth 2 provider identified by ID
245+
* token, raw nonce, and full name. This method is specific to the Sign in with Apple OAuth 2
246+
* provider as this provider requires the full name to be passed explicitly.
247+
*
248+
* @param idToken The IDToken associated with the Sign in with Apple Auth credential being created.
249+
* @param rawNonce The raw nonce associated with the Sign in with Apple Auth credential being
250+
* created.
251+
* @param fullName The full name associated with the Sign in with Apple Auth credential being
252+
* created.
253+
* @return An `AuthCredential`.
254+
*/
255+
@objc(appleCredentialWithIDToken:rawNonce:fullName:)
256+
public static func appleCredential(withIDToken idToken: String,
257+
rawNonce: String?,
258+
fullName: PersonNameComponents?) -> OAuthCredential {
259+
return OAuthCredential(withProviderID: AuthProviderString.apple.rawValue,
260+
idToken: idToken,
261+
rawNonce: rawNonce,
262+
fullName: fullName)
263+
}
264+
243265
@available(iOS 13, tvOS 13, macOS 10.15, watchOS 8, *)
244266
public func credential(with UIDelegate: AuthUIDelegate?) async throws -> AuthCredential {
245267
return try await withCheckedThrowingContinuation { continuation in

FirebaseAuth/Sources/Swift/Backend/RPC/VerifyAssertionRequest.swift

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,28 @@ private let kSessionIDKey = "sessionId"
9595
*/
9696
private let kTenantIDKey = "tenantId"
9797

98+
/** @var kUserKey
99+
@brief The key for the "user" value in the request. The value is a JSON object that contains the
100+
name of the user.
101+
*/
102+
private let kUserKey = "user"
103+
104+
/** @var kNameKey
105+
@brief The key for the "name" value in the request. The value is a JSON object that contains the
106+
first and/or last name of the user.
107+
*/
108+
private let kNameKey = "name"
109+
110+
/** @var kFirstNameKey
111+
@brief The key for the "firstName" value in the request.
112+
*/
113+
private let kFirstNameKey = "firstName"
114+
115+
/** @var kLastNameKey
116+
@brief The key for the "lastName" value in the request.
117+
*/
118+
private let kLastNameKey = "lastName"
119+
98120
/** @class FIRVerifyAssertionRequest
99121
@brief Represents the parameters for the verifyAssertion endpoint.
100122
@see https://developers.google.com/identity/toolkit/web/reference/relyingparty/verifyAssertion
@@ -170,6 +192,11 @@ private let kTenantIDKey = "tenantId"
170192
*/
171193
@objc public var autoCreate: Bool = false
172194

195+
/** @property fullName
196+
@brief A full name from the IdP.
197+
*/
198+
@objc public var fullName: PersonNameComponents?
199+
173200
/** @var response
174201
@brief The corresponding response for this request
175202
*/
@@ -210,6 +237,24 @@ private let kTenantIDKey = "tenantId"
210237
queryItems.append(URLQueryItem(name: kIdentifierKey, value: inputEmail))
211238
}
212239

240+
if fullName?.givenName != nil || fullName?.familyName != nil {
241+
var nameDict = [String: String]()
242+
if let given = fullName?.givenName {
243+
nameDict[kFirstNameKey] = given
244+
}
245+
if let lastName = fullName?.familyName {
246+
nameDict[kLastNameKey] = lastName
247+
}
248+
let userDict = [kNameKey: nameDict]
249+
do {
250+
let userJson = try JSONSerialization.data(withJSONObject: userDict)
251+
let jsonString = String(data: userJson, encoding: .utf8)
252+
queryItems.append(URLQueryItem(name: kUserKey, value: jsonString))
253+
} catch {
254+
fatalError("Auth Internal error: failed to serialize dictionary to json: \(error)")
255+
}
256+
}
257+
213258
components.queryItems = queryItems
214259

215260
var body: [String: AnyHashable] = [

0 commit comments

Comments
 (0)