f_grecaptcha 1.0.0

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

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

1. Depend on it

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


dependencies:
  f_grecaptcha: "^1.0.0"

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

Alternatively, your editor might support pub get or 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.0 Feb 12, 2018 Go to the documentation of f_grecaptcha 1.0.0 Download f_grecaptcha 1.0.0 archive

Analysis

This feature is new.
We welcome feedback.
More details: scoring.

We analyzed this package, and provided a score, details, and suggestions below.

  • tool failures on Feb 12, 2018
  • Dart: 2.0.0-dev.20.0
  • pana: 0.10.1
  • Flutter: 0.0.22

Scores

Popularity:
Describes how popular the package is relative to other packages. [more]
0 / 100
Health:
Code health derived from static analysis. [more]
96 / 100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
76 / 100
Overall score:
Weighted score of the above. [more]
44

Platforms

Detected platforms: unsure

Error(s) prevent platform classification.

Suggestions

  • Fix dependencies in pubspec.yaml.

    Running flutter packages pub upgrade failed with the following output:

    ERR: Invalid argument(s): Minimum version ("2.0.0-dev.23.0") must be less than maximum ("2.0.0-dev.16.0").
    ERR: package:pub_semver/src/version_range.dart 59         new VersionRange
     package:pub/src/pubspec.dart 366                     Pubspec._ensureEnvironment
     package:pub/src/pubspec.dart 331                     Pubspec.dartSdkConstraint
     package:pub/src/solver/backtracking_solver.dart 511  BacktrackingSolver._checkPubspecMatchesSdkConstraint
     package:pub/src/solver/backtracking_solver.dart 474  BacktrackingSolver._checkVersion
     ===== asynchronous gap ===========================
     package:pub/src/solver/backtracking_solver.dart 432  BacktrackingSolver._findValidVersion.<fn>
     ===== asynchronous gap ===========================
     dart:async                                           Future.doWhile
     package:pub/src/solver/backtracking_solver.dart 430  BacktrackingSolver._findValidVersion
     package:pub/src/solver/backtracking_solver.dart 337  BacktrackingSolver._versionQueueFor
     ===== asynchronous gap ===========================
     package:pub/src/solver/backtracking_solver.dart 290  BacktrackingSolver._solve.<fn>
     ===== asynchronous gap ===========================
     dart:async                                           _completeOnAsyncReturn
     package:pub/src/solver/backtracking_solver.dart 315  BacktrackingSolver._solve.<fn>
     dart:async                                           _completeOnAsyncReturn
     package:pub/src/solver/version_selection.dart 64     VersionSelection.select
     dart:async                                           _completeOnAsyncReturn
     package:pub/src/solver/version_selection.dart 91     VersionSelection._addDependencies
     dart:async                                           _completeOnAsyncReturn
     package:pub/src/solver/version_selection.dart 91     VersionSelection._addDependencies
    ERR: This is an unexpected error. Please run
     
         pub --trace upgrade --verbosity io --no-precompile
     
     and include the logs in an issue on https://github.com/dart-lang/pub/issues/new
    
  • Fix platform conflicts.

    Make sure none of the libraries use mutually exclusive dependendencies.

  • Fix issues reported by dartanalyzer.

    dartanalyzer reported 1 error(s) and 0 warning(s).