keychain

A simple class that can be extended to create a type safe, cached store for passwords, api keys, etc.

The first time an extended Keychain class is initiated, the keys are obtained based on how they are created and then cached for future instances of the extended Keychain class.

This package uses dart:io and is intended for command line / server applications although there is a KeychainLight class that currently only supports String keys (see below).

shell (set an environment variable)

ENV_VAR=anEnvVar

/home/path/to/project/secrets/keys.json

{
  "object": {
    "key": "aJsonVar"
  }
}

project/lib/secrets/keys.json

{
  "object": {
    "anotherKey": "anotherJsonVar"
  }
}

/home/path/to/project/lib/bin/main.dart

import "package:keychain/keychain.dart"

class Keys extends Keychain {

  final String str = Keychain.fromString('aString');
  final String env = Keychain.fromEnv('ENV_VAR');
  final String json = Keychain.fromJsonFile('key', 'project/secrets/keys.json');
  final Future<String> resource = Keychain.fromJsonResource('anotherKey', 'package:this_package/secrets/keys.json');

}

void main() {
  var keys = Keys();
  print(keys.str); // aString
  print(keys.env); // anEnvVar
  print(keys.json); // aJsonVar
  print(await keys.resource); // anotherJsonVar
}

The path for a json file must start with a directory on the path to the running script. For example, if the script is at /home/path/to/project/lib/bin/main.dart, you should put the json file somewhere along this path such as /home/path/to/project/secrets/keys.json (there needs to be one overlapping directory). This file can then be included in the extended class as in the example above ('project/secrets/keys.json' -> project is the overlapping directory).

KeychainLight does not import dart:io but therefore only supports String keys currently. To use KeychainLight:

import 'package:keychain/keychain_light.dart'

class Keys extends KeychainLight {
  final String str = KeychainLight.fromString('aString');
}

void main() {
  var keys = Keys();
  print(keys.str); // aString
}

Libraries

keychain
keychain_light