diff --git a/FirebaseAuth/Sources/Swift/Backend/AuthBackend.swift b/FirebaseAuth/Sources/Swift/Backend/AuthBackend.swift index 474f665147f..3c54b2792fd 100644 --- a/FirebaseAuth/Sources/Swift/Backend/AuthBackend.swift +++ b/FirebaseAuth/Sources/Swift/Backend/AuthBackend.swift @@ -60,7 +60,8 @@ final class AuthBackend: AuthBackendProtocol { } } - static func request(withURL url: URL, + static func request(for url: URL, + httpMethod: String, contentType: String, requestConfiguration: AuthRequestConfiguration) async -> URLRequest { // Kick off tasks for the async header values. @@ -76,7 +77,7 @@ final class AuthBackend: AuthBackendProtocol { request.setValue(clientVersion, forHTTPHeaderField: "X-Client-Version") request.setValue(Bundle.main.bundleIdentifier, forHTTPHeaderField: "X-Ios-Bundle-Identifier") request.setValue(requestConfiguration.appID, forHTTPHeaderField: "X-Firebase-GMPID") - request.httpMethod = requestConfiguration.httpMethod + request.httpMethod = httpMethod let preferredLocalizations = Bundle.main.preferredLocalizations if preferredLocalizations.count > 0 { request.setValue(preferredLocalizations.first, forHTTPHeaderField: "Accept-Language") @@ -163,21 +164,11 @@ final class AuthBackend: AuthBackendProtocol { /// - Returns: The response. fileprivate func callInternal(with request: T) async throws -> T.Response { var bodyData: Data? - if request.containsPostBody { - var postBody: [String: AnyHashable] - do { - // TODO: Can unencodedHTTPRequestBody ever throw? - // They don't today, but there are a few fatalErrors that might better be implemented as - // thrown errors.. Although perhaps the case of 'containsPostBody' returning false could - // perhaps be modeled differently so that the failing unencodedHTTPRequestBody could only - // be called when a body exists... - postBody = try request.unencodedHTTPRequestBody() - } catch { - throw AuthErrorUtils.RPCRequestEncodingError(underlyingError: error) - } - var JSONWritingOptions: JSONSerialization.WritingOptions = .init(rawValue: 0) + if let postBody = request.unencodedHTTPRequestBody { #if DEBUG - JSONWritingOptions = JSONSerialization.WritingOptions.prettyPrinted + let JSONWritingOptions = JSONSerialization.WritingOptions.prettyPrinted + #else + let JSONWritingOptions = JSONSerialization.WritingOptions(rawValue: 0) #endif guard JSONSerialization.isValidJSONObject(postBody) else { diff --git a/FirebaseAuth/Sources/Swift/Backend/AuthBackendRPCIssuer.swift b/FirebaseAuth/Sources/Swift/Backend/AuthBackendRPCIssuer.swift index 6d2c540ff37..bcf4f6dcab8 100644 --- a/FirebaseAuth/Sources/Swift/Backend/AuthBackendRPCIssuer.swift +++ b/FirebaseAuth/Sources/Swift/Backend/AuthBackendRPCIssuer.swift @@ -52,9 +52,12 @@ final class AuthBackendRPCIssuer: AuthBackendRPCIssuerProtocol { body: Data?, contentType: String) async -> (Data?, Error?) { let requestConfiguration = request.requestConfiguration() - let request = await AuthBackend.request(withURL: request.requestURL(), - contentType: contentType, - requestConfiguration: requestConfiguration) + let request = await AuthBackend.request( + for: request.requestURL(), + httpMethod: body == nil ? "GET" : "POST", + contentType: contentType, + requestConfiguration: requestConfiguration + ) let fetcher = fetcherService.fetcher(with: request) if let _ = requestConfiguration.emulatorHostAndPort { fetcher.allowLocalhostRequest = true diff --git a/FirebaseAuth/Sources/Swift/Backend/AuthRPCRequest.swift b/FirebaseAuth/Sources/Swift/Backend/AuthRPCRequest.swift index 34f40d82f88..eeacfbea897 100644 --- a/FirebaseAuth/Sources/Swift/Backend/AuthRPCRequest.swift +++ b/FirebaseAuth/Sources/Swift/Backend/AuthRPCRequest.swift @@ -22,14 +22,11 @@ protocol AuthRPCRequest { /// Gets the request's full URL. func requestURL() -> URL - /// Returns whether the request contains a post body or not. Requests without a post body are - /// GET requests. A default implementation returns `true`. - var containsPostBody: Bool { get } - /// Creates unencoded HTTP body representing the request. - /// - Throws: Any error which occurred constructing the request. /// - Returns: The HTTP body data representing the request before any encoding. - func unencodedHTTPRequestBody() throws -> [String: AnyHashable] + /// - Note: Requests with a post body are POST requests. Requests without a + /// post body are GET requests. + var unencodedHTTPRequestBody: [String: AnyHashable]? { get } /// The request configuration. func requestConfiguration() -> AuthRequestConfiguration @@ -41,7 +38,6 @@ protocol AuthRPCRequest { // in Obj-C. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *) extension AuthRPCRequest { - var containsPostBody: Bool { return true } func injectRecaptchaFields(recaptchaResponse: String?, recaptchaVersion: String) { fatalError("Internal FirebaseAuth Error: unimplemented injectRecaptchaFields") } diff --git a/FirebaseAuth/Sources/Swift/Backend/AuthRequestConfiguration.swift b/FirebaseAuth/Sources/Swift/Backend/AuthRequestConfiguration.swift index 47aff1be47e..510e9287ca0 100644 --- a/FirebaseAuth/Sources/Swift/Backend/AuthRequestConfiguration.swift +++ b/FirebaseAuth/Sources/Swift/Backend/AuthRequestConfiguration.swift @@ -38,9 +38,6 @@ class AuthRequestConfiguration { /// The appCheck is used to generate a token. var appCheck: AppCheckInterop? - /// The HTTP method used in the request. - var httpMethod: String - /// Additional framework marker that will be added as part of the header of every request. var additionalFrameworkMarker: String? @@ -57,6 +54,5 @@ class AuthRequestConfiguration { self.auth = auth self.heartbeatLogger = heartbeatLogger self.appCheck = appCheck - httpMethod = "POST" } } diff --git a/FirebaseAuth/Sources/Swift/Backend/IdentityToolkitRequest.swift b/FirebaseAuth/Sources/Swift/Backend/IdentityToolkitRequest.swift index 3a5d5a3b18a..08f9f9f30d6 100644 --- a/FirebaseAuth/Sources/Swift/Backend/IdentityToolkitRequest.swift +++ b/FirebaseAuth/Sources/Swift/Backend/IdentityToolkitRequest.swift @@ -70,8 +70,6 @@ class IdentityToolkitRequest { tenantID = requestConfiguration.auth?.tenantID } - var containsPostBody: Bool { return true } - func queryParams() -> String { return "" } diff --git a/FirebaseAuth/Sources/Swift/Backend/RPC/CreateAuthURIRequest.swift b/FirebaseAuth/Sources/Swift/Backend/RPC/CreateAuthURIRequest.swift index 419419f4862..987b7d28fe6 100644 --- a/FirebaseAuth/Sources/Swift/Backend/RPC/CreateAuthURIRequest.swift +++ b/FirebaseAuth/Sources/Swift/Backend/RPC/CreateAuthURIRequest.swift @@ -78,7 +78,7 @@ class CreateAuthURIRequest: IdentityToolkitRequest, AuthRPCRequest { super.init(endpoint: kCreateAuthURIEndpoint, requestConfiguration: requestConfiguration) } - func unencodedHTTPRequestBody() throws -> [String: AnyHashable] { + var unencodedHTTPRequestBody: [String: AnyHashable]? { var postBody: [String: AnyHashable] = [ kIdentifierKey: identifier, kContinueURIKey: continueURI, diff --git a/FirebaseAuth/Sources/Swift/Backend/RPC/DeleteAccountRequest.swift b/FirebaseAuth/Sources/Swift/Backend/RPC/DeleteAccountRequest.swift index 81856a0c027..543d5af3b8f 100644 --- a/FirebaseAuth/Sources/Swift/Backend/RPC/DeleteAccountRequest.swift +++ b/FirebaseAuth/Sources/Swift/Backend/RPC/DeleteAccountRequest.swift @@ -41,7 +41,7 @@ class DeleteAccountRequest: IdentityToolkitRequest, AuthRPCRequest { super.init(endpoint: kDeleteAccountEndpoint, requestConfiguration: requestConfiguration) } - func unencodedHTTPRequestBody() throws -> [String: AnyHashable] { + var unencodedHTTPRequestBody: [String: AnyHashable]? { [ kIDTokenKey: accessToken, kLocalIDKey: localID, diff --git a/FirebaseAuth/Sources/Swift/Backend/RPC/EmailLinkSignInRequest.swift b/FirebaseAuth/Sources/Swift/Backend/RPC/EmailLinkSignInRequest.swift index b57c2170450..72a23517985 100644 --- a/FirebaseAuth/Sources/Swift/Backend/RPC/EmailLinkSignInRequest.swift +++ b/FirebaseAuth/Sources/Swift/Backend/RPC/EmailLinkSignInRequest.swift @@ -51,7 +51,7 @@ class EmailLinkSignInRequest: IdentityToolkitRequest, AuthRPCRequest { super.init(endpoint: kEmailLinkSigninEndpoint, requestConfiguration: requestConfiguration) } - func unencodedHTTPRequestBody() throws -> [String: AnyHashable] { + var unencodedHTTPRequestBody: [String: AnyHashable]? { var postBody: [String: AnyHashable] = [ kEmailKey: email, kOOBCodeKey: oobCode, diff --git a/FirebaseAuth/Sources/Swift/Backend/RPC/GetAccountInfoRequest.swift b/FirebaseAuth/Sources/Swift/Backend/RPC/GetAccountInfoRequest.swift index 3e30fae5242..5dba854b172 100644 --- a/FirebaseAuth/Sources/Swift/Backend/RPC/GetAccountInfoRequest.swift +++ b/FirebaseAuth/Sources/Swift/Backend/RPC/GetAccountInfoRequest.swift @@ -39,7 +39,7 @@ class GetAccountInfoRequest: IdentityToolkitRequest, AuthRPCRequest { super.init(endpoint: kGetAccountInfoEndpoint, requestConfiguration: requestConfiguration) } - func unencodedHTTPRequestBody() throws -> [String: AnyHashable] { + var unencodedHTTPRequestBody: [String: AnyHashable]? { return [kIDTokenKey: accessToken] } } diff --git a/FirebaseAuth/Sources/Swift/Backend/RPC/GetOOBConfirmationCodeRequest.swift b/FirebaseAuth/Sources/Swift/Backend/RPC/GetOOBConfirmationCodeRequest.swift index c1de49fd071..e2437a569bd 100644 --- a/FirebaseAuth/Sources/Swift/Backend/RPC/GetOOBConfirmationCodeRequest.swift +++ b/FirebaseAuth/Sources/Swift/Backend/RPC/GetOOBConfirmationCodeRequest.swift @@ -228,7 +228,7 @@ class GetOOBConfirmationCodeRequest: IdentityToolkitRequest, AuthRPCRequest { requestConfiguration: requestConfiguration) } - func unencodedHTTPRequestBody() throws -> [String: AnyHashable] { + var unencodedHTTPRequestBody: [String: AnyHashable]? { var body: [String: AnyHashable] = [ kRequestTypeKey: requestType.value, ] diff --git a/FirebaseAuth/Sources/Swift/Backend/RPC/GetProjectConfigRequest.swift b/FirebaseAuth/Sources/Swift/Backend/RPC/GetProjectConfigRequest.swift index 1b8f714b60e..1a1b011ff01 100644 --- a/FirebaseAuth/Sources/Swift/Backend/RPC/GetProjectConfigRequest.swift +++ b/FirebaseAuth/Sources/Swift/Backend/RPC/GetProjectConfigRequest.swift @@ -15,7 +15,6 @@ import Foundation /// The "getProjectConfig" endpoint. - private let kGetProjectConfigEndPoint = "getProjectConfig" @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *) @@ -23,14 +22,10 @@ class GetProjectConfigRequest: IdentityToolkitRequest, AuthRPCRequest { typealias Response = GetProjectConfigResponse init(requestConfiguration: AuthRequestConfiguration) { - requestConfiguration.httpMethod = "GET" super.init(endpoint: kGetProjectConfigEndPoint, requestConfiguration: requestConfiguration) } - func unencodedHTTPRequestBody() throws -> [String: AnyHashable] { - // TODO: Probably nicer to throw, but what should we throw? - fatalError() + var unencodedHTTPRequestBody: [String: AnyHashable]? { + nil } - - override var containsPostBody: Bool { return false } } diff --git a/FirebaseAuth/Sources/Swift/Backend/RPC/GetRecaptchaConfigRequest.swift b/FirebaseAuth/Sources/Swift/Backend/RPC/GetRecaptchaConfigRequest.swift index fc8e287a52f..075d8273a15 100644 --- a/FirebaseAuth/Sources/Swift/Backend/RPC/GetRecaptchaConfigRequest.swift +++ b/FirebaseAuth/Sources/Swift/Backend/RPC/GetRecaptchaConfigRequest.swift @@ -48,7 +48,6 @@ class GetRecaptchaConfigRequest: IdentityToolkitRequest, AuthRPCRequest { typealias Response = GetRecaptchaConfigResponse required init(requestConfiguration: AuthRequestConfiguration) { - requestConfiguration.httpMethod = "GET" super.init( endpoint: kGetRecaptchaConfigEndpoint, requestConfiguration: requestConfiguration, @@ -56,12 +55,10 @@ class GetRecaptchaConfigRequest: IdentityToolkitRequest, AuthRPCRequest { ) } - func unencodedHTTPRequestBody() throws -> [String: AnyHashable] { - return [:] + var unencodedHTTPRequestBody: [String: AnyHashable]? { + nil } - override var containsPostBody: Bool { return false } - override func queryParams() -> String { var queryParams = "&\(kClientTypeKey)=\(clientType)&\(kVersionKey)=\(kRecaptchaVersion)" if let tenantID { diff --git a/FirebaseAuth/Sources/Swift/Backend/RPC/MultiFactor/Enroll/FinalizeMFAEnrollmentRequest.swift b/FirebaseAuth/Sources/Swift/Backend/RPC/MultiFactor/Enroll/FinalizeMFAEnrollmentRequest.swift index 89374c9de4f..0c1154d5a68 100644 --- a/FirebaseAuth/Sources/Swift/Backend/RPC/MultiFactor/Enroll/FinalizeMFAEnrollmentRequest.swift +++ b/FirebaseAuth/Sources/Swift/Backend/RPC/MultiFactor/Enroll/FinalizeMFAEnrollmentRequest.swift @@ -71,7 +71,7 @@ class FinalizeMFAEnrollmentRequest: IdentityToolkitRequest, AuthRPCRequest { ) } - func unencodedHTTPRequestBody() throws -> [String: AnyHashable] { + var unencodedHTTPRequestBody: [String: AnyHashable]? { var body: [String: AnyHashable] = [:] if let idToken = idToken { body["idToken"] = idToken diff --git a/FirebaseAuth/Sources/Swift/Backend/RPC/MultiFactor/Enroll/StartMFAEnrollmentRequest.swift b/FirebaseAuth/Sources/Swift/Backend/RPC/MultiFactor/Enroll/StartMFAEnrollmentRequest.swift index 3896ec14b3d..467733768fc 100644 --- a/FirebaseAuth/Sources/Swift/Backend/RPC/MultiFactor/Enroll/StartMFAEnrollmentRequest.swift +++ b/FirebaseAuth/Sources/Swift/Backend/RPC/MultiFactor/Enroll/StartMFAEnrollmentRequest.swift @@ -64,7 +64,7 @@ class StartMFAEnrollmentRequest: IdentityToolkitRequest, AuthRPCRequest { ) } - func unencodedHTTPRequestBody() throws -> [String: AnyHashable] { + var unencodedHTTPRequestBody: [String: AnyHashable]? { var body: [String: AnyHashable] = [:] if let idToken = idToken { body["idToken"] = idToken diff --git a/FirebaseAuth/Sources/Swift/Backend/RPC/MultiFactor/SignIn/FinalizeMFASignInRequest.swift b/FirebaseAuth/Sources/Swift/Backend/RPC/MultiFactor/SignIn/FinalizeMFASignInRequest.swift index 399e426d456..7e8b67eca96 100644 --- a/FirebaseAuth/Sources/Swift/Backend/RPC/MultiFactor/SignIn/FinalizeMFASignInRequest.swift +++ b/FirebaseAuth/Sources/Swift/Backend/RPC/MultiFactor/SignIn/FinalizeMFASignInRequest.swift @@ -36,7 +36,7 @@ class FinalizeMFASignInRequest: IdentityToolkitRequest, AuthRPCRequest { useIdentityPlatform: true) } - func unencodedHTTPRequestBody() throws -> [String: AnyHashable] { + var unencodedHTTPRequestBody: [String: AnyHashable]? { var body: [String: AnyHashable] = [:] if let mfaPendingCredential = mfaPendingCredential { body["mfaPendingCredential"] = mfaPendingCredential diff --git a/FirebaseAuth/Sources/Swift/Backend/RPC/MultiFactor/SignIn/StartMFASignInRequest.swift b/FirebaseAuth/Sources/Swift/Backend/RPC/MultiFactor/SignIn/StartMFASignInRequest.swift index 1098c2ef4ea..82e1a1721bd 100644 --- a/FirebaseAuth/Sources/Swift/Backend/RPC/MultiFactor/SignIn/StartMFASignInRequest.swift +++ b/FirebaseAuth/Sources/Swift/Backend/RPC/MultiFactor/SignIn/StartMFASignInRequest.swift @@ -41,7 +41,7 @@ class StartMFASignInRequest: IdentityToolkitRequest, AuthRPCRequest { ) } - func unencodedHTTPRequestBody() throws -> [String: AnyHashable] { + var unencodedHTTPRequestBody: [String: AnyHashable]? { var body: [String: AnyHashable] = [:] if let MFAPendingCredential = MFAPendingCredential { body["mfaPendingCredential"] = MFAPendingCredential diff --git a/FirebaseAuth/Sources/Swift/Backend/RPC/MultiFactor/Unenroll/WithdrawMFARequest.swift b/FirebaseAuth/Sources/Swift/Backend/RPC/MultiFactor/Unenroll/WithdrawMFARequest.swift index 19d9a04faed..f2324db3ca2 100644 --- a/FirebaseAuth/Sources/Swift/Backend/RPC/MultiFactor/Unenroll/WithdrawMFARequest.swift +++ b/FirebaseAuth/Sources/Swift/Backend/RPC/MultiFactor/Unenroll/WithdrawMFARequest.swift @@ -36,7 +36,7 @@ class WithdrawMFARequest: IdentityToolkitRequest, AuthRPCRequest { useIdentityPlatform: true) } - func unencodedHTTPRequestBody() throws -> [String: AnyHashable] { + var unencodedHTTPRequestBody: [String: AnyHashable]? { var postBody: [String: AnyHashable] = [:] if let idToken = idToken { postBody["idToken"] = idToken diff --git a/FirebaseAuth/Sources/Swift/Backend/RPC/ResetPasswordRequest.swift b/FirebaseAuth/Sources/Swift/Backend/RPC/ResetPasswordRequest.swift index aa39c927b83..c0b2629683d 100644 --- a/FirebaseAuth/Sources/Swift/Backend/RPC/ResetPasswordRequest.swift +++ b/FirebaseAuth/Sources/Swift/Backend/RPC/ResetPasswordRequest.swift @@ -48,7 +48,7 @@ class ResetPasswordRequest: IdentityToolkitRequest, AuthRPCRequest { super.init(endpoint: kResetPasswordEndpoint, requestConfiguration: requestConfiguration) } - func unencodedHTTPRequestBody() throws -> [String: AnyHashable] { + var unencodedHTTPRequestBody: [String: AnyHashable]? { var postBody: [String: AnyHashable] = [:] postBody[kOOBCodeKey] = oobCode diff --git a/FirebaseAuth/Sources/Swift/Backend/RPC/RevokeTokenRequest.swift b/FirebaseAuth/Sources/Swift/Backend/RPC/RevokeTokenRequest.swift index c3afae3b573..94e6deeeabb 100644 --- a/FirebaseAuth/Sources/Swift/Backend/RPC/RevokeTokenRequest.swift +++ b/FirebaseAuth/Sources/Swift/Backend/RPC/RevokeTokenRequest.swift @@ -71,7 +71,7 @@ class RevokeTokenRequest: IdentityToolkitRequest, AuthRPCRequest { useIdentityPlatform: true) } - func unencodedHTTPRequestBody() throws -> [String: AnyHashable] { + var unencodedHTTPRequestBody: [String: AnyHashable]? { let body: [String: AnyHashable] = [ kProviderIDKey: providerID, kTokenTypeKey: "\(tokenType.rawValue)", diff --git a/FirebaseAuth/Sources/Swift/Backend/RPC/SecureTokenRequest.swift b/FirebaseAuth/Sources/Swift/Backend/RPC/SecureTokenRequest.swift index d11275dcf29..175b1889b96 100644 --- a/FirebaseAuth/Sources/Swift/Backend/RPC/SecureTokenRequest.swift +++ b/FirebaseAuth/Sources/Swift/Backend/RPC/SecureTokenRequest.swift @@ -134,9 +134,7 @@ class SecureTokenRequest: AuthRPCRequest { return URL(string: urlString)! } - var containsPostBody: Bool { return true } - - func unencodedHTTPRequestBody() throws -> [String: AnyHashable] { + var unencodedHTTPRequestBody: [String: AnyHashable]? { var postBody: [String: AnyHashable] = [ kGrantTypeKey: grantType.value, ] diff --git a/FirebaseAuth/Sources/Swift/Backend/RPC/SendVerificationTokenRequest.swift b/FirebaseAuth/Sources/Swift/Backend/RPC/SendVerificationTokenRequest.swift index af090fdd3b3..a87ff833c10 100644 --- a/FirebaseAuth/Sources/Swift/Backend/RPC/SendVerificationTokenRequest.swift +++ b/FirebaseAuth/Sources/Swift/Backend/RPC/SendVerificationTokenRequest.swift @@ -60,7 +60,7 @@ class SendVerificationCodeRequest: IdentityToolkitRequest, AuthRPCRequest { ) } - func unencodedHTTPRequestBody() throws -> [String: AnyHashable] { + var unencodedHTTPRequestBody: [String: AnyHashable]? { var postBody: [String: AnyHashable] = [:] postBody[kPhoneNumberKey] = phoneNumber switch codeIdentity { diff --git a/FirebaseAuth/Sources/Swift/Backend/RPC/SetAccountInfoRequest.swift b/FirebaseAuth/Sources/Swift/Backend/RPC/SetAccountInfoRequest.swift index 82842ace520..5e310d4a656 100644 --- a/FirebaseAuth/Sources/Swift/Backend/RPC/SetAccountInfoRequest.swift +++ b/FirebaseAuth/Sources/Swift/Backend/RPC/SetAccountInfoRequest.swift @@ -136,7 +136,7 @@ class SetAccountInfoRequest: IdentityToolkitRequest, AuthRPCRequest { super.init(endpoint: kSetAccountInfoEndpoint, requestConfiguration: requestConfiguration) } - func unencodedHTTPRequestBody() throws -> [String: AnyHashable] { + var unencodedHTTPRequestBody: [String: AnyHashable]? { var postBody: [String: AnyHashable] = [:] if let accessToken { postBody[kIDTokenKey] = accessToken diff --git a/FirebaseAuth/Sources/Swift/Backend/RPC/SignInWithGameCenterRequest.swift b/FirebaseAuth/Sources/Swift/Backend/RPC/SignInWithGameCenterRequest.swift index adab76dcd34..7d722326f71 100644 --- a/FirebaseAuth/Sources/Swift/Backend/RPC/SignInWithGameCenterRequest.swift +++ b/FirebaseAuth/Sources/Swift/Backend/RPC/SignInWithGameCenterRequest.swift @@ -76,7 +76,7 @@ class SignInWithGameCenterRequest: IdentityToolkitRequest, AuthRPCRequest { ) } - func unencodedHTTPRequestBody() throws -> [String: AnyHashable] { + var unencodedHTTPRequestBody: [String: AnyHashable]? { var postBody: [String: AnyHashable] = [ "playerId": playerID, "publicKeyUrl": publicKeyURL.absoluteString, diff --git a/FirebaseAuth/Sources/Swift/Backend/RPC/SignUpNewUserRequest.swift b/FirebaseAuth/Sources/Swift/Backend/RPC/SignUpNewUserRequest.swift index 12308573334..68cfe6b5e38 100644 --- a/FirebaseAuth/Sources/Swift/Backend/RPC/SignUpNewUserRequest.swift +++ b/FirebaseAuth/Sources/Swift/Backend/RPC/SignUpNewUserRequest.swift @@ -89,7 +89,7 @@ class SignUpNewUserRequest: IdentityToolkitRequest, AuthRPCRequest { super.init(endpoint: kSignupNewUserEndpoint, requestConfiguration: requestConfiguration) } - func unencodedHTTPRequestBody() throws -> [String: AnyHashable] { + var unencodedHTTPRequestBody: [String: AnyHashable]? { var postBody: [String: AnyHashable] = [:] if let email { postBody[kEmailKey] = email diff --git a/FirebaseAuth/Sources/Swift/Backend/RPC/VerifyAssertionRequest.swift b/FirebaseAuth/Sources/Swift/Backend/RPC/VerifyAssertionRequest.swift index 9e8d8b6b86d..30c924ff403 100644 --- a/FirebaseAuth/Sources/Swift/Backend/RPC/VerifyAssertionRequest.swift +++ b/FirebaseAuth/Sources/Swift/Backend/RPC/VerifyAssertionRequest.swift @@ -135,7 +135,7 @@ class VerifyAssertionRequest: IdentityToolkitRequest, AuthRPCRequest { super.init(endpoint: kVerifyAssertionEndpoint, requestConfiguration: requestConfiguration) } - func unencodedHTTPRequestBody() throws -> [String: AnyHashable] { + var unencodedHTTPRequestBody: [String: AnyHashable]? { var components = URLComponents() var queryItems: [URLQueryItem] = [URLQueryItem(name: kProviderIDKey, value: providerID)] if let providerIDToken = providerIDToken { diff --git a/FirebaseAuth/Sources/Swift/Backend/RPC/VerifyCustomTokenRequest.swift b/FirebaseAuth/Sources/Swift/Backend/RPC/VerifyCustomTokenRequest.swift index af1518ee653..b8a97278908 100644 --- a/FirebaseAuth/Sources/Swift/Backend/RPC/VerifyCustomTokenRequest.swift +++ b/FirebaseAuth/Sources/Swift/Backend/RPC/VerifyCustomTokenRequest.swift @@ -40,7 +40,7 @@ class VerifyCustomTokenRequest: IdentityToolkitRequest, AuthRPCRequest { super.init(endpoint: kVerifyCustomTokenEndpoint, requestConfiguration: requestConfiguration) } - func unencodedHTTPRequestBody() throws -> [String: AnyHashable] { + var unencodedHTTPRequestBody: [String: AnyHashable]? { var postBody: [String: AnyHashable] = [ kTokenKey: token, ] diff --git a/FirebaseAuth/Sources/Swift/Backend/RPC/VerifyPasswordRequest.swift b/FirebaseAuth/Sources/Swift/Backend/RPC/VerifyPasswordRequest.swift index 2c9fc19d17c..4c1096d5fbe 100644 --- a/FirebaseAuth/Sources/Swift/Backend/RPC/VerifyPasswordRequest.swift +++ b/FirebaseAuth/Sources/Swift/Backend/RPC/VerifyPasswordRequest.swift @@ -79,7 +79,7 @@ class VerifyPasswordRequest: IdentityToolkitRequest, AuthRPCRequest { super.init(endpoint: kVerifyPasswordEndpoint, requestConfiguration: requestConfiguration) } - func unencodedHTTPRequestBody() throws -> [String: AnyHashable] { + var unencodedHTTPRequestBody: [String: AnyHashable]? { var body: [String: AnyHashable] = [ kEmailKey: email, kPasswordKey: password, diff --git a/FirebaseAuth/Sources/Swift/Backend/RPC/VerifyPhoneNumberRequest.swift b/FirebaseAuth/Sources/Swift/Backend/RPC/VerifyPhoneNumberRequest.swift index 791c6fc9e01..07ddf167527 100644 --- a/FirebaseAuth/Sources/Swift/Backend/RPC/VerifyPhoneNumberRequest.swift +++ b/FirebaseAuth/Sources/Swift/Backend/RPC/VerifyPhoneNumberRequest.swift @@ -133,7 +133,7 @@ class VerifyPhoneNumberRequest: IdentityToolkitRequest, AuthRPCRequest { ) } - func unencodedHTTPRequestBody() throws -> [String: AnyHashable] { + var unencodedHTTPRequestBody: [String: AnyHashable]? { var postBody: [String: AnyHashable] = [:] if let verificationID { postBody[kVerificationIDKey] = verificationID diff --git a/FirebaseAuth/Sources/Swift/Backend/VerifyClientRequest.swift b/FirebaseAuth/Sources/Swift/Backend/VerifyClientRequest.swift index f62f9def230..5821fc55c7e 100644 --- a/FirebaseAuth/Sources/Swift/Backend/VerifyClientRequest.swift +++ b/FirebaseAuth/Sources/Swift/Backend/VerifyClientRequest.swift @@ -27,7 +27,7 @@ class VerifyClientRequest: IdentityToolkitRequest, AuthRPCRequest { /// The key for the isSandbox request parameter. private static let isSandboxKey = "isSandbox" - func unencodedHTTPRequestBody() throws -> [String: AnyHashable] { + var unencodedHTTPRequestBody: [String: AnyHashable]? { var postBody = [String: AnyHashable]() if let appToken = appToken { postBody[Self.appTokenKey] = appToken diff --git a/FirebaseAuth/Tests/Unit/AuthBackendTests.swift b/FirebaseAuth/Tests/Unit/AuthBackendTests.swift index 5feaf239e3b..6056bbae362 100644 --- a/FirebaseAuth/Tests/Unit/AuthBackendTests.swift +++ b/FirebaseAuth/Tests/Unit/AuthBackendTests.swift @@ -50,13 +50,7 @@ class AuthBackendTests: RPCBaseTests { let underlyingError = try XCTUnwrap(rpcError.userInfo[NSUnderlyingErrorKey] as? NSError) XCTAssertEqual(underlyingError.domain, AuthErrorUtils.internalErrorDomain) - XCTAssertEqual(underlyingError.code, AuthInternalErrorCode.RPCRequestEncodingError.rawValue) - - let underlyingUnderlying = try XCTUnwrap(underlyingError - .userInfo[NSUnderlyingErrorKey] as? NSError) - XCTAssertEqual(underlyingUnderlying.domain, kFakeErrorDomain) - XCTAssertEqual(underlyingUnderlying.code, kFakeErrorCode) - + XCTAssertEqual(underlyingError.code, AuthInternalErrorCode.JSONSerializationError.rawValue) XCTAssertNil(underlyingError.userInfo[AuthErrorUtils.userInfoDeserializedResponseKey]) XCTAssertNil(underlyingError.userInfo[AuthErrorUtils.userInfoDataKey]) } @@ -670,11 +664,17 @@ class AuthBackendTests: RPCBaseTests { return try! XCTUnwrap(URL(string: kFakeRequestURL)) } - func unencodedHTTPRequestBody() throws -> [String: AnyHashable] { - if let encodingError { - throw encodingError + var unencodedHTTPRequestBody: [String: AnyHashable]? { + if encodingError == nil { + return requestBody } - return requestBody + // Else, return an unencodable request body that will cause an error to be thrown. + struct UnencodableObject: Hashable { + static func == (lhs: UnencodableObject, rhs: UnencodableObject) -> Bool { + true + } + } + return ["foo": UnencodableObject()] } static func makeRequestConfiguration() -> AuthRequestConfiguration { @@ -684,8 +684,6 @@ class AuthBackendTests: RPCBaseTests { ) } - var containsPostBody: Bool { return true } - private let configuration: AuthRequestConfiguration let encodingError: NSError? @@ -724,8 +722,8 @@ class AuthBackendTests: RPCBaseTests { return fakeRequest.requestURL() } - func unencodedHTTPRequestBody() throws -> [String: AnyHashable] { - return try fakeRequest.unencodedHTTPRequestBody() + var unencodedHTTPRequestBody: [String: AnyHashable]? { + fakeRequest.unencodedHTTPRequestBody } func requestConfiguration() -> FirebaseAuth.AuthRequestConfiguration { diff --git a/FirebaseAuth/Tests/Unit/Fakes/FakeBackendRPCIssuer.swift b/FirebaseAuth/Tests/Unit/Fakes/FakeBackendRPCIssuer.swift index 3a40aac37d7..9131a19f98f 100644 --- a/FirebaseAuth/Tests/Unit/Fakes/FakeBackendRPCIssuer.swift +++ b/FirebaseAuth/Tests/Unit/Fakes/FakeBackendRPCIssuer.swift @@ -148,9 +148,13 @@ final class FakeBackendRPCIssuer: AuthBackendRPCIssuerProtocol, @unchecked Senda // Use the real implementation so that the complete request can // be verified during testing. completeRequest = Task { - await AuthBackend.request(withURL: requestURL!, - contentType: contentType, - requestConfiguration: request.requestConfiguration()) + await AuthBackend + .request( + for: request.requestURL(), + httpMethod: requestData == nil ? "GET" : "POST", + contentType: contentType, + requestConfiguration: request.requestConfiguration() + ) } decodedRequest = try? JSONSerialization.jsonObject(with: body) as? [String: Any] } diff --git a/FirebaseAuth/Tests/Unit/GetRecaptchaConfigTests.swift b/FirebaseAuth/Tests/Unit/GetRecaptchaConfigTests.swift index 75149c38d45..6638d6bfcc1 100644 --- a/FirebaseAuth/Tests/Unit/GetRecaptchaConfigTests.swift +++ b/FirebaseAuth/Tests/Unit/GetRecaptchaConfigTests.swift @@ -25,7 +25,7 @@ class GetRecaptchaConfigTests: RPCBaseTests { func testGetRecaptchaConfigRequest() async throws { let request = GetRecaptchaConfigRequest(requestConfiguration: makeRequestConfiguration()) // let _ = try await authBackend.call(with: request) - XCTAssertFalse(request.containsPostBody) + XCTAssertNil(request.unencodedHTTPRequestBody) // Confirm that the request has no decoded body as it is get request. XCTAssertNil(rpcIssuer.decodedRequest) diff --git a/FirebaseAuth/Tests/Unit/RPCBaseTests.swift b/FirebaseAuth/Tests/Unit/RPCBaseTests.swift index 9a8f9026ea9..bb7a247fadf 100644 --- a/FirebaseAuth/Tests/Unit/RPCBaseTests.swift +++ b/FirebaseAuth/Tests/Unit/RPCBaseTests.swift @@ -91,7 +91,7 @@ class RPCBaseTests: XCTestCase { rpcIssuer.respondBlock = { XCTAssertEqual(self.rpcIssuer.requestURL?.absoluteString, expected) if checkPostBody { - XCTAssertFalse(request.containsPostBody) + XCTAssertNil(request.unencodedHTTPRequestBody) } else if let requestDictionary = self.rpcIssuer.decodedRequest as? [String: AnyHashable] { XCTAssertEqual(requestDictionary[key], value) } else {