Skip to content

feat(auth): add support for initializeRecaptchaConfig #17365

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,25 @@ public void revokeTokenWithAuthorizationCode(
result.success();
}

@Override
public void initializeRecaptchaConfig(
@NonNull GeneratedAndroidFirebaseAuth.AuthPigeonFirebaseApp app,
@NonNull GeneratedAndroidFirebaseAuth.VoidResult result) {
FirebaseAuth firebaseAuth = getAuthFromPigeon(app);
firebaseAuth
.initializeRecaptchaConfig()
.addOnCompleteListener(
task -> {
if (task.isSuccessful()) {
result.success();
} else {
result.error(
FlutterFirebaseAuthPluginException.parserExceptionToFlutter(
task.getException()));
}
});
}

@Override
public Task<Map<String, Object>> getPluginConstantsForFirebaseApp(FirebaseApp firebaseApp) {
TaskCompletionSource<Map<String, Object>> taskCompletionSource = new TaskCompletionSource<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2642,6 +2642,8 @@ void revokeTokenWithAuthorizationCode(
@NonNull String authorizationCode,
@NonNull VoidResult result);

void initializeRecaptchaConfig(@NonNull AuthPigeonFirebaseApp app, @NonNull VoidResult result);

/** The codec used by FirebaseAuthHostApi. */
static @NonNull MessageCodec<Object> getCodec() {
return FirebaseAuthHostApiCodec.INSTANCE;
Expand Down Expand Up @@ -3395,6 +3397,38 @@ public void error(Throwable error) {
channel.setMessageHandler(null);
}
}
{
BasicMessageChannel<Object> channel =
new BasicMessageChannel<>(
binaryMessenger,
"dev.flutter.pigeon.firebase_auth_platform_interface.FirebaseAuthHostApi.initializeRecaptchaConfig"
+ messageChannelSuffix,
getCodec());
if (api != null) {
channel.setMessageHandler(
(message, reply) -> {
ArrayList<Object> wrapped = new ArrayList<Object>();
ArrayList<Object> args = (ArrayList<Object>) message;
AuthPigeonFirebaseApp appArg = (AuthPigeonFirebaseApp) args.get(0);
VoidResult resultCallback =
new VoidResult() {
public void success() {
wrapped.add(0, null);
reply.reply(wrapped);
}

public void error(Throwable error) {
ArrayList<Object> wrappedError = wrapError(error);
reply.reply(wrappedError);
}
};

api.initializeRecaptchaConfig(appArg, resultCallback);
});
} else {
channel.setMessageHandler(null);
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2172,4 +2172,22 @@ - (void)verifyBeforeUpdateEmailApp:(nonnull AuthPigeonFirebaseApp *)app
}];
}

- (void)initializeRecaptchaConfigApp:(AuthPigeonFirebaseApp *)app
completion:(void (^)(FlutterError *_Nullable))completion {
#if TARGET_OS_OSX
NSLog(@"initializeRecaptchaConfigWithCompletion is not supported on the "
@"MacOS platform.");
completion(nil);
#else
FIRAuth *auth = [self getFIRAuthFromAppNameFromPigeon:app];
[auth initializeRecaptchaConfigWithCompletion:^(NSError *_Nullable error) {
if (error != nil) {
completion([FLTFirebaseAuthPlugin convertToFlutterError:error]);
} else {
completion(nil);
}
}];
#endif
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -1553,6 +1553,32 @@ void SetUpFirebaseAuthHostApiWithSuffix(id<FlutterBinaryMessenger> binaryMesseng
[channel setMessageHandler:nil];
}
}
{
FlutterBasicMessageChannel *channel = [[FlutterBasicMessageChannel alloc]
initWithName:[NSString
stringWithFormat:@"%@%@",
@"dev.flutter.pigeon.firebase_auth_platform_interface."
@"FirebaseAuthHostApi.initializeRecaptchaConfig",
messageChannelSuffix]
binaryMessenger:binaryMessenger
codec:FirebaseAuthHostApiGetCodec()];
if (api) {
NSCAssert([api respondsToSelector:@selector(initializeRecaptchaConfigApp:completion:)],
@"FirebaseAuthHostApi api (%@) doesn't respond to "
@"@selector(initializeRecaptchaConfigApp:completion:)",
api);
[channel setMessageHandler:^(id _Nullable message, FlutterReply callback) {
NSArray *args = message;
AuthPigeonFirebaseApp *arg_app = GetNullableObjectAtIndex(args, 0);
[api initializeRecaptchaConfigApp:arg_app
completion:^(FlutterError *_Nullable error) {
callback(wrapResult(nil, error));
}];
}];
} else {
[channel setMessageHandler:nil];
}
}
}
@interface FirebaseAuthUserHostApiCodecReader : FlutterStandardReader
@end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,8 @@ NSObject<FlutterMessageCodec> *FirebaseAuthHostApiGetCodec(void);
- (void)revokeTokenWithAuthorizationCodeApp:(AuthPigeonFirebaseApp *)app
authorizationCode:(NSString *)authorizationCode
completion:(void (^)(FlutterError *_Nullable))completion;
- (void)initializeRecaptchaConfigApp:(AuthPigeonFirebaseApp *)app
completion:(void (^)(FlutterError *_Nullable))completion;
@end

