diff --git a/app/lib/authentik_api.dart b/app/lib/authentik_api.dart index 1abe739..2bcfa16 100644 --- a/app/lib/authentik_api.dart +++ b/app/lib/authentik_api.dart @@ -27,7 +27,7 @@ Future getSessionCookie(oauth2.Client client) async { return sessionCookie; } -Future getUserSettings( +Future> getUserSettings( oauth2.Client client, String sessionCookie, ) async { @@ -45,7 +45,7 @@ Future getUserSettings( } final fields = flowJson['fields'] as List; - var userSettingsObj = {}; + Map userSettingsObj = {}; for (var field in fields) { if (field['field_key'] == null || field['initial_value'] == null) { throw Exception( diff --git a/app/lib/main.dart b/app/lib/main.dart index 744bc68..edf8492 100644 --- a/app/lib/main.dart +++ b/app/lib/main.dart @@ -22,7 +22,6 @@ void main() { runApp( MultiProvider( providers: [ - ChangeNotifierProvider(create: (context) => SSHKeyList()), ChangeNotifierProvider( create: (context) => AuthentikUserSettingsChangeDialogState(), ), @@ -197,13 +196,18 @@ class _MyHomePageState extends State { ), Text('Current output:'), Consumer( - builder: (BuildContext context, SSHKeyList sshKeyList, Widget? child) { - return Text( - sshKeyList.keysToKeep.isEmpty - ? 'No keys selected to keep.' - : 'Keys selected to keep: ${sshKeyList.keysToKeep.join(', ')}', - ); - }, + builder: + ( + BuildContext context, + AuthentikUserSettingsChangeDialogState dialogState, + Widget? child, + ) { + return Text( + dialogState.keysToKeep.isEmpty + ? 'No keys selected to keep.' + : 'Keys selected to keep: ${dialogState.keysToKeep.join(', ')}', + ); + }, ), Text(outputText, style: Theme.of(context).textTheme.headlineMedium), // Generated code for this Text Widget... @@ -306,15 +310,11 @@ class _MyHomePageState extends State { children = [Text("Loading...")]; case AuthentikUserSettingsChangeDialogStatus.userSettingsObtained: title = Text("User Settings Obtained"); - children = [ - Text("You can now edit your user settings."), - ElevatedButton( - onPressed: () async { - await authentikApiState.save(); - }, - child: Text("Save User Settings"), - ), - ]; + children = [Text("You can now edit your user settings.")]; + for (var key in authentikApiState.allKeys) { + children.add(KeepOrDeleteKey(sshKey: key)); + } + actions.add( TextButton( onPressed: () { @@ -359,11 +359,17 @@ class AuthentikUserSettingsChangeDialogState extends ChangeNotifier { oauth2.Client? oauthClient; String? sessionCookie; - Object? userSettings; + Map? userSettings; HttpServer? server; AuthentikUserSettingsChangeDialogStatus get status => _status; + List _allKeys = []; + List _keysToKeep = []; + + List get allKeys => _allKeys; + List get keysToKeep => _keysToKeep; + Future getOAuth2Client() async { // This is a placeholder for OAuth2 client initialization. // Replace with your actual OAuth2 client setup. @@ -465,6 +471,21 @@ class AuthentikUserSettingsChangeDialogState extends ChangeNotifier { } _status = AuthentikUserSettingsChangeDialogStatus.userSettingsObtained; + + if (userSettings != null && + userSettings!['attributes.sshPublicKeys'] != null) { + // If we have SSH keys, add them to the list of all keys + final sshKeysString = userSettings!['attributes.sshPublicKeys'] as String; + _allKeys = sshKeysString + .split('\n') + .where((key) => key.isNotEmpty) + .toList(); + } else { + throw Exception( + "Expected 'attributes.sshPublicKeys' in user settings, but got: $userSettings", + ); + } + notifyListeners(); } @@ -494,6 +515,8 @@ class AuthentikUserSettingsChangeDialogState extends ChangeNotifier { throw Exception("Cannot save, not started or no settings obtained."); } + userSettings!['attributes.sshPublicKeys'] = _keysToKeep.join('\n'); + _status = AuthentikUserSettingsChangeDialogStatus.savingUserSettings; notifyListeners(); @@ -505,6 +528,29 @@ class AuthentikUserSettingsChangeDialogState extends ChangeNotifier { exit(); } + void keepKey(String key) { + if (_keysToKeep.contains(key)) { + // If the key is already in the list, do nothing + return; + } + _keysToKeep.add(key); + notifyListeners(); + } + + void selectKeyForDeletion(String key) { + _keysToKeep.remove(key); + notifyListeners(); + } + + bool isKeySelectedToKeep(String key) { + return _keysToKeep.contains(key); + } + + void clearKeys() { + _keysToKeep.clear(); + notifyListeners(); + } + // Future doOAuth1() async { // final client = await getOAuth2Client(); @@ -536,37 +582,6 @@ class AuthentikUserSettingsChangeDialogState extends ChangeNotifier { // } } -class SSHKeyList extends ChangeNotifier { - final List _allKeys = []; - final List _keysToKeep = []; - - List get allKeys => _allKeys; - List get keysToKeep => _keysToKeep; - - void keepKey(String key) { - if (_keysToKeep.contains(key)) { - // If the key is already in the list, do nothing - return; - } - _keysToKeep.add(key); - notifyListeners(); - } - - void selectKeyForDeletion(String key) { - _keysToKeep.remove(key); - notifyListeners(); - } - - bool isKeySelectedToKeep(String key) { - return _keysToKeep.contains(key); - } - - void clearKeys() { - _keysToKeep.clear(); - notifyListeners(); - } -} - class KeepOrDeleteKey extends StatelessWidget { final String sshKey; @@ -575,9 +590,10 @@ class KeepOrDeleteKey extends StatelessWidget { @override Widget build(BuildContext context) { final key = sshKey; - final isKeySelectedToKeep = context.select( - (keylist) => keylist.isKeySelectedToKeep(key), - ); + final isKeySelectedToKeep = context + .select( + (keylist) => keylist.isKeySelectedToKeep(key), + ); final buttonStyleKeepActive = ButtonStyle( backgroundColor: MaterialStateProperty.all(Colors.blue), @@ -625,7 +641,7 @@ class KeepOrDeleteKey extends StatelessWidget { children: [ TextButton( onPressed: () { - Provider.of( + Provider.of( context, listen: false, ).selectKeyForDeletion(sshKey); @@ -635,7 +651,7 @@ class KeepOrDeleteKey extends StatelessWidget { ), TextButton( onPressed: () { - Provider.of( + Provider.of( context, listen: false, ).keepKey(sshKey);