π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
import 'dart:async';
import 'dart:convert';
import 'dart:typed_data';
import 'package:app_links/app_links.dart';
import 'package:ente_accounts/ente_accounts.dart';
import 'package:ente_accounts/widgets/account_app_bar_logo.dart';
import 'package:ente_components/ente_components.dart';
import 'package:ente_configuration/base_configuration.dart';
import 'package:ente_pure_utils/ente_pure_utils.dart';
import 'package:ente_strings/ente_strings.dart';
import 'package:ente_ui/components/alert_bottom_sheet.dart';
import 'package:ente_ui/theme/ente_theme.dart';
import 'package:ente_ui/utils/dialog_util.dart';
import 'package:ente_ui/utils/toast_util.dart';
import 'package:flutter/material.dart';
import 'package:logging/logging.dart';
import 'package:url_launcher/url_launcher_string.dart';
class PasskeyPage extends StatefulWidget {
final BaseConfiguration config;
final String sessionID;
final Uint8List? keyEncryptionKey;
final String totp2FASessionID;
final String accountsUrl;
final String redirectUrl;
final String clientPackage;
const PasskeyPage(
this.config,
this.sessionID, {
required this.totp2FASessionID,
required this.accountsUrl,
required this.redirectUrl,
required this.clientPackage,
this.keyEncryptionKey,
super.key,
});
@override
State<PasskeyPage> createState() => _PasskeyPageState();
}
class _PasskeyPageState extends State<PasskeyPage> {
final Logger _logger = Logger("PasskeyPage");
@override
void initState() {
launchPasskey();
_initDeepLinks();
super.initState();
}
@override
void dispose() {
super.dispose();
}
Future<void> launchPasskey() async {
await launchUrlString(
"${widget.accountsUrl}/passkeys/verify?"
"passkeySessionID=${widget.sessionID}"
"&redirect=${widget.redirectUrl}"
"&clientPackage=${widget.clientPackage}",
mode: LaunchMode.externalApplication,
);
}
Future<void> checkStatus() async {
Map<String, dynamic>? response;
try {
response = await UserService.instance.getTokenForPasskeySession(
widget.sessionID,
);
} on AccountsError_SessionExpired {
if (!mounted) {
return;
}
await showAlertBottomSheet(
context,
title: context.strings.loginSessionExpired,
message: context.strings.loginSessionExpiredDetails,
assetPath: 'assets/warning-grey.png',
);
if (mounted) {
Navigator.of(context).pop();
}
return;
} catch (e, s) {
_logger.severe("failed to check status", e, s);
if (mounted) {
showGenericErrorDialog(context: context, error: e).ignore();
}
return;
}
if (response == null) {
if (mounted) {
showToast(context, context.strings.passKeyPendingVerification);
}
return;
}
await UserService.instance.onPassKeyVerified(
mounted ? context : null,
response,
keyEncryptionKey: widget.keyEncryptionKey,
);
}
Future<void> _handleDeeplink(String? link) async {
if (!context.mounted ||
widget.config.hasConfiguredAccount() ||
link == null) {
_logger.warning('ignored deeplink: contextMounted ${context.mounted}');
return;
}
try {
final expectedPrefix = widget.redirectUrl.toLowerCase();
if (mounted && link.toLowerCase().startsWith(expectedPrefix)) {
if (widget.config.isLoggedIn()) {
_logger.info('ignored deeplink: already configured');
showToast(context, 'Account is already configured.');
return;
}
final parsedUri = Uri.parse(link);
final sessionID = parsedUri.queryParameters['passkeySessionID'];
if (sessionID != widget.sessionID) {
showToast(context, "Session ID mismatch");
_logger.warning('ignored deeplink: sessionID mismatch');
return;
}
final String? authResponse = parsedUri.queryParameters['response'];
String base64String = authResponse!.toString();
while (base64String.length % 4 != 0) {
base64String += '=';
}
final res = utf8.decode(base64.decode(base64String));
final json = jsonDecode(res) as Map<String, dynamic>;
await UserService.instance.onPassKeyVerified(
mounted ? context : null,
json,
keyEncryptionKey: widget.keyEncryptionKey,
);
} else {
_logger.info(
'ignored deeplink: ${baseUrlForLogging(link)} mounted $mounted',
);
}
} catch (e, s) {
_logger.severe('passKey: failed to handle deeplink', e, s);
if (mounted) {
showGenericErrorDialog(context: context, error: e).ignore();
}
}
}
Future<bool> _initDeepLinks() async {
final appLinks = AppLinks();
appLinks.stringLinkStream.listen(
_handleDeeplink,
onError: (err) {
_logger.severe(err);
},
);
return false;
}
@override
Widget build(BuildContext context) {
final colorScheme = getEnteColorScheme(context);
return Scaffold(
backgroundColor: colorScheme.backgroundBase,
appBar: AppBar(
elevation: 0,
scrolledUnderElevation: 0,
backgroundColor: colorScheme.backgroundBase,
centerTitle: true,
title: const AccountAppBarLogo(),
leading: IconButton(
icon: const Icon(Icons.arrow_back),
color: getAccountAppBarColor(context),
onPressed: () {
Navigator.of(context).pop();
},
),
),
body: _getBody(),
);
}
Widget _getBody() {
final colorScheme = getEnteColorScheme(context);
final textTheme = getEnteTextTheme(context);
return SafeArea(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const SizedBox(height: 12),
Text(
context.strings.passkeyAuthTitle,
style: textTheme.h3Bold.copyWith(color: colorScheme.textBase),
),
Text(
context.strings.waitingForVerification,
style: textTheme.body.copyWith(color: colorScheme.textMuted),
),
const SizedBox(height: 24),
ButtonComponent(
label: context.strings.tryAgain,
onTap: () => unawaited(launchPasskey()),
shouldSurfaceExecutionStates: false,
),
const SizedBox(height: 16),
ButtonComponent(
label: context.strings.checkStatus,
variant: ButtonComponentVariant.secondary,
onTap: () async {
try {
await checkStatus();
} catch (e) {
debugPrint('failed to check status %e');
if (mounted) {
showGenericErrorDialog(context: context, error: e).ignore();
}
}
},
shouldSurfaceExecutionStates: false,
),
const SizedBox(height: 16),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
widget.totp2FASessionID.isNotEmpty
? GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () {
routeToPage(
context,
TwoFactorAuthenticationPage(
widget.totp2FASessionID,
keyEncryptionKey: widget.keyEncryptionKey,
),
);
},
child: Text(
context.strings.loginWithTOTP,
style: textTheme.body.copyWith(
color: colorScheme.primary700,
decoration: TextDecoration.underline,
decorationColor: colorScheme.primary700,
),
),
)
: const SizedBox.shrink(),
GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () {
UserService.instance.recoverTwoFactor(
context,
widget.sessionID,
TwoFactorType.passkey,
);
},
child: Text(
context.strings.recoverAccount,
style: textTheme.body.copyWith(
color: colorScheme.primary700,
decoration: TextDecoration.underline,
decorationColor: colorScheme.primary700,
),
),
),
],
),
],
),
),
);
}
}