Flutter OTP迁移辅助插件otpauth_migration的使用

Flutter OTP迁移辅助插件otpauth_migration的使用

简介

otpauth_migration 是一个 Dart 包,用于编码和解码 otpauth-migration URI 格式。这种格式用于从 Google Authenticator 应用程序导入和导出双因素认证 (2FA) 密钥。

特性

  • OtpAuthMigration - 主类(无状态)
  • String OtpAuthMigration.encode(List<String> uris) - 将多个 OTP URI 编码为迁移 URI。
  • List<String> OtpAuthMigration.decode(String uri) - 将迁移 URI 解码为多个 OTP URI。

要求

  • Dart 2.16.1

导入

import 'package:otpauth_migration/otpauth_migration.dart';

使用

示例1:解码迁移URI

final otpAuthParser = OtpAuthMigration();
var otp_uris = otpAuthParser.decode("otpauth-migration://offline?data=Cj8KFGnEpnTMQ7KDguNWnddyGyCbSVLaEhhBQ01FIENvOmpvaG5AZXhhbXBsZS5jb20aB0FDTUUgQ28gASgBMAIKRAoUXkj+5MY2arwKjsnH2aDsbm6TAlYSG0JldGEgTHRkLjpob21lckBleGFtcGxlLmNvbRoJQmV0YSBMdGQuIAEoATACCkgKFDDFyzUNPgYoI3q/KGHBdcNU9ptWEh1DYXRzICYgRG9nczptYXJnZUBleGFtcGxlLmNvbRoLQ2F0cyAmIERvZ3MgASgBMAIKSAoUunHzbm5h/LUO0yilLMI+dYZY1eISHURhaWx5IEJ1Z2xlOnBldGVyQGV4YW1wbGUuY29tGgtEYWlseSBCdWdsZSABKAEwAhABGAEgACjDnb+uAg==");
// otp_uris = [
//   "otpauth://totp/ACME Co:john.doe@example.com?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&issuer=ACME Co",
//   "otpauth://totp/Beta Ltd:john@exaple.com?secret=JBSWY3DPEHPK3PXP&issuer=Beta Ltd"
// ]
print(otp_uris);

示例2:编码为迁移URI

final otpAuthParser = OtpAuthMigration();
var otp_migration = otpAuthParser.encode([
    "otpauth://totp/ACME%20Co:john.doe@example.com?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&issuer=ACME+Co",
    "otpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&issuer=Example"
]);
// otp_migration =
// "otpauth-migration://offline?data=CkMKFD3GyqSCSm0oh2eyMx4gtDFmy4XZEhxBQ01FIENvOmpvaG4uZG9lQGV4YW1wbGUuY29tGgdBQ01FIENvIAEoATACCjUKCkhlbGxvId6tvu8SGEV4YW1wbGU6YWxpY2VAZ29vZ2xlLmNvbRoHRXhhbXBsZSABKAEwAhABGAEgACjn4Pv4Ag=="
print(otp_migration);

测试

% dart test

示例代码

可以在 example 文件夹中找到更多的示例代码。

% cd example
% dart run otpauth_migration_example.dart

额外信息

  • 需要对输入进行更多的错误检查(并添加相关的测试)
  • 修复版本、batchSize、batchIndex、batchId
  • 添加 Github Actions 徽章(以显示 Github Action 测试结果)
  • RFC 3548 描述(以及图示)

构建

% protoc -I=./proto --dart_out=lib/generated proto/GoogleAuthenticatorImport.proto

API 文档

% dart doc .

格式化和分析

% dart analyze
% dart format .

发布

% dart pub publish

更多关于Flutter OTP迁移辅助插件otpauth_migration的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter OTP迁移辅助插件otpauth_migration的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


在Flutter项目中,如果你需要将OTP(一次性密码)从现有的身份验证应用迁移到新的应用中,otpauth_migration 插件可以是一个非常有用的工具。这个插件可以帮助你生成一个迁移URI,用户可以通过扫描这个URI来将他们的OTP从一个应用迁移到另一个应用。

以下是如何在Flutter项目中使用 otpauth_migration 插件的一个简单示例。

1. 添加依赖

首先,你需要在你的 pubspec.yaml 文件中添加 otpauth_migration 依赖:

dependencies:
  flutter:
    sdk: flutter
  otpauth_migration: ^最新版本号  # 请替换为实际发布的最新版本号

然后运行 flutter pub get 来获取依赖。

2. 导入包

在你的 Dart 文件中导入 otpauth_migration 包:

import 'package:otpauth_migration/otpauth_migration.dart';

3. 生成迁移URI

假设你有一个现有的OTP URI,你想迁移它。以下是如何生成迁移URI的代码示例:

void main() {
  // 现有的OTP URI
  String existingOtpUri = "otpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&issuer=Google";

  // 新的应用名称(用户将迁移到这个应用)
  String newAppName = "MyNewAuthApp";

  // 生成迁移URI
  try {
    String migrationUri = OtpAuthMigration.generateMigrationUri(
      existingOtpUri: existingOtpUri,
      newAppName: newAppName,
    );

    print("Migration URI: $migrationUri");
    // 你可以显示这个URI给用户,让他们扫描以完成迁移
  } catch (e) {
    print("Error generating migration URI: $e");
  }
}

在这个例子中,OtpAuthMigration.generateMigrationUri 方法接受两个参数:existingOtpUri(现有的OTP URI)和 newAppName(新的应用名称)。这个方法会返回一个迁移URI,你可以显示给用户,让他们使用他们的身份验证应用扫描这个URI以完成迁移过程。

4. 显示迁移URI给用户

你可以使用 Flutter 的 UI 组件(如 TextAlertDialog)来显示这个迁移URI给用户。例如:

import 'package:flutter/material.dart';

void showMigrationUriDialog(BuildContext context, String migrationUri) {
  showDialog(
    context: context,
    builder: (context) => AlertDialog(
      title: Text('Migration URI'),
      content: Text(migrationUri),
      actions: <Widget>[
        TextButton(
          onPressed: () => Navigator.of(context).pop(),
          child: Text('OK'),
        ),
      ],
    ),
  );
}

在你的主函数或某个按钮点击事件中调用这个函数来显示对话框:

void _showMigrationUri(BuildContext context) async {
  String migrationUri = await _generateMigrationUri();
  showMigrationUriDialog(context, migrationUri);
}

Future<String> _generateMigrationUri() async {
  String existingOtpUri = "otpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&issuer=Google";
  String newAppName = "MyNewAuthApp";
  
  try {
    return OtpAuthMigration.generateMigrationUri(
      existingOtpUri: existingOtpUri,
      newAppName: newAppName,
    );
  } catch (e) {
    throw Exception("Error generating migration URI: $e");
  }
}

确保在实际项目中处理异常和边缘情况,比如验证输入的OTP URI是否有效。

通过上述步骤,你就可以在Flutter项目中使用 otpauth_migration 插件来生成OTP迁移URI,并显示给用户以完成迁移过程。

回到顶部