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
21 changes: 16 additions & 5 deletions pm-stack/console/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,16 @@ the owner (`PM_CONSOLE_OWNER_EMAILS`).
- **Today:** Cloudflare Access at the public edge runs the interactive
sign-in and issues the token. The sign-in methods are set on the Access
application, not here.
- **Google:** an Access sign-in method. It turns on when
`scripts/expose.py` finds the secrets `google.access-sso.client-id` and
`google.access-sso.client-secret` (an OAuth client whose redirect URI is
`https://<team>.cloudflareaccess.com/cdn-cgi/access/callback`).
- **Google:** an Access sign-in method, offered beside the emailed code.
It needs an OAuth client, which Google lets only a signed-in person
make. The app walks through it under More, Sign-in: it shows the two
addresses to give Google, takes the client ID and secret, and hands
them to `scripts/expose.py --google-stdin`, which gives them to
Cloudflare. They are kept nowhere else: not in a file, a log, the
record of actions, or (the action is never held) the edge. The same
screen replaces the client later. `expose.py` also turns Google on by
itself if it finds the secrets `google.access-sso.client-id` and
`google.access-sso.client-secret`.
- **The central OIDC provider (#9077):** either an Access sign-in method
(generic OIDC), or trusted directly: add
`{name, issuer, jwksUri, audience}` to the file named by
Expand All @@ -146,14 +152,19 @@ app's link to this origin), and the two phone paths under

```bash
bash pm-stack/console/install.sh # user unit pm-console, port 4872, loopback only; and its two timers
python3 pm-stack/console/scripts/expose.py --owner you@example.com # first time: Access, console env, tunnel route, DNS, the edge's route
python3 pm-stack/console/scripts/expose.py --check # what public exposure would change
python3 pm-stack/console/scripts/expose.py # Access, console env, tunnel route, DNS, the edge's route
python3 pm-stack/console/scripts/expose.py # later runs (the timer's): the owner is remembered
python3 pm-stack/console/scripts/expose.py --status # which sign-in methods are on, as JSON; changes nothing
python3 pm-stack/console/scripts/expose.py --edge off # hostname straight to this host, not through the edge
cd pm-stack/console && bun test
bun run scripts/demo.ts # the app with made-up state, on this host alone
bun run scripts/edge-demo.ts # the same behind the edge
```

The owner's address is given once with `--owner` and remembered in the
console's environment file; it is not written in the script.

`expose.py` is safe to run again: it changes only what is not already so
and refuses to touch DNS records or Access policies it did not write. It
puts the sign-in gate in place first and the route last, and stops before
Expand Down
17 changes: 17 additions & 0 deletions pm-stack/console/public/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -602,3 +602,20 @@ dialog label {
min-height: 22px;
height: 22px;
}

/* Sign-in: the numbered steps, and a value to copy that may be long. */
ol.steps {
margin: 0.5rem 0;
padding-left: 1.3rem;
display: grid;
gap: 0.6rem;
}
code.wrap {
flex: 1;
min-width: 0;
overflow-wrap: anywhere;
font-size: 0.85rem;
}
.card input + input {
margin-top: 0.5rem;
}
67 changes: 67 additions & 0 deletions pm-stack/console/public/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,7 @@ function viewMore() {
tile("more/services", "Services", ops.services ? `${ops.services.filter((u) => u.active === "failed").length} failed of ${ops.services.length}` : "start, stop, logs"),
tile("more/notify", "Notifications", "choose what reaches this device"),
tile("more/phone", "Phones", "the Android app and its home-screen widget"),
tile("more/signin", "Sign-in", "Google first, the emailed code as the fallback"),
),
];
for (const g of ops.more || []) {
Expand Down Expand Up @@ -980,6 +981,71 @@ function collectUrl() {
return `intent://finish#Intent;scheme=pmconsole;package=${me.androidPackage || "today.developing.pmconsole"};S.browser_fallback_url=${back};end`;
}

/** Sign-in: what is on, and the one part only the owner can do to put
* Google first. Google lets nobody but a signed-in person make the
* client its sign-in runs on; everything after that is done here. */
async function viewSignin() {
let s;
try {
s = await api("signin");
} catch (error) {
return [empty(`The sign-in settings could not be read just now: ${error.message}`)];
}
const copy = (text) =>
h(
"button",
{
class: "small ghost",
onclick: () =>
navigator.clipboard.writeText(text).then(
() => toast("Copied"),
() => toast("Could not copy. Select the text and copy it.", true),
),
},
"Copy",
);
const value = (text) => h("div", { class: "row" }, h("code", { class: "wrap" }, text), copy(text));
const id = h("input", { type: "text", placeholder: "Client ID", autocomplete: "off", autocapitalize: "none", spellcheck: false });
const secret = h("input", { type: "password", placeholder: "Client secret", autocomplete: "off" });
const send = async (e) => {
const done = await act("sso.google", { clientId: id.value.trim(), clientSecret: secret.value.trim() }, "Google sign-in is on", e.target);
secret.value = "";
if (done) draw();
else e.target.disabled = false;
};
return [
card(
h("h3", {}, s.google ? "Google sign-in is on" : "Google sign-in is not on yet"),
h(
"p",
{ class: "meta" },
s.google
? "The sign-in page offers Google. The code sent by email stays as the fallback."
: s.pin
? "For now the sign-in page asks for a code sent by email."
: "No sign-in method is on.",
),
),
h("h2", {}, s.google ? "Use a different Google client" : "Turn it on"),
card(
h("p", { class: "meta" }, "Google lets only a signed-in person make the client its sign-in runs on, so this part is yours. A few minutes, once."),
h(
"ol",
{ class: "steps" },
h("li", {}, "Open ", ext("https://console.cloud.google.com/auth/clients", "Google Cloud, Clients"), ". If it asks, set the app up first: any name, audience External."),
h("li", {}, "Create client. Application type: Web application."),
h("li", {}, "Authorized JavaScript origins, add:", value(s.origin)),
h("li", {}, "Authorized redirect URIs, add:", value(s.redirect)),
h("li", {}, "Create, then paste the two values it shows:"),
),
id,
secret,
h("div", { class: "row end" }, h("button", { onclick: send }, s.google ? "Use this client" : "Turn on Google sign-in")),
h("p", { class: "meta" }, "Both values go to Cloudflare, which runs the sign-in page, and are kept nowhere else."),
),
];
}

async function viewPhone(sub) {
if (sub?.startsWith("link=")) {
const request = sub.slice(5);
Expand Down Expand Up @@ -1332,6 +1398,7 @@ const VIEWS = {
"more/services": ["Services", viewServices, "ops"],
"more/notify": ["Notifications", viewNotify],
"more/phone": ["Phones", viewPhone],
"more/signin": ["Sign-in", viewSignin],
};
let route = "now";
let drawSeq = 0;
Expand Down
32 changes: 23 additions & 9 deletions pm-stack/console/scripts/demo-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { readConfig } from "../src/config.ts";
import { Devices, Limiter } from "../src/devices.ts";
import { Docs } from "../src/docs.ts";
import { fileStore } from "../src/file-store.ts";
import { Ops } from "../src/ops.ts";
import { Ops, type Run } from "../src/ops.ts";
import { Push } from "../src/push.ts";
import type { Deps } from "../src/server.ts";

Expand Down Expand Up @@ -337,6 +337,25 @@ const state = {
],
};

/** The commands the console runs, answered from nothing: every one
* succeeds with an empty list, and the sign-in script remembers whether
* it was handed a Google client. */
let demoGoogle = false;
const demoRun: Run = async (argv) => {
if (argv.includes("--google-stdin")) {
demoGoogle = true;
return { code: 0, stdout: "changed: access: add Google sign-in\n", stderr: "" };
}
if (argv.includes("--status")) {
return {
code: 0,
stdout: `${JSON.stringify({ google: demoGoogle, pin: true, team: "your-team.cloudflareaccess.com" })}\n`,
stderr: "",
};
}
return { code: 0, stdout: "[]", stderr: "" };
};

const cfg = readConfig({ HOME: "/tmp", PM_CONSOLE_DASHBOARD_URL: "" });
const docs = new Docs(
Object.fromEntries(Object.entries(state).map(([name, data]) => [name, { ttlMs: 3_600_000, load: async () => data }])),
Expand All @@ -345,15 +364,10 @@ const done = (result: unknown) => ({ run: async () => result, touch: [] as strin
// Linking a phone is the real thing, kept in a file under /tmp.
const devices = new Devices(fileStore("/tmp/pm-console-demo-devices.json"));
const phones = { devices, link: new Limiter(30, 60_000), refused: new Limiter(60, 60_000) };
const real = realActions(
new Ops(cfg, async () => ({ code: 0, stdout: "[]", stderr: "" })),
undefined as never,
undefined,
devices,
);
const real = realActions(new Ops(cfg, demoRun), undefined as never, undefined, devices);
const table = new Proxy({} as Record<string, ReturnType<typeof done>>, {
get: (_t, kind) =>
typeof kind === "string" && kind.startsWith("device.")
typeof kind === "string" && (kind.startsWith("device.") || kind === "sso.google")
? real[kind]
: done(
kind === "ask.send"
Expand All @@ -369,7 +383,7 @@ const table = new Proxy({} as Record<string, ReturnType<typeof done>>, {
/** The made-up host: canned state, and actions that all succeed. */
export const demoDeps: Deps = {
cfg,
ops: new Ops(cfg, async () => ({ code: 0, stdout: "[]", stderr: "" })),
ops: new Ops(cfg, demoRun),
docs,
phones,
push: new Push(fileStore("/tmp/pm-console-demo-push.json"), "mailto:you@example.com", async () => ({
Expand Down
Loading