Skip to content

Commit

Permalink
Simplify approach
Browse files Browse the repository at this point in the history
  • Loading branch information
ncooke3 committed Dec 6, 2024
1 parent 1a591ed commit ae603e5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 54 deletions.
42 changes: 11 additions & 31 deletions FirebaseAuth/Sources/ObjC/FIRRecaptchaBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -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<RCARecaptchaClientProtocol> _Nullable result,
NSError *_Nullable error)) {
SEL selector = NSSelectorFromString(@"getClientWithSiteKey:completion:");
if (recaptchaClass && [recaptchaClass respondsToSelector:selector]) {
void (*funcWithoutTimeout)(id, SEL, NSString *,
void (^)(id<RCARecaptchaClientProtocol> _Nullable recaptchaClient,
NSError *_Nullable error)) =
(void *)[recaptchaClass methodForSelector:selector];
funcWithoutTimeout(recaptchaClass, selector, siteKey,
^(id<RCARecaptchaClientProtocol> _Nonnull client, NSError *_Nullable error) {
if (error) {
completionHandler(nil, error);
} else {
completionHandler(client, nil);
}
});
Class<RCARecaptchaProtocol> _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<RCARecaptchaProtocol>)klass;
}

id<RCAActionProtocol> _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<RCAActionProtocol> customAction =
funcWithCustomAction([klass alloc], customActionSelector, actionString);
return customAction;
Class<RCAActionProtocol> _Nonnull __fir_castToRecaptchaActionProtocolFromClass(
Class _Nonnull klass) {
if ([klass conformsToProtocol:@protocol(RCAActionProtocol)]) {
NSLog(@"RCAActionProtocol - true");
} else {
return nil;
NSLog(@"RCAActionProtocol - false");
}
return (Class<RCAActionProtocol>)klass;
}

#endif
12 changes: 4 additions & 8 deletions FirebaseAuth/Sources/Public/FirebaseAuth/FIRRecaptchaBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<RCARecaptchaClientProtocol> _Nullable result,
NSError *_Nullable error));
Class<RCARecaptchaProtocol> _Nonnull __fir_castToRecaptchaProtocolFromClass(Class _Nonnull klass);

id<RCAActionProtocol> _Nullable __fir_initActionFromClass(Class _Nonnull klass,
NSString *_Nonnull actionString);
Class<RCAActionProtocol> _Nonnull __fir_castToRecaptchaActionProtocolFromClass(
Class _Nonnull klass);

#endif
20 changes: 5 additions & 15 deletions FirebaseAuth/Sources/Swift/Utilities/AuthRecaptchaVerifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit ae603e5

Please sign in to comment.