|
18 | 18 | #import "FirebaseAuth/Sources/Public/FirebaseAuth/FIRRecaptchaBridge.h" |
19 | 19 | #import "RecaptchaInterop/RecaptchaInterop.h" |
20 | 20 |
|
21 | | -// This is thread safe since it is only called by the AuthRecaptchaVerifier singleton. |
22 | | -static id<RCARecaptchaClientProtocol> recaptchaClient; |
23 | | - |
24 | | -static void retrieveToken(NSString *actionString, |
25 | | - NSString *fakeToken, |
26 | | - FIRAuthRecaptchaTokenCallback callback) { |
27 | | - Class RecaptchaActionClass = NSClassFromString(@"RecaptchaEnterprise.RCAAction"); |
28 | | - SEL customActionSelector = NSSelectorFromString(@"initWithCustomAction:"); |
29 | | - if (!RecaptchaActionClass) { |
30 | | - // Fall back to attempting to connect with pre-18.7.0 RecaptchaEnterprise. |
31 | | - RecaptchaActionClass = NSClassFromString(@"RecaptchaAction"); |
32 | | - } |
33 | | - |
34 | | - if (RecaptchaActionClass && |
35 | | - [RecaptchaActionClass instancesRespondToSelector:customActionSelector]) { |
36 | | - // Initialize with a custom action |
37 | | - id (*funcWithCustomAction)(id, SEL, NSString *) = (id(*)( |
38 | | - id, SEL, NSString *))[RecaptchaActionClass instanceMethodForSelector:customActionSelector]; |
39 | | - |
40 | | - id<RCAActionProtocol> customAction = |
41 | | - funcWithCustomAction([RecaptchaActionClass alloc], customActionSelector, actionString); |
42 | | - if (customAction) { |
43 | | - [recaptchaClient execute:customAction |
44 | | - completion:^(NSString *_Nullable token, NSError *_Nullable error) { |
45 | | - if (!error) { |
46 | | - callback(token, nil, YES, YES); |
47 | | - return; |
48 | | - } else { |
49 | | - callback(fakeToken, nil, YES, YES); |
50 | | - } |
51 | | - }]; |
52 | | - } else { |
53 | | - // RecaptchaAction class creation failed. |
54 | | - callback(@"", nil, YES, NO); |
55 | | - } |
56 | | - |
| 21 | +Class<RCARecaptchaProtocol> _Nonnull __fir_castToRecaptchaProtocolFromClass(Class _Nonnull klass) { |
| 22 | + if ([klass conformsToProtocol:@protocol(RCARecaptchaProtocol)]) { |
| 23 | + NSLog(@"RCARecaptchaProtocol - true"); |
57 | 24 | } else { |
58 | | - // RecaptchaEnterprise not linked. |
59 | | - callback(@"", nil, NO, NO); |
| 25 | + NSLog(@"RCARecaptchaProtocol - false"); |
60 | 26 | } |
| 27 | + return (Class<RCARecaptchaProtocol>)klass; |
61 | 28 | } |
62 | 29 |
|
63 | | -void FIRRecaptchaGetToken(NSString *siteKey, |
64 | | - NSString *actionString, |
65 | | - NSString *fakeToken, |
66 | | - FIRAuthRecaptchaTokenCallback callback) { |
67 | | - if (recaptchaClient != nil) { |
68 | | - retrieveToken(actionString, fakeToken, callback); |
69 | | - return; |
70 | | - } |
71 | | - |
72 | | - // Why not use `conformsToProtocol`? |
73 | | - Class RecaptchaClass = NSClassFromString(@"RecaptchaEnterprise.RCARecaptcha"); |
74 | | - SEL selector = NSSelectorFromString(@"fetchClientWithSiteKey:completion:"); |
75 | | - if (!RecaptchaClass) { |
76 | | - // Fall back to attempting to connect with pre-18.7.0 RecaptchaEnterprise. |
77 | | - RecaptchaClass = NSClassFromString(@"Recaptcha"); |
78 | | - selector = NSSelectorFromString(@"getClientWithSiteKey:completion:"); |
79 | | - } |
80 | | - |
81 | | - if (RecaptchaClass && [RecaptchaClass respondsToSelector:selector]) { |
82 | | - void (*funcWithoutTimeout)(id, SEL, NSString *, |
83 | | - void (^)(id<RCARecaptchaClientProtocol> _Nullable recaptchaClient, |
84 | | - NSError *_Nullable error)) = |
85 | | - (void *)[RecaptchaClass methodForSelector:selector]; |
86 | | - funcWithoutTimeout(RecaptchaClass, selector, siteKey, |
87 | | - ^(id<RCARecaptchaClientProtocol> _Nonnull client, NSError *_Nullable error) { |
88 | | - if (error) { |
89 | | - callback(@"", error, YES, YES); |
90 | | - } else { |
91 | | - recaptchaClient = client; |
92 | | - retrieveToken(actionString, fakeToken, callback); |
93 | | - } |
94 | | - }); |
| 30 | +Class<RCAActionProtocol> _Nonnull __fir_castToRecaptchaActionProtocolFromClass( |
| 31 | + Class _Nonnull klass) { |
| 32 | + if ([klass conformsToProtocol:@protocol(RCAActionProtocol)]) { |
| 33 | + NSLog(@"RCAActionProtocol - true"); |
95 | 34 | } else { |
96 | | - // RecaptchaEnterprise not linked. |
97 | | - callback(@"", nil, NO, NO); |
| 35 | + NSLog(@"RCAActionProtocol - false"); |
98 | 36 | } |
| 37 | + return (Class<RCAActionProtocol>)klass; |
99 | 38 | } |
| 39 | + |
100 | 40 | #endif |
0 commit comments