@bbthorson/atproto-cf-auth 0.2.1 → 0.4.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/README.md +152 -15
- package/dist/accounts.d.ts +91 -0
- package/dist/accounts.js +179 -0
- package/dist/accounts.js.map +1 -0
- package/dist/client.d.ts +3 -16
- package/dist/client.js +3 -28
- package/dist/client.js.map +1 -1
- package/dist/controller.d.ts +86 -0
- package/dist/controller.js +154 -0
- package/dist/controller.js.map +1 -0
- package/dist/element.css +254 -0
- package/dist/element.js +246 -56
- package/dist/element.js.map +1 -1
- package/dist/search.d.ts +16 -0
- package/dist/search.js +38 -0
- package/dist/search.js.map +1 -0
- package/dist/server/auth.d.ts +60 -1
- package/dist/server/auth.js +159 -11
- package/dist/server/auth.js.map +1 -1
- package/dist/server/handoff.d.ts +49 -0
- package/dist/server/handoff.js +97 -0
- package/dist/server/handoff.js.map +1 -0
- package/dist/server/index.d.ts +2 -0
- package/dist/server/index.js +2 -0
- package/dist/server/index.js.map +1 -1
- package/dist/server/scopes.d.ts +57 -0
- package/dist/server/scopes.js +84 -0
- package/dist/server/scopes.js.map +1 -0
- package/package.json +8 -5
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Scopes asked for only where they can be granted.
|
|
3
|
+
*
|
|
4
|
+
* A permission the user's PDS can't honour has no business on their consent
|
|
5
|
+
* screen, and a strict authorization server is within its rights to reject the
|
|
6
|
+
* whole sign-in over it (`invalid_scope`): space scopes asked of a PDS without
|
|
7
|
+
* spaces fail the login outright, not just the feature. So an app lists the
|
|
8
|
+
* scopes it always needs in `scope`, and the rest as conditional scopes, each
|
|
9
|
+
* asked for only when its test passes for the PDS signing this person in.
|
|
10
|
+
*
|
|
11
|
+
* Every scope either list names is published in the client metadata all the
|
|
12
|
+
* same: the metadata's `scope` bounds what the client may ask for, and some
|
|
13
|
+
* authorization servers reject a request that asks for more than it declares.
|
|
14
|
+
*/
|
|
15
|
+
/** Space-separated scopes, each once, in first-seen order. */
|
|
16
|
+
export function joinScopes(...scopes) {
|
|
17
|
+
const seen = new Set();
|
|
18
|
+
for (const scope of scopes) {
|
|
19
|
+
for (const token of (scope ?? "").split(/\s+/))
|
|
20
|
+
if (token)
|
|
21
|
+
seen.add(token);
|
|
22
|
+
}
|
|
23
|
+
return [...seen].join(" ");
|
|
24
|
+
}
|
|
25
|
+
/** The base scope plus every conditional scope whose test passes for this PDS. */
|
|
26
|
+
export async function negotiateScope(base, conditional, pds) {
|
|
27
|
+
const granted = await Promise.all(conditional.map(async (c) => {
|
|
28
|
+
try {
|
|
29
|
+
return (await c.when(pds)) ? c.scope : undefined;
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
}));
|
|
35
|
+
return joinScopes(base, ...granted);
|
|
36
|
+
}
|
|
37
|
+
const DESCRIBE = "community.lexicon.service.describe";
|
|
38
|
+
/**
|
|
39
|
+
* A `when` test: does the PDS say it serves method `nsid`?
|
|
40
|
+
*
|
|
41
|
+
* Asks the PDS's `community.lexicon.service.describe`, a community convention
|
|
42
|
+
* (the lexicon.community set) that a service uses to list the methods it
|
|
43
|
+
* serves. A PDS that doesn't answer, answers with an error, or doesn't list
|
|
44
|
+
* `nsid` fails the test: an optional scope is left off rather than risk the
|
|
45
|
+
* sign-in. Answers are cached per PDS.
|
|
46
|
+
*
|
|
47
|
+
* ```ts
|
|
48
|
+
* conditionalScopes: [
|
|
49
|
+
* { scope: "space:com.example.notes?manage=create", when: pdsServesMethod("com.atproto.space.createSpace") },
|
|
50
|
+
* ]
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export function pdsServesMethod(nsid, options = {}) {
|
|
54
|
+
const doFetch = options.fetch ?? fetch;
|
|
55
|
+
const cacheMs = options.cacheMs ?? 10 * 60 * 1000;
|
|
56
|
+
const timeoutMs = options.timeoutMs ?? 3000;
|
|
57
|
+
const cache = new Map();
|
|
58
|
+
return async ({ pds }) => {
|
|
59
|
+
const key = pds.origin;
|
|
60
|
+
const hit = cache.get(key);
|
|
61
|
+
if (hit && Date.now() - hit.at < cacheMs)
|
|
62
|
+
return hit.methods.has(nsid);
|
|
63
|
+
let methods = new Set();
|
|
64
|
+
try {
|
|
65
|
+
const res = await doFetch(`${key}/xrpc/${DESCRIBE}`, { signal: AbortSignal.timeout(timeoutMs) });
|
|
66
|
+
if (res.ok) {
|
|
67
|
+
const body = (await res.json());
|
|
68
|
+
if (Array.isArray(body.methods)) {
|
|
69
|
+
for (const m of body.methods) {
|
|
70
|
+
const value = typeof m === "string" ? m : m?.value;
|
|
71
|
+
if (typeof value === "string")
|
|
72
|
+
methods.add(value);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
methods = new Set();
|
|
79
|
+
}
|
|
80
|
+
cache.set(key, { methods, at: Date.now() });
|
|
81
|
+
return methods.has(nsid);
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
//# sourceMappingURL=scopes.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scopes.js","sourceRoot":"","sources":["../../src/server/scopes.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAmBH,8DAA8D;AAC9D,MAAM,UAAU,UAAU,CAAC,GAAG,MAA8B;IAC1D,MAAM,IAAI,GAAG,IAAI,GAAG,EAAU,CAAC;IAC/B,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;QAC3B,KAAK,MAAM,KAAK,IAAI,CAAC,KAAK,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC;YAAE,IAAI,KAAK;gBAAE,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IAC7E,CAAC;IACD,OAAO,CAAC,GAAG,IAAI,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAC7B,CAAC;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,IAAI,GAAG,EAAU,CAAC;QAChC,IAAI,CAAC;YACH,MAAM,GAAG,GAAG,MAAM,OAAO,CAAC,GAAG,GAAG,SAAS,QAAQ,EAAE,EAAE,EAAE,MAAM,EAAE,WAAW,CAAC,OAAO,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC;YACjG,IAAI,GAAG,CAAC,EAAE,EAAE,CAAC;gBACX,MAAM,IAAI,GAAG,CAAC,MAAM,GAAG,CAAC,IAAI,EAAE,CAA0B,CAAC;gBACzD,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC;oBAChC,KAAK,MAAM,CAAC,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;wBAC7B,MAAM,KAAK,GAAG,OAAO,CAAC,KAAK,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAE,CAAgC,EAAE,KAAK,CAAC;wBACnF,IAAI,OAAO,KAAK,KAAK,QAAQ;4BAAE,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;oBACpD,CAAC;gBACH,CAAC;YACH,CAAC;QACH,CAAC;QAAC,MAAM,CAAC;YACP,OAAO,GAAG,IAAI,GAAG,EAAE,CAAC;QACtB,CAAC;QACD,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE,IAAI,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC;QAC5C,OAAO,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IAC3B,CAAC,CAAC;AACJ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bbthorson/atproto-cf-auth",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Bluesky (AT-Proto OAuth) sign-in for apps on Cloudflare Workers: server handlers, D1/KV session stores, and framework-free browser primitives.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -9,7 +9,8 @@
|
|
|
9
9
|
"url": "git+https://github.com/bbthorson/atproto-cf-auth.git"
|
|
10
10
|
},
|
|
11
11
|
"sideEffects": [
|
|
12
|
-
"./dist/element.js"
|
|
12
|
+
"./dist/element.js",
|
|
13
|
+
"*.css"
|
|
13
14
|
],
|
|
14
15
|
"exports": {
|
|
15
16
|
"./server": {
|
|
@@ -23,7 +24,8 @@
|
|
|
23
24
|
"./element": {
|
|
24
25
|
"types": "./dist/element.d.ts",
|
|
25
26
|
"default": "./dist/element.js"
|
|
26
|
-
}
|
|
27
|
+
},
|
|
28
|
+
"./element.css": "./dist/element.css"
|
|
27
29
|
},
|
|
28
30
|
"files": [
|
|
29
31
|
"dist",
|
|
@@ -31,7 +33,7 @@
|
|
|
31
33
|
"LICENSE"
|
|
32
34
|
],
|
|
33
35
|
"scripts": {
|
|
34
|
-
"build": "rm -rf dist && tsc -p tsconfig.build.json",
|
|
36
|
+
"build": "rm -rf dist && tsc -p tsconfig.build.json && cp src/element.css dist/element.css",
|
|
35
37
|
"typecheck": "tsc --noEmit",
|
|
36
38
|
"test": "vitest run",
|
|
37
39
|
"prepublishOnly": "npm run typecheck && npm test && npm run build"
|
|
@@ -57,7 +59,8 @@
|
|
|
57
59
|
"oauth",
|
|
58
60
|
"cloudflare-workers",
|
|
59
61
|
"d1",
|
|
60
|
-
"web-component"
|
|
62
|
+
"web-component",
|
|
63
|
+
"typeahead"
|
|
61
64
|
],
|
|
62
65
|
"publishConfig": {
|
|
63
66
|
"access": "public"
|