From ae603e5565d0e9bd699cd83e6b8d8582d975b534 Mon Sep 17 00:00:00 2001 From: Nick Cooke Date: Fri, 6 Dec 2024 10:34:09 -0500 Subject: [PATCH] Simplify approach --- .../Sources/ObjC/FIRRecaptchaBridge.m | 42 +++++-------------- .../Public/FirebaseAuth/FIRRecaptchaBridge.h | 12 ++---- .../Utilities/AuthRecaptchaVerifier.swift | 20 +++------ 3 files changed, 20 insertions(+), 54 deletions(-) diff --git a/FirebaseAuth/Sources/ObjC/FIRRecaptchaBridge.m b/FirebaseAuth/Sources/ObjC/FIRRecaptchaBridge.m index cf93edc3295..6ed445cf54e 100644 --- a/FirebaseAuth/Sources/ObjC/FIRRecaptchaBridge.m +++ b/FirebaseAuth/Sources/ObjC/FIRRecaptchaBridge.m @@ -18,43 +18,23 @@ #import "FirebaseAuth/Sources/Public/FirebaseAuth/FIRRecaptchaBridge.h" #import "RecaptchaInterop/RecaptchaInterop.h" -void __objc_getClientWithSiteKey( - NSString *siteKey, - Class recaptchaClass, - void (^completionHandler)(id _Nullable result, - NSError *_Nullable error)) { - SEL selector = NSSelectorFromString(@"getClientWithSiteKey:completion:"); - if (recaptchaClass && [recaptchaClass respondsToSelector:selector]) { - void (*funcWithoutTimeout)(id, SEL, NSString *, - void (^)(id _Nullable recaptchaClient, - NSError *_Nullable error)) = - (void *)[recaptchaClass methodForSelector:selector]; - funcWithoutTimeout(recaptchaClass, selector, siteKey, - ^(id _Nonnull client, NSError *_Nullable error) { - if (error) { - completionHandler(nil, error); - } else { - completionHandler(client, nil); - } - }); +Class _Nonnull __fir_castToRecaptchaProtocolFromClass(Class _Nonnull klass) { + if ([klass conformsToProtocol:@protocol(RCARecaptchaProtocol)]) { + NSLog(@"RCARecaptchaProtocol - true"); } else { - completionHandler(nil, nil); // TODO(ncooke3): Add error just in case. + NSLog(@"RCARecaptchaProtocol - false"); } + return (Class)klass; } -id _Nullable __fir_initActionFromClass(Class _Nonnull klass, - NSString *_Nonnull actionString) { - SEL customActionSelector = NSSelectorFromString(@"initWithCustomAction:"); - if (klass && [klass instancesRespondToSelector:customActionSelector]) { - id (*funcWithCustomAction)(id, SEL, NSString *) = - (id(*)(id, SEL, NSString *))[klass instanceMethodForSelector:customActionSelector]; - - id customAction = - funcWithCustomAction([klass alloc], customActionSelector, actionString); - return customAction; +Class _Nonnull __fir_castToRecaptchaActionProtocolFromClass( + Class _Nonnull klass) { + if ([klass conformsToProtocol:@protocol(RCAActionProtocol)]) { + NSLog(@"RCAActionProtocol - true"); } else { - return nil; + NSLog(@"RCAActionProtocol - false"); } + return (Class)klass; } #endif diff --git a/FirebaseAuth/Sources/Public/FirebaseAuth/FIRRecaptchaBridge.h b/FirebaseAuth/Sources/Public/FirebaseAuth/FIRRecaptchaBridge.h index bded2e63c91..05de63862e0 100644 --- a/FirebaseAuth/Sources/Public/FirebaseAuth/FIRRecaptchaBridge.h +++ b/FirebaseAuth/Sources/Public/FirebaseAuth/FIRRecaptchaBridge.h @@ -17,16 +17,12 @@ #if TARGET_OS_IOS -@protocol RCARecaptchaClientProtocol; +@protocol RCARecaptchaProtocol; @protocol RCAActionProtocol; -void __objc_getClientWithSiteKey( - NSString *_Nonnull siteKey, - Class _Nonnull recaptchaClass, - void (^_Nonnull completionHandler)(id _Nullable result, - NSError *_Nullable error)); +Class _Nonnull __fir_castToRecaptchaProtocolFromClass(Class _Nonnull klass); -id _Nullable __fir_initActionFromClass(Class _Nonnull klass, - NSString *_Nonnull actionString); +Class _Nonnull __fir_castToRecaptchaActionProtocolFromClass( + Class _Nonnull klass); #endif diff --git a/FirebaseAuth/Sources/Swift/Utilities/AuthRecaptchaVerifier.swift b/FirebaseAuth/Sources/Swift/Utilities/AuthRecaptchaVerifier.swift index d54aa5188c2..8fc78d8ea74 100644 --- a/FirebaseAuth/Sources/Swift/Utilities/AuthRecaptchaVerifier.swift +++ b/FirebaseAuth/Sources/Swift/Utilities/AuthRecaptchaVerifier.swift @@ -177,18 +177,8 @@ } else if let recaptcha = NSClassFromString("Recaptcha") { // Fall back to attempting to connect with pre-18.7.0 RecaptchaEnterprise. do { - let client: any RCARecaptchaClientProtocol = - try await withCheckedThrowingContinuation { continuation in - __objc_getClientWithSiteKey(siteKey, recaptcha) { client, error in - if let error { - continuation.resume(throwing: error) - } - if let client { - continuation.resume(returning: client) - } - // TODO(ncooke3): Handle other case. - } - } + let recaptcha = __fir_castToRecaptchaProtocolFromClass(recaptcha) + let client = try await recaptcha.getClient(withSiteKey: siteKey) recaptchaClient = client return await retrieveToken(actionString: actionString, fakeToken: fakeToken) } catch { @@ -208,10 +198,10 @@ let action = recaptchaAction.init(customAction: actionString) let token = try? await recaptchaClient!.execute(withAction: action) return (token ?? "NO_RECAPTCHA", nil, true, true) - } else if - let recaptchaAction = NSClassFromString("RecaptchaAction"), - let action = __fir_initActionFromClass(recaptchaAction, actionString) { + } else if let recaptchaAction = NSClassFromString("RecaptchaAction") { // Fall back to attempting to connect with pre-18.7.0 RecaptchaEnterprise. + let recaptchaAction = __fir_castToRecaptchaActionProtocolFromClass(recaptchaAction) + let action = recaptchaAction.init(customAction: actionString) let token = try? await recaptchaClient!.execute(withAction: action) return (token ?? "NO_RECAPTCHA", nil, true, true) } else {