[Huawei Developers] Basavaraj.navi:
Introduction

In this article, we will learn about Huawei Safety detect kit.

What is Safety detect?

Safety Detect builds robust security capabilities, including system integrity check (SysIntegrity), app security check (AppsCheck), malicious URL check (URLCheck), fake user detection (UserDetect), and malicious Wi-Fi detection (WifiDetect), into your app, effectively protecting it against security threats.

Why do we need to use safety detect?

Mobile applications capture almost 90% of people’s time on mobile devices, while the rest of the time is spent browsing the web. So basically now a day’s mobile usage is more than the web. Since all the users are using smart mobile phones for daily needs like, reading news, email, online shopping, booking taxi, and wallets to pay, educational apps etc. Even for banking transaction there was time people use to stand in the queue to deposit or withdraw, but now everything can be done in mobile application with just 2 to 3 clicks. Since everything happening over the phone definitely we should provide security to mobile apps.

Now let’s see what all are the security features provided by Huawei Safety detect kit.

SysIntegrity

AppsCheck

URLCheck

UserDetect

WifiDetect

Integration of Safety detect

1. Configure application on the AGC.

2. Client application development process.

Configure application on the AGC

Follow the steps.

Step 1: We need to register as a developer account in AppGallery Connect. If you are already developer ignore this step.

Step 2: Create an app by referring to Creating a Project and Creating an App in the Project

Step 3: Set the data storage location based on current location.

Step 4: Enabling Safety detect. Open AppGallery connect, choose Manage API > Safety Detect

Step 5: Generating a Signing Certificate Fingerprint.

Step 6: Configuring the Signing Certificate Fingerprint.

Step 7: Download your agconnect-services.json file, paste it into the app root directory.

Client application development process

Follow the steps

Step 1: Create flutter application in the Android studio (Any IDE which is your favorite).

Step 2: Add the App level gradle dependencies. Choose inside project Android > app > build.gradle.

1

2	apply plugin: 'com.android.application'

apply plugin: 'com.huawei.agconnect'

Root level gradle dependencies.

1

2	maven { url 'https://developer.huawei.com/repo/' }

classpath 'com.huawei.agconnect:agcp:1.4.1.300'

Step 3: Download plugin from here

Step 4: Add downloaded file into outside project directory. Declare plugin path in pubspec.yaml file under dependencies.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30	dependencies:

flutter:

sdk: flutter

huawei_account:

path: ../huawei_account/

huawei_location:

path: ../huawei_location/

huawei_map:

path: ../huawei_map/

huawei_analytics:

path: ../huawei_analytics/

huawei_site:

path: ../huawei_site/

huawei_push:

path: ../huawei_push/

huawei_dtm:

path: ../huawei_dtm/

huawei_ml:

path: ../huawei_ml/

huawei_safetydetect:

path: ../huawei_safetydetect/

agconnect_crash: ^1.0.0

agconnect_remote_config: ^1.0.0

http: ^0.12.2

camera:

path_provider:

path:

image_picker:

fluttertoast: ^7.1.6

shared_preferences: ^0.5.12+4

Step 5: Build flutter application

In this example, I’m checking user detection in the taxi booking application. Whenever user sign up with Huawei, I’m checking user detection success, then it will be navigating user to home screen to book a cab.

SysIntegrity: Checks whether the device running your app is secure, for example, whether it is rooted. The SysIntegrity API is called to check the system integrity of a device. If the device is not safe, appropriate measures are taken.

AppsCheck: Obtains a list of malicious apps. You can obtain all malicious applications and evaluate whether you can restrict the behavior of your application based on the risk.

You can directly call the getMaliciousAppsList() method to get all the malicious apps.

URLCheck: Determines the threat type of a specific URL. You can determine the dangerous URLs using URL Check API. Currently UrlSafety API provide determinate Malware and Phishing threats. When you visit a URL, this API checks whether the URL is a malicious one. If so, you can evaluate the risk and alert the user about the risk or block the URL.

User Detect: Checks whether your app is interacting with a fake user. This API can help your app to prevent batch registration, credential stuffing attacks, activity bonus hunting, and content crawling. If a user is a suspicious one or risky one, a verification code is sent to the user for secondary verification. If the detection result indicates that the user is a real one, the user can sign in to my app. Otherwise, the user is not allowed to Home page.

WifiDetect: Checks whether the Wi-Fi to be connected is secure. This API checks characteristics of the Wi-Fi and router to be connected, analyzes the Wi-Fi information, and returns the Wi-Fi detection results after classification, helping you to prevent possible attacks to your app from malicious Wi-Fi. If attacks are detected, app can interrupt the user operation or it will asks user permission.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505	import 'dart:convert';

