This simple flutter plugin allows using the SafetyNet API on Android devices to verify that the user is human.
First, include the plugin in your project's dependencies by including it in
the relevant section of your pubspec.yaml
:
dependencies:
f_grecaptcha: ^1.0.0
Next, you wil need to register your app in the reCAPTCHA admin console. Go
to https://www.google.com/recaptcha/admin#list and register by filling out
the form. Be sure to select "reCAPTCHA-Android" as the type. A form field
asking for your Android package name will appear. You can copy it from the
manifest file located under android/app/src/main/AndroidManifest.xml
in
your project directory. It is the value of the package
attribute of the
root XML-tag. You can also visit the instructions
page from the android documentation for a more detailed guide, only the
section "Adding a SafetyNet API dependency" is relevant, the plugin will do
the rest.
After having your app registered with the reCAPTCHA API, you can invoke the
following method anywhere in your dart code, most commonly after a button
has been pressed. Replace SITE_KEY
with the site key the admin interface
shows after registering your app.
FGrecaptcha.verifyWithRecaptcha(SITE_KEY).then((result) {
// You can send the result token, along with some form fields, to your
// server, which can verify the token using an endpoint proved by the
// reCAPTCHA API for servers, see https://developers.google.com/recaptcha/docs/verify
}, onError: (e, s) {
// An error doesn't have to mean that the user is not a human. Errors
// can also occur when the sitekey is invalid or does not match your
// application, when the device is not supported or when a network
// error occurs.
// You should inform the user of errors, explaining why they can't
// proceed. As the plugin is not available for iOS, you might consider
// skipping the reCAPTCHA step when FGrecaptcha.isAvailable is false.
print("Could not verify:\n $e at $s");
}
);
Simply checking that FGrecaptcha.verifyWithRecaptcha
returned a value is
not enough to be sure that the user is a human. Instead, you would have to
verify the token returned in your applications backend server. You can
accomplish that by following the instructions at https://developers.google.com/recaptcha/docs/verify.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:f_grecaptcha/f_grecaptcha.dart';
void main() => runApp(new MyApp());
// Outside of this example app, you need to provide your own site key. You can
// generate them on https://www.google.com/recaptcha/admin#list by selecting
// reCAPTCHA Android. The readme of this plugin contains a more detailed
// explanation.
const String SITE_KEY = "6LdE10UUAAAAAD5Mw7XeDU2VUgMchgAI_qk3sos8";
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
enum _VerificationStep {
SHOWING_BUTTON, WORKING, ERROR, VERIFIED
}
class _MyAppState extends State<MyApp> {
// Start by showing the button inviting the user to use the example
_VerificationStep _step = _VerificationStep.SHOWING_BUTTON;
void _startVerification() {
setState(() => _step = _VerificationStep.WORKING);
FGrecaptcha.verifyWithRecaptcha(SITE_KEY).then((result) {
/* When using reCaptcha in a production app, you would now send the $result
to your backend server, so that it can verify it as well. In most
cases, an ideal way to do this is sending it together with some form
fields, for instance when creating a new account. Your backend server
would then take the result field and make a request to the reCaptcha
API to verify that the user of the device where the registration
request is from is a human. It could then continue processing the
request and complete the registration. */
setState(() => _step = _VerificationStep.VERIFIED);
}, onError: (e, s) {
print("Could not verify:\n$e at $s");
setState(() => _step = _VerificationStep.ERROR);
});
}
@override
Widget build(BuildContext context) {
Widget content;
switch (_step) {
case _VerificationStep.SHOWING_BUTTON:
content = new Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: [
new Text("This example will use the reCaptcha API to verify that you're human"),
new RaisedButton(
onPressed: _startVerification,
child: const Text("VERIFY"),
)
]
);
break;
case _VerificationStep.WORKING:
content = new Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: [
new CircularProgressIndicator(),
new Text("Trying to figure out whether you're human"),
]
);
break;
case _VerificationStep.VERIFIED:
content = new Text(
"The reCaptcha API returned a token, indicating that you're a human. "
"In real world use case, you would send use the token returned to "
"your backend-server so that it can verify it as well."
);
break;
case _VerificationStep.ERROR:
content = new Text(
"We could not verify that you're a human :( This can occur if you "
"have no internet connection (or if you really are a a bot)."
);
}
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: new Text('reCaptcha example'),
),
body: new Center(
child: content,
),
),
);
}
}
Add this to your package's pubspec.yaml file:
dependencies:
f_grecaptcha: ^1.0.1
You can install packages from the command line:
with Flutter:
$ flutter packages get
Alternatively, your editor might support flutter packages get
.
Check the docs for your editor to learn more.
Now in your Dart code, you can use:
import 'package:f_grecaptcha/f_grecaptcha.dart';
Version | Uploaded | Documentation | Archive |
---|---|---|---|
1.0.1 | Sep 15, 2018 |
|
|
1.0.0 | Feb 12, 2018 |
|
|
Popularity:
Describes how popular the package is relative to other packages.
[more]
|
52
|
Health:
Code health derived from static analysis.
[more]
|
100
|
Maintenance:
Reflects how tidy and up-to-date the package is.
[more]
|
100
|
Overall:
Weighted score of the above.
[more]
|
76
|
We analyzed this package on Feb 14, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:
Detected platforms: Flutter
References Flutter, and has no conflicting libraries.
Format lib/f_grecaptcha.dart
.
Run flutter format
to format lib/f_grecaptcha.dart
.
Package | Constraint | Resolved | Available |
---|---|---|---|
Direct dependencies | |||
Dart SDK | >=1.19.0 <3.0.0 | ||
flutter | 0.0.0 | ||
Transitive dependencies | |||
collection | 1.14.11 | ||
meta | 1.1.6 | 1.1.7 | |
sky_engine | 0.0.99 | ||
typed_data | 1.1.6 | ||
vector_math | 2.0.8 |