Skip to content

Commit d672490

Browse files
committed
add do {
1 parent ccd08e6 commit d672490

File tree

1 file changed

+36
-32
lines changed

1 file changed

+36
-32
lines changed

FirebaseAuth/Sources/Swift/Utilities/AuthRecaptchaVerifier.swift

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -126,68 +126,72 @@
126126
return actionString
127127
#else
128128

129-
let (token, error, linked, actionCreated) = await recaptchaToken(siteKey: siteKey, actionString: actionString, fakeToken: "NO_RECAPTCHA")
130-
131-
guard linked else {
132-
throw AuthErrorUtils.recaptchaSDKNotLinkedError()
133-
}
134-
guard actionCreated else {
135-
throw AuthErrorUtils.recaptchaActionCreationFailed()
136-
}
137-
if let error {
138-
throw error
139-
}
140-
if token == "NO_RECAPTCHA" {
141-
AuthLog.logInfo(code: "I-AUT000031",
142-
message: "reCAPTCHA token retrieval failed. NO_RECAPTCHA sent as the fake code.")
143-
} else {
144-
AuthLog.logInfo(
145-
code: "I-AUT000030",
146-
message: "reCAPTCHA token retrieval succeeded."
129+
let (token, error, linked, actionCreated) = await recaptchaToken(
130+
siteKey: siteKey,
131+
actionString: actionString,
132+
fakeToken: "NO_RECAPTCHA"
147133
)
148-
}
149-
return token
134+
135+
guard linked else {
136+
throw AuthErrorUtils.recaptchaSDKNotLinkedError()
137+
}
138+
guard actionCreated else {
139+
throw AuthErrorUtils.recaptchaActionCreationFailed()
140+
}
141+
if let error {
142+
throw error
143+
}
144+
if token == "NO_RECAPTCHA" {
145+
AuthLog.logInfo(code: "I-AUT000031",
146+
message: "reCAPTCHA token retrieval failed. NO_RECAPTCHA sent as the fake code.")
147+
} else {
148+
AuthLog.logInfo(
149+
code: "I-AUT000030",
150+
message: "reCAPTCHA token retrieval succeeded."
151+
)
152+
}
153+
return token
150154
#endif // !(COCOAPODS || SWIFT_PACKAGE)
151155
}
152156

153157
private static var recaptchaClient: (any RCARecaptchaClientProtocol)?
154158

155-
private func recaptchaToken(
156-
siteKey: String,
157-
actionString: String,
158-
fakeToken: String
159-
) async -> (token: String, error: Error?, linked: Bool, actionCreated: Bool) {
159+
private func recaptchaToken(siteKey: String,
160+
actionString: String,
161+
fakeToken: String) async -> (token: String, error: Error?,
162+
linked: Bool, actionCreated: Bool) {
160163
if recaptchaClient != nil {
161164
return await retrieveToken(actionString: actionString, fakeToken: fakeToken)
162165
}
163166

164-
if let recaptcha = NSClassFromString("RecaptchaEnterprise.RCARecaptcha") as? RCARecaptchaProtocol.Type {
167+
if let recaptcha =
168+
NSClassFromString("RecaptchaEnterprise.RCARecaptcha") as? RCARecaptchaProtocol.Type {
165169
do {
166170
// TODO(ncooke3): This should be `fetchClient(withSiteKey:)`.
167171
let client = try await recaptcha.getClient(withSiteKey: siteKey)
168172
recaptchaClient = client
169173
return await retrieveToken(actionString: actionString, fakeToken: fakeToken)
170174
} catch {
171-
return ("", error, true, true);
175+
return ("", error, true, true)
172176
}
173177
} else if let recaptcha = NSClassFromString("Recaptcha") {
174178
// Fall back to attempting to connect with pre-18.7.0 RecaptchaEnterprise.
179+
do {
175180
let client = try await recaptcha.getClient(withSiteKey: siteKey)
176181
recaptchaClient = client
177182
return await retrieveToken(actionString: actionString, fakeToken: fakeToken)
178183
} catch {
179-
return ("", error, true, true);
184+
return ("", error, true, true)
180185
}
181186
} else {
182187
// RecaptchaEnterprise not linked.
183188
return ("", nil, false, false)
184189
}
185190
}
186191

187-
private func retrieveToken(
188-
actionString: String,
189-
fakeToken: String
190-
) async -> (token: String, error: Error?, linked: Bool, actionCreated: Bool) {
192+
private func retrieveToken(actionString: String,
193+
fakeToken: String) async -> (token: String, error: Error?,
194+
linked: Bool, actionCreated: Bool) {
191195
let recaptchaAction = (
192196
NSClassFromString("RecaptchaEnterprise.RCAAction") ?? NSClassFromString("RecaptchaAction")
193197
) as? RCAActionProtocol.Type

0 commit comments

Comments
 (0)