Get ssh and set states to widgets
This commit is contained in:
parent
b4b0291540
commit
861ea5ab50
@ -27,7 +27,7 @@ Future<String> getSessionCookie(oauth2.Client client) async {
|
|||||||
return sessionCookie;
|
return sessionCookie;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<Object> getUserSettings(
|
Future<Map<String, dynamic>> getUserSettings(
|
||||||
oauth2.Client client,
|
oauth2.Client client,
|
||||||
String sessionCookie,
|
String sessionCookie,
|
||||||
) async {
|
) async {
|
||||||
@ -45,7 +45,7 @@ Future<Object> getUserSettings(
|
|||||||
}
|
}
|
||||||
|
|
||||||
final fields = flowJson['fields'] as List<dynamic>;
|
final fields = flowJson['fields'] as List<dynamic>;
|
||||||
var userSettingsObj = {};
|
Map<String, dynamic> userSettingsObj = {};
|
||||||
for (var field in fields) {
|
for (var field in fields) {
|
||||||
if (field['field_key'] == null || field['initial_value'] == null) {
|
if (field['field_key'] == null || field['initial_value'] == null) {
|
||||||
throw Exception(
|
throw Exception(
|
||||||
|
@ -22,7 +22,6 @@ void main() {
|
|||||||
runApp(
|
runApp(
|
||||||
MultiProvider(
|
MultiProvider(
|
||||||
providers: [
|
providers: [
|
||||||
ChangeNotifierProvider(create: (context) => SSHKeyList()),
|
|
||||||
ChangeNotifierProvider(
|
ChangeNotifierProvider(
|
||||||
create: (context) => AuthentikUserSettingsChangeDialogState(),
|
create: (context) => AuthentikUserSettingsChangeDialogState(),
|
||||||
),
|
),
|
||||||
@ -197,13 +196,18 @@ class _MyHomePageState extends State<MyHomePage> {
|
|||||||
),
|
),
|
||||||
Text('Current output:'),
|
Text('Current output:'),
|
||||||
Consumer(
|
Consumer(
|
||||||
builder: (BuildContext context, SSHKeyList sshKeyList, Widget? child) {
|
builder:
|
||||||
return Text(
|
(
|
||||||
sshKeyList.keysToKeep.isEmpty
|
BuildContext context,
|
||||||
? 'No keys selected to keep.'
|
AuthentikUserSettingsChangeDialogState dialogState,
|
||||||
: 'Keys selected to keep: ${sshKeyList.keysToKeep.join(', ')}',
|
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),
|
Text(outputText, style: Theme.of(context).textTheme.headlineMedium),
|
||||||
// Generated code for this Text Widget...
|
// Generated code for this Text Widget...
|
||||||
@ -306,15 +310,11 @@ class _MyHomePageState extends State<MyHomePage> {
|
|||||||
children = [Text("Loading...")];
|
children = [Text("Loading...")];
|
||||||
case AuthentikUserSettingsChangeDialogStatus.userSettingsObtained:
|
case AuthentikUserSettingsChangeDialogStatus.userSettingsObtained:
|
||||||
title = Text("User Settings Obtained");
|
title = Text("User Settings Obtained");
|
||||||
children = [
|
children = [Text("You can now edit your user settings.")];
|
||||||
Text("You can now edit your user settings."),
|
for (var key in authentikApiState.allKeys) {
|
||||||
ElevatedButton(
|
children.add(KeepOrDeleteKey(sshKey: key));
|
||||||
onPressed: () async {
|
}
|
||||||
await authentikApiState.save();
|
|
||||||
},
|
|
||||||
child: Text("Save User Settings"),
|
|
||||||
),
|
|
||||||
];
|
|
||||||
actions.add(
|
actions.add(
|
||||||
TextButton(
|
TextButton(
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
@ -359,11 +359,17 @@ class AuthentikUserSettingsChangeDialogState extends ChangeNotifier {
|
|||||||
|
|
||||||
oauth2.Client? oauthClient;
|
oauth2.Client? oauthClient;
|
||||||
String? sessionCookie;
|
String? sessionCookie;
|
||||||
Object? userSettings;
|
Map<String, dynamic>? userSettings;
|
||||||
HttpServer? server;
|
HttpServer? server;
|
||||||
|
|
||||||
AuthentikUserSettingsChangeDialogStatus get status => _status;
|
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 {
|
Future<oauth2.Client?> getOAuth2Client() async {
|
||||||
// This is a placeholder for OAuth2 client initialization.
|
// This is a placeholder for OAuth2 client initialization.
|
||||||
// Replace with your actual OAuth2 client setup.
|
// Replace with your actual OAuth2 client setup.
|
||||||
@ -465,6 +471,21 @@ class AuthentikUserSettingsChangeDialogState extends ChangeNotifier {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_status = AuthentikUserSettingsChangeDialogStatus.userSettingsObtained;
|
_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();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -494,6 +515,8 @@ class AuthentikUserSettingsChangeDialogState extends ChangeNotifier {
|
|||||||
throw Exception("Cannot save, not started or no settings obtained.");
|
throw Exception("Cannot save, not started or no settings obtained.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
userSettings!['attributes.sshPublicKeys'] = _keysToKeep.join('\n');
|
||||||
|
|
||||||
_status = AuthentikUserSettingsChangeDialogStatus.savingUserSettings;
|
_status = AuthentikUserSettingsChangeDialogStatus.savingUserSettings;
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
|
|
||||||
@ -505,6 +528,29 @@ class AuthentikUserSettingsChangeDialogState extends ChangeNotifier {
|
|||||||
exit();
|
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 {
|
// Future<void> doOAuth1() async {
|
||||||
// final client = await getOAuth2Client();
|
// 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 {
|
class KeepOrDeleteKey extends StatelessWidget {
|
||||||
final String sshKey;
|
final String sshKey;
|
||||||
|
|
||||||
@ -575,9 +590,10 @@ class KeepOrDeleteKey extends StatelessWidget {
|
|||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final key = sshKey;
|
final key = sshKey;
|
||||||
final isKeySelectedToKeep = context.select<SSHKeyList, bool>(
|
final isKeySelectedToKeep = context
|
||||||
(keylist) => keylist.isKeySelectedToKeep(key),
|
.select<AuthentikUserSettingsChangeDialogState, bool>(
|
||||||
);
|
(keylist) => keylist.isKeySelectedToKeep(key),
|
||||||
|
);
|
||||||
|
|
||||||
final buttonStyleKeepActive = ButtonStyle(
|
final buttonStyleKeepActive = ButtonStyle(
|
||||||
backgroundColor: MaterialStateProperty.all(Colors.blue),
|
backgroundColor: MaterialStateProperty.all(Colors.blue),
|
||||||
@ -625,7 +641,7 @@ class KeepOrDeleteKey extends StatelessWidget {
|
|||||||
children: [
|
children: [
|
||||||
TextButton(
|
TextButton(
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
Provider.of<SSHKeyList>(
|
Provider.of<AuthentikUserSettingsChangeDialogState>(
|
||||||
context,
|
context,
|
||||||
listen: false,
|
listen: false,
|
||||||
).selectKeyForDeletion(sshKey);
|
).selectKeyForDeletion(sshKey);
|
||||||
@ -635,7 +651,7 @@ class KeepOrDeleteKey extends StatelessWidget {
|
|||||||
),
|
),
|
||||||
TextButton(
|
TextButton(
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
Provider.of<SSHKeyList>(
|
Provider.of<AuthentikUserSettingsChangeDialogState>(
|
||||||
context,
|
context,
|
||||||
listen: false,
|
listen: false,
|
||||||
).keepKey(sshKey);
|
).keepKey(sshKey);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user