import 'dart:math';

import 'dart:typed_data';

import 'package:agconnect_crash/agconnect_crash.dart';

import 'package:flutter/material.dart';

import 'package:flutter/services.dart';

import 'package:huawei_account/huawei_account.dart';

import 'package:huawei_safetydetect/huawei_safetydetect.dart';

import 'package:taxibooking/analytics/analyticsutil.dart';

import 'package:taxibooking/constants/constants.dart';

import 'package:taxibooking/models/accountmodel.dart';

import 'package:taxibooking/ui/accountscreen.dart';

import 'package:taxibooking/ui/widgets/custom_shape.dart';

import 'package:taxibooking/ui/widgets/customappbar.dart';

import 'package:taxibooking/ui/widgets/responsive_ui.dart';

import 'package:taxibooking/ui/widgets/textformfield.dart';

import 'package:taxibooking/utils/validator.dart';

import 'home.dart';

class SignUpScreen extends StatefulWidget {

@override

_SignUpScreenState createState() => _SignUpScreenState();

}

class _SignUpScreenState extends State<SignUpScreen> {

bool checkBoxValue = false;

double _height;

double _width;

double _pixelRatio;

bool _large;

bool _medium;

String appId;

@override

Widget build(BuildContext context) {

_height = MediaQuery.of(context).size.height;

_width = MediaQuery.of(context).size.width;

_pixelRatio = MediaQuery.of(context).devicePixelRatio;

_large = ResponsiveWidget.isScreenLarge(_width, _pixelRatio);

_medium = ResponsiveWidget.isScreenMedium(_width, _pixelRatio);

return Material(

child: Scaffold(

body: Container(

height: _height,

width: _width,

margin: EdgeInsets.only(bottom: 5),

child: SingleChildScrollView(

child: Column(

children: <Widget>[

Opacity(opacity: 0.88, child: CustomAppBar()),

clipShape(),

form(),

acceptTermsTextRow(),

SizedBox(

height: _height / 35,

),

button(),

infoTextRow(),

socialIconsRow(),

signInTextRow(),

],

),

),

),

),

);

}

@override

void initState() {

super.initState();

getAppId();

//getWifiDetectStatus();

AnalyticsUtil.pageEnd("Sign up screen");

}

@override

void dispose() {

super.dispose();

AnalyticsUtil.pageEnd("Sign up screen");

}

Widget clipShape() {

return Stack(

children: <Widget>[

Opacity(

opacity: 0.75,

child: ClipPath(

clipper: CustomShapeClipper(),

child: Container(

height: _large

? _height / 8

: (_medium ? _height / 7 : _height / 6.5),

decoration: BoxDecoration(

gradient: LinearGradient(

colors: [Colors.orange[200], Colors.pinkAccent],

),

),

),

),

),

Opacity(

opacity: 0.5,

child: ClipPath(

clipper: CustomShapeClipper2(),

child: Container(

height: _large

? _height / 12

: (_medium ? _height / 11 : _height / 10),

decoration: BoxDecoration(

gradient: LinearGradient(

colors: [Colors.orange[200], Colors.pinkAccent],

),

),

),

),

),

Container(

height: _height / 5.5,

alignment: Alignment.center,

decoration: BoxDecoration(

boxShadow: [

BoxShadow(

spreadRadius: 0.0,

color: Colors.black26,

offset: Offset(1.0, 10.0),

blurRadius: 20.0),

],

color: Colors.white,

shape: BoxShape.circle,

),

child: GestureDetector(

onTap: () {

print('Adding photo');

},

child: Icon(

Icons.add_a_photo,

size: _large ? 40 : (_medium ? 33 : 31),

color: Colors.red[500],

)),

),

],

);

}

Widget form() {

return Container(

margin: EdgeInsets.only(

left: _width / 12.0, right: _width / 12.0, top: _height / 20.0),

child: Form(

child: Column(

children: <Widget>[

firstNameTextFormField(),

SizedBox(height: _height / 60.0),

lastNameTextFormField(),

SizedBox(height: _height / 60.0),

emailTextFormField(),

SizedBox(height: _height / 60.0),

phoneTextFormField(),

SizedBox(height: _height / 60.0),

passwordTextFormField(),

],

),

),

);

}

Widget firstNameTextFormField() {

return CustomTextField(

keyboardType: TextInputType.text,

icon: Icons.person,

hint: "First Name",

);

}

Widget lastNameTextFormField() {

return CustomTextField(

keyboardType: TextInputType.text,

icon: Icons.person,

hint: "Last Name",

);

}

Widget emailTextFormField() {

return CustomTextField(

keyboardType: TextInputType.emailAddress,

icon: Icons.email,

hint: "Email ID",

);

}

Widget phoneTextFormField() {

return CustomTextField(

keyboardType: TextInputType.number,

icon: Icons.phone,

hint: "Mobile Number",

);

}

Widget passwordTextFormField() {

return CustomTextField(

keyboardType: TextInputType.text,

obscureText: true,

icon: Icons.lock,

hint: "Password",

);

}

Widget acceptTermsTextRow() {

return Container(

margin: EdgeInsets.only(top: _height / 100.0),

child: Row(

mainAxisAlignment: MainAxisAlignment.center,

children: <Widget>[

Checkbox(

activeColor: Colors.red[500],

value: checkBoxValue,

onChanged: (bool newValue) {

setState(() {

checkBoxValue = newValue;

});

}),

Text(

"I accept all terms and conditions",

style: TextStyle(

fontWeight: FontWeight.w400,

fontSize: _large ? 12 : (_medium ? 11 : 10)),

),

],

),

);

}

Widget button() {

return RaisedButton(

elevation: 0,

shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(30.0)),

onPressed: () {

print("Routing to your account");

},

textColor: Colors.white,

padding: EdgeInsets.all(0.0),

child: Container(

alignment: Alignment.center,

// height: _height / 20,

width: _large ? _width / 4 : (_medium ? _width / 3.75 : _width / 3.5),

decoration: BoxDecoration(

borderRadius: BorderRadius.all(Radius.circular(20.0)),

gradient: LinearGradient(

colors: <Color>[Colors.yellow[800], Colors.red],

),

),

padding: const EdgeInsets.all(12.0),

child: Text(

'SIGN UP',

style: TextStyle(fontSize: _large ? 14 : (_medium ? 12 : 10)),

),

),

);

}