extern void SetUpFirebaseAuthHostApi(id<FlutterBinaryMessenger> binaryMessenger,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,12 @@ class FirebaseAuth extends FirebasePluginPlatform {
return _delegate.revokeTokenWithAuthorizationCode(authorizationCode);
}

/// Initializes the reCAPTCHA Enterprise client proactively to enhance reCAPTCHA signal collection and
/// to complete reCAPTCHA-protected flows in a single attempt.
Future<void> initializeRecaptchaConfig() {
return _delegate.initializeRecaptchaConfig();
}

@override
String toString() {
return 'FirebaseAuth(app: ${app.name})';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,15 @@ class MethodChannelFirebaseAuth extends FirebaseAuthPlatform {
);
}
}

@override
Future<void> initializeRecaptchaConfig() async {
try {
await _api.initializeRecaptchaConfig(pigeonDefault);
} catch (e, stack) {
convertPlatformException(e, stack);
}
}
}

/// Simple helper class to make nullable values transferable through StreamControllers.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1478,6 +1478,30 @@ class FirebaseAuthHostApi {
return;
}
}

Future<void> initializeRecaptchaConfig(AuthPigeonFirebaseApp app) async {
final String __pigeon_channelName =
'dev.flutter.pigeon.firebase_auth_platform_interface.FirebaseAuthHostApi.initializeRecaptchaConfig$__pigeon_messageChannelSuffix';
final BasicMessageChannel<Object?> __pigeon_channel =
BasicMessageChannel<Object?>(
__pigeon_channelName,
pigeonChannelCodec,
binaryMessenger: __pigeon_binaryMessenger,
);
final List<Object?>? __pigeon_replyList =
await __pigeon_channel.send(<Object?>[app]) as List<Object?>?;
if (__pigeon_replyList == null) {
throw _createConnectionError(__pigeon_channelName);
} else if (__pigeon_replyList.length > 1) {
throw PlatformException(
code: __pigeon_replyList[0]! as String,
message: __pigeon_replyList[1] as String?,
details: __pigeon_replyList[2],
);
} else {
return;
}
}
}

