Commit de63ac8c4d
Verified · cmc
Layout: unified · split
.github/FUNDING.yml +1 −1
| @@ -1,2 +1,2 @@ | |||
| 1 | github: [ccleberg] | 1 | github: [krazywarez] |
| 2 | custom: ["https://cleberg.net/tips/"] | 2 | custom: ["https://cleberg.net/tips/"] |
CONTRIBUTING.org +2 −1
| @@ -80,7 +80,8 @@ Setup, once: | |||
| 80 | - Create a GitHub OAuth app with the callback URL =https://krz.sh/apps/orgstar/license= and | 80 | - Create a GitHub OAuth app with the callback URL =https://krz.sh/apps/orgstar/license= and |
| 81 | put its client ID in =GITHUB_CLIENT_ID= in =wrangler.toml=. | 81 | put its client ID in =GITHUB_CLIENT_ID= in =wrangler.toml=. |
| 82 | - =npx wrangler secret put GITHUB_CLIENT_SECRET= with the OAuth app's secret, and | 82 | - =npx wrangler secret put GITHUB_CLIENT_SECRET= with the OAuth app's secret, and |
| 83 | =GITHUB_TOKEN= with a token of the sponsored account (=read:user=), which renewals use. | 83 | =GITHUB_TOKEN= with a classic token of an owner of the krazywarez organization (=read:org=, |
| 84 | =read:user=), which renewals use to list its sponsors. | ||
| 84 | - =packaging/license-issuer/keygen.sh= set =LICENSE_PRIVATE_KEY= and the app's public key. | 85 | - =packaging/license-issuer/keygen.sh= set =LICENSE_PRIVATE_KEY= and the app's public key. |
| 85 | Running it again invalidates every key issued so far. | 86 | Running it again invalidates every key issued so far. |
| 86 | 87 | ||
README.org +2 −2
| @@ -3,7 +3,7 @@ | |||
| 3 | #+ATTR_HTML: :alt orgstar on iPad, Mac and iPhone | 3 | #+ATTR_HTML: :alt orgstar on iPad, Mac and iPhone |
| 4 | [[file:ios/AppStore/header-21x9.png]] | 4 | [[file:ios/AppStore/header-21x9.png]] |
| 5 | 5 | ||
| 6 | Support orgstar through [[https://github.com/sponsors/ccleberg][GitHub Sponsors]]. | 6 | Support orgstar through [[https://github.com/sponsors/krazywarez][GitHub Sponsors]]. |
| 7 | 7 | ||
| 8 | orgstar is an editor for [[https://orgmode.org][Org mode]] files on the Mac, iPhone and | 8 | orgstar is an editor for [[https://orgmode.org][Org mode]] files on the Mac, iPhone and |
| 9 | iPad. It reads and writes the same plain-text =.org= files Emacs does, and leaves every | 9 | iPad. It reads and writes the same plain-text =.org= files Emacs does, and leaves every |
| @@ -67,7 +67,7 @@ backlinks; widgets, the Apple Watch app and the clock Live Activity; Calendar an | |||
| 67 | the agenda; and Shortcuts actions. | 67 | the agenda; and Shortcuts actions. |
| 68 | 68 | ||
| 69 | - iPhone and iPad: in-app, $1.99 a month, $19.99 a year or $39.99 once. | 69 | - iPhone and iPad: in-app, $1.99 a month, $19.99 a year or $39.99 once. |
| 70 | - Mac: by [[https://github.com/sponsors/ccleberg][sponsoring on GitHub]], $60 once for a license key that doesn't expire, | 70 | - Mac: by [[https://github.com/sponsors/krazywarez][sponsoring on GitHub]], $60 once for a license key that doesn't expire, |
| 71 | or $5 a month for a key orgstar renews while you sponsor. Sign in with GitHub at | 71 | or $5 a month for a key orgstar renews while you sponsor. Sign in with GitHub at |
| 72 | [[https://krz.sh/apps/orgstar/license][krz.sh/apps/orgstar/license]] for the key and enter it in Settings ▸ orgstar+. | 72 | [[https://krz.sh/apps/orgstar/license][krz.sh/apps/orgstar/license]] for the key and enter it in Settings ▸ orgstar+. |
| 73 | 73 | ||
packaging/license-issuer/.dev.vars.example +2 −2
| @@ -3,8 +3,8 @@ | |||
| 3 | # The GitHub OAuth app's client secret. For local runs, use a second OAuth app whose callback | 3 | # The GitHub OAuth app's client secret. For local runs, use a second OAuth app whose callback |
| 4 | # URL is http://localhost:8787/license, and override GITHUB_CLIENT_ID here. | 4 | # URL is http://localhost:8787/license, and override GITHUB_CLIENT_ID here. |
| 5 | GITHUB_CLIENT_SECRET= | 5 | GITHUB_CLIENT_SECRET= |
| 6 | # A token of the SPONSORABLE account with read:user, for renewals (classic token, or | 6 | # A classic token of an owner of the SPONSORABLE organization with read:org and read:user, |
| 7 | # fine-grained with no repository access). | 7 | # for renewals: it lists the organization's sponsors, private ones included. |
| 8 | GITHUB_TOKEN= | 8 | GITHUB_TOKEN= |
| 9 | # PKCS#8 PEM from keygen.sh, on one line with \n for line breaks. | 9 | # PKCS#8 PEM from keygen.sh, on one line with \n for line breaks. |
| 10 | LICENSE_PRIVATE_KEY= | 10 | LICENSE_PRIVATE_KEY= |
packaging/license-issuer/worker.js +45 −21
| @@ -19,7 +19,7 @@ | |||
| 19 | // | 19 | // |
| 20 | // Vars (wrangler.toml): SPONSORABLE, LIFETIME_DOLLARS, MONTHLY_DOLLARS, RENEW_DAYS, | 20 | // Vars (wrangler.toml): SPONSORABLE, LIFETIME_DOLLARS, MONTHLY_DOLLARS, RENEW_DAYS, |
| 21 | // GITHUB_CLIENT_ID. Secrets (`wrangler secret put NAME`; locally .dev.vars, see | 21 | // GITHUB_CLIENT_ID. Secrets (`wrangler secret put NAME`; locally .dev.vars, see |
| 22 | // .dev.vars.example): GITHUB_CLIENT_SECRET, GITHUB_TOKEN (SPONSORABLE's token, for renewals), | 22 | // .dev.vars.example): GITHUB_CLIENT_SECRET, GITHUB_TOKEN (an owner of SPONSORABLE, for renewals), |
| 23 | // LICENSE_PRIVATE_KEY (PKCS#8 PEM, from keygen.sh). | 23 | // LICENSE_PRIVATE_KEY (PKCS#8 PEM, from keygen.sh). |
| 24 | 24 | ||
| 25 | const FEATURES = [ | 25 | const FEATURES = [ |
| @@ -114,9 +114,11 @@ async function license(request, url, env) { | |||
| 114 | token, | 114 | token, |
| 115 | `query($login: String!) { | 115 | `query($login: String!) { |
| 116 | viewer { id login } | 116 | viewer { id login } |
| 117 | user(login: $login) { | 117 | owner: repositoryOwner(login: $login) { |
| 118 | sponsorshipForViewerAsSponsor(activeOnly: false) { | 118 | ... on Sponsorable { |
| 119 | isActive createdAt tier { monthlyPriceInDollars isOneTime } | 119 | sponsorshipForViewerAsSponsor(activeOnly: false) { |
| 120 | isActive createdAt tier { monthlyPriceInDollars isOneTime } | ||
| 121 | } | ||
| 120 | } | 122 | } |
| 121 | } | 123 | } |
| 122 | }`, | 124 | }`, |
| @@ -125,7 +127,8 @@ async function license(request, url, env) { | |||
| 125 | if (!data) return page("GitHub can't be reached right now. Try again in a minute.", 502); | 127 | if (!data) return page("GitHub can't be reached right now. Try again in a minute.", 502); |
| 126 | 128 | ||
| 127 | const { viewer } = data; | 129 | const { viewer } = data; |
| 128 | const kind = entitlement(data.user?.sponsorshipForViewerAsSponsor, config); | 130 | const sponsorship = data.owner?.sponsorshipForViewerAsSponsor; |
| 131 | const kind = entitlement(sponsorship, config); | ||
| 129 | const sponsor = `https://github.com/sponsors/${config.sponsorable}`; | 132 | const sponsor = `https://github.com/sponsors/${config.sponsorable}`; |
| 130 | const clear = { "set-cookie": `${STATE_COOKIE}=; Path=${url.pathname}; Max-Age=0; HttpOnly; Secure; SameSite=Lax` }; | 133 | const clear = { "set-cookie": `${STATE_COOKIE}=; Path=${url.pathname}; Max-Age=0; HttpOnly; Secure; SameSite=Lax` }; |
| 131 | if (!kind) | 134 | if (!kind) |
| @@ -139,7 +142,7 @@ async function license(request, url, env) { | |||
| 139 | 142 | ||
| 140 | const t = "github:" + viewer.id; | 143 | const t = "github:" + viewer.id; |
| 141 | const payload = kind === "lifetime" | 144 | const payload = kind === "lifetime" |
| 142 | ? { e: viewer.login, t, i: seconds(data.user.sponsorshipForViewerAsSponsor.createdAt) } | 145 | ? { e: viewer.login, t, i: seconds(sponsorship.createdAt) } |
| 143 | : monthlyPayload(viewer.login, t, config.renewDays); | 146 | : monthlyPayload(viewer.login, t, config.renewDays); |
| 144 | const key = await sign(payload, env.LICENSE_PRIVATE_KEY); | 147 | const key = await sign(payload, env.LICENSE_PRIVATE_KEY); |
| 145 | const note = kind === "lifetime" | 148 | const note = kind === "lifetime" |
| @@ -172,21 +175,42 @@ async function renew(request, env) { | |||
| 172 | const payload = await verify((await request.text()).replace(/\s/g, ""), env.LICENSE_PRIVATE_KEY); | 175 | const payload = await verify((await request.text()).replace(/\s/g, ""), env.LICENSE_PRIVATE_KEY); |
| 173 | if (!payload?.x || !payload.t?.startsWith("github:")) return text("Not a monthly orgstar+ key.", 400); | 176 | if (!payload?.x || !payload.t?.startsWith("github:")) return text("Not a monthly orgstar+ key.", 400); |
| 174 | 177 | ||
| 175 | const data = await graphql( | 178 | const found = await activeSponsorship(env.GITHUB_TOKEN, config.sponsorable, payload.t.slice("github:".length)); |
| 176 | env.GITHUB_TOKEN, | 179 | if (found === undefined) return text("GitHub can't be reached right now.", 502); |
| 177 | `query($id: ID!) { | 180 | if (entitlement(found, config) !== "monthly") return text("The monthly sponsorship has ended.", 403); |
| 178 | node(id: $id) { | 181 | return text(await sign(monthlyPayload(found.sponsorEntity.login, payload.t, config.renewDays), env.LICENSE_PRIVATE_KEY), 200); |
| 179 | ... on User { login sponsorshipForViewerAsSponsorable(activeOnly: true) { isActive tier { monthlyPriceInDollars isOneTime } } } | 182 | } |
| 180 | ... on Organization { login sponsorshipForViewerAsSponsorable(activeOnly: true) { isActive tier { monthlyPriceInDollars isOneTime } } } | 183 | |
| 181 | } | 184 | /// The active sponsorship of `sponsorable` by the account with node ID `id`, from the |
| 182 | }`, | 185 | /// sponsorable's own list (private ones too, so `token` belongs to it or an owner of it); null |
| 183 | { id: payload.t.slice("github:".length) }, | 186 | /// when there is none, undefined when GitHub can't be reached. |
| 184 | ); | 187 | async function activeSponsorship(token, sponsorable, id) { |
| 185 | if (!data) return text("GitHub can't be reached right now.", 502); | 188 | let after = null; |
| 186 | const node = data.node; | 189 | for (;;) { |
| 187 | if (entitlement(node?.sponsorshipForViewerAsSponsorable, config) !== "monthly") | 190 | const data = await graphql( |
| 188 | return text("The monthly sponsorship has ended.", 403); | 191 | token, |
| 189 | return text(await sign(monthlyPayload(node.login, payload.t, config.renewDays), env.LICENSE_PRIVATE_KEY), 200); | 192 | `query($login: String!, $after: String) { |
| 193 | owner: repositoryOwner(login: $login) { | ||
| 194 | ... on Sponsorable { | ||
| 195 | sponsorshipsAsMaintainer(first: 100, after: $after, activeOnly: true, includePrivate: true) { | ||
| 196 | nodes { | ||
| 197 | isActive tier { monthlyPriceInDollars isOneTime } | ||
| 198 | sponsorEntity { ... on User { id login } ... on Organization { id login } } | ||
| 199 | } | ||
| 200 | pageInfo { hasNextPage endCursor } | ||
| 201 | } | ||
| 202 | } | ||
| 203 | } | ||
| 204 | }`, | ||
| 205 | { login: sponsorable, after }, | ||
| 206 | ); | ||
| 207 | const list = data?.owner?.sponsorshipsAsMaintainer; | ||
| 208 | if (!list) return undefined; | ||
| 209 | const match = list.nodes.find((node) => node.sponsorEntity?.id === id); | ||
| 210 | if (match) return match; | ||
| 211 | if (!list.pageInfo.hasNextPage) return null; | ||
| 212 | after = list.pageInfo.endCursor; | ||
| 213 | } | ||
| 190 | } | 214 | } |
| 191 | 215 | ||
| 192 | async function graphql(token, query, variables) { | 216 | async function graphql(token, query, variables) { |
packaging/license-issuer/worker.test.mjs +14 −8
| @@ -67,7 +67,7 @@ async function callback(sponsor, { state = "abc", cookie = "abc" } = {}) { | |||
| 67 | return withFetch( | 67 | return withFetch( |
| 68 | (url) => url.includes("access_token") | 68 | (url) => url.includes("access_token") |
| 69 | ? json({ access_token: "visitor-token" }) | 69 | ? json({ access_token: "visitor-token" }) |
| 70 | : json({ data: { viewer: { id: "U_1", login: "octo" }, user: { sponsorshipForViewerAsSponsor: sponsor } } }), | 70 | : json({ data: { viewer: { id: "U_1", login: "octo" }, owner: { sponsorshipForViewerAsSponsor: sponsor } } }), |
| 71 | () => worker.fetch( | 71 | () => worker.fetch( |
| 72 | new Request(`https://krz.sh/apps/orgstar/license?code=c&state=${state}`, { headers: { cookie: `orgstar_state=${cookie}` } }), | 72 | new Request(`https://krz.sh/apps/orgstar/license?code=c&state=${state}`, { headers: { cookie: `orgstar_state=${cookie}` } }), |
| 73 | env, | 73 | env, |
| @@ -99,13 +99,19 @@ test("a callback whose state doesn't match the cookie is refused", async () => { | |||
| 99 | assert.equal((await callback(sponsorship(5), { cookie: "other" })).status, 400); | 99 | assert.equal((await callback(sponsorship(5), { cookie: "other" })).status, 400); |
| 100 | }); | 100 | }); |
| 101 | 101 | ||
| 102 | /// …/renew with `key`, GitHub answering `sponsor` for the key's account. | 102 | /// …/renew with `key`, GitHub listing the maintainer's sponsors in `pages`, with `sponsor` as |
| 103 | async function renewal(key, sponsor) { | 103 | /// U_1's sponsorship on the last page. |
| 104 | let asked; | 104 | async function renewal(key, sponsor, pages = 1) { |
| 105 | const asked = []; | ||
| 105 | const response = await withFetch( | 106 | const response = await withFetch( |
| 106 | (url, init) => { | 107 | (url, init) => { |
| 107 | asked = JSON.parse(init.body).variables; | 108 | const { variables } = JSON.parse(init.body); |
| 108 | return json({ data: { node: { login: "octo-renamed", sponsorshipForViewerAsSponsorable: sponsor } } }); | 109 | asked.push(variables); |
| 110 | const page = asked.length; | ||
| 111 | const nodes = [{ ...sponsorship(5), sponsorEntity: { id: `U_other${page}`, login: "someone" } }]; | ||
| 112 | if (page === pages && sponsor) nodes.push({ ...sponsor, sponsorEntity: { id: "U_1", login: "octo-renamed" } }); | ||
| 113 | const pageInfo = { hasNextPage: page < pages, endCursor: `cursor${page}` }; | ||
| 114 | return json({ data: { owner: { sponsorshipsAsMaintainer: { nodes, pageInfo } } } }); | ||
| 109 | }, | 115 | }, |
| 110 | () => worker.fetch(new Request("https://krz.sh/apps/orgstar/renew", { method: "POST", body: key }), env), | 116 | () => worker.fetch(new Request("https://krz.sh/apps/orgstar/renew", { method: "POST", body: key }), env), |
| 111 | ); | 117 | ); |
| @@ -114,9 +120,9 @@ async function renewal(key, sponsor) { | |||
| 114 | 120 | ||
| 115 | test("renewal extends a monthly key while the sponsorship is active", async () => { | 121 | test("renewal extends a monthly key while the sponsorship is active", async () => { |
| 116 | const old = await sign({ e: "octo", t: "github:U_1", i: 1, x: 2 }, pem); | 122 | const old = await sign({ e: "octo", t: "github:U_1", i: 1, x: 2 }, pem); |
| 117 | const { response, asked } = await renewal(old, sponsorship(5)); | 123 | const { response, asked } = await renewal(old, sponsorship(5), 2); |
| 118 | assert.equal(response.status, 200); | 124 | assert.equal(response.status, 200); |
| 119 | assert.deepEqual(asked, { id: "U_1" }); | 125 | assert.deepEqual(asked, [{ login: "maintainer", after: null }, { login: "maintainer", after: "cursor1" }]); |
| 120 | const fresh = await verify((await response.text()).trim(), pem); | 126 | const fresh = await verify((await response.text()).trim(), pem); |
| 121 | assert.equal(fresh.e, "octo-renamed"); | 127 | assert.equal(fresh.e, "octo-renamed"); |
| 122 | assert.ok(fresh.x > Date.now() / 1000 + 39 * 86400); | 128 | assert.ok(fresh.x > Date.now() / 1000 + 39 * 86400); |
packaging/license-issuer/wrangler.toml +1 −1
| @@ -8,7 +8,7 @@ routes = [ | |||
| 8 | ] | 8 | ] |
| 9 | 9 | ||
| 10 | [vars] | 10 | [vars] |
| 11 | SPONSORABLE = "ccleberg" | 11 | SPONSORABLE = "krazywarez" |
| 12 | LIFETIME_DOLLARS = "60" | 12 | LIFETIME_DOLLARS = "60" |
| 13 | MONTHLY_DOLLARS = "5" | 13 | MONTHLY_DOLLARS = "5" |
| 14 | # A monthly key lasts this long; the app renews it well before. | 14 | # A monthly key lasts this long; the app renews it well before. |