Spelunk: sign each device in through the browser instead of keeping Spelunk API keys in the vault

A Spelunk API key reaches one organisation, so a server elsewhere - oc-linux in Orange Climate - was
refused before it reached the agent. Each device now signs in to a Spelunk once, under Settings ->
Security -> Spelunk or from a Spelunk host's editor: the OAuth authorization code flow with PKCE for a
native app (RFC 8252), with Spelunk as the authorization server. The browser opens Spelunk's
/api/app-sign-in/authorize with a loopback redirect (the app's own scheme on Android), the person signs
in there however they normally do and allows "DodoSSH on <device>", and the code is redeemed at
/api/app-sign-in/token for an app session token. That token opens terminals in every organisation its
person is in, with the role they have on each server at the time; tunnel tickets send no organisation.

The sign-in stays on the device: sealed in the local cache under the LocalCacheKey (AAD resource type
18, bound to its row), never synced, wiped with the cache, and only ever sent to the Spelunk it came
from. A 401 from Spelunk forgets it; SIGN OUT ends it there and here, and so does signing out of
DodoSSH. Before every Spelunk connection DodoSSH asks its own identity provider whether its sign-in
still stands (a forced refresh, at most once a minute), and an account it refuses is signed out of
DodoSSH and of every Spelunk on the device.

A route names no key any more: Spelunk hosts are written at host schema 11, which an older build shows
read-only and asks to be updated for, and a payload that still names a key credential decodes with the
id skipped. The SpelunkKey item type is gone: SyncEntityType 14, ChangeEntityType 14 and AAD type 17
are retired, the server drops spelunk_key and its change-log rows, and the client cache drops its
cached items, outbox entries and conflicts of that type. A link still never starts a sign-in: from a
Spelunk this device is not signed in to it opens the editor, which offers to.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
laurensandClaude Opus 5.5 committed 2026-10-20 16:26:25 +02:00
1 parent 902761d37b
commit ef365f462e
62 files changed
+4327 -1945

No files matched your search

@@ -22,7 +22,7 @@ namespace DodoSSH.Client.Session.Tests;
/// </remarks>
public sealed class SpelunkTunnelTests : IAsyncLifetime
{
private const string ApiKey = "spelunk_0123456789abcdefghijklmnopqrstuvwxyzABCDEFG";
private const string SignInToken = "spelunkapp_0123456789abcdefghijklmnopqrstuvwxyzABCDEFG";
private static readonly Guid ServerId = Guid.Parse("0192f0c8-bbbb-7c3d-8e4f-5a6b7c8d9e0b");
@@ -44,7 +44,7 @@ public sealed class SpelunkTunnelTests : IAsyncLifetime
var ticket = spelunk.Requests[0];
ticket.Method.ShouldBe("POST");
ticket.Path.ShouldBe($"/api/servers/{ServerId}/tunnel-tickets");
ticket.Headers["Authorization"].ShouldBe($"Bearer {ApiKey}");
ticket.Headers["Authorization"].ShouldBe($"Bearer {SignInToken}");
ticket.Body.ShouldBe("""{"port":22}""");
var upgrade = spelunk.Requests[1];
@@ -98,7 +98,8 @@ public sealed class SpelunkTunnelTests : IAsyncLifetime
}
[Theory]
[InlineData(HttpStatusCode.Unauthorized, "did not accept the API key")]
[InlineData(HttpStatusCode.Unauthorized, "signed this device out")]
[InlineData(HttpStatusCode.Forbidden, "not allowed to open a terminal")]
[InlineData(HttpStatusCode.NotFound, "does not know this server")]
[InlineData(HttpStatusCode.ServiceUnavailable, "agent on this server is not connected")]
[InlineData(HttpStatusCode.InternalServerError, "HTTP 500")]
@@ -202,24 +203,18 @@ public sealed class SpelunkTunnelTests : IAsyncLifetime
}
[Fact]
public async Task AKeyWithALineBreakInTheMiddle_IsRefusedBeforeAnythingIsSent()
public async Task ADamagedSignIn_IsRefusedBeforeAnythingIsSent_AsASignedOutDevice()
{
// It came from Spelunk and was checked then, so a token that is not a header value is a damaged cache,
// and the remedy is the one a 401 gets: sign in again.
var refusal = await Should.ThrowAsync<SpelunkTunnelException>(
new SpelunkTunnel().OpenAsync(spelunk.ApiBase, ServerId, "spelunk_abc\ndef", 22, signIn: null, Token));
new SpelunkTunnel().OpenAsync(spelunk.ApiBase, ServerId, "spelunkapp_abc\ndef", 22, signIn: null, Token));
refusal.Message.ShouldContain("does not hold one");
refusal.Message.ShouldContain("Sign in to Spelunk again");
refusal.StatusCode.ShouldBe(HttpStatusCode.Unauthorized);
spelunk.Requests.ShouldBeEmpty();
}
[Fact]
public async Task AKeyPastedWithANewline_IsTrimmed()
{
await using var route = await new SpelunkTunnel()
.OpenAsync(spelunk.ApiBase, ServerId, ApiKey + "\r\n", 22, signIn: null, Token);
spelunk.Requests[0].Headers["Authorization"].ShouldBe($"Bearer {ApiKey}");
}
[Fact]
public async Task AnUnreachableSpelunk_SaysWhere()
{
@@ -230,7 +225,7 @@ public sealed class SpelunkTunnelTests : IAsyncLifetime
closed.Stop();
var refusal = await Should.ThrowAsync<SpelunkTunnelException>(
new SpelunkTunnel().OpenAsync($"http://127.0.0.1:{port}", ServerId, ApiKey, 22, signIn: null, Token));
new SpelunkTunnel().OpenAsync($"http://127.0.0.1:{port}", ServerId, SignInToken, 22, signIn: null, Token));
refusal.Message.ShouldContain($"could not be reached at http://127.0.0.1:{port}");
}
@@ -389,14 +384,14 @@ public sealed class SpelunkTunnelTests : IAsyncLifetime
}
}
/// <summary>Opens a tunnel to the test server with the one key and server the suite uses.</summary>
/// <summary>Opens a tunnel to the test server with the one sign-in and server the suite uses.</summary>
file static class Opening
{
internal static Task<SpelunkTunnelRoute> OpenAsync(this string apiBase, int port, SpelunkSignIn? signIn = null) =>
new SpelunkTunnel().OpenAsync(
apiBase,
Guid.Parse("0192f0c8-bbbb-7c3d-8e4f-5a6b7c8d9e0b"),
"spelunk_0123456789abcdefghijklmnopqrstuvwxyzABCDEFG",
"spelunkapp_0123456789abcdefghijklmnopqrstuvwxyzABCDEFG",
port,
signIn,
TestContext.Current.CancellationToken);