Skip to content

Security Implementation Example

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:alphyn_sdk/alphyn_sdk.dart';
class SecureApp extends StatefulWidget {
@override
_SecureAppState createState() => _SecureAppState();
}
class _SecureAppState extends State<SecureApp> {
SecurityLevel _securityLevel = SecurityLevel.unknown;
List<String> _threats = [];
bool _isMonitoring = false;
@override
void initState() {
super.initState();
_initializeSecurity();
}
Future<void> _initializeSecurity() async {
try {
// Check if SDK is initialized
final isInitialized = await AlphynSdk.isInitialized();
if (isInitialized != true) {
setState(() {
_securityLevel = SecurityLevel.initializationError;
});
return;
}
// Perform initial security check
await _performSecurityCheck();
// Start continuous monitoring
_startContinuousMonitoring();
} catch (e) {
print('Security initialization error: $e');
setState(() {
_securityLevel = SecurityLevel.initializationError;
});
}
}
Future<void> _performSecurityCheck() async {
try {
// getRejectReasons() returns a newline-separated string; split it into a list
final rejectReasonsRaw = await AlphynSdk.getRejectReasons();
final rejectReasons = rejectReasonsRaw.split('\n').where((line) => line.trim().isNotEmpty).toList();
final result = await AlphynSdk.getScanResult();
setState(() {
_threats = rejectReasons;
_securityLevel = _determineSecurityLevel(rejectReasons, result);
});
_applySecurityPolicy(_securityLevel);
} catch (e) {
print('Security check error: $e');
}
}
SecurityLevel _determineSecurityLevel(List<String> threats, String? result) {
// Device is trusted if no threats detected
if (threats.isEmpty) {
return SecurityLevel.trusted;
} else if (threats.length < 3) {
return SecurityLevel.flagged;
} else {
return SecurityLevel.untrusted;
}
}
void _applySecurityPolicy(SecurityLevel level) {
switch (level) {
case SecurityLevel.trusted:
print('✅ Security Policy: Full Access');
break;
case SecurityLevel.flagged:
print('⚠️ Security Policy: Restricted Access');
_showSecurityWarning();
break;
case SecurityLevel.untrusted:
print('🚨 Security Policy: Critical Restriction');
_showCriticalAlert();
break;
case SecurityLevel.unknown:
case SecurityLevel.initializationError:
print('❓ Security Policy: Unable to determine');
break;
}
}
void _startContinuousMonitoring() {
if (_isMonitoring) return;
_isMonitoring = true;
Timer.periodic(const Duration(minutes: 5), (timer) {
if (!mounted) {
timer.cancel();
return;
}
_performSecurityCheck();
});
}
void _showSecurityWarning() {
if (!mounted) return;
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Row(
children: [
Icon(Icons.warning_amber, color: Colors.orange),
SizedBox(width: 8),
Text('Security Notice'),
],
),
content: Text(
'Security concerns detected:\n\n${_threats.join('\n')}',
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('Acknowledge'),
),
],
),
);
}
void _showCriticalAlert() {
if (!mounted) return;
showDialog(
context: context,
barrierDismissible: false,
builder: (context) => AlertDialog(
title: const Row(
children: [
Icon(Icons.error, color: Colors.red),
SizedBox(width: 8),
Text('Critical Security Alert'),
],
),
content: Text(
'Critical security violations detected:\n\n${_threats.join('\n')}\n\n'
'Sensitive features have been disabled for your protection.',
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('I Understand'),
),
],
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Secure Application'),
backgroundColor: _getSecurityColor(),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
_getSecurityIcon(),
size: 64,
color: _getSecurityColor(),
),
const SizedBox(height: 16),
Text(
_securityLevel.displayName,
style: Theme.of(context).textTheme.headlineMedium,
),
if (_threats.isNotEmpty) ...[
const SizedBox(height: 24),
Text(
'Threats detected: ${_threats.length}',
style: Theme.of(context).textTheme.bodyLarge,
),
],
],
),
),
);
}
Color _getSecurityColor() {
switch (_securityLevel) {
case SecurityLevel.trusted:
return Colors.green;
case SecurityLevel.flagged:
return Colors.orange;
case SecurityLevel.untrusted:
return Colors.red;
default:
return Colors.grey;
}
}
IconData _getSecurityIcon() {
switch (_securityLevel) {
case SecurityLevel.trusted:
return Icons.verified_user;
case SecurityLevel.flagged:
return Icons.warning;
case SecurityLevel.untrusted:
return Icons.dangerous;
default:
return Icons.help;
}
}
}
enum SecurityLevel {
trusted,
flagged,
untrusted,
unknown,
initializationError;
String get displayName {
switch (this) {
case SecurityLevel.trusted:
return 'Trusted';
case SecurityLevel.flagged:
return 'Flagged';
case SecurityLevel.untrusted:
return 'Untrusted';
case SecurityLevel.unknown:
return 'Unknown';
case SecurityLevel.initializationError:
return 'Initialization Error';
}
}
}