Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing API credentials in a flutter application

I am using googleapis services in my flutter app which requires some credentials in JSON format. What is the best way to store this credentials in my App?

Can I keep a JSON file in my asset folder and read it in my main function?

Or should I hardcode the credentials in my main function? I'm new to flutter development.

My code looks like the following

import 'package:googleapis/storage/v1.dart';
import 'package:googleapis_auth/auth_io.dart';

final _credentials = new ServiceAccountCredentials.fromJson(r'''
{
  "private_key_id": ...,
  "private_key": ...,
  "client_email": ...,
  "client_id": ...,
  "type": "service_account"
}
''');

const _SCOPES = const [StorageApi.DevstorageReadOnlyScope];

void main() {
  clientViaServiceAccount(_credentials, _SCOPES).then((http_client) {
    var storage = new StorageApi(http_client);
    storage.buckets.list('dart-on-cloud').then((buckets) {
      print("Received ${buckets.items.length} bucket names:");
      for (var file in buckets.items) {
        print(file.name);
      }
    });
  });
}

Where I should keep the following credentials:

{
  "private_key_id": ...,
  "private_key": ...,
  "client_email": ...,
  "client_id": ...,
  "type": "service_account"
}

I don't think hardcoding like above is a good idea.

I think this should work:https://medium.com/@sokrato/storing-your-secret-keys-in-flutter-c0b9af1c0f69

Thanks.

like image 973
Unnikrishnan Avatar asked Apr 13 '19 07:04

Unnikrishnan


People also ask

How do you store credentials in flutter?

For storing sensitive information like credentials you should use the Keychain unter iOS and the Keystore under Android. There is a perfect library for that called flutter_secure_storage . Here's how to use that: // Create storage final storage = new FlutterSecureStorage(); // Store password await storage.


1 Answers

For storing sensitive information like credentials you should use the Keychain unter iOS and the Keystore under Android.

There is a perfect library for that called flutter_secure_storage.

Here's how to use that:

// Create storage
final storage = new FlutterSecureStorage();

// Store password 
await storage.write(key: "password", value: "my-secret-password");

// Read value 
String myPassword = await storage.read(key: "password");

To use it add flutter_secure_storage: 3.2.1+1 to you pubspec.yaml and run flutter packages get in a terminal.

Here is the package and a more detailled example on how to use it: https://pub.dartlang.org/packages/flutter_secure_storage

like image 195
Robin Reiter Avatar answered Sep 17 '22 14:09

Robin Reiter