-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Auth] Add support for upcoming Recaptcha changes #14201
base: main
Are you sure you want to change the base?
Changes from all commits
c36c220
ccd08e6
d672490
b570287
f56161a
40c62c1
00dff7f
a0b4b85
98ef15f
0aa8328
e991148
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll refactor this further in a following PR. It more easily reviewable now as it's a direct port of the ObjC bridge logic. |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -125,38 +125,86 @@ | |||||||
// No recaptcha on internal build system. | ||||||||
return actionString | ||||||||
#else | ||||||||
return try await withCheckedThrowingContinuation { continuation in | ||||||||
FIRRecaptchaGetToken(siteKey, actionString, | ||||||||
"NO_RECAPTCHA") { (token: String, error: Error?, | ||||||||
linked: Bool, actionCreated: Bool) in | ||||||||
guard linked else { | ||||||||
continuation.resume(throwing: AuthErrorUtils.recaptchaSDKNotLinkedError()) | ||||||||
return | ||||||||
} | ||||||||
guard actionCreated else { | ||||||||
continuation.resume(throwing: AuthErrorUtils.recaptchaActionCreationFailed()) | ||||||||
return | ||||||||
} | ||||||||
if let error { | ||||||||
continuation.resume(throwing: error) | ||||||||
return | ||||||||
} else { | ||||||||
if token == "NO_RECAPTCHA" { | ||||||||
AuthLog.logInfo(code: "I-AUT000031", | ||||||||
message: "reCAPTCHA token retrieval failed. NO_RECAPTCHA sent as the fake code.") | ||||||||
} else { | ||||||||
AuthLog.logInfo( | ||||||||
code: "I-AUT000030", | ||||||||
message: "reCAPTCHA token retrieval succeeded." | ||||||||
) | ||||||||
} | ||||||||
continuation.resume(returning: token) | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
let (token, error, linked, actionCreated) = await recaptchaToken( | ||||||||
siteKey: siteKey, | ||||||||
actionString: actionString, | ||||||||
fakeToken: "NO_RECAPTCHA" | ||||||||
) | ||||||||
|
||||||||
guard linked else { | ||||||||
throw AuthErrorUtils.recaptchaSDKNotLinkedError() | ||||||||
} | ||||||||
guard actionCreated else { | ||||||||
throw AuthErrorUtils.recaptchaActionCreationFailed() | ||||||||
} | ||||||||
if let error { | ||||||||
throw error | ||||||||
} | ||||||||
if token == "NO_RECAPTCHA" { | ||||||||
AuthLog.logInfo(code: "I-AUT000031", | ||||||||
message: "reCAPTCHA token retrieval failed. NO_RECAPTCHA sent as the fake code.") | ||||||||
} else { | ||||||||
AuthLog.logInfo( | ||||||||
code: "I-AUT000030", | ||||||||
message: "reCAPTCHA token retrieval succeeded." | ||||||||
) | ||||||||
} | ||||||||
return token | ||||||||
#endif // !(COCOAPODS || SWIFT_PACKAGE) | ||||||||
} | ||||||||
|
||||||||
private static var recaptchaClient: (any RCARecaptchaClientProtocol)? | ||||||||
|
||||||||
private func recaptchaToken(siteKey: String, | ||||||||
actionString: String, | ||||||||
fakeToken: String) async -> (token: String, error: Error?, | ||||||||
linked: Bool, actionCreated: Bool) { | ||||||||
if let recaptchaClient { | ||||||||
return await retrieveToken( | ||||||||
actionString: actionString, | ||||||||
fakeToken: fakeToken, | ||||||||
recaptchaClient: recaptchaClient | ||||||||
) | ||||||||
} | ||||||||
|
||||||||
if let recaptcha = | ||||||||
NSClassFromString("RecaptchaEnterprise.RCARecaptcha") as? RCARecaptchaProtocol.Type { | ||||||||
do { | ||||||||
// let client = try await recaptcha.fetchClient(withSiteKey: siteKey) | ||||||||
let client = try await recaptcha.getClient(withSiteKey: siteKey) | ||||||||
Comment on lines
+174
to
+175
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. when recaptcha SDK is released:
Suggested change
|
||||||||
recaptchaClient = client | ||||||||
return await retrieveToken( | ||||||||
actionString: actionString, | ||||||||
fakeToken: fakeToken, | ||||||||
recaptchaClient: client | ||||||||
) | ||||||||
} catch { | ||||||||
return ("", error, true, true) | ||||||||
} | ||||||||
} else { | ||||||||
// RecaptchaEnterprise not linked. | ||||||||
return ("", nil, false, false) | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
private func retrieveToken(actionString: String, | ||||||||
fakeToken: String, | ||||||||
recaptchaClient: RCARecaptchaClientProtocol) async -> (token: String, | ||||||||
error: Error?, | ||||||||
linked: Bool, | ||||||||
actionCreated: Bool) { | ||||||||
if let recaptchaAction = | ||||||||
NSClassFromString("RecaptchaEnterprise.RCAAction") as? RCAActionProtocol.Type { | ||||||||
let action = recaptchaAction.init(customAction: actionString) | ||||||||
let token = try? await recaptchaClient.execute(withAction: action) | ||||||||
return (token ?? "NO_RECAPTCHA", nil, true, true) | ||||||||
} else { | ||||||||
// RecaptchaEnterprise not linked. | ||||||||
return ("", nil, false, false) | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
func retrieveRecaptchaConfig(forceRefresh: Bool) async throws { | ||||||||
if !forceRefresh { | ||||||||
if let tenantID = auth?.tenantID { | ||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: update with final Recaptcha version
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And the Recaptcha non-beta version publishing gates the merge.