@tempoxyz/auth 0.0.0-pr-66.sha-7651eb5 → 0.0.0-pr-66.sha-278d6ea

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.
@@ -61,7 +61,10 @@ export declare namespace tempoAuth {
61
61
  type Decorator<chainId extends number> = {
62
62
  /** Tempo Auth's actions, with the extension's instance. */
63
63
  auth: {
64
- /** `Actions.authorize`, which sets the client's account. */
64
+ /**
65
+ * `Actions.authorize`, which sets the client's account, or empties it when the user has no
66
+ * session. A sign-in or sign-out that completes first keeps its account.
67
+ */
65
68
  authorize: <const id extends number = chainId>(options?: Actions.authorize.Options<id>) => Promise<Actions.authorize.ReturnType<id>>;
66
69
  device: {
67
70
  /** `Actions.device.authorize`, whose `wait` sets the client's account. */
package/dist/Decorator.js CHANGED
@@ -39,15 +39,29 @@ export function tempoAuth(options) {
39
39
  if (!state)
40
40
  throw new AccountRequiredError();
41
41
  const auth = Auth.create({ ...options, chainId: client.chain.id });
42
+ // Counts changes to the client's account, so a session check that a sign-in or sign-out
43
+ // overtook leaves the newer account in place.
44
+ let version = 0;
42
45
  /** Sets the client's account to the authorization's, and returns the authorization. */
43
46
  const set = (authorization) => {
44
- if (authorization)
47
+ if (authorization) {
45
48
  state.account = authorization.account;
49
+ version++;
50
+ }
46
51
  return authorization;
47
52
  };
48
53
  return {
49
54
  auth: {
50
- authorize: async (options = {}) => set(await Actions.authorize(auth, options)),
55
+ async authorize(options = {}) {
56
+ const started = version;
57
+ const authorization = await Actions.authorize(auth, options);
58
+ if (version !== started)
59
+ return authorization;
60
+ // Without a session, the client stops signing as the user it last authorized.
61
+ if (!authorization)
62
+ state.account = undefined;
63
+ return set(authorization);
64
+ },
51
65
  device: {
52
66
  async authorize(options = {}) {
53
67
  const device = await Actions.device.authorize(auth, options);
@@ -96,6 +110,7 @@ export function tempoAuth(options) {
96
110
  }
97
111
  finally {
98
112
  state.account = undefined;
113
+ version++;
99
114
  }
100
115
  },
101
116
  tempoAccount: {
@@ -61,17 +61,13 @@ export async function jwks(auth) {
61
61
  */
62
62
  export async function userInfo(auth, options) {
63
63
  const response = await (auth.fetch ?? fetch)(new URL('/oauth2/userinfo', auth.url), { headers: { authorization: `Bearer ${options.accessToken}` } });
64
- // The issuer refuses a bad token in `WWW-Authenticate`, which may come without a body (RFC 6750 §3).
65
- if (response.status === 401) {
66
- const body = (await response
67
- .json()
68
- .catch(() => ({})));
64
+ // RFC 6750 §3 names a rejected bearer token `invalid_token`. The issuer's 401 body uses its own
65
+ // code, or none, so the refusal always carries the standard one.
66
+ if (response.status === 401)
69
67
  throw Api.error({
70
68
  error: 'invalid_token',
71
69
  error_description: 'Invalid or expired access token. Refresh the tokens, or sign in again.',
72
- ...body,
73
70
  }, { status: 401 });
74
- }
75
71
  const claims = await Api.json(response);
76
72
  return { email: claims.email, id: claims.sub };
77
73
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tempoxyz/auth",
3
- "version": "0.0.0-pr-66.sha-7651eb5",
3
+ "version": "0.0.0-pr-66.sha-278d6ea",
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": {