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,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.',
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user