Ask for the password once and retry when a machine requires elevation
Carry the protocol error code, wrap each machine's gateway so an elevation_required refusal shows one native password dialog, elevates that connection and retries; send the upload token with file uploads; add a Settings sign-in security section with the weak-password warning and sign out everywhere; accept the __Host- session cookie.
This commit is contained in:
@@ -0,0 +1,185 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../core/session/server_session.dart';
|
||||
import '../../../ui/adaptive/host.dart';
|
||||
import 'session_security_api.dart';
|
||||
|
||||
/// The server's sign-in security: a warning when its password predates the
|
||||
/// strength rule, and "Sign out everywhere", which revokes every session of
|
||||
/// this server's web login (this device included).
|
||||
class SettingsSecuritySection extends StatefulWidget {
|
||||
const SettingsSecuritySection({
|
||||
super.key,
|
||||
required this.endpoint,
|
||||
required this.onSignedOut,
|
||||
this.web = kIsWeb,
|
||||
});
|
||||
|
||||
/// The server's websocket address; the proxy endpoints sit beside it.
|
||||
final String endpoint;
|
||||
|
||||
/// Reconnects once this device's session was revoked (native).
|
||||
final VoidCallback onSignedOut;
|
||||
|
||||
/// The web app leaves for the sign-in page instead.
|
||||
final bool web;
|
||||
|
||||
@override
|
||||
State<SettingsSecuritySection> createState() =>
|
||||
_SettingsSecuritySectionState();
|
||||
}
|
||||
|
||||
class _SettingsSecuritySectionState extends State<SettingsSecuritySection> {
|
||||
SessionSecurity? _security;
|
||||
String? _message;
|
||||
bool _busy = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_load();
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(SettingsSecuritySection oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (oldWidget.endpoint != widget.endpoint) _load();
|
||||
}
|
||||
|
||||
Future<void> _load() async {
|
||||
final endpoint = widget.endpoint;
|
||||
try {
|
||||
final security = await loadSessionSecurity(endpoint);
|
||||
if (!mounted || endpoint != widget.endpoint) return;
|
||||
setState(() {
|
||||
_security = security;
|
||||
_message = null;
|
||||
});
|
||||
} on SessionSecurityException catch (error) {
|
||||
if (!mounted || endpoint != widget.endpoint) return;
|
||||
setState(() {
|
||||
_security = null;
|
||||
_message = error.message;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> _confirm() async {
|
||||
const title = Text('Sign out everywhere?');
|
||||
const body = Text(
|
||||
'Every browser and device signed in to this server, this one '
|
||||
'included, must sign in again. Open connections close now.',
|
||||
);
|
||||
if (usesCupertinoHost(context)) {
|
||||
return await showCupertinoDialog<bool>(
|
||||
context: context,
|
||||
builder: (context) => CupertinoAlertDialog(
|
||||
title: title,
|
||||
content: body,
|
||||
actions: [
|
||||
CupertinoDialogAction(
|
||||
onPressed: () => Navigator.pop(context, false),
|
||||
child: const Text('Cancel'),
|
||||
),
|
||||
CupertinoDialogAction(
|
||||
key: const Key('settings-revoke-all-confirm'),
|
||||
isDestructiveAction: true,
|
||||
onPressed: () => Navigator.pop(context, true),
|
||||
child: const Text('Sign out everywhere'),
|
||||
),
|
||||
],
|
||||
),
|
||||
) ??
|
||||
false;
|
||||
}
|
||||
return await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: title,
|
||||
content: body,
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context, false),
|
||||
child: const Text('Cancel'),
|
||||
),
|
||||
FilledButton(
|
||||
key: const Key('settings-revoke-all-confirm'),
|
||||
onPressed: () => Navigator.pop(context, true),
|
||||
child: const Text('Sign out everywhere'),
|
||||
),
|
||||
],
|
||||
),
|
||||
) ??
|
||||
false;
|
||||
}
|
||||
|
||||
Future<void> _revokeAll() async {
|
||||
if (!await _confirm() || !mounted) return;
|
||||
setState(() {
|
||||
_busy = true;
|
||||
_message = null;
|
||||
});
|
||||
try {
|
||||
await revokeAllSessions(widget.endpoint);
|
||||
} on SessionSecurityException catch (error) {
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_busy = false;
|
||||
_message = error.message;
|
||||
});
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (widget.web) {
|
||||
openWebSignIn();
|
||||
return;
|
||||
}
|
||||
await ServerSession.signOut();
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_busy = false;
|
||||
_message = 'Signed out everywhere. Sign in again to reconnect.';
|
||||
});
|
||||
widget.onSignedOut();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final security = _security;
|
||||
final button = usesCupertinoHost(context)
|
||||
? CupertinoButton(
|
||||
key: const Key('settings-revoke-all'),
|
||||
onPressed: _busy ? null : _revokeAll,
|
||||
child: const Text('Sign out everywhere'),
|
||||
)
|
||||
: OutlinedButton(
|
||||
key: const Key('settings-revoke-all'),
|
||||
onPressed: _busy ? null : _revokeAll,
|
||||
child: const Text('Sign out everywhere'),
|
||||
);
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
if (security != null && security.weakPassword)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(bottom: 8),
|
||||
child: Text(
|
||||
key: const Key('settings-weak-password'),
|
||||
'This server\'s sign-in password is weak. '
|
||||
'${security.rule.isEmpty ? '' : '${security.rule} '}'
|
||||
'Run ./password on the server to choose a stronger one.',
|
||||
),
|
||||
),
|
||||
const Text(
|
||||
'Signing out everywhere ends every session of this server\'s web '
|
||||
'login. Changing the password with ./password does the same.',
|
||||
),
|
||||
Align(alignment: AlignmentDirectional.centerStart, child: button),
|
||||
if (_message != null)
|
||||
Semantics(liveRegion: true, child: Text(_message!)),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user