Skip to content

Commit

Permalink
Handle missing authority and default authority location on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-ancell committed Nov 29, 2023
1 parent 25c9ed6 commit 64e90c9
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions lib/src/x11_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -298,13 +298,21 @@ class X11Client {
var authorityPath = Platform.environment['XAUTHORITY'];
if (authorityPath == null) {
var home = Platform.environment['HOME'];
if (home == null) {
throw 'Unable to determine HOME';
if (home == null && Platform.isWindows) {
var username = Platform.environment['USERNAME'];
if (username != null) {
home = '/users/$username';
}
}

authorityPath = '$home/.Xauthority';
if (home != null) {
authorityPath = '$home/.Xauthority';
}
}
X11AuthorityFile? authorityFile;
if (authorityPath != null) {
authorityFile = await X11AuthorityFileLoader().load(authorityPath);
}
var authorityFile = await X11AuthorityFileLoader().load(authorityPath);
var authorizationName = '';
var authorizationData = <int>[];

Expand All @@ -314,9 +322,10 @@ class X11Client {
socketAddress = InternetAddress('/tmp/.X11-unix/X$displayNumber',
type: InternetAddressType.unix);

var records = authorityFile.records.where((r) =>
r.address.type == X11AuthorityAddressType.local &&
r.authorizationName == 'MIT-MAGIC-COOKIE-1');
var records = authorityFile?.records.where((r) =>
r.address.type == X11AuthorityAddressType.local &&
r.authorizationName == 'MIT-MAGIC-COOKIE-1') ??
[];
if (records.isNotEmpty) {
var record = records.first;
authorizationName = 'MIT-MAGIC-COOKIE-1';
Expand Down Expand Up @@ -350,11 +359,12 @@ class X11Client {
}

// FIXME: Also match display?
var records = authorityFile.records.where((r) =>
(r.address.type == addressType ||
r.address.type == X11AuthorityAddressType.wild) &&
authHostMatches(r.address.address, host) &&
r.authorizationName == 'MIT-MAGIC-COOKIE-1');
var records = authorityFile?.records.where((r) =>
(r.address.type == addressType ||
r.address.type == X11AuthorityAddressType.wild) &&
authHostMatches(r.address.address, host) &&
r.authorizationName == 'MIT-MAGIC-COOKIE-1') ??
[];
if (records.isNotEmpty) {
var record = records.first;
authorizationName = 'MIT-MAGIC-COOKIE-1';
Expand Down

0 comments on commit 64e90c9

Please sign in to comment.