class _FirebaseAuthUserHostApiCodec extends StandardMessageCodec {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -706,4 +706,10 @@ abstract class FirebaseAuthPlatform extends PlatformInterface {
throw UnimplementedError(
'revokeTokenWithAuthorizationCode() is not implemented');
}

/// Initializes the reCAPTCHA Enterprise client proactively to enhance reCAPTCHA signal collection and
/// to complete reCAPTCHA-protected flows in a single attempt.
Future<void> initializeRecaptchaConfig() {
throw UnimplementedError('initializeRecaptchaConfig() is not implemented');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,11 @@ abstract class FirebaseAuthHostApi {
AuthPigeonFirebaseApp app,
String authorizationCode,
);

@async
void initializeRecaptchaConfig(
AuthPigeonFirebaseApp app,
);
}

class PigeonIdTokenResult {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ abstract class TestFirebaseAuthHostApi {
Future<void> revokeTokenWithAuthorizationCode(
AuthPigeonFirebaseApp app, String authorizationCode);

Future<void> initializeRecaptchaConfig(AuthPigeonFirebaseApp app);

static void setUp(
TestFirebaseAuthHostApi? api, {
BinaryMessenger? binaryMessenger,
Expand Down Expand Up @@ -993,6 +995,38 @@ abstract class TestFirebaseAuthHostApi {
});
}
}
{
final BasicMessageChannel<Object?> __pigeon_channel = BasicMessageChannel<
Object?>(
'dev.flutter.pigeon.firebase_auth_platform_interface.FirebaseAuthHostApi.initializeRecaptchaConfig$messageChannelSuffix',
pigeonChannelCodec,
binaryMessenger: binaryMessenger);
if (api == null) {
_testBinaryMessengerBinding!.defaultBinaryMessenger
.setMockDecodedMessageHandler<Object?>(__pigeon_channel, null);
} else {
_testBinaryMessengerBinding!.defaultBinaryMessenger
.setMockDecodedMessageHandler<Object?>(__pigeon_channel,
(Object? message) async {
assert(message != null,
'Argument for dev.flutter.pigeon.firebase_auth_platform_interface.FirebaseAuthHostApi.initializeRecaptchaConfig was null.');
final List<Object?> args = (message as List<Object?>?)!;
final AuthPigeonFirebaseApp? arg_app =
(args[0] as AuthPigeonFirebaseApp?);
assert(arg_app != null,
'Argument for dev.flutter.pigeon.firebase_auth_platform_interface.FirebaseAuthHostApi.initializeRecaptchaConfig was null, expected non-null AuthPigeonFirebaseApp.');
try {
await api.initializeRecaptchaConfig(arg_app!);
return wrapResponse(empty: true);
} on PlatformException catch (e) {
return wrapResponse(error: e);
} catch (e) {
return wrapResponse(
error: PlatformException(code: 'error', message: e.toString()));
}
});
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,13 @@ class FirebaseAuthWeb extends FirebaseAuthPlatform {
'revokeTokenWithAuthorizationCode() is only available on apple platforms.',
);
}

@override
Future<void> initializeRecaptchaConfig() async {
await guardAuthExceptions(
() => delegate.initializeRecaptchaConfig(),
);
}
}

String getOriginName(String appName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,11 @@ class Auth extends JsObjectWrapper<auth_interop.AuthJsImpl> {
.verifyPasswordResetCode(jsObject, code.toJS)
.toDart
.then((value) => (value! as JSString).toDart);

/// Initializes the reCAPTCHA Enterprise client proactively to enhance reCAPTCHA signal collection and
/// to complete reCAPTCHA-protected flows in a single attempt.
Future initializeRecaptchaConfig() =>
auth_interop.initializeRecaptchaConfig(jsObject).toDart;
}

/// Represents an auth provider.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -984,3 +984,6 @@ class PhoneAuthCredentialJsImpl extends AuthCredential {
extension PhoneAuthCredentialJsImplExtension on PhoneAuthCredentialJsImpl {
external JSObject toJSON();
}

@JS()
external JSPromise initializeRecaptchaConfig(AuthJsImpl auth);
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,23 @@ void main() {
},
skip: true,
);

group(
'initializeRecaptchaConfig',
() {
test('initializeRecaptchaConfig completes without throwing',
() async {
// Skipping this test as initializeRecaptchaConfig is not supported
// by the Firebase emulator suite.
try {
await FirebaseAuth.instance.initializeRecaptchaConfig();
} catch (e) {
fail('Should not have thrown: $e');
}
});
},
skip: true,
);
},
// macOS skipped because it needs keychain sharing entitlement. See: https://github.com/firebase/flutterfire/issues/9538
skip: defaultTargetPlatform == TargetPlatform.macOS,
Expand Down
Loading