@tollbooth-dpyc/web 2.0.0 → 2.0.1
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/dist/holder.js +12 -7
- package/package.json +1 -1
package/dist/holder.js
CHANGED
|
@@ -26,8 +26,12 @@ function operator() {
|
|
|
26
26
|
export function holderEnabled() {
|
|
27
27
|
return Boolean(tollboothConfig().operatorUrl);
|
|
28
28
|
}
|
|
29
|
+
/**
|
|
30
|
+
* Back to the page the sign-in started on, since that page finishes it: a site
|
|
31
|
+
* whose `/` is not the sign-in page would otherwise drop the code on the floor.
|
|
32
|
+
*/
|
|
29
33
|
function redirectUri() {
|
|
30
|
-
return `${globalThis.location.origin}
|
|
34
|
+
return `${globalThis.location.origin}${globalThis.location.pathname}`;
|
|
31
35
|
}
|
|
32
36
|
function readTokens() {
|
|
33
37
|
try {
|
|
@@ -49,13 +53,13 @@ async function challengeFor(verifier) {
|
|
|
49
53
|
const digest = new Uint8Array(await crypto.subtle.digest("SHA-256", new TextEncoder().encode(verifier)));
|
|
50
54
|
return btoa(String.fromCharCode(...digest)).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
51
55
|
}
|
|
52
|
-
async function register() {
|
|
56
|
+
async function register(redirect) {
|
|
53
57
|
const r = await fetch(`${operator()}/register`, {
|
|
54
58
|
method: "POST",
|
|
55
59
|
headers: { "Content-Type": "application/json" },
|
|
56
60
|
body: JSON.stringify({
|
|
57
61
|
client_name: tollboothConfig().appName,
|
|
58
|
-
redirect_uris: [
|
|
62
|
+
redirect_uris: [redirect],
|
|
59
63
|
grant_types: ["authorization_code", "refresh_token"],
|
|
60
64
|
response_types: ["code"],
|
|
61
65
|
token_endpoint_auth_method: "none",
|
|
@@ -90,16 +94,17 @@ export function isHolder() {
|
|
|
90
94
|
* sends it straight back. `resume` comes back from `completeHolderConnect`.
|
|
91
95
|
*/
|
|
92
96
|
export async function connectAsHolder(resume) {
|
|
93
|
-
const
|
|
97
|
+
const redirect = redirectUri();
|
|
98
|
+
const clientId = readTokens()?.clientId || (await register(redirect));
|
|
94
99
|
const verifier = randomUrlSafe(48);
|
|
95
100
|
const state = randomUrlSafe(16);
|
|
96
|
-
const pending = { clientId, verifier, state, resume };
|
|
101
|
+
const pending = { clientId, verifier, state, resume, redirectUri: redirect };
|
|
97
102
|
globalThis.sessionStorage.setItem(PENDING, JSON.stringify(pending));
|
|
98
103
|
const url = new URL(`${operator()}/authorize`);
|
|
99
104
|
url.search = new URLSearchParams({
|
|
100
105
|
response_type: "code",
|
|
101
106
|
client_id: clientId,
|
|
102
|
-
redirect_uri:
|
|
107
|
+
redirect_uri: redirect,
|
|
103
108
|
state,
|
|
104
109
|
code_challenge: await challengeFor(verifier),
|
|
105
110
|
code_challenge_method: "S256",
|
|
@@ -123,7 +128,7 @@ export async function completeHolderConnect() {
|
|
|
123
128
|
globalThis.history.replaceState(null, "", globalThis.location.pathname);
|
|
124
129
|
if (params.get("state") !== pending.state)
|
|
125
130
|
throw new Error("The service's reply did not match this sign-in.");
|
|
126
|
-
writeTokens(await token({ grant_type: "authorization_code", code, redirect_uri: redirectUri
|
|
131
|
+
writeTokens(await token({ grant_type: "authorization_code", code, redirect_uri: pending.redirectUri, code_verifier: pending.verifier }, pending.clientId));
|
|
127
132
|
return pending.resume;
|
|
128
133
|
}
|
|
129
134
|
/** The bearer for the next request, refreshed when it is about to lapse; null when not a holder. */
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tollbooth-dpyc/web",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"description": "Shared browser SDK for Tollbooth DPYC operator front ends: npub sign-in, proofs, MCP client, profile and wallet",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Lonnie VanZandt",
|