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:
@@ -21,7 +21,14 @@ Future<ChatUploadReply> postChatUpload(
|
||||
required String name,
|
||||
required String mime,
|
||||
required Uint8List bytes,
|
||||
}) => platform.postChatUpload(uri, name: name, mime: mime, bytes: bytes);
|
||||
Map<String, String> headers = const {},
|
||||
}) => platform.postChatUpload(
|
||||
uri,
|
||||
name: name,
|
||||
mime: mime,
|
||||
bytes: bytes,
|
||||
headers: headers,
|
||||
);
|
||||
|
||||
/// The header that carries the file name; the server refuses a post without it.
|
||||
const chatUploadNameHeader = 'X-Pipelines-Upload-Name';
|
||||
|
||||
@@ -10,6 +10,7 @@ Future<ChatUploadReply> postChatUpload(
|
||||
required String name,
|
||||
required String mime,
|
||||
required Uint8List bytes,
|
||||
Map<String, String> headers = const {},
|
||||
}) async {
|
||||
final client = HttpClient()..connectionTimeout = const Duration(seconds: 15);
|
||||
try {
|
||||
@@ -21,6 +22,7 @@ Future<ChatUploadReply> postChatUpload(
|
||||
..set(chatUploadNameHeader, Uri.encodeComponent(name))
|
||||
..set(HttpHeaders.contentTypeHeader, mime)
|
||||
..contentLength = bytes.length;
|
||||
headers.forEach(request.headers.set);
|
||||
request.add(bytes);
|
||||
final response = await request.close();
|
||||
final body = await response.transform(utf8.decoder).join();
|
||||
|
||||
@@ -7,4 +7,5 @@ Future<ChatUploadReply> postChatUpload(
|
||||
required String name,
|
||||
required String mime,
|
||||
required Uint8List bytes,
|
||||
Map<String, String> headers = const {},
|
||||
}) => throw UnsupportedError('Uploads are not supported on this platform.');
|
||||
|
||||
@@ -28,6 +28,7 @@ Future<ChatUploadReply> postChatUpload(
|
||||
required String name,
|
||||
required String mime,
|
||||
required Uint8List bytes,
|
||||
Map<String, String> headers = const {},
|
||||
}) async {
|
||||
final response = await _fetch(
|
||||
uri.toString().toJS,
|
||||
@@ -36,6 +37,7 @@ Future<ChatUploadReply> postChatUpload(
|
||||
headers: {
|
||||
chatUploadNameHeader: Uri.encodeComponent(name),
|
||||
'Content-Type': mime,
|
||||
...headers,
|
||||
}.jsify()!,
|
||||
body: bytes.toJS,
|
||||
credentials: 'same-origin'.toJS,
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import '../../../core/api_gateway.dart';
|
||||
import '../../../core/servers/elevation/elevating_gateway.dart';
|
||||
import '../../../core/transport/pipelines_client.dart';
|
||||
import '../data/directory_listing.dart';
|
||||
import '../domain/chat_attachment_picker.dart';
|
||||
@@ -113,7 +114,12 @@ class FilesController extends ChangeNotifier {
|
||||
final failures = <String>[];
|
||||
for (final file in files) {
|
||||
try {
|
||||
await uploadFileInto(endpoint, directory, file);
|
||||
await uploadFileInto(
|
||||
endpoint,
|
||||
directory,
|
||||
file,
|
||||
elevation: elevationOf(gateway),
|
||||
);
|
||||
} on FilesUploadException catch (error) {
|
||||
failures.add(error.message);
|
||||
} finally {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import '../../../core/servers/elevation/machine_elevation.dart';
|
||||
import '../../../core/transport/server_url.dart';
|
||||
import '../data/upload/chat_upload.dart';
|
||||
import '../domain/chat_attachment_picker.dart';
|
||||
@@ -12,6 +13,7 @@ typedef FilesUploadPost =
|
||||
required String name,
|
||||
required String mime,
|
||||
required Uint8List bytes,
|
||||
Map<String, String> headers,
|
||||
});
|
||||
|
||||
/// The transport the Files panel uploads with.
|
||||
@@ -33,11 +35,16 @@ class FilesUploadException implements Exception {
|
||||
/// the stored path the server reports. Every failure is a
|
||||
/// [FilesUploadException]; nothing is retried or renamed on the caller's
|
||||
/// behalf, so an existing file is reported rather than replaced.
|
||||
///
|
||||
/// [elevation] is the machine's step-up state: its token rides on the
|
||||
/// request as [elevationHeader], and a 403 `elevation_required` refusal asks
|
||||
/// for the password once and retries.
|
||||
Future<String> uploadFileInto(
|
||||
String endpoint,
|
||||
String directory,
|
||||
ChatPickedFile file,
|
||||
) async {
|
||||
ChatPickedFile file, {
|
||||
MachineElevation? elevation,
|
||||
}) async {
|
||||
final name = file.name.trim();
|
||||
if (name.isEmpty) {
|
||||
throw const FilesUploadException(
|
||||
@@ -56,16 +63,25 @@ Future<String> uploadFileInto(
|
||||
'No server is configured to upload files to.',
|
||||
);
|
||||
}
|
||||
final ChatUploadReply reply;
|
||||
try {
|
||||
reply = await postFileUpload(
|
||||
uri,
|
||||
name: name,
|
||||
mime: file.mime.trim().isEmpty ? 'application/octet-stream' : file.mime,
|
||||
bytes: file.bytes,
|
||||
);
|
||||
} catch (error) {
|
||||
throw FilesUploadException('"$name" could not be uploaded: $error');
|
||||
Future<ChatUploadReply> post() async {
|
||||
final token = elevation?.token;
|
||||
try {
|
||||
return await postFileUpload(
|
||||
uri,
|
||||
name: name,
|
||||
mime: file.mime.trim().isEmpty ? 'application/octet-stream' : file.mime,
|
||||
bytes: file.bytes,
|
||||
headers: {elevationHeader: ?token},
|
||||
);
|
||||
} catch (error) {
|
||||
throw FilesUploadException('"$name" could not be uploaded: $error');
|
||||
}
|
||||
}
|
||||
|
||||
var reply = await post();
|
||||
if (elevation != null && refusedForElevation(reply)) {
|
||||
elevation.clear();
|
||||
if (await elevation.ensure()) reply = await post();
|
||||
}
|
||||
final reason = reply.body.trim();
|
||||
switch (reply.status) {
|
||||
@@ -75,6 +91,10 @@ Future<String> uploadFileInto(
|
||||
throw const FilesUploadException(
|
||||
'Sign in to upload files (Settings → Sign in).',
|
||||
);
|
||||
case 403 when refusedForElevation(reply):
|
||||
throw FilesUploadException(
|
||||
'"$name" was not uploaded: confirm the password to change files.',
|
||||
);
|
||||
case 409:
|
||||
throw FilesUploadException('"$name" already exists in this folder.');
|
||||
case 413:
|
||||
@@ -97,3 +117,17 @@ Future<String> uploadFileInto(
|
||||
}
|
||||
throw FilesUploadException('"$name" upload returned no stored path.');
|
||||
}
|
||||
|
||||
/// The request header carrying a confirmed connection's elevation token.
|
||||
const elevationHeader = 'X-Pipelines-Elevation';
|
||||
|
||||
/// Whether [reply] is the server's 403 step-up refusal.
|
||||
bool refusedForElevation(ChatUploadReply reply) {
|
||||
if (reply.status != 403) return false;
|
||||
try {
|
||||
final decoded = jsonDecode(reply.body);
|
||||
return decoded is Map && decoded['code'] == elevationRequiredCode;
|
||||
} on FormatException {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import '../../../core/session/session_probe.dart';
|
||||
import 'session_security_http_stub.dart'
|
||||
if (dart.library.io) 'session_security_http_io.dart'
|
||||
if (dart.library.js_interop) 'session_security_http_web.dart'
|
||||
as platform;
|
||||
|
||||
/// One answer from the web proxy's session endpoints.
|
||||
typedef SecurityReply = ({int status, String body});
|
||||
|
||||
/// Sends one request to the web proxy with this device's session: the kept
|
||||
/// cookie on native, the browser's same-origin cookie on the web.
|
||||
typedef SecurityRequest =
|
||||
Future<SecurityReply> Function(String method, Uri uri);
|
||||
|
||||
/// The transport the section uses; widget tests swap in a recorder.
|
||||
SecurityRequest sendSecurityRequest = platform.sendSecurityRequest;
|
||||
|
||||
/// Leaves the web app for the sign-in page after every session was revoked.
|
||||
/// A no-op on native, where the app forgets its kept cookie instead.
|
||||
void Function() openWebSignIn = platform.openWebSignIn;
|
||||
|
||||
/// What the proxy reports about the sign-in itself.
|
||||
class SessionSecurity {
|
||||
const SessionSecurity({required this.weakPassword, required this.rule});
|
||||
|
||||
/// The current password predates the strength rule.
|
||||
final bool weakPassword;
|
||||
|
||||
/// The rule for new passwords, in the proxy's words.
|
||||
final String rule;
|
||||
}
|
||||
|
||||
/// Why the session endpoints could not be used, in words Settings shows.
|
||||
class SessionSecurityException implements Exception {
|
||||
const SessionSecurityException(this.message);
|
||||
final String message;
|
||||
@override
|
||||
String toString() => message;
|
||||
}
|
||||
|
||||
/// The proxy path [path] beside the websocket [endpoint].
|
||||
Uri? securityUri(String endpoint, String path) {
|
||||
final parsed = Uri.tryParse(endpoint.trim());
|
||||
if (parsed == null) return null;
|
||||
return loginUriFor(parsed)?.replace(path: path);
|
||||
}
|
||||
|
||||
/// Reads GET /api/session.
|
||||
Future<SessionSecurity> loadSessionSecurity(String endpoint) async {
|
||||
final uri = securityUri(endpoint, '/api/session');
|
||||
if (uri == null) {
|
||||
throw const SessionSecurityException('Set the server address first.');
|
||||
}
|
||||
final reply = await _send('GET', uri);
|
||||
if (reply.status == 401) {
|
||||
throw const SessionSecurityException(
|
||||
'Sign in to see this server\'s sign-in security.',
|
||||
);
|
||||
}
|
||||
if (reply.status == 404) {
|
||||
throw const SessionSecurityException(
|
||||
'This server\'s web login does not report sign-in security.',
|
||||
);
|
||||
}
|
||||
final decoded = _decode(reply);
|
||||
return SessionSecurity(
|
||||
weakPassword: decoded['weak_password'] == true,
|
||||
rule: '${decoded['password_rule'] ?? ''}'.trim(),
|
||||
);
|
||||
}
|
||||
|
||||
/// Sends POST /api/sessions/revoke-all: every device signs in again.
|
||||
Future<void> revokeAllSessions(String endpoint) async {
|
||||
final uri = securityUri(endpoint, '/api/sessions/revoke-all');
|
||||
if (uri == null) {
|
||||
throw const SessionSecurityException('Set the server address first.');
|
||||
}
|
||||
final reply = await _send('POST', uri);
|
||||
if (reply.status == 401) {
|
||||
throw const SessionSecurityException('Already signed out.');
|
||||
}
|
||||
if (_decode(reply)['revoked'] != true) {
|
||||
throw const SessionSecurityException(
|
||||
'The server did not confirm the sign-out.',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<SecurityReply> _send(String method, Uri uri) async {
|
||||
try {
|
||||
return await sendSecurityRequest(method, uri);
|
||||
} catch (error) {
|
||||
throw SessionSecurityException('Could not reach the web login: $error');
|
||||
}
|
||||
}
|
||||
|
||||
Map<String, dynamic> _decode(SecurityReply reply) {
|
||||
if (reply.status != 200) {
|
||||
throw SessionSecurityException(
|
||||
'The web login answered HTTP ${reply.status}.',
|
||||
);
|
||||
}
|
||||
try {
|
||||
final decoded = jsonDecode(reply.body);
|
||||
if (decoded is Map<String, dynamic>) return decoded;
|
||||
} on FormatException {
|
||||
// Reported below.
|
||||
}
|
||||
throw const SessionSecurityException(
|
||||
'The web login sent an unreadable reply.',
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import '../../../core/session/server_session.dart';
|
||||
import 'session_security_api.dart';
|
||||
|
||||
/// Native: the kept session cookie, only for the selected server's origin.
|
||||
Future<SecurityReply> sendSecurityRequest(String method, Uri uri) async {
|
||||
final client = HttpClient()..connectionTimeout = const Duration(seconds: 10);
|
||||
try {
|
||||
final request = await client.openUrl(method, uri);
|
||||
request.followRedirects = false;
|
||||
ServerSession.headersFor(uri).forEach(request.headers.set);
|
||||
final response = await request.close().timeout(const Duration(seconds: 15));
|
||||
final body = await response.transform(utf8.decoder).join();
|
||||
return (status: response.statusCode, body: body);
|
||||
} finally {
|
||||
client.close(force: true);
|
||||
}
|
||||
}
|
||||
|
||||
void openWebSignIn() {}
|
||||
@@ -0,0 +1,6 @@
|
||||
import 'session_security_api.dart';
|
||||
|
||||
Future<SecurityReply> sendSecurityRequest(String method, Uri uri) =>
|
||||
throw UnsupportedError('Not supported on this platform.');
|
||||
|
||||
void openWebSignIn() {}
|
||||
@@ -0,0 +1,23 @@
|
||||
import 'dart:js_interop';
|
||||
|
||||
import 'package:web/web.dart' as web;
|
||||
|
||||
import 'session_security_api.dart';
|
||||
|
||||
/// Web: the browser sends the sign-in cookie to the page's own origin only.
|
||||
Future<SecurityReply> sendSecurityRequest(String method, Uri uri) async {
|
||||
final response = await web.window
|
||||
.fetch(
|
||||
uri.toString().toJS,
|
||||
web.RequestInit(
|
||||
method: method,
|
||||
credentials: 'same-origin',
|
||||
redirect: 'manual',
|
||||
),
|
||||
)
|
||||
.toDart;
|
||||
final body = (await response.text().toDart).toDart;
|
||||
return (status: response.status, body: body);
|
||||
}
|
||||
|
||||
void openWebSignIn() => web.window.location.assign('/login');
|
||||
@@ -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!)),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ import '../terminal/terminal_layout.dart';
|
||||
import 'settings_terminal_section.dart';
|
||||
import '../../ui/adaptive/segmented_bar.dart';
|
||||
import 'dart:async';
|
||||
import 'package:flutter/foundation.dart' show kIsWeb;
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
@@ -31,6 +32,7 @@ import 'settings_android_download_section.dart';
|
||||
import 'settings_schedules_section.dart';
|
||||
import 'settings_sections.dart';
|
||||
import 'settings_sign_in_section.dart';
|
||||
import 'security/settings_security_section.dart';
|
||||
import 'settings_updates_section.dart';
|
||||
import '../updates/app_updates.dart';
|
||||
import 'settings_store.dart';
|
||||
|
||||
@@ -40,6 +40,14 @@ extension _SettingsTopicPages on _SettingsScreenState {
|
||||
onChanged: _reconnectAfterSignIn,
|
||||
),
|
||||
),
|
||||
if (widget.showSignIn || kIsWeb)
|
||||
_single(
|
||||
'Sign-in security',
|
||||
SettingsSecuritySection(
|
||||
endpoint: _endpoint,
|
||||
onSignedOut: _reconnectAfterSignIn,
|
||||
),
|
||||
),
|
||||
SettingsWorkspaceSection(
|
||||
controller: _controller,
|
||||
workspaceField: _workspaceField,
|
||||
|
||||
Reference in New Issue
Block a user