Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions apps/server/src/auth/RpcAuthorization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,9 @@ export const RPC_REQUIRED_SCOPES = {
[WS_METHODS.deviceTestHost]: AuthSettingsWriteScope,
[WS_METHODS.deviceEasAccount]: AuthSettingsWriteScope,
[WS_METHODS.deviceSimlockIdentity]: AuthSettingsWriteScope,
[WS_METHODS.deviceRevylAccount]: AuthSettingsWriteScope,
[WS_METHODS.deviceRevylSignIn]: AuthSettingsWriteScope,
[WS_METHODS.deviceRevylSignOut]: AuthSettingsWriteScope,
[WS_METHODS.deviceList]: AuthOrchestrationReadScope,
[WS_METHODS.deviceOpen]: AuthOrchestrationOperateScope,
[WS_METHODS.deviceClose]: AuthOrchestrationOperateScope,
Expand Down
11 changes: 11 additions & 0 deletions apps/server/src/device/DeviceService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import * as TestClock from "effect/testing/TestClock";
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
import { HttpClient, HttpClientResponse } from "effect/http";
import * as ServerConfig from "../config.ts";
import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
import * as ExternalLauncher from "../process/externalLauncher.ts";
import * as ProcessRunner from "../processRunner.ts";
import * as ServerSettings from "../serverSettings.ts";
import * as DeviceHost from "./DeviceHost.ts";
Expand Down Expand Up @@ -768,6 +770,12 @@ describe("device setup consent", () => {
);
});

/** Revyl sign-in state no SSH or local test reaches. */
const layerRevylOAuth = Layer.mergeAll(
Layer.mock(ServerSecretStore.ServerSecretStore)({}),
Layer.mock(ExternalLauncher.ExternalLauncher)({}),
);

