Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

[auth] implemented logic for period & TOTP

Aman Raj Singh Mourya04bf4527d85f
diff --git a/auth/lib/onboarding/view/common/field_label.dart b/auth/lib/onboarding/view/common/field_label.dart
index 2b4de42f93..4ba7589749 100644
--- a/auth/lib/onboarding/view/common/field_label.dart
+++ b/auth/lib/onboarding/view/common/field_label.dart
@@ -3,10 +3,12 @@ import 'package:flutter/material.dart';
class FieldLabel extends StatelessWidget {
final String label;
+ final double width;
const FieldLabel(
this.label, {
super.key,
+ this.width = 80,
});
@override
@@ -14,7 +16,7 @@ class FieldLabel extends StatelessWidget {
return Padding(
padding: const EdgeInsets.only(right: 12.0),
child: SizedBox(
- width: 80,
+ width: width,
child: Text(
label,
style: getEnteTextTheme(context).miniBoldMuted,
diff --git a/auth/lib/onboarding/view/setup_enter_secret_key_page.dart b/auth/lib/onboarding/view/setup_enter_secret_key_page.dart
index 5c91a78c2d..c25b6a84ef 100644
--- a/auth/lib/onboarding/view/setup_enter_secret_key_page.dart
+++ b/auth/lib/onboarding/view/setup_enter_secret_key_page.dart
@@ -18,6 +18,7 @@ import 'package:ente_auth/ui/components/buttons/button_widget.dart';
import 'package:ente_auth/ui/components/custom_icon_widget.dart';
import 'package:ente_auth/ui/components/models/button_result.dart';
import 'package:ente_auth/ui/custom_icon_page.dart';
+import 'package:ente_auth/ui/topt_selector_widget.dart';
import 'package:ente_auth/ui/utils/icon_utils.dart';
import 'package:ente_auth/utils/dialog_util.dart';
import 'package:ente_auth/utils/toast_util.dart';
@@ -40,11 +41,13 @@ class _SetupEnterSecretKeyPageState extends State<SetupEnterSecretKeyPage> {
final int _notesLimit = 500;
final int _otherTextLimit = 200;
final int defaultDigits = 6;
+ final int defaultPeriodInSeconds = 30;
late TextEditingController _issuerController;
late TextEditingController _accountController;
late TextEditingController _secretController;
late TextEditingController _notesController;
late TextEditingController _digitsController;
+ late TextEditingController _periodController;
late bool _secretKeyObscured;
late List<String> selectedTags = [...?widget.code?.display.tags];
List<String> allTags = [];
@@ -53,6 +56,8 @@ class _SetupEnterSecretKeyPageState extends State<SetupEnterSecretKeyPage> {
String _customIconID = "";
late IconType _iconSrc;
late Algorithm _algorithm;
+ late Type _type;
+ final ValueNotifier<bool> showAdvancedOptions = ValueNotifier<bool>(false);
@override
void initState() {
@@ -74,6 +79,11 @@ class _SetupEnterSecretKeyPageState extends State<SetupEnterSecretKeyPage> {
? widget.code!.digits.toString()
: defaultDigits.toString(),
);
+ _periodController = TextEditingController(
+ text: widget.code != null
+ ? widget.code!.period.toString()
+ : defaultPeriodInSeconds.toString(),
+ );
_secretKeyObscured = widget.code != null;
_loadTags();
@@ -112,6 +122,7 @@ class _SetupEnterSecretKeyPageState extends State<SetupEnterSecretKeyPage> {
: IconType.customIcon;
_algorithm = widget.code == null ? Algorithm.sha1 : widget.code!.algorithm;
+ _type = widget.code == null ? Type.totp : widget.code!.type;
super.initState();
}
@@ -134,6 +145,7 @@ class _SetupEnterSecretKeyPageState extends State<SetupEnterSecretKeyPage> {
_accountController.dispose();
_notesController.dispose();
_digitsController.dispose();
+ _periodController.dispose();
super.dispose();
}
@@ -282,76 +294,7 @@ class _SetupEnterSecretKeyPageState extends State<SetupEnterSecretKeyPage> {
),
const SizedBox(height: 12),
widget.code == null
- ? Theme(
- data: Theme.of(context).copyWith(
- splashColor: Colors.transparent,
- highlightColor: Colors.transparent,
- hoverColor: Colors.transparent,
- ),
- child: ExpansionTile(
- shape: RoundedRectangleBorder(
- borderRadius: BorderRadius.circular(8.0),
- ),
- collapsedShape: RoundedRectangleBorder(
- borderRadius: BorderRadius.circular(8.0),
- ),
- collapsedBackgroundColor: Colors.transparent,
- tilePadding: EdgeInsets.zero,
- title: Text(
- "Advanced",
- style: getEnteTextTheme(context).small,
- ),
- children: <Widget>[
- Row(
- children: [
- const FieldLabel("Digits"),
- Expanded(
- child: TextFormField(
- keyboardType: TextInputType.number,
- // The validator receives the text that the user has entered.
- validator: (value) {
- if (value == null || value.isEmpty) {
- return "Please enter a number";
- }
- final intValue = int.tryParse(value);
- if (intValue == null) {
- return "Only integers are allowed";
- }
- if (intValue < 1 || intValue > 10) {
- return "OTP digits must be between 1 and 10";
- }
- return null;
- },
- maxLines: 1,
- decoration: const InputDecoration(
- contentPadding: EdgeInsets.symmetric(
- vertical: 12.0,
- ),
- ),
- style: getEnteTextTheme(context).small,
- controller: _digitsController,
- ),
- ),
- ],
- ),
- const SizedBox(height: 22),
- Row(
- children: [
- const FieldLabel("Algorithm"),
- AlgorithmSelectorWidget(
- currentAlgorithm: _algorithm,
- onSelected: (newAlgorithm) async {
- setState(() {
- _algorithm = newAlgorithm;
- });
- },
- ),
- ],
- ),
- const SizedBox(height: 12),
- ],
- ),
- )
+ ? advanceOptionWidget()
: const SizedBox.shrink(),
const SizedBox(height: 12),
Wrap(
@@ -419,11 +362,25 @@ class _SetupEnterSecretKeyPageState extends State<SetupEnterSecretKeyPage> {
return;
}
+ final period =
+ int.tryParse(_periodController.text.trim());
+ if (period != null && (period < 10 || period > 60)) {
+ String message =
+ "Period must be between 10 and 60 seconds";
+ _showIncorrectDetailsDialog(
+ context,
+ message: message,
+ );
+ return;
+ }
+
if ((_accountController.text.trim().isEmpty &&
_issuerController.text.trim().isEmpty) ||
_secretController.text.trim().isEmpty ||
_digitsController.text.trim().isEmpty ||
- digits == null) {
+ digits == null ||
+ _periodController.text.trim().isEmpty ||
+ period == null) {
String message;
if (_secretController.text.trim().isEmpty) {
message = context.l10n.secretCanNotBeEmpty;
@@ -431,6 +388,10 @@ class _SetupEnterSecretKeyPageState extends State<SetupEnterSecretKeyPage> {
message = "Digits cannot be empty";
} else if (digits == null) {
message = "Digits is not a integer";
+ } else if (_periodController.text.isEmpty) {
+ message = "Period cannot be empty";
+ } else if (period == null) {
+ message = "Period is not a integer";
} else {
message =
context.l10n.bothIssuerAndAccountCanNotBeEmpty;
@@ -462,6 +423,7 @@ class _SetupEnterSecretKeyPageState extends State<SetupEnterSecretKeyPage> {
final secret = _secretController.text.trim().replaceAll(' ', '');
final notes = _notesController.text.trim();
final digits = int.tryParse(_digitsController.text.trim());
+ final period = int.tryParse(_periodController.text.trim());
final isStreamCode = issuer.toLowerCase() == "steam" ||
issuer.toLowerCase().contains('steampowered.com');
@@ -498,13 +460,14 @@ class _SetupEnterSecretKeyPageState extends State<SetupEnterSecretKeyPage> {
final Code newCode = widget.code == null
? Code.fromAccountAndSecret(
- isStreamCode ? Type.steam : Type.totp,
+ isStreamCode ? Type.steam : _type,
account,
issuer,
secret,
display,
isStreamCode ? Code.steamDigits : digits!,
algorithm: _algorithm,
+ period: period!,
)
: widget.code!.copyWith(
account: account,
@@ -513,6 +476,8 @@ class _SetupEnterSecretKeyPageState extends State<SetupEnterSecretKeyPage> {
display: display,
algorithm: _algorithm,
digits: digits!,
+ type: _type,
+ period: period,
);
// Verify the validity of the code
@@ -558,4 +523,159 @@ class _SetupEnterSecretKeyPageState extends State<SetupEnterSecretKeyPage> {
_iconSrc = newCustomIcon.type;
});
}
+
+ Widget advanceOptionWidget() {
+ return Padding(
+ padding: const EdgeInsets.only(top: 16.0),
+ child: Column(
+ crossAxisAlignment: CrossAxisAlignment.start,
+ children: [
+ GestureDetector(
+ onTap: () {
+ showAdvancedOptions.value = !showAdvancedOptions.value;
+ },
+ child: Row(
+ mainAxisAlignment: MainAxisAlignment.spaceBetween,
+ children: [
+ const Text(
+ 'Advanced',
+ ),
+ ValueListenableBuilder<bool>(
+ valueListenable: showAdvancedOptions,
+ builder: (context, isExpanded, child) {
+ return Icon(
+ isExpanded
+ ? Icons.keyboard_arrow_up
+ : Icons.keyboard_arrow_down,
+ size: 24,
+ );
+ },
+ ),
+ ],
+ ),
+ ),
+ ValueListenableBuilder<bool>(
+ valueListenable: showAdvancedOptions,
+ builder: (context, isExpanded, child) {
+ return AnimatedSwitcher(
+ duration: const Duration(milliseconds: 300),
+ transitionBuilder: (child, animation) {
+ return SizeTransition(
+ sizeFactor: animation,
+ child: child,
+ );
+ },
+ child: isExpanded
+ ? SizedBox(
+ width: 400,
+ child: Padding(
+ padding: const EdgeInsets.only(
+ top: 16,
+ ),
+ child: GridView.count(
+ crossAxisCount: 2,
+ shrinkWrap: true,
+ childAspectRatio: 2.5,
+ crossAxisSpacing: 16,
+ mainAxisSpacing: 14,
+ physics: const NeverScrollableScrollPhysics(),
+ children: [
+ Row(
+ mainAxisAlignment: MainAxisAlignment.start,
+ children: [
+ const FieldLabel("Algorithm", width: 60),
+ AlgorithmSelectorWidget(
+ currentAlgorithm: _algorithm,
+ onSelected: (newAlgorithm) async {
+ setState(() {
+ _algorithm = newAlgorithm;
+ });
+ },
+ ),
+ ],
+ ),
+ Row(
+ mainAxisAlignment: MainAxisAlignment.start,
+ children: [
+ const FieldLabel("TOTP", width: 60),
+ ToptSelectorWidget(
+ currentTopt: _type,
+ onSelected: (newTopt) async {
+ setState(() {
+ _type = newTopt;
+ });
+ },
+ ),
+ ],
+ ),
+ Row(
+ children: [
+ const FieldLabel("Period", width: 60),
+ Expanded(
+ child: TextFormField(
+ keyboardType: TextInputType.number,
+ // The validator receives the text that the user has entered.
+ validator: (value) {
+ if (value == null || value.isEmpty) {
+ return "Please enter a number";
+ }
+ final intValue = int.tryParse(value);
+ if (intValue == null) {
+ return "Only integers are allowed";
+ }
+ if (intValue < 1 || intValue > 60) {
+ return "Period must be between 1 and 60";
+ }
+ return null;
+ },
+ maxLines: 1,
+ style: getEnteTextTheme(
+ context,
+ ).small,
+ controller: _periodController,
+ ),
+ ),
+ ],
+ ),
+ Row(
+ children: [
+ const FieldLabel("Digits", width: 60),
+ Expanded(
+ child: TextFormField(
+ keyboardType: TextInputType.number,
+ // The validator receives the text that the user has entered.
+ validator: (value) {
+ if (value == null || value.isEmpty) {
+ return "Please enter a number";
+ }
+ final intValue = int.tryParse(value);
+ if (intValue == null) {
+ return "Only integers are allowed";
+ }
+ if (intValue < 1 || intValue > 10) {
+ return "OTP digits must be between 1 and 10";
+ }
+ return null;
+ },
+ maxLines: 1,
+ style: getEnteTextTheme(
+ context,
+ ).small,
+ controller: _digitsController,
+ ),
+ ),
+ ],
+ ),
+ ],
+ ),
+ ),
+ )
+ : const SizedBox.shrink(),
+ );
+ },
+ ),
+ ],
+ ),
+ );
+ }
}