@tempoxyz/auth 0.0.0-pr-17.sha-08e5136 → 0.0.0-pr-18.sha-50947da

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
  /**
@@ -227,7 +228,7 @@ export function handleRedirect(auth) {
227
228
  return undefined;
228
229
  // The app keeps its own query and fragment, without the callback's parameters.
229
230
  const clean = new URL(url);
230
- for (const name of ['account', 'code', 'key_authorization', 'state'])
231
+ for (const name of ['code', 'state'])
231
232
  clean.searchParams.delete(name);
232
233
  window.history.replaceState(null, '', clean.pathname + clean.search + clean.hash);
233
234
  return handleCallback(auth, { pending: JSON.parse(stored), url });
@@ -257,11 +258,11 @@ export async function redirect(auth, options) {
257
258
  * })
258
259
  * ```
259
260
  */
260
- export function refresh(auth, options) {
261
- return token(auth, {
261
+ export async function refresh(auth, options) {
262
+ return tokens(await token(auth, {
262
263
  grant_type: 'refresh_token',
263
264
  refresh_token: options.refreshToken,
264
- });
265
+ }));
265
266
  }
266
267
  /**
267
268
  * Emails the user a sign-in code. The response is the same for every address.
@@ -383,14 +384,14 @@ function storage(auth) {
383
384
  * OAuth form posts to WorkOS.
384
385
  */
385
386
  async function token(auth, body) {
386
- return tokens(await json(await (auth.fetch ?? fetch)(new URL('/oauth2/token', auth.url), {
387
+ return json(await (auth.fetch ?? fetch)(new URL('/oauth2/token', auth.url), {
387
388
  body: new URLSearchParams({
388
389
  ...body,
389
390
  client_id: auth.clientId,
390
391
  ...(auth.clientSecret ? { client_secret: auth.clientSecret } : {}),
391
392
  }),
392
393
  method: 'POST',
393
- })));
394
+ }));
394
395
  }
395
396
  /** Reads a token response into `Tokens`. */
396
397
  function tokens(response) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tempoxyz/auth",
3
- "version": "0.0.0-pr-17.sha-08e5136",
3
+ "version": "0.0.0-pr-18.sha-50947da",
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": {