const layerSecretResolver = Layer.succeed(SecretResolver.SecretResolver, {
label: (provider) => Effect.succeed(provider),
read: (references) => Effect.succeed(references.map(() => undefined)),
Expand Down Expand Up @@ -820,6 +828,7 @@ it.effect.each([
Effect.gen(function* () {
const { host } = yield* fixture();
const service = yield* DeviceService.make.pipe(
Effect.provide(layerRevylOAuth),
Effect.provideService(DeviceHost.DeviceHost, host),
Effect.provideService(ProcessRunner.ProcessRunner, {
run: () => Effect.die("Testing SSH does not install local tools"),
Expand Down Expand Up @@ -1902,6 +1911,7 @@ it.effect("reconciles SSH host settings without publishing no-op changes", () =>
stopAgent: Effect.void,
};
const service = yield* DeviceService.make.pipe(
Effect.provide(layerRevylOAuth),
Effect.provideService(DeviceHost.DeviceHost, localHost),
Effect.provideService(ServerSettings.ServerSettingsService, {
...settings,
Expand Down Expand Up @@ -2024,6 +2034,7 @@ it.effect.each(["exit", "missing"] as const)(
stderrInvalidUtf8: false,
};
const service = yield* DeviceService.make.pipe(
Effect.provide(layerRevylOAuth),
Effect.provideService(DeviceHost.DeviceHost, host),
Effect.provideService(ProcessRunner.ProcessRunner, {
run: ({ command, args }) =>
Expand Down
66 changes: 62 additions & 4 deletions apps/server/src/device/DeviceService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import {
type DeviceSimlockIdentity,
DeviceSimlockIdentityError,
type DeviceSimlockIdentityInput,
type DeviceRevylAccount,
DeviceRevylAuthError,
type DeviceRevylAccountInput,
type DeviceHostConfig,
type EasDeviceHostConfig,
type RevylDeviceHostConfig,
Expand Down Expand Up @@ -83,6 +86,7 @@ import * as DeviceHost from "./DeviceHost.ts";
import * as SshDeviceHost from "./SshDeviceHost.ts";
import * as EasDeviceHost from "./EasDeviceHost.ts";
import * as RevylDeviceHost from "./RevylDeviceHost.ts";
import * as RevylOAuth from "./RevylOAuth.ts";
import * as SimlockDeviceHost from "./SimlockDeviceHost.ts";
import * as HostProcess from "@t3tools/shared/HostProcess";
import { makeExpoApi } from "./ExpoApi.ts";
Expand Down Expand Up @@ -192,6 +196,16 @@ export class DeviceService extends Context.Service<
readonly simlockIdentity: (
input: DeviceSimlockIdentityInput,
) => Effect.Effect<DeviceSimlockIdentity, DeviceSimlockIdentityError>;
/** A saved Revyl host's Sign in with Revyl state, and signing it in or out on this server. */
readonly revylAccount: (
input: DeviceRevylAccountInput,
) => Effect.Effect<DeviceRevylAccount, DeviceRevylAuthError>;
readonly revylSignIn: (
input: DeviceRevylAccountInput,
) => Effect.Effect<DeviceRevylAccount, DeviceRevylAuthError>;
readonly revylSignOut: (
input: DeviceRevylAccountInput,
) => Effect.Effect<DeviceRevylAccount, DeviceRevylAuthError>;
/**
* The agent-device flags that reach the device: its config and session,
* and the id agent-device selects it by, which can differ from its id here.
Expand Down Expand Up @@ -1214,6 +1228,11 @@ export const makeWithHosts = Effect.fn("DeviceService.makeWithHosts")(function*
// Sessions live in memory, so this removes every launcher a previous run left behind.
yield* pruneLaunchers;

const revylUnavailable = () =>
Effect.fail(
new DeviceRevylAuthError({ reason: "Revyl hosts are unavailable in this device service." }),
);

return {
...DeviceService.of({
testHost,
Expand Down Expand Up @@ -1247,6 +1266,9 @@ export const makeWithHosts = Effect.fn("DeviceService.makeWithHosts")(function*
}),
);
}),
revylAccount: revylUnavailable,
revylSignIn: revylUnavailable,
revylSignOut: revylUnavailable,
agentTarget: Effect.fn("DeviceService.agentTarget")(function* (input) {
const host = yield* resolveHost(input.hostId);
const ready = yield* agentReadinessIfSupported(input.hostId);
Expand Down Expand Up @@ -1563,6 +1585,7 @@ export const make = Effect.gen(function* () {
yield* Effect.context<Effect.Services<ReturnType<typeof SimlockDeviceHost.make>>>();
const revylContext =
yield* Effect.context<Effect.Services<ReturnType<typeof RevylDeviceHost.make>>>();
const revylAccounts = yield* RevylOAuth.make;
const agentDeviceCli = Effect.fn("DeviceService.agentDeviceCli")(
function* (hostId: string) {
const nodePath = yield* resolveNodeExecutable("Device automation");
Expand Down Expand Up @@ -1643,10 +1666,11 @@ export const make = Effect.gen(function* () {
if (host.kind === "revyl") {
// Closing this scope ends the host's Revyl sessions, so a changed key or limit never
// leaves a billed session behind.
const instance = yield* RevylDeviceHost.make(host, agentDeviceCli(host.id)).pipe(
Effect.provideService(Scope.Scope, hostScope),
Effect.provide(revylContext),
);
const instance = yield* RevylDeviceHost.make(
host,
agentDeviceCli(host.id),
revylAccounts,
).pipe(Effect.provideService(Scope.Scope, hostScope), Effect.provide(revylContext));
hosts.set(host.id, instance);
configured.set(host.id, { config: host, scope: hostScope });
continue;
Expand Down Expand Up @@ -1811,10 +1835,44 @@ export const make = Effect.gen(function* () {
Effect.provide(simlockContext),
);

const revylHost = Effect.fn("DeviceService.revylHost")(
function* (hostId: string) {
const host = (yield* settings.getSettings).revylDeviceHosts.find(
(candidate) => candidate.id === hostId,
);
if (!host) return yield* new DeviceRevylAuthError({ reason: "Save the Revyl host first." });
const apiOrigin = RevylDeviceHost.revylApiOrigin(yield* HostProcess.Environment);
if (apiOrigin === null)
return yield* new DeviceRevylAuthError({
reason: "REVYL_API_URL must be an https origin, such as https://backend.revyl.ai.",
});
return { host, apiOrigin };
},
Effect.catchTags({
ServerSettingsError: () =>
Effect.fail(
new DeviceRevylAuthError({ reason: "Could not read the Revyl host's settings." }),
),
}),
Effect.provide(revylContext),
);

return {
...service,
easAccount,
simlockIdentity,
revylAccount: Effect.fn("DeviceService.revylAccount")(function* ({ hostId }) {
const { host, apiOrigin } = yield* revylHost(hostId);
return yield* revylAccounts.account(host, apiOrigin);
}),
revylSignIn: Effect.fn("DeviceService.revylSignIn")(function* ({ hostId }) {
const { host, apiOrigin } = yield* revylHost(hostId);
return yield* revylAccounts.signIn(host, apiOrigin);
}),
revylSignOut: Effect.fn("DeviceService.revylSignOut")(function* ({ hostId }) {
const { host } = yield* revylHost(hostId);
return yield* revylAccounts.signOut(host);
}),
};
});

Expand Down
Loading