Get ssh and set states to widgets

This commit is contained in:
lemoer 2025-07-05 20:30:51 +02:00
parent b4b0291540
commit 861ea5ab50
2 changed files with 72 additions and 56 deletions

View File

@ -27,7 +27,7 @@ Future<String> getSessionCookie(oauth2.Client client) async {
return sessionCookie;
}
Future<Object> getUserSettings(
Future<Map<String, dynamic>> getUserSettings(
oauth2.Client client,
String sessionCookie,
) async {
@ -45,7 +45,7 @@ Future<Object> getUserSettings(
}
final fields = flowJson['fields'] as List<dynamic>;
var userSettingsObj = {};
Map<String, dynamic> userSettingsObj = {};
for (var field in fields) {
if (field['field_key'] == null || field['initial_value'] == null) {
throw Exception(

View File

@ -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<MyHomePage> {
),
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<MyHomePage> {
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<String, dynamic>? userSettings;
HttpServer? server;
AuthentikUserSettingsChangeDialogStatus get status => _status;
List<String> _allKeys = [];
List<String> _keysToKeep = [];
List<String> get allKeys => _allKeys;
List<String> get keysToKeep => _keysToKeep;
Future<oauth2.Client?> 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<void> doOAuth1() async {
// final client = await getOAuth2Client();
@ -536,37 +582,6 @@ class AuthentikUserSettingsChangeDialogState extends ChangeNotifier {
// }
}
class SSHKeyList extends ChangeNotifier {
final List<String> _allKeys = [];
final List<String> _keysToKeep = [];
List<String> get allKeys => _allKeys;
List<String> 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<SSHKeyList, bool>(
(keylist) => keylist.isKeySelectedToKeep(key),
);
final isKeySelectedToKeep = context
.select<AuthentikUserSettingsChangeDialogState, bool>(
(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<SSHKeyList>(
Provider.of<AuthentikUserSettingsChangeDialogState>(
context,
listen: false,
).selectKeyForDeletion(sshKey);
@ -635,7 +651,7 @@ class KeepOrDeleteKey extends StatelessWidget {
),
TextButton(
onPressed: () {
Provider.of<SSHKeyList>(
Provider.of<AuthentikUserSettingsChangeDialogState>(
context,
listen: false,
).keepKey(sshKey);