merge: isolate credentials across saved servers
This commit is contained in:
@@ -15,6 +15,48 @@ class ServerSession {
|
||||
ServerSession._();
|
||||
|
||||
static const _key = 'pipelines.session.cookie';
|
||||
static String? _serverId;
|
||||
static Set<Uri> _origins = {};
|
||||
static int _generation = 0;
|
||||
static final Map<String, int> _versions = {};
|
||||
|
||||
static String? get serverId => _serverId;
|
||||
|
||||
static String _scopeKey(String id) =>
|
||||
'pipelines.session.v2.${Uri.encodeComponent(id)}.cookie';
|
||||
|
||||
/// Selects one logical server. Aliases explicitly trust the same credential.
|
||||
/// Clears the previous cookie synchronously, before loading this profile.
|
||||
static Future<void> activateServer({
|
||||
required String id,
|
||||
required Iterable<Uri> endpoints,
|
||||
bool migrateLegacy = false,
|
||||
}) async {
|
||||
final origins = endpoints.map(loginUriFor).toSet();
|
||||
if (id.isEmpty || origins.isEmpty || origins.contains(null)) {
|
||||
throw ArgumentError('A server needs an id and valid trusted endpoints.');
|
||||
}
|
||||
final generation = ++_generation;
|
||||
_serverId = id;
|
||||
_origins = origins.cast<Uri>();
|
||||
cookie = null;
|
||||
final key = _scopeKey(id);
|
||||
final version = _versions[key];
|
||||
try {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
if (_versions[key] != version) return;
|
||||
var value = prefs.getString(key);
|
||||
if (value == null && migrateLegacy && prefs.containsKey(_key)) {
|
||||
value = prefs.getString(_key) ?? '';
|
||||
await prefs.setString(key, value);
|
||||
}
|
||||
if (_generation == generation && _versions[key] == version) {
|
||||
cookie = value == null || value.isEmpty ? null : value;
|
||||
}
|
||||
} catch (_) {
|
||||
// The newly selected profile stays signed out if storage is unavailable.
|
||||
}
|
||||
}
|
||||
|
||||
/// The `name=value` session cookie, or null when signed out.
|
||||
static String? cookie;
|
||||
@@ -25,23 +67,44 @@ class ServerSession {
|
||||
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;
|
||||
/// Credentials are only sent to the selected profile's exact HTTP origins.
|
||||
/// Legacy callers bind their single origin by checking or signing in first.
|
||||
static Map<String, String> headersFor(Uri endpoint) {
|
||||
final origin = loginUriFor(endpoint);
|
||||
if (origin == null || !_origins.contains(origin)) {
|
||||
return const {};
|
||||
}
|
||||
return headers;
|
||||
}
|
||||
|
||||
static Future<void> _save(String? value) async {
|
||||
cookie = value;
|
||||
static Future<void> load() async {
|
||||
final generation = ++_generation;
|
||||
_serverId = null;
|
||||
_origins = {};
|
||||
cookie = null;
|
||||
try {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
if (value == null) {
|
||||
if (_generation == generation) cookie = prefs.getString(_key);
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
static Future<void> _save(
|
||||
String? value,
|
||||
String key,
|
||||
int generation,
|
||||
int version,
|
||||
) async {
|
||||
if (_versions[key] != version) return;
|
||||
if (_generation == generation) cookie = value;
|
||||
try {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
if (_versions[key] != version) return;
|
||||
if (value == null && key == _key) {
|
||||
await prefs.remove(_key);
|
||||
} else {
|
||||
await prefs.setString(_key, value);
|
||||
// An empty scoped value records an intentional sign-out and prevents
|
||||
// subsequent legacy migration from reviving the old credential.
|
||||
await prefs.setString(key, value ?? '');
|
||||
}
|
||||
} catch (_) {}
|
||||
}
|
||||
@@ -52,21 +115,37 @@ class ServerSession {
|
||||
static Future<String?> signIn(Uri endpoint, String password) async {
|
||||
final login = loginUriFor(endpoint);
|
||||
if (login == null) return 'The server address has no web login.';
|
||||
if (_serverId != null && !_origins.contains(login)) {
|
||||
return 'The server address is not a trusted alias of the selected server.';
|
||||
}
|
||||
final generation = _generation;
|
||||
final key = _serverId == null ? _key : _scopeKey(_serverId!);
|
||||
final version = (_versions[key] ?? 0) + 1;
|
||||
_versions[key] = version;
|
||||
final result = await impl.postLogin(login, password);
|
||||
if (result.cookie != null) {
|
||||
await _save(result.cookie);
|
||||
if (_serverId == null && _generation == generation) _origins = {login};
|
||||
await _save(result.cookie, key, generation, version);
|
||||
return null;
|
||||
}
|
||||
return result.error ?? 'Sign-in failed.';
|
||||
}
|
||||
|
||||
static Future<void> signOut() => _save(null);
|
||||
static Future<void> signOut() {
|
||||
final key = _serverId == null ? _key : _scopeKey(_serverId!);
|
||||
final version = (_versions[key] ?? 0) + 1;
|
||||
_versions[key] = version;
|
||||
return _save(null, key, _generation, version);
|
||||
}
|
||||
|
||||
/// Whether [endpoint]'s server accepts the kept session.
|
||||
static Future<SessionState> check(Uri endpoint) async {
|
||||
final login = loginUriFor(endpoint);
|
||||
if (login == null) return SessionState.unreachable;
|
||||
final status = await impl.probeLogin(login, headers);
|
||||
// A pre-profile client supplies its sole server through this API. Bind
|
||||
// once, not on every probe, so probing another host cannot leak its cookie.
|
||||
if (_serverId == null && _origins.isEmpty) _origins = {login};
|
||||
final status = await impl.probeLogin(login, headersFor(login));
|
||||
if (status == null) return SessionState.unreachable;
|
||||
// The web login sends a signed-in visitor on; it shows its form otherwise.
|
||||
if (status >= 300 && status < 400) return SessionState.signedIn;
|
||||
@@ -85,7 +164,9 @@ Uri? loginUriFor(Uri endpoint) {
|
||||
'ws' || 'http' => 'http',
|
||||
_ => null,
|
||||
};
|
||||
if (scheme == null || endpoint.host.isEmpty) return null;
|
||||
if (scheme == null || endpoint.host.isEmpty || endpoint.userInfo.isNotEmpty) {
|
||||
return null;
|
||||
}
|
||||
return Uri(
|
||||
scheme: scheme,
|
||||
host: endpoint.host,
|
||||
|
||||
@@ -1,8 +1,37 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:web_socket_channel/io.dart';
|
||||
import 'package:web_socket_channel/web_socket_channel.dart';
|
||||
|
||||
import '../session/server_session.dart';
|
||||
|
||||
/// A native socket sends the kept session cookie with the upgrade.
|
||||
WebSocketChannel connect(Uri uri) =>
|
||||
IOWebSocketChannel.connect(uri, headers: ServerSession.headers);
|
||||
WebSocketChannel connect(Uri uri) {
|
||||
final client = _HandshakeClient();
|
||||
final socket = WebSocket.connect(
|
||||
uri.toString(),
|
||||
headers: ServerSession.headersFor(uri),
|
||||
customClient: client,
|
||||
).whenComplete(client.close);
|
||||
return IOWebSocketChannel(socket);
|
||||
}
|
||||
|
||||
/// WebSocket.connect only uses openUrl on its custom client. Suppress automatic
|
||||
/// redirects: the SDK's default redirect policy can forward cookies to another
|
||||
/// port or a subdomain, neither of which is necessarily a trusted alias.
|
||||
class _HandshakeClient implements HttpClient {
|
||||
final _client = HttpClient();
|
||||
|
||||
@override
|
||||
Future<HttpClientRequest> openUrl(String method, Uri url) async {
|
||||
final request = await _client.openUrl(method, url);
|
||||
request.followRedirects = false;
|
||||
return request;
|
||||
}
|
||||
|
||||
@override
|
||||
void close({bool force = false}) => _client.close(force: force);
|
||||
|
||||
@override
|
||||
dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ Future<ArtifactFetchResult> fetchArtifact(Uri uri) async {
|
||||
final request = await client.getUrl(uri);
|
||||
request.followRedirects = false;
|
||||
// A phone behind the web login carries its kept session.
|
||||
ServerSession.headers.forEach(request.headers.set);
|
||||
ServerSession.headersFor(uri).forEach(request.headers.set);
|
||||
final response = await request.close();
|
||||
final bytes = BytesBuilder(copy: false);
|
||||
await for (final chunk in response) {
|
||||
|
||||
@@ -16,7 +16,7 @@ Future<ChatUploadReply> postChatUpload(
|
||||
final request = await client.postUrl(uri);
|
||||
request.followRedirects = false;
|
||||
// A phone behind the web login carries its kept session.
|
||||
ServerSession.headers.forEach(request.headers.set);
|
||||
ServerSession.headersFor(uri).forEach(request.headers.set);
|
||||
request.headers
|
||||
..set(chatUploadNameHeader, Uri.encodeComponent(name))
|
||||
..set(HttpHeaders.contentTypeHeader, mime)
|
||||
|
||||
@@ -16,8 +16,9 @@ Future<int?> installedVersionCode() async {
|
||||
|
||||
Future<HttpClientResponse> _get(HttpClient client, Uri uri) async {
|
||||
final request = await client.getUrl(uri);
|
||||
request.followRedirects = false;
|
||||
// The updates sit behind the same web login the app signed in to.
|
||||
ServerSession.headers.forEach(request.headers.set);
|
||||
ServerSession.headersFor(uri).forEach(request.headers.set);
|
||||
final response = await request.close();
|
||||
if (response.statusCode == 401 || response.statusCode == 302) {
|
||||
await response.drain<void>();
|
||||
|
||||
Reference in New Issue
Block a user