decode method

List<String> decode(
  1. String value, {
  2. bool debug = false,
})

decode a given otpauth-migration URI into a list of one or more otpauth URIs

Implementation

List<String> decode(String value, { bool debug = false }) {
  // check prefix "otpauth-migration://offline?data="
  // extract suffix - Base64 encode
  List<String> results = [];

  RegExp exp = RegExp(r"otpauth-migration\:\/\/offline\?data=(.*)$");
  final match = exp.firstMatch(value);
  final encoded = match?.group(1);
  if (encoded != null) {
    var decoded = base64.decode(encoded);

    try {
      final gai = GoogleAuthenticatorImport.fromBuffer(decoded);

      if(debug) print(gai);

      //print(gai.otpParameters.length);
      for (var param in gai.otpParameters) {
        //print(param);
        var base32 = _decodeBase32(param.secret);
        //print("otpauth://totp/${param.name}?secret=${base32}");
        final name = Uri.encodeFull(param.name);
        final issuer = Uri.encodeComponent(param.issuer);
        results.add("otpauth://totp/$name?secret=$base32&issuer=$issuer");
        }

      //print("good");
      return results;
    } catch(e) {
      return results;
    }

  } else {
    //print("bad");
    return [];
  }
}