starlight-annotations 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +33 -0
- package/README.md +5 -1
- package/package.json +3 -2
- package/server.d.ts +16 -0
- package/src/client/annotations.js +2 -1
- package/src/client/backend.js +2 -1
- package/src/server/index.js +23 -7
- package/src/server/oauth.js +34 -2
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# starlight-annotations
|
|
2
|
+
|
|
3
|
+
## 0.3.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 05c032c: Checks write access again while a session lasts, not only at sign-in. Before saving a note, and when a signed-in reader loads a page, the server asks the provider again whether they may still write (`writers`), reusing the answer for `recheck` seconds (default 60). If their access was removed, the save is refused and they are signed out, instead of keeping write access until their 30-day session ends. Set `recheck: false` to check only at sign-in, as before. Custom auths can implement the same with an optional `allowed(user)`.
|
|
8
|
+
|
|
9
|
+
## 0.2.0
|
|
10
|
+
|
|
11
|
+
### Minor Changes
|
|
12
|
+
|
|
13
|
+
- ccdae74: Adds GitLab and Gitea/Forgejo storage and sign-in, next to GitHub: `gitlab({ host, project, … })` for gitlab.com and self-managed GitLab, and `gitea({ host, repo, … })` (also exported as `forgejo`) for Gitea, Forgejo and Codeberg. Both are experimental: they are tested against simulated APIs. The OAuth flow, session and retry logic are now shared by all providers; GitHub behaves as before.
|
|
14
|
+
|
|
15
|
+
## 0.1.2
|
|
16
|
+
|
|
17
|
+
### Patch Changes
|
|
18
|
+
|
|
19
|
+
- 8c153d2: `writers: 'collaborators'` (the default) now accepts only people with write, maintain or admin access to the repository. Read and triage access are refused; on a public repository GitHub reports read access for anyone, so before this fix any GitHub user could sign in and annotate.
|
|
20
|
+
|
|
21
|
+
## 0.1.1
|
|
22
|
+
|
|
23
|
+
### Patch Changes
|
|
24
|
+
|
|
25
|
+
- 7ebce53: Loads the Markdown parser only when a page has notes or a reader starts writing one. The script added to every page drops from about 100 kB to 27 kB.
|
|
26
|
+
- 943a525: Fixes the annotation UI having no styles in production builds: the stylesheet is now added to Starlight's `customCss`, which is linked on every page. In 0.1.0 the Notes button and panel appeared unstyled at the end of the page.
|
|
27
|
+
- 1d6c768: Points the npm homepage to the documentation site, and adds badges, feature icons and more search keywords to the package page.
|
|
28
|
+
|
|
29
|
+
## 0.1.0
|
|
30
|
+
|
|
31
|
+
### Minor Changes
|
|
32
|
+
|
|
33
|
+
- 859bd1b: First release. Readers select text on a Starlight page and leave Markdown notes with replies; notes are stored as one JSON file per page on an `annotations` branch of a GitHub repository, through OAuth sign-in and a Web-standard handler, or in the reader's browser with `backend: 'browser'`.
|
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
|
|
8
8
|
Let readers highlight text and leave notes on your [Starlight](https://starlight.astro.build) docs. Notes are JSON files committed to your Git repository, not stored by a third-party service.
|
|
9
9
|
|
|
10
|
-
**📖 [Documentation](https://alessandroros.github.io/starlight-annotations/) · ✨ [Live demo](https://alessandroros.github.io/starlight-annotations/guides/demo/)**
|
|
10
|
+
**📖 [Documentation](https://alessandroros.github.io/starlight-annotations/) · ✨ [Live demo](https://alessandroros.github.io/starlight-annotations/guides/demo/) · 📋 [Changelog](https://alessandroros.github.io/starlight-annotations/changelog/)**
|
|
11
11
|
|
|
12
12
|
## Features
|
|
13
13
|
|
|
@@ -71,6 +71,10 @@ On GitLab or Gitea/Forgejo, use `gitlab({ host, project, … })` or `gitea({ hos
|
|
|
71
71
|
|
|
72
72
|
No server? `starlightAnnotations({ backend: 'browser' })` keeps notes in each reader's browser.
|
|
73
73
|
|
|
74
|
+
## 📋 Versions
|
|
75
|
+
|
|
76
|
+
The package follows semantic versioning. While it is `0.x`, a minor version may change behavior or options: check the [changelog](https://alessandroros.github.io/starlight-annotations/changelog/) before upgrading.
|
|
77
|
+
|
|
74
78
|
## 📄 License
|
|
75
79
|
|
|
76
80
|
MIT © Alessandro Rosa
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "starlight-annotations",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "🖍️ Let readers highlight text and leave notes on Starlight docs. Notes are committed to your Git repository on GitHub, GitLab, Gitea or Forgejo.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Alessandro Rosa",
|
|
@@ -52,7 +52,8 @@
|
|
|
52
52
|
"index.d.ts",
|
|
53
53
|
"server.d.ts",
|
|
54
54
|
"server-node.d.ts",
|
|
55
|
-
"src"
|
|
55
|
+
"src",
|
|
56
|
+
"CHANGELOG.md"
|
|
56
57
|
],
|
|
57
58
|
"scripts": {
|
|
58
59
|
"test": "vitest run"
|
package/server.d.ts
CHANGED
|
@@ -24,6 +24,12 @@ export interface Auth {
|
|
|
24
24
|
mode?: string;
|
|
25
25
|
user(request: Request): Promise<User | null>;
|
|
26
26
|
route?(route: string, request: Request, url: URL, prefix: string): Promise<Response | null>;
|
|
27
|
+
/** Whether a signed-in user may still write. Called before every save and on page loads. */
|
|
28
|
+
allowed?(user: User): Promise<boolean>;
|
|
29
|
+
/** The message for a user who may no longer write. */
|
|
30
|
+
noAccess?(login: string): string;
|
|
31
|
+
/** A `Set-Cookie` value that ends the session, sent with a refusal. */
|
|
32
|
+
clearSession?: string;
|
|
27
33
|
}
|
|
28
34
|
|
|
29
35
|
export interface HandlerOptions {
|
|
@@ -61,6 +67,11 @@ export interface GitHubAuthOptions {
|
|
|
61
67
|
writers?: 'collaborators' | 'anyone' | string[] | ((profile: { login: string; id: number }) => boolean | Promise<boolean>);
|
|
62
68
|
repo?: string;
|
|
63
69
|
token?: string;
|
|
70
|
+
/**
|
|
71
|
+
* Seconds between checks that a signed-in user may still write, on saves and page loads.
|
|
72
|
+
* `false` checks only at sign-in. Default 60.
|
|
73
|
+
*/
|
|
74
|
+
recheck?: number | false;
|
|
64
75
|
/** Set to false only for plain-http local testing. Default true. */
|
|
65
76
|
cookieSecure?: boolean;
|
|
66
77
|
}
|
|
@@ -91,6 +102,11 @@ interface CommonAuthOptions {
|
|
|
91
102
|
* `token`. `"anyone"` accepts every signed-in user.
|
|
92
103
|
*/
|
|
93
104
|
writers?: 'collaborators' | 'anyone' | string[] | ((profile: { login: string; id: number }) => boolean | Promise<boolean>);
|
|
105
|
+
/**
|
|
106
|
+
* Seconds between checks that a signed-in user may still write, on saves and page loads.
|
|
107
|
+
* `false` checks only at sign-in. Default 60.
|
|
108
|
+
*/
|
|
109
|
+
recheck?: number | false;
|
|
94
110
|
/** Set to false only for plain-http local testing. Default true. */
|
|
95
111
|
cookieSecure?: boolean;
|
|
96
112
|
}
|
|
@@ -373,7 +373,8 @@ async function start(root) {
|
|
|
373
373
|
const data = await backend.send(page, change);
|
|
374
374
|
if (!data.ok) {
|
|
375
375
|
state.error = data.error;
|
|
376
|
-
|
|
376
|
+
// Not signed in, or the session ended because access was removed.
|
|
377
|
+
if (data.status === 401 || data.signedOut) state.user = null;
|
|
377
378
|
render();
|
|
378
379
|
return null;
|
|
379
380
|
}
|
package/src/client/backend.js
CHANGED
|
@@ -31,7 +31,8 @@ function apiBackend(config, base) {
|
|
|
31
31
|
body: JSON.stringify({ page, change }),
|
|
32
32
|
});
|
|
33
33
|
const data = await response.json().catch(() => ({}));
|
|
34
|
-
if (!response.ok)
|
|
34
|
+
if (!response.ok)
|
|
35
|
+
return { ok: false, status: response.status, signedOut: Boolean(data.signedOut), error: data.error || `Error ${response.status}` };
|
|
35
36
|
return { ok: true, ...data };
|
|
36
37
|
},
|
|
37
38
|
loginUrl: () => `${api}/login?return=${here()}`,
|
package/src/server/index.js
CHANGED
|
@@ -24,6 +24,7 @@ export { AnnotationError } from '../core.js';
|
|
|
24
24
|
export function createAnnotationsHandler({ storage, auth, prefix = ROUTE_PREFIX }) {
|
|
25
25
|
if (!storage) throw new Error('starlight-annotations: createAnnotationsHandler needs a storage.');
|
|
26
26
|
const root = prefix.replace(/\/$/, '');
|
|
27
|
+
const stillAllowed = async (user) => (auth?.allowed ? auth.allowed(user) : true);
|
|
27
28
|
|
|
28
29
|
return async function handleAnnotations(request) {
|
|
29
30
|
const url = new URL(request.url);
|
|
@@ -39,13 +40,21 @@ export function createAnnotationsHandler({ storage, auth, prefix = ROUTE_PREFIX
|
|
|
39
40
|
if (route === '/api' && request.method === 'GET') {
|
|
40
41
|
const page = checkPage(url.searchParams.get('page'));
|
|
41
42
|
const doc = await storage.load(page);
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
43
|
+
let user = auth ? await auth.user(request) : null;
|
|
44
|
+
// Reading never fails on the access check: if the provider does not
|
|
45
|
+
// answer, the reader stays signed in and the next write asks again.
|
|
46
|
+
const revoked = user && (await stillAllowed(user).catch(() => true)) === false;
|
|
47
|
+
if (revoked) user = null;
|
|
48
|
+
return json(
|
|
49
|
+
{
|
|
50
|
+
annotations: doc.annotations,
|
|
51
|
+
user: user && { login: user.login, name: user.name },
|
|
52
|
+
mode: auth?.mode ?? storage.mode ?? 'api',
|
|
53
|
+
canSignIn: Boolean(auth?.route),
|
|
54
|
+
},
|
|
55
|
+
200,
|
|
56
|
+
revoked && auth.clearSession ? { 'Set-Cookie': auth.clearSession } : {},
|
|
57
|
+
);
|
|
49
58
|
}
|
|
50
59
|
|
|
51
60
|
if (route === '/api' && request.method === 'POST') {
|
|
@@ -53,6 +62,10 @@ export function createAnnotationsHandler({ storage, auth, prefix = ROUTE_PREFIX
|
|
|
53
62
|
if (origin && origin !== url.origin) return json({ error: 'Cross-site request refused.' }, 403);
|
|
54
63
|
const user = auth ? await auth.user(request) : null;
|
|
55
64
|
if (!user) return json({ error: 'Sign in first.' }, 401);
|
|
65
|
+
if (!(await stillAllowed(user))) {
|
|
66
|
+
const error = auth.noAccess ? auth.noAccess(user.login) : 'You may no longer annotate these docs.';
|
|
67
|
+
return json({ error, signedOut: true }, 403, auth.clearSession ? { 'Set-Cookie': auth.clearSession } : {});
|
|
68
|
+
}
|
|
56
69
|
const { page, change } = await request.json();
|
|
57
70
|
const result = await storage.update(checkPage(page), (doc) => applyChange(doc, change, user), user);
|
|
58
71
|
return json(result);
|
|
@@ -88,5 +101,8 @@ export function staticAuth(user) {
|
|
|
88
101
|
* mode?: string,
|
|
89
102
|
* user(request: Request): Promise<User | null>,
|
|
90
103
|
* route?(route: string, request: Request, url: URL, prefix: string): Promise<Response | null>,
|
|
104
|
+
* allowed?(user: User): Promise<boolean>,
|
|
105
|
+
* noAccess?(login: string): string,
|
|
106
|
+
* clearSession?: string,
|
|
91
107
|
* }} Auth
|
|
92
108
|
*/
|
package/src/server/oauth.js
CHANGED
|
@@ -3,7 +3,9 @@
|
|
|
3
3
|
//
|
|
4
4
|
// The reader's access token is used once, at the callback, to read who they
|
|
5
5
|
// are, and is then dropped. The session is an HMAC-signed cookie, so the
|
|
6
|
-
// server keeps no state. `writers` decides who may write
|
|
6
|
+
// server keeps no state. `writers` decides who may write, at sign-in and
|
|
7
|
+
// again while the session lasts (every `recheck` seconds), so removing
|
|
8
|
+
// someone's access to the repository also ends their session.
|
|
7
9
|
|
|
8
10
|
import {
|
|
9
11
|
base64url,
|
|
@@ -19,6 +21,8 @@ import {
|
|
|
19
21
|
const SESSION_COOKIE = 'annot_session';
|
|
20
22
|
const STATE_COOKIE = 'annot_state';
|
|
21
23
|
const SESSION_AGE = 60 * 60 * 24 * 30;
|
|
24
|
+
const RECHECK = 60;
|
|
25
|
+
const CACHE_SIZE = 500;
|
|
22
26
|
|
|
23
27
|
/**
|
|
24
28
|
* @param {{
|
|
@@ -31,6 +35,7 @@ const SESSION_AGE = 60 * 60 * 24 * 30;
|
|
|
31
35
|
* clientSecret: string,
|
|
32
36
|
* sessionSecret: string,
|
|
33
37
|
* cookieSecure?: boolean,
|
|
38
|
+
* recheck?: number | false,
|
|
34
39
|
* writers?: 'collaborators' | 'anyone' | string[] | ((profile: object) => boolean | Promise<boolean>),
|
|
35
40
|
* profile: (userToken: string) => Promise<{ login: string, id: number, name?: string }>,
|
|
36
41
|
* canWrite: (profile: { login: string, id: number }) => Promise<boolean>,
|
|
@@ -40,6 +45,7 @@ const SESSION_AGE = 60 * 60 * 24 * 30;
|
|
|
40
45
|
export function oauthAuth(options) {
|
|
41
46
|
const { mode, where, authorizeUrl, tokenUrl, scope, clientId, clientSecret, sessionSecret, cookieSecure = true } = options;
|
|
42
47
|
const writers = options.writers ?? 'collaborators';
|
|
48
|
+
const recheck = options.recheck ?? RECHECK;
|
|
43
49
|
required({ clientId, clientSecret, sessionSecret }, where);
|
|
44
50
|
|
|
45
51
|
const cookie = (name, value, maxAge, path = '/') =>
|
|
@@ -52,6 +58,17 @@ export function oauthAuth(options) {
|
|
|
52
58
|
return options.canWrite(profile);
|
|
53
59
|
}
|
|
54
60
|
|
|
61
|
+
// Answers of `mayWrite` for signed-in users, kept `recheck` seconds. It
|
|
62
|
+
// lives in memory, per server instance: a cold start just asks again.
|
|
63
|
+
const answers = new Map();
|
|
64
|
+
function remember(user, allowed) {
|
|
65
|
+
if (!recheck) return;
|
|
66
|
+
const key = `${user.id}:${user.login}`;
|
|
67
|
+
answers.delete(key);
|
|
68
|
+
answers.set(key, { allowed, until: Date.now() + recheck * 1000 });
|
|
69
|
+
if (answers.size > CACHE_SIZE) answers.delete(answers.keys().next().value);
|
|
70
|
+
}
|
|
71
|
+
|
|
55
72
|
async function hmac(value) {
|
|
56
73
|
const key = await crypto.subtle.importKey('raw', new TextEncoder().encode(sessionSecret), { name: 'HMAC', hash: 'SHA-256' }, false, ['sign']);
|
|
57
74
|
return base64url(new Uint8Array(await crypto.subtle.sign('HMAC', key, new TextEncoder().encode(value))));
|
|
@@ -59,6 +76,19 @@ export function oauthAuth(options) {
|
|
|
59
76
|
|
|
60
77
|
return {
|
|
61
78
|
mode,
|
|
79
|
+
clearSession: cookie(SESSION_COOKIE, '', 0),
|
|
80
|
+
|
|
81
|
+
/** Whether a signed-in user may still write. Asks the provider at most every `recheck` seconds. */
|
|
82
|
+
async allowed(user) {
|
|
83
|
+
if (recheck === false) return true;
|
|
84
|
+
const saved = answers.get(`${user.id}:${user.login}`);
|
|
85
|
+
if (saved && saved.until > Date.now()) return saved.allowed;
|
|
86
|
+
const allowed = await mayWrite(user);
|
|
87
|
+
remember(user, allowed);
|
|
88
|
+
return allowed;
|
|
89
|
+
},
|
|
90
|
+
|
|
91
|
+
noAccess: options.noAccess,
|
|
62
92
|
|
|
63
93
|
async user(request) {
|
|
64
94
|
const value = readCookie(request, SESSION_COOKIE);
|
|
@@ -114,7 +144,9 @@ export function oauthAuth(options) {
|
|
|
114
144
|
if (!userToken) return text('The sign-in was not confirmed. Try again.', 400, [clear]);
|
|
115
145
|
|
|
116
146
|
const profile = await options.profile(userToken);
|
|
117
|
-
|
|
147
|
+
const allowed = await mayWrite(profile);
|
|
148
|
+
remember(profile, allowed);
|
|
149
|
+
if (!allowed) return text(options.noAccess(profile.login), 403, [clear]);
|
|
118
150
|
|
|
119
151
|
const body = base64url(
|
|
120
152
|
new TextEncoder().encode(
|