]> git.proxmox.com Git - flutter/proxmox_login_manager.git/commitdiff
update to new-format TFA login
authorThomas Lamprecht <t.lamprecht@proxmox.com>
Mon, 3 Jul 2023 10:19:32 +0000 (12:19 +0200)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Mon, 3 Jul 2023 10:35:59 +0000 (12:35 +0200)
use the initState method to avoid that we override the initial
selected TFA method on every re-build and also to avoid running into
a transitive error when TFA login suceeded, as the handler for that
in the API dart lib will set tfa to null then, causing a null
dereference error if we'd get that in the (re)build method.

Co-authored-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
lib/proxmox_login_form.dart
lib/proxmox_tfa_form.dart

index a057d0f0f54a98fa4e75c2f26253e83767efc777..d92417316da6442d713d3afde1d2333049abfcd8 100644 (file)
@@ -431,7 +431,7 @@ class _ProxmoxLoginPageState extends State<ProxmoxLoginPage> {
       var client = await proxclient.authenticate(
           '$username@$realm', password, origin, settings.sslValidation!);
 
-      if (client.credentials.tfa) {
+      if (client.credentials.tfa != null) {
         client = await Navigator.of(context).push(MaterialPageRoute(
           builder: (context) => ProxmoxTfaForm(
             apiClient: client,
index dfdb921089eab7733d37d39444c263e8ffb75598..0caa8bd8e2c5a4c6946fbbbe7599522f8ebb23cb 100644 (file)
@@ -14,6 +14,16 @@ class ProxmoxTfaForm extends StatefulWidget {
 class _ProxmoxTfaFormState extends State<ProxmoxTfaForm> {
   final TextEditingController _codeController = TextEditingController();
   bool _isLoading = false;
+  List<String> _tfa_kinds = [];
+  String _selected_tfa_kind = "";
+
+  @override
+  void initState() {
+    super.initState();
+    _tfa_kinds = widget.apiClient!.credentials.tfa!.kinds().toList();
+    _selected_tfa_kind = _tfa_kinds[0];
+  }
+
   @override
   Widget build(BuildContext context) {
     return Theme(
@@ -67,13 +77,37 @@ class _ProxmoxTfaFormState extends State<ProxmoxTfaForm> {
                       Padding(
                         padding: const EdgeInsets.fromLTRB(0, 50.0, 0, 8.0),
                         child: Container(
-                          width: 150,
-                          child: TextField(
-                              controller: _codeController,
-                              textAlign: TextAlign.center,
-                              decoration: InputDecoration(labelText: 'Code'),
-                              autofocus: true,
-                              onSubmitted: (value) => _submitTfaCode()),
+                          width: 175,
+                          child: Column(
+                            children: <Widget>[
+                              DropdownButtonFormField(
+                                decoration: InputDecoration(
+                                    labelText: 'Method',
+                                    icon: Icon(Icons.input)),
+                                items: _tfa_kinds
+                                    .map((e) => DropdownMenuItem(
+                                          child: ListTile(title: Text(e)),
+                                          value: e,
+                                        ))
+                                    .toList(),
+                                onChanged: (String? value) {
+                                  setState(() {
+                                    _selected_tfa_kind = value!;
+                                  });
+                                },
+                                selectedItemBuilder: (context) =>
+                                    _tfa_kinds.map((e) => Text(e)).toList(),
+                                value: _selected_tfa_kind,
+                              ),
+                              TextField(
+                                  controller: _codeController,
+                                  textAlign: TextAlign.center,
+                                  decoration: InputDecoration(
+                                      labelText: 'Code', icon: Icon(Icons.pin)),
+                                  autofocus: true,
+                                  onSubmitted: (value) => _submitTfaCode()),
+                            ],
+                          ),
                         ),
                       ),
                       Expanded(
@@ -113,8 +147,8 @@ class _ProxmoxTfaFormState extends State<ProxmoxTfaForm> {
       _isLoading = true;
     });
     try {
-      final client =
-          await widget.apiClient!.finishTfaChallenge(_codeController.text);
+      final client = await widget.apiClient!
+          .finishTfaChallenge(_selected_tfa_kind, _codeController.text);
       Navigator.of(context).pop(client);
     } on ProxmoxApiException catch (e) {
       showDialog(