Skip to content

Commit d2ff93c

Browse files
authored
[Auth] Remove httpMethod from AuthRequestConfiguration (#14143)
1 parent e8c71d1 commit d2ff93c

33 files changed

+64
-88
lines changed

FirebaseAuth/Sources/Swift/Backend/AuthBackend.swift

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ final class AuthBackend: AuthBackendProtocol {
6060
}
6161
}
6262

63-
static func request(withURL url: URL,
63+
static func request(for url: URL,
64+
httpMethod: String,
6465
contentType: String,
6566
requestConfiguration: AuthRequestConfiguration) async -> URLRequest {
6667
// Kick off tasks for the async header values.
@@ -76,7 +77,7 @@ final class AuthBackend: AuthBackendProtocol {
7677
request.setValue(clientVersion, forHTTPHeaderField: "X-Client-Version")
7778
request.setValue(Bundle.main.bundleIdentifier, forHTTPHeaderField: "X-Ios-Bundle-Identifier")
7879
request.setValue(requestConfiguration.appID, forHTTPHeaderField: "X-Firebase-GMPID")
79-
request.httpMethod = requestConfiguration.httpMethod
80+
request.httpMethod = httpMethod
8081
let preferredLocalizations = Bundle.main.preferredLocalizations
8182
if preferredLocalizations.count > 0 {
8283
request.setValue(preferredLocalizations.first, forHTTPHeaderField: "Accept-Language")
@@ -163,21 +164,11 @@ final class AuthBackend: AuthBackendProtocol {
163164
/// - Returns: The response.
164165
fileprivate func callInternal<T: AuthRPCRequest>(with request: T) async throws -> T.Response {
165166
var bodyData: Data?
166-
if request.containsPostBody {
167-
var postBody: [String: AnyHashable]
168-
do {
169-
// TODO: Can unencodedHTTPRequestBody ever throw?
170-
// They don't today, but there are a few fatalErrors that might better be implemented as
171-
// thrown errors.. Although perhaps the case of 'containsPostBody' returning false could
172-
// perhaps be modeled differently so that the failing unencodedHTTPRequestBody could only
173-
// be called when a body exists...
174-
postBody = try request.unencodedHTTPRequestBody()
175-
} catch {
176-
throw AuthErrorUtils.RPCRequestEncodingError(underlyingError: error)
177-
}
178-
var JSONWritingOptions: JSONSerialization.WritingOptions = .init(rawValue: 0)
167+
if let postBody = request.unencodedHTTPRequestBody {
179168
#if DEBUG
180-
JSONWritingOptions = JSONSerialization.WritingOptions.prettyPrinted
169+
let JSONWritingOptions = JSONSerialization.WritingOptions.prettyPrinted
170+
#else
171+
let JSONWritingOptions = JSONSerialization.WritingOptions(rawValue: 0)
181172
#endif
182173

183174
guard JSONSerialization.isValidJSONObject(postBody) else {

FirebaseAuth/Sources/Swift/Backend/AuthBackendRPCIssuer.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,12 @@ final class AuthBackendRPCIssuer: AuthBackendRPCIssuerProtocol {
5252
body: Data?,
5353
contentType: String) async -> (Data?, Error?) {
5454
let requestConfiguration = request.requestConfiguration()
55-
let request = await AuthBackend.request(withURL: request.requestURL(),
56-
contentType: contentType,
57-
requestConfiguration: requestConfiguration)
55+
let request = await AuthBackend.request(
56+
for: request.requestURL(),
57+
httpMethod: body == nil ? "GET" : "POST",
58+
contentType: contentType,
59+
requestConfiguration: requestConfiguration
60+
)
5861
let fetcher = fetcherService.fetcher(with: request)
5962
if let _ = requestConfiguration.emulatorHostAndPort {
6063
fetcher.allowLocalhostRequest = true

FirebaseAuth/Sources/Swift/Backend/AuthRPCRequest.swift

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,11 @@ protocol AuthRPCRequest {
2222
/// Gets the request's full URL.
2323
func requestURL() -> URL
2424

25-
/// Returns whether the request contains a post body or not. Requests without a post body are
26-
/// GET requests. A default implementation returns `true`.
27-
var containsPostBody: Bool { get }
28-
2925
/// Creates unencoded HTTP body representing the request.
30-
/// - Throws: Any error which occurred constructing the request.
3126
/// - Returns: The HTTP body data representing the request before any encoding.
32-
func unencodedHTTPRequestBody() throws -> [String: AnyHashable]
27+
/// - Note: Requests with a post body are POST requests. Requests without a
28+
/// post body are GET requests.
29+
var unencodedHTTPRequestBody: [String: AnyHashable]? { get }
3330

3431
/// The request configuration.
3532
func requestConfiguration() -> AuthRequestConfiguration
@@ -41,7 +38,6 @@ protocol AuthRPCRequest {
4138
// in Obj-C.
4239
@available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
4340
extension AuthRPCRequest {
44-
var containsPostBody: Bool { return true }
4541
func injectRecaptchaFields(recaptchaResponse: String?, recaptchaVersion: String) {
4642
fatalError("Internal FirebaseAuth Error: unimplemented injectRecaptchaFields")
4743
}

FirebaseAuth/Sources/Swift/Backend/AuthRequestConfiguration.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ class AuthRequestConfiguration {
3838
/// The appCheck is used to generate a token.
3939
var appCheck: AppCheckInterop?
4040

41-
/// The HTTP method used in the request.
42-
var httpMethod: String
43-
4441
/// Additional framework marker that will be added as part of the header of every request.
4542
var additionalFrameworkMarker: String?
4643

@@ -57,6 +54,5 @@ class AuthRequestConfiguration {
5754
self.auth = auth
5855
self.heartbeatLogger = heartbeatLogger
5956
self.appCheck = appCheck
60-
httpMethod = "POST"
6157
}
6258
}

FirebaseAuth/Sources/Swift/Backend/IdentityToolkitRequest.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,6 @@ class IdentityToolkitRequest {
7070
tenantID = requestConfiguration.auth?.tenantID
7171
}
7272

73-
var containsPostBody: Bool { return true }
74-
7573
func queryParams() -> String {
7674
return ""
7775
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class CreateAuthURIRequest: IdentityToolkitRequest, AuthRPCRequest {
7878
super.init(endpoint: kCreateAuthURIEndpoint, requestConfiguration: requestConfiguration)
7979
}
8080

81-
func unencodedHTTPRequestBody() throws -> [String: AnyHashable] {
81+
var unencodedHTTPRequestBody: [String: AnyHashable]? {
8282
var postBody: [String: AnyHashable] = [
8383
kIdentifierKey: identifier,
8484
kContinueURIKey: continueURI,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class DeleteAccountRequest: IdentityToolkitRequest, AuthRPCRequest {
4141
super.init(endpoint: kDeleteAccountEndpoint, requestConfiguration: requestConfiguration)
4242
}
4343

44-
func unencodedHTTPRequestBody() throws -> [String: AnyHashable] {
44+
var unencodedHTTPRequestBody: [String: AnyHashable]? {
4545
[
4646
kIDTokenKey: accessToken,
4747
kLocalIDKey: localID,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class EmailLinkSignInRequest: IdentityToolkitRequest, AuthRPCRequest {
5151
super.init(endpoint: kEmailLinkSigninEndpoint, requestConfiguration: requestConfiguration)
5252
}
5353

54-
func unencodedHTTPRequestBody() throws -> [String: AnyHashable] {
54+
var unencodedHTTPRequestBody: [String: AnyHashable]? {
5555
var postBody: [String: AnyHashable] = [
5656
kEmailKey: email,
5757
kOOBCodeKey: oobCode,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class GetAccountInfoRequest: IdentityToolkitRequest, AuthRPCRequest {
3939
super.init(endpoint: kGetAccountInfoEndpoint, requestConfiguration: requestConfiguration)
4040
}
4141

42-
func unencodedHTTPRequestBody() throws -> [String: AnyHashable] {
42+
var unencodedHTTPRequestBody: [String: AnyHashable]? {
4343
return [kIDTokenKey: accessToken]
4444
}
4545
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ class GetOOBConfirmationCodeRequest: IdentityToolkitRequest, AuthRPCRequest {
228228
requestConfiguration: requestConfiguration)
229229
}
230230

231-
func unencodedHTTPRequestBody() throws -> [String: AnyHashable] {
231+
var unencodedHTTPRequestBody: [String: AnyHashable]? {
232232
var body: [String: AnyHashable] = [
233233
kRequestTypeKey: requestType.value,
234234
]

0 commit comments

Comments
 (0)