f_grecaptcha 1.0.1

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 76

f_grecaptcha

This simple flutter plugin allows using the SafetyNet API on Android devices to verify that the user is human.

Using the plugin

Preparations

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.

Verifying users

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");
    }
);

Server-side verification

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.

1.0.0.

  • Initial release, reCAPTCHA verification API provided

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,
        ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  f_grecaptcha: ^1.0.1

2. Install it

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.

3. Import it

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 Go to the documentation of f_grecaptcha 1.0.1 Download f_grecaptcha 1.0.1 archive
1.0.0 Feb 12, 2018 Go to the documentation of f_grecaptcha 1.0.0 Download f_grecaptcha 1.0.0 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
51
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
Learn more about scoring.

We analyzed this package on Oct 10, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0
  • pana: 0.12.4
  • Flutter: 0.9.5

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Format lib/f_grecaptcha.dart.

Run flutter format to format lib/f_grecaptcha.dart.

Dependencies

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
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8