feat(app): the native app signs in at the web login and carries the session on its socket and downloads

This commit is contained in:
2026-10-23 19:51:43 +01:00
parent d6fbea1665
commit 2fc0a4394b
13 changed files with 335 additions and 1 deletions
+80
View File
@@ -0,0 +1,80 @@
import 'package:shared_preferences/shared_preferences.dart';
import 'server_session_stub.dart'
if (dart.library.io) 'server_session_io.dart'
as impl;
/// The web sign-in the app carries on a phone.
///
/// A browser keeps the server's login cookie itself. The native app signs in
/// with the same password, keeps the returned session cookie, and sends it on
/// the websocket and on file downloads, so it can reach a server that sits
/// behind the web login (for example over Tailscale). A server with no login
/// needs none of this.
class ServerSession {
ServerSession._();
static const _key = 'pipelines.session.cookie';
/// The `name=value` session cookie, or null when signed out.
static String? cookie;
/// Headers that carry the session, empty when signed out.
static Map<String, String> get headers {
final value = cookie;
return value == null || value.isEmpty ? const {} : {'Cookie': value};
}
static Future<void> load() async {
try {
final prefs = await SharedPreferences.getInstance();
cookie = prefs.getString(_key);
} catch (_) {
cookie = null;
}
}
static Future<void> _save(String? value) async {
cookie = value;
try {
final prefs = await SharedPreferences.getInstance();
if (value == null) {
await prefs.remove(_key);
} else {
await prefs.setString(_key, value);
}
} catch (_) {}
}
/// Signs in at the web login beside [endpoint] (its `/login` page) with
/// [password] and keeps the session. Returns null on success, otherwise
/// what went wrong in words.
static Future<String?> signIn(Uri endpoint, String password) async {
final login = loginUriFor(endpoint);
if (login == null) return 'The server address has no web login.';
final result = await impl.postLogin(login, password);
if (result.cookie != null) {
await _save(result.cookie);
return null;
}
return result.error ?? 'Sign-in failed.';
}
static Future<void> signOut() => _save(null);
}
/// The web login page for a websocket endpoint: same host and port, http(s).
Uri? loginUriFor(Uri endpoint) {
final scheme = switch (endpoint.scheme) {
'wss' || 'https' => 'https',
'ws' || 'http' => 'http',
_ => null,
};
if (scheme == null || endpoint.host.isEmpty) return null;
return Uri(
scheme: scheme,
host: endpoint.host,
port: endpoint.hasPort ? endpoint.port : null,
path: '/login',
);
}
@@ -0,0 +1,42 @@
import 'dart:io';
/// Posts the password to the web login and returns its session cookie.
Future<({String? cookie, String? error})> postLogin(
Uri login,
String password,
) async {
final client = HttpClient()..connectionTimeout = const Duration(seconds: 15);
try {
final request = await client.postUrl(login);
request.followRedirects = false;
request.headers.contentType = ContentType(
'application',
'x-www-form-urlencoded',
charset: 'utf-8',
);
request.write(
Uri(queryParameters: {'password': password, 'remember': 'on'}).query,
);
final response = await request.close();
await response.drain<void>();
for (final cookie in response.cookies) {
if (cookie.name == 'pipelines_session' && cookie.value.isNotEmpty) {
return (cookie: '${cookie.name}=${cookie.value}', error: null);
}
}
return (
cookie: null,
error: switch (response.statusCode) {
401 => 'Wrong password.',
429 => 'Too many attempts. Wait 30 seconds.',
_ => 'Sign-in failed (HTTP ${response.statusCode}).',
},
);
} on IOException catch (error) {
return (cookie: null, error: 'Could not reach the server: $error');
} on FormatException catch (error) {
return (cookie: null, error: error.message);
} finally {
client.close(force: true);
}
}
@@ -0,0 +1,5 @@
/// The browser keeps its own login cookie, so there is nothing to post here.
Future<({String? cookie, String? error})> postLogin(
Uri login,
String password,
) async => (cookie: null, error: 'Sign in through the browser page.');