Widget infoTextRow() {

return Container(

margin: EdgeInsets.only(top: _height / 40.0),

child: Row(

mainAxisAlignment: MainAxisAlignment.center,

children: <Widget>[

Text(

"Or create using social media",

style: TextStyle(

fontWeight: FontWeight.w400,

fontSize: _large ? 12 : (_medium ? 11 : 10)),

),

],

),

);

}

Widget socialIconsRow() {

return Container(

margin: EdgeInsets.only(top: _height / 80.0),

child: Row(

mainAxisSize: MainAxisSize.min,

children: <Widget>[

GestureDetector(

onTap: () {

print("Huawei sign in clicked");

signInWithHuaweiAccount();

},

child: CircleAvatar(

radius: 25.0,

backgroundImage:

AssetImage("assets/images/huaweilogo_288x288.png"),

),

),

SizedBox(

width: 20,

),

CircleAvatar(

radius: 25,

backgroundImage: AssetImage("assets/images/fblogo.jpg"),

),

SizedBox(

width: 20,

),

CircleAvatar(

radius: 25,

backgroundImage: AssetImage("assets/images/twitterlogo.jpg"),

),

],

),

);

}

Widget signInTextRow() {

return Container(

margin: EdgeInsets.only(top: _height / 20.0),

child: Row(

mainAxisAlignment: MainAxisAlignment.center,

children: <Widget>[

Text(

"Already have an account?",

style: TextStyle(fontWeight: FontWeight.w400),

),

SizedBox(

width: 5,

),

GestureDetector(

onTap: () {

Navigator.of(context).pop(SIGN_IN);

print("Routing to Sign up screen");

},

child: Text(

"Sign in",

style: TextStyle(

fontWeight: FontWeight.w800,

color: Colors.red[800],

fontSize: 19),

),

)

],

),

);

}

void signInWithHuaweiAccount() async {

HmsAuthParamHelper authParamHelper = new HmsAuthParamHelper();

authParamHelper

..setIdToken()

..setAuthorizationCode()

..setAccessToken()

..setProfile()

..setEmail()

..setScopeList([HmsScope.openId, HmsScope.email, HmsScope.profile])

..setRequestCode(8888);

try {

final HmsAuthHuaweiId accountInfo =

await HmsAuthService.signIn(authParamHelper: authParamHelper);

setState(() {

var accountDetails = AccountInformation(

accountInfo.displayName,

accountInfo.email,

accountInfo.givenName,

accountInfo.familyName,

accountInfo.avatarUriString);

AnalyticsUtil.addCustomEvent(

accountInfo.displayName,

accountInfo.email,

accountInfo.givenName,

accountInfo.familyName,

accountInfo.avatarUriString);

AnalyticsUtil.setUserId("ABC123456789");

AnalyticsUtil.setUserProfile(accountInfo.displayName);

AnalyticsUtil.getAnalyticsClient()

.setUserProfile("Email", accountInfo.email);

AnalyticsUtil.getAnalyticsClient()

.setUserProfile("Family Name", accountInfo.familyName);

AnalyticsUtil.getAnalyticsClient()

.setUserProfile("Profile pic", accountInfo.avatarUriString);

/* AGCCrash.instance.setUserId("ABC123456789");

AGCCrash.instance.setCustomKey("Email", accountInfo.email);

AGCCrash.instance.setCustomKey("Family Name", accountInfo.familyName);

AGCCrash.instance

.setCustomKey("Profile pic", accountInfo.avatarUriString);

AGCCrash.instance.log(

level: LogLevel.info,

message: "Mr: " +

accountInfo.displayName +

"has successfully logged in");

AGCCrash.instance.testIt();*/

/*Navigator.push(

context,

MaterialPageRoute(

builder: (context) => Homepage(

accountInformation: accountDetails,

),

));*/

detectUser(accountDetails);

print("account name: " + accountInfo.displayName);

});

} on Exception catch (exception) {

print(exception.toString());

print("error: " + exception.toString());

/* AGCCrash.instance

.log(level: LogLevel.error, message: exception.toString());

AGCCrash.instance.testIt();*/

}

}

//safety detect

getAppId() async {

String appID = await SafetyDetect.getAppID;

setState(() {

appId = appID;

Validator().showToast("App Id: "+appID);

});

}

checkSysIntegrity() async {

Random secureRandom = Random.secure();

List randomIntegers = List<int>();

for (var i = 0; i < 24; i++) {

randomIntegers.add(secureRandom.nextInt(255));

}

Uint8List nonce = Uint8List.fromList(randomIntegers);

try {

String result = await SafetyDetect.sysIntegrity(nonce, appId);

List<String> jwsSplit = result.split(".");

String decodedText = utf8.decode(base64Url.decode(jwsSplit[1]));

Validator().showToast("SysIntegrityCheck result is: $decodedText");

} on PlatformException catch (e) {

Validator().showToast("Error occurred while getting SysIntegrityResult. Error is : $e");

}

}

void getMaliciousAppsList() async {

List<MaliciousAppData> maliciousApps = List();

maliciousApps = await SafetyDetect.getMaliciousAppsList();

setState(() {

Validator().showToast("malicious apps: ${maliciousApps.toString()}");

});

}

detectUser(AccountInformation accountDetails) async {

try {

String token = await SafetyDetect.userDetection(appId);

print("User verification succeeded, user token: $token");

if(token!=null){

Navigator.push(

context,

MaterialPageRoute(

builder: (context) => Homepage(

accountInformation: accountDetails,

),

));

}else{

Validator().showToast("user detection failed");

}

} on PlatformException catch (e) {

print(

"Error occurred: " + e.code + ":" + SafetyDetectStatusCodes[e.code]);

}

}

void loadUrl(String url) async {

Future.delayed(const Duration(seconds: 5), () async {

urlCheck(url);

});

}

void urlCheck(String url) async {

List<UrlThreatType> threatTypes = [

UrlThreatType.malware,

UrlThreatType.phishing

];

List<UrlCheckThreat> urlCheckResults =

await SafetyDetect.urlCheck(url, appId, threatTypes);

if (urlCheckResults.length == 0) {

Validator().showToast("No threat is detected for the URL");

} else {

urlCheckResults.forEach((element) {

print("${element.getUrlThreatType} is detected on the URL");

});

}

}

getWifiDetectStatus() async {

try {

WifiDetectResponse wifiDetectStatus =

await SafetyDetect.getWifiDetectStatus();

Validator().showToast('Wifi detect status is: ${wifiDetectStatus.getWifiDetectType.toString()}');

} on PlatformException catch (e) {

if (e.code.toString() == "19003") {

Validator().showToast(' The WifiDetect API is unavailable in this region');

}

}

}

}

Result

Tips and Tricks

Download latest HMS Flutter plugin.

Check dependencies downloaded properly.

Latest HMS Core APK is required.

Set minSDK 19 or later.

WifiDetect function available only in Chinese mainland.

UserDetect function not available in Chinese mainland.

Conclusion

In this article, we have learnt integration of Huawei safety detect and types of security provided by Huawei safety detect kit. Nowadays everything is happening over the phone, so as developer it is our responsibility to provide security to application. Otherwise users do not use the application. Thank Huawei for giving such great Safety detect kit to build secure application.

Reference

Safety detect

Happy coding​