@tempoxyz/auth 0.0.0-pr-21.sha-ca15e9a → 0.0.0-pr-19.sha-2c8fa8e

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/Actions.d.ts CHANGED
@@ -52,9 +52,9 @@ export declare namespace authorizeDevice {
52
52
  };
53
53
  }
54
54
  /**
55
- * Returns the URL that signs the user in on Tempo Auth's pages and redirects back with a code
56
- * and the key authorization, and the {@link Pending} state to keep until then, such as in a
57
- * sealed cookie. {@link handleCallback} finishes it.
55
+ * Returns the URL that signs the user in on Tempo Auth's pages and redirects back with a code,
56
+ * and the {@link Pending} state to keep until then, such as in a sealed cookie.
57
+ * {@link handleCallback} redeems the code for the tokens and the key authorization.
58
58
  *
59
59
  * @example
60
60
  * ```ts
@@ -130,8 +130,8 @@ export declare namespace getTempoAccounts {
130
130
  type ReturnType = readonly TempoAccount[];
131
131
  }
132
132
  /**
133
- * Finishes a sign-in redirect: checks its state, redeems the code with the PKCE verifier (and the
134
- * client secret, for a backend), and reads the key authorization from the callback URL.
133
+ * Finishes a sign-in redirect: checks its state, and redeems the code with the PKCE verifier (and
134
+ * the client secret, for a backend) for the tokens and the key authorization.
135
135
  *
136
136
  * @example
137
137
  * ```ts
package/dist/Actions.js CHANGED
@@ -89,9 +89,9 @@ export async function authorizeDevice(auth, options) {
89
89
  };
90
90
  }
91
91
  /**
92
- * Returns the URL that signs the user in on Tempo Auth's pages and redirects back with a code
93
- * and the key authorization, and the {@link Pending} state to keep until then, such as in a
94
- * sealed cookie. {@link handleCallback} finishes it.
92
+ * Returns the URL that signs the user in on Tempo Auth's pages and redirects back with a code,
93
+ * and the {@link Pending} state to keep until then, such as in a sealed cookie.
94
+ * {@link handleCallback} redeems the code for the tokens and the key authorization.
95
95
  *
96
96
  * @example
97
97
  * ```ts
@@ -171,8 +171,8 @@ export async function getTempoAccounts(auth) {
171
171
  return tempo.map(({ address }) => ({ address }));
172
172
  }
173
173
  /**
174
- * Finishes a sign-in redirect: checks its state, redeems the code with the PKCE verifier (and the
175
- * client secret, for a backend), and reads the key authorization from the callback URL.
174
+ * Finishes a sign-in redirect: checks its state, and redeems the code with the PKCE verifier (and
175
+ * the client secret, for a backend) for the tokens and the key authorization.
176
176
  *
177
177
  * @example
178
178
  * ```ts
@@ -186,25 +186,26 @@ export async function handleCallback(auth, options) {
186
186
  const { pending } = options;
187
187
  const params = new URL(options.url).searchParams;
188
188
  const code = params.get('code');
189
- const account = params.get('account');
190
- const keyAuthorization = params.get('key_authorization');
191
- if (!code ||
192
- !account ||
193
- !keyAuthorization ||
194
- params.get('state') !== pending.state)
189
+ if (!code || params.get('state') !== pending.state)
190
+ throw new RequestError({
191
+ message: 'Sign-in failed. Start again.',
192
+ status: 400,
193
+ });
194
+ const body = await token(auth, {
195
+ code,
196
+ code_verifier: pending.verifier,
197
+ grant_type: 'authorization_code',
198
+ redirect_uri: pending.redirectUri,
199
+ });
200
+ if (!body.account || !body.key_authorization)
195
201
  throw new RequestError({
196
202
  message: 'Sign-in failed. Start again.',
197
203
  status: 400,
198
204
  });
199
205
  return authorization({
200
- account: account,
201
- keyAuthorization: JSON.parse(Base64.toString(keyAuthorization)),
202
- tokens: await token(auth, {
203
- code,
204
- code_verifier: pending.verifier,
205
- grant_type: 'authorization_code',
206
- redirect_uri: pending.redirectUri,
207
- }),
206
+ account: body.account,
207
+ keyAuthorization: body.key_authorization,
208
+ tokens: body,
208
209
  }, pending);
209
210
  }
210
211
  /**
@@ -223,13 +224,16 @@ export function handleRedirect(auth) {
223
224
  const stored = storage(auth).getItem(storageKey);
224
225
  storage(auth).removeItem(storageKey);
225
226
  const url = new URL(window.location.href);
226
- if (!stored || !url.searchParams.has('code'))
227
+ if (!url.searchParams.has('code'))
227
228
  return undefined;
228
- // The app keeps its own query and fragment, without the callback's parameters.
229
+ // The app keeps its own query and fragment, without the callback's parameters. A code this page
230
+ // did not request is dropped too, since it redeems for whoever crafted its request.
229
231
  const clean = new URL(url);
230
- for (const name of ['account', 'code', 'key_authorization', 'state'])
232
+ for (const name of ['code', 'state'])
231
233
  clean.searchParams.delete(name);
232
234
  window.history.replaceState(null, '', clean.pathname + clean.search + clean.hash);
235
+ if (!stored)
236
+ return undefined;
233
237
  return handleCallback(auth, { pending: JSON.parse(stored), url });
234
238
  })();
235
239
  return redirected;
@@ -257,11 +261,11 @@ export async function redirect(auth, options) {
257
261
  * })
258
262
  * ```
259
263
  */
260
- export function refresh(auth, options) {
261
- return token(auth, {
264
+ export async function refresh(auth, options) {
265
+ return tokens(await token(auth, {
262
266
  grant_type: 'refresh_token',
263
267
  refresh_token: options.refreshToken,
264
- });
268
+ }));
265
269
  }
266
270
  /**
267
271
  * Emails the user a sign-in code. The response is the same for every address.
@@ -383,14 +387,14 @@ function storage(auth) {
383
387
  * OAuth form posts to WorkOS.
384
388
  */
385
389
  async function token(auth, body) {
386
- return tokens(await json(await (auth.fetch ?? fetch)(new URL('/oauth2/token', auth.url), {
390
+ return json(await (auth.fetch ?? fetch)(new URL('/oauth2/token', auth.url), {
387
391
  body: new URLSearchParams({
388
392
  ...body,
389
393
  client_id: auth.clientId,
390
394
  ...(auth.clientSecret ? { client_secret: auth.clientSecret } : {}),
391
395
  }),
392
396
  method: 'POST',
393
- })));
397
+ }));
394
398
  }
395
399
  /** Reads a token response into `Tokens`. */
396
400
  function tokens(response) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tempoxyz/auth",
3
- "version": "0.0.0-pr-21.sha-ca15e9a",
3
+ "version": "0.0.0-pr-19.sha-2c8fa8e",
4
4
  "description": "Signs users in to Tempo through Tempo Auth, and returns viem access key accounts on their Tempo Accounts.",
5
5
  "type": "module",
6
6
  "exports": {