Flutter华为FIDO认证插件huawei_fido的使用
Flutter华为FIDO认证插件huawei_fido的使用
简介
HUAWEI FIDO插件为您的应用提供了基于WebAuthn标准的FIDO2认证。它为应用程序和浏览器提供了Android Java API,并允许用户通过漫游验证器(USB、NFC和蓝牙验证器)和平台验证器(指纹和3D面部验证器)完成认证。此外,FIDO还为您的应用提供了强大的本地生物识别认证能力,包括指纹认证和3D面部认证。这使得您的应用能够为用户提供安全且易于使用的无密码认证,同时确保可靠的认证结果。该插件提供了由HUAWEI FIDO SDK提供的HmsBioAuthnManager
、HmsBioAuthnPrompt
、HmsFaceManager
和HmsFido2Client
API。
-
HmsBioAuthnManager
- 检查FIDO BioAuthn指纹认证是否可用的API入口。
-
HmsBioAuthnPrompt
- FIDO BioAuthn指纹认证API的主要入口。
-
HmsFaceManager
- FIDO 3D面部认证API的主要入口。
HmsFaceManager
需要支持3D面部识别的设备。
- FIDO 3D面部认证API的主要入口。
-
HmsFido2Client
- FIDO2 API的入口。允许通过平台验证器或跨平台验证器进行注册和认证。
更多详情请访问官方文档。
安装
请参阅pub.dev 和 AppGallery Connect 配置。
文档
其他主题
- 了解更多关于WebAuthn的信息,请参阅这里。
问题或反馈
如果您在使用HMS样本时遇到问题,请尝试以下选项:
- 在Stack Overflow上提问。请务必使用标签
huawei-mobile-services
。 - 在Github上提交问题或提出建议。
- 在Huawei Developer Forum中提出一般性问题,寻求建议和意见。
- 查看Huawei Developer Docs以获取所有HMS Core Kit的官方文档。
如果在示例中发现任何错误,请向Github仓库提交问题。
许可证
HUAWEI FIDO Flutter插件遵循Apache 2.0许可证。
示例代码
/*
Copyright 2021-2022. Huawei Technologies Co., Ltd. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License")
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import 'package:flutter/material.dart';
import 'package:huawei_fido_example/bioauthn/face_manager_example.dart';
import './bioauthn/bio_authn_manager_example.dart';
import './bioauthn/bio_authn_prompt_example.dart';
import './fidoclient/fido_example.dart';
import './widgets/custom_button.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) {
return const MaterialApp(
debugShowCheckedModeBanner: false,
home: Home(),
);
}
}
class Home extends StatelessWidget {
const Home({Key? key}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('HUAWEI FIDO EXAMPLE')),
body: Column(
children: [
customButton('BIOAUTHN MANAGER EXAMPLE', () {
Navigator.of(context).push(
MaterialPageRoute<dynamic>(
builder: (BuildContext context) => const BioAuthnManagerExample()),
);
}),
customButton('BIOAUTHN PROMPT EXAMPLE', () {
Navigator.of(context).push(
MaterialPageRoute<dynamic>(
builder: (BuildContext context) => const BioAuthnPromptExample()),
);
}),
customButton('FACE MANAGER EXAMPLE', () {
Navigator.of(context).push(
MaterialPageRoute<dynamic>(
builder: (BuildContext context) => const FaceManagerExample()),
);
}),
customButton('FIDO2CLIENT EXAMPLE', () {
Navigator.of(context).push(
MaterialPageRoute<dynamic>(
builder: (BuildContext context) => const FidoExample()),
);
}),
],
));
}
}
更多关于Flutter华为FIDO认证插件huawei_fido的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter华为FIDO认证插件huawei_fido的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
在Flutter中集成华为FIDO认证插件huawei_fido
,你可以按照以下步骤进行。以下是一个简单的代码示例,展示如何使用该插件进行FIDO认证。
首先,确保你的Flutter项目已经配置好对华为服务的支持。这通常涉及在pubspec.yaml
文件中添加依赖,以及在android/build.gradle
和android/app/build.gradle
文件中添加必要的配置。
1. 添加依赖
在pubspec.yaml
文件中添加huawei_fido
依赖:
dependencies:
flutter:
sdk: flutter
huawei_fido: ^最新版本号 # 请替换为实际的最新版本号
然后运行flutter pub get
来获取依赖。
2. 配置Android项目
确保你的android/app/build.gradle
文件中已经配置了华为的服务插件,并且你的应用已经注册了华为开发者账号,获取了必要的AGConnect配置文件(agconnect-services.json
),并将其放置在android/app/
目录下。
3. 使用huawei_fido插件进行FIDO认证
以下是一个简单的代码示例,展示如何使用huawei_fido
插件进行FIDO认证:
import 'package:flutter/material.dart';
import 'package:huawei_fido/huawei_fido.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _result = "等待认证...";
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('华为FIDO认证示例'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(_result),
SizedBox(height: 20),
ElevatedButton(
onPressed: _startFIDOAuthentication,
child: Text('开始FIDO认证'),
),
],
),
),
),
);
}
Future<void> _startFIDOAuthentication() async {
try {
// 配置FIDO认证参数(这里以指纹认证为例)
var authParam = AuthParam()
..authType = AuthType.BIOMETRIC_FINGERPRINT // 指定认证类型为指纹
..authTitle = "指纹认证" // 设置认证界面的标题
..authSubtitle = "请验证您的指纹"; // 设置认证界面的副标题
// 调用认证方法
var result = await HuaweiFido.auth(authParam);
// 处理认证结果
if (result.authSuccess) {
setState(() {
_result = "认证成功!";
});
} else {
setState(() {
_result = "认证失败,错误码:${result.errorCode}, 错误信息:${result.errorMessage}";
});
}
} catch (e) {
setState(() {
_result = "发生错误:$e";
});
}
}
}
// AuthParam和AuthResult是huawei_fido插件中定义的类,用于传递认证参数和接收认证结果
class AuthParam {
AuthType authType;
String authTitle;
String authSubtitle;
// 可以根据需要添加其他参数
}
enum AuthType { BIOMETRIC_FINGERPRINT, // 指纹认证
// 可以添加其他认证类型,如面部识别等
}
class AuthResult {
bool authSuccess;
int errorCode;
String errorMessage;
// 构造函数和其他必要字段
AuthResult({
required this.authSuccess,
required this.errorCode,
required this.errorMessage,
});
}
// 注意:上面的AuthParam和AuthResult类只是示例,实际使用时请使用huawei_fido插件中定义的类。
注意事项
- 实际使用中,
AuthParam
和AuthResult
类应由huawei_fido
插件提供,上面的示例只是为了展示如何组织代码结构。 - 确保你的应用已经在华为开发者控制台中注册,并且已经下载了对应的
agconnect-services.json
文件。 - 根据实际需求调整认证参数和错误处理逻辑。
这个示例展示了如何使用huawei_fido
插件进行基本的FIDO认证。根据你的具体需求,你可能需要调整认证参数、处理更多的认证类型或细化错误处理逻辑。