safetynet_attestation 1.0.1 copy "safetynet_attestation: ^1.0.1" to clipboard
safetynet_attestation: ^1.0.1 copied to clipboard

PlatformAndroid

SafetyNet Attestation API for Flutter. The SafetyNet Attestation API is an anti-abuse API that allows app developers to assess the Android device their app is running on.

example/lib/main.dart

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:safetynet_attestation/models/jws_payload_model.dart';
import 'package:safetynet_attestation/safetynet_attestation.dart';
import 'package:safetynet_attestation/safetynet_attestation_platform_interface.dart';

void main() => runApp(const MyApp());

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  GooglePlayServicesAvailability? _gmsStatus;

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  Future<void> initPlatformState() async {
    GooglePlayServicesAvailability? gmsAvailability;
    try {
      gmsAvailability = await SafetynetAttestation().googlePlayServicesAvailability();
    } on PlatformException {
      gmsAvailability = null;
    }

    if (!mounted) return;

    setState(() {
      _gmsStatus = gmsAvailability;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('SafetyNet Attestation plugin example app'),
        ),
        body: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          mainAxisSize: MainAxisSize.max,
          children: <Widget>[
            Text(
              'Google Play Services status: ${_gmsStatus ?? 'unknown'}',
              textAlign: TextAlign.center,
            ),
            Offstage(
              offstage: _gmsStatus != GooglePlayServicesAvailability.success,
              child: const Padding(
                padding: EdgeInsets.only(top: 50.0),
                child: SafetyNetAttestationWidget(),
              ),
            )
          ],
        ),
      ),
    );
  }
}

class SafetyNetAttestationWidget extends StatefulWidget {
  const SafetyNetAttestationWidget({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() => _SafetyNetAttestationWidgetState();
}

class _SafetyNetAttestationWidgetState extends State<SafetyNetAttestationWidget> {
  bool isLoading = false;

  @override
  Widget build(BuildContext context) {
    if (isLoading) {
      return const CircularProgressIndicator();
    } else {
      return MaterialButton(
        onPressed: () {
          requestSafetyNetAttestation();
          setState(() {
            isLoading = true;
          });
        },
        color: Colors.blueAccent,
        child: const Text('Request SafetyNet Attestation'),
      );
    }
  }

  void requestSafetyNetAttestation() async {
    String dialogTitle, dialogMessage;
    try {
      JWSPayloadModel res =
          await SafetynetAttestation().playIntegrityApiManualPayload(projectNumber: 1);

      // JWSPayloadModel res = await SafetynetAttestation()
      //     .playIntegrityApiPayload(projectNumber: 1, token: "adasdasdasdas");

      dialogTitle = 'SafetyNet Attestation Payload';
      dialogMessage = res.accountDetails.appLicensingVerdict;
    } catch (e) {
      dialogTitle = 'ERROR - SafetyNet Attestation Payload';

      if (e is PlatformException) {
        dialogMessage = e.message ?? "Error";
      } else {
        dialogMessage = e.toString();
      }
    }

    showDialog(
        context: context,
        builder: (context) {
          return AlertDialog(
            title: Text(dialogTitle),
            content: Text(dialogMessage),
            actions: <Widget>[
              MaterialButton(
                  onPressed: () {
                    Navigator.pop(context);
                  },
                  child: const Text('OK'))
            ],
          );
        });

    setState(() {
      isLoading = false;
    });
  }
}
18
likes
130
pub points
81%
popularity

Publisher

verified publishercodesfirst.com

SafetyNet Attestation API for Flutter. The SafetyNet Attestation API is an anti-abuse API that allows app developers to assess the Android device their app is running on.

Repository (GitHub)
View/report issues

Documentation

API reference

License

Apache-2.0 (LICENSE)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on safetynet_attestation