Skip to content

Commit

Permalink
[Auth] Remove httpMethod from AuthRequestConfiguration (#14143)
Browse files Browse the repository at this point in the history
  • Loading branch information
ncooke3 authored Nov 19, 2024
1 parent e8c71d1 commit d2ff93c
Show file tree
Hide file tree
Showing 33 changed files with 64 additions and 88 deletions.
23 changes: 7 additions & 16 deletions FirebaseAuth/Sources/Swift/Backend/AuthBackend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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")
Expand Down Expand Up @@ -163,21 +164,11 @@ final class AuthBackend: AuthBackendProtocol {
/// - Returns: The response.
fileprivate func callInternal<T: AuthRPCRequest>(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 {
Expand Down
9 changes: 6 additions & 3 deletions FirebaseAuth/Sources/Swift/Backend/AuthBackendRPCIssuer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 3 additions & 7 deletions FirebaseAuth/Sources/Swift/Backend/AuthRPCRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand All @@ -57,6 +54,5 @@ class AuthRequestConfiguration {
self.auth = auth
self.heartbeatLogger = heartbeatLogger
self.appCheck = appCheck
httpMethod = "POST"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ class IdentityToolkitRequest {
tenantID = requestConfiguration.auth?.tenantID
}

var containsPostBody: Bool { return true }

func queryParams() -> String {
return ""
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,17 @@
import Foundation

/// The "getProjectConfig" endpoint.

private let kGetProjectConfigEndPoint = "getProjectConfig"

@available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
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 }
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,17 @@ class GetRecaptchaConfigRequest: IdentityToolkitRequest, AuthRPCRequest {
typealias Response = GetRecaptchaConfigResponse

required init(requestConfiguration: AuthRequestConfiguration) {
requestConfiguration.httpMethod = "GET"
super.init(
endpoint: kGetRecaptchaConfigEndpoint,
requestConfiguration: requestConfiguration,
useIdentityPlatform: true
)
}

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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit d2ff93c

Please sign in to comment.