@tempoxyz/auth 0.0.0-pr-67.sha-d4901e4 → 0.0.0-pr-63.sha-1b5c8c2
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 +47 -253
- package/dist/Actions.d.ts +8 -6
- package/dist/Actions.js +1 -0
- package/dist/Auth.d.ts +69 -3
- package/dist/Auth.js +61 -35
- package/dist/Authorization.d.ts +8 -5
- package/dist/Decorator.d.ts +139 -0
- package/dist/Decorator.js +158 -0
- package/dist/Key.d.ts +17 -4
- package/dist/Key.js +8 -3
- package/dist/Permissions.d.ts +8 -7
- package/dist/Permissions.js +1 -1
- package/dist/actions/device.d.ts +12 -9
- package/dist/actions/device.js +12 -3
- package/dist/actions/email.d.ts +5 -5
- package/dist/actions/oauth.d.ts +85 -0
- package/dist/actions/oauth.js +75 -0
- package/dist/actions/redirect.d.ts +2 -2
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/internal/accessKey.d.ts +1 -1
- package/dist/internal/account.d.ts +13 -0
- package/dist/internal/account.js +2 -0
- package/dist/internal/api.d.ts +4 -3
- package/dist/internal/api.js +2 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,294 +1,88 @@
|
|
|
1
1
|
# @tempoxyz/auth
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Signs users in to Tempo through [Tempo Auth](../../docs/README.md), and returns their tokens for Tempo APIs and a [Viem](https://viem.sh) access key account on their Tempo Account.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## Overview
|
|
6
6
|
|
|
7
7
|
```ts
|
|
8
8
|
import { Actions, Auth } from '@tempoxyz/auth'
|
|
9
9
|
import { createClient, Expiry, Scopes } from 'viem/tempo'
|
|
10
|
-
import { tempo, tempoTestnet } from 'viem/tempo/chains'
|
|
11
10
|
import * as Tokens from 'viem/tokens'
|
|
12
11
|
|
|
13
|
-
|
|
14
|
-
const
|
|
12
|
+
const auth = Auth.create({ clientId: 'client_…' })
|
|
13
|
+
const client = createClient()
|
|
14
|
+
const ousd = Tokens.ousd(client.chain.id).address
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
clientId: 'client_…',
|
|
19
|
-
url: 'https://auth.tempo.xyz',
|
|
20
|
-
})
|
|
21
|
-
const client = createClient({
|
|
22
|
-
chain: tempoTestnet,
|
|
23
|
-
feeToken: ousd,
|
|
24
|
-
})
|
|
16
|
+
// 1. Email the user a sign-in code.
|
|
17
|
+
await Actions.email.sendCode(auth, { email: 'user@example.com' })
|
|
25
18
|
|
|
26
|
-
|
|
19
|
+
// 2. Sign in with the code, and authorize an access key.
|
|
20
|
+
const result = await Actions.email.signIn(auth, {
|
|
21
|
+
code: '123456',
|
|
22
|
+
email: 'user@example.com',
|
|
27
23
|
permissions: {
|
|
28
24
|
expiry: Expiry.days(1),
|
|
29
25
|
limits: [{ limit: 10_000_000n, token: ousd }],
|
|
30
26
|
scopes: [Scopes.tip20(ousd).transfer()],
|
|
31
27
|
},
|
|
32
28
|
})
|
|
29
|
+
if ('step' in result) throw new Error('This address signs in through SSO.')
|
|
33
30
|
|
|
31
|
+
// 3. Call a Tempo API with the session cookie…
|
|
32
|
+
await fetch('https://api.example/me', { credentials: 'include' })
|
|
33
|
+
|
|
34
|
+
// …or call the Tempo chain with the access key, through Viem.
|
|
34
35
|
await client.token.transferSync({
|
|
35
|
-
account:
|
|
36
|
+
account: result.account,
|
|
36
37
|
amount: 10_000n,
|
|
37
38
|
to: '0x…',
|
|
38
39
|
token: ousd,
|
|
39
40
|
})
|
|
40
41
|
```
|
|
41
42
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
```bash
|
|
45
|
-
pnpm add @tempoxyz/auth viem
|
|
46
|
-
```
|
|
47
|
-
|
|
48
|
-
## Before you start
|
|
49
|
-
|
|
50
|
-
- **Register the app.** Create a WorkOS Connect app in the Tempo Issuer environment, and register every redirect URI it uses. Its `client_id` is the `clientId` that `Auth.create` takes. An app with a backend also receives a client secret.
|
|
51
|
-
- **Allow the app's origin.** Tempo Auth allows the origins of every first-party Connect app's `http(s)` redirect URIs, within about three minutes of a change. A mobile app has no origin, and registers its custom scheme as its redirect URI instead.
|
|
52
|
-
|
|
53
|
-
The CLI device grant needs neither. It signs in as Tempo Auth's own `tempo-cli` app.
|
|
54
|
-
|
|
55
|
-
## Access keys
|
|
56
|
-
|
|
57
|
-
Each sign-in creates a key for the app and requests that Tempo Auth authorize it on the user's Tempo Account. The optional `permissions` option specifies what the key may do, with viem's `Expiry` and `Scopes` helpers. Tempo Auth fills every field it leaves out with the headless ceiling:
|
|
58
|
-
|
|
59
|
-
| Field | Meaning |
|
|
60
|
-
| --------- | ----------------------------------------------------------------------------------------------------------- |
|
|
61
|
-
| `chainId` | The chain the key acts on. Defaults to the instance's `chainId` |
|
|
62
|
-
| `expiry` | When the key expires, in Unix seconds, such as `Expiry.days(1)`. Defaults to a year |
|
|
63
|
-
| `limits` | One-time spending limits per token, such as `[{ limit: 10_000_000n, token }]`. Defaults to about $1,000 of each token the `scopes` call, or of OUSD |
|
|
64
|
-
| `scopes` | The calls the key may make, such as `[Scopes.tip20(token).transfer()]`. Defaults to transfers of each token the key limits |
|
|
65
|
-
|
|
66
|
-
In a browser, the key is a non-extractable WebCrypto P256 key, so no script can read its private key. A CLI, a native app, or a backend uses a secp256k1 key, which it stores as a string.
|
|
67
|
-
|
|
68
|
-
To authorize a key the app already holds, pass it as `accessKey`, a viem account. The library never reads its private key. Its `access` names the Tempo Account, which defaults to the user's first:
|
|
69
|
-
|
|
70
|
-
```ts
|
|
71
|
-
const accessKey = Account.fromWebCryptoP256(keyPair, { access: tempoAccount })
|
|
72
|
-
await Actions.authorize(auth, { accessKey })
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
Flows without a Tempo Auth page (`authorize`, `email.signIn`) must stay within Tempo Auth's ceiling: about $1,000 per token, for at most a year. [Permissions](../../docs/permissions.md) explains the defaults and the bounds on each flow. Flows with a page (`redirect.start`, `redirect.createUrl`, `device.authorize`) show the limits to the user, who approves them, so they may go beyond it.
|
|
76
|
-
|
|
77
|
-
The returned `account` attaches the key's authorization to its first transaction, which registers the key on chain.
|
|
78
|
-
|
|
79
|
-
## Flows
|
|
80
|
-
|
|
81
|
-
### Your own sign-in UI
|
|
82
|
-
|
|
83
|
-
For a browser app that renders its own email form. The page calls Tempo Auth with the user's Tempo Auth session, so it works across every app on the same site.
|
|
84
|
-
|
|
85
|
-
```ts
|
|
86
|
-
// A user already signed in to Tempo Auth, from any app, needs no form.
|
|
87
|
-
const authorization = await Actions.authorize(auth)
|
|
88
|
-
|
|
89
|
-
// Otherwise, sign in with an emailed code.
|
|
90
|
-
await Actions.email.sendCode(auth, { email })
|
|
91
|
-
const result = await Actions.email.signIn(auth, { code, email })
|
|
92
|
-
|
|
93
|
-
// An address that requires SSO continues at its connection, and returns to this page.
|
|
94
|
-
if ('step' in result) Actions.provider.signIn(auth, { url: result.step.url })
|
|
95
|
-
```
|
|
96
|
-
|
|
97
|
-
To offer OAuth providers such as Google, list the ones Tempo Auth enables and send the browser to one. It returns to the current page signed in, where `authorize` finishes.
|
|
98
|
-
|
|
99
|
-
```ts
|
|
100
|
-
const providers = await Actions.provider.list(auth) // [{ id: 'google', name: 'Google' }]
|
|
101
|
-
Actions.provider.signIn(auth, { provider: 'google' })
|
|
102
|
-
```
|
|
103
|
-
|
|
104
|
-
### Tempo Auth's pages
|
|
105
|
-
|
|
106
|
-
For a browser app without its own sign-in UI. The browser leaves for Tempo Auth's sign-in page, which shows the key's limits, and returns with a code.
|
|
107
|
-
|
|
108
|
-
```ts
|
|
109
|
-
await Actions.redirect.start(auth)
|
|
110
|
-
|
|
111
|
-
// On the page the browser returns to, and on every load:
|
|
112
|
-
const authorization = await Actions.redirect.complete(auth)
|
|
113
|
-
```
|
|
114
|
-
|
|
115
|
-
`redirect.start` keeps its state, with the new key, in IndexedDB until the browser returns. Pass `storage` to `Auth.create` to keep it elsewhere. An app that supplies `accessKey` passes it to `redirect.complete` too.
|
|
116
|
-
|
|
117
|
-
### A mobile app
|
|
118
|
-
|
|
119
|
-
For a React Native or Expo app. It calls the same actions as a browser app, with no `Origin`, and opens Tempo Auth's pages in the system browser, which returns to the app's custom scheme. Import `react-native-get-random-values` first, since keys need `crypto.getRandomValues`.
|
|
120
|
-
|
|
121
|
-
```ts
|
|
122
|
-
import * as WebBrowser from 'expo-web-browser'
|
|
123
|
-
|
|
124
|
-
const auth = Auth.create({
|
|
125
|
-
clientId: 'client_…',
|
|
126
|
-
redirectUri: 'my-app://callback',
|
|
127
|
-
url: 'https://auth.tempo.xyz',
|
|
128
|
-
})
|
|
129
|
-
|
|
130
|
-
// Your own sign-in UI, as in a browser.
|
|
131
|
-
await Actions.email.sendCode(auth, { email })
|
|
132
|
-
const result = await Actions.email.signIn(auth, { code, email })
|
|
133
|
-
|
|
134
|
-
// Or Tempo Auth's pages, which also offer Google and SSO.
|
|
135
|
-
const { pending, url } = await Actions.redirect.createUrl(auth)
|
|
136
|
-
const session = await WebBrowser.openAuthSessionAsync(url, auth.redirectUri)
|
|
137
|
-
if (session.type === 'success')
|
|
138
|
-
authorization = await Actions.redirect.handleCallback(auth, {
|
|
139
|
-
pending,
|
|
140
|
-
url: session.url,
|
|
141
|
-
})
|
|
142
|
-
```
|
|
143
|
-
|
|
144
|
-
Keep the authorization in the device's secure store, as [Keep a sign-in](#keep-a-sign-in) describes.
|
|
43
|
+
Every sign-in returns the same `Authorization`: the user, the tokens for Tempo APIs, and the access key, as data in `key` and as a Viem account in `account`. A browser calls Tempo APIs with its session cookie, so its script never handles the access token.
|
|
145
44
|
|
|
146
|
-
|
|
45
|
+
## Usage with Viem
|
|
147
46
|
|
|
148
|
-
|
|
47
|
+
`tempoAuth` extends a Viem client with the same actions as `client.auth`. It reads the chain from the client, and each sign-in sets the client's account, so the client sends with the access key.
|
|
149
48
|
|
|
150
49
|
```ts
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
const { pending, url } = await Actions.redirect.createUrl(auth, {
|
|
155
|
-
redirectUri: 'https://app.example/auth/callback',
|
|
156
|
-
})
|
|
157
|
-
// Keep `pending` in an encrypted, HttpOnly cookie, then redirect to `url`.
|
|
158
|
-
|
|
159
|
-
// GET /auth/callback
|
|
160
|
-
const authorization = await Actions.redirect.handleCallback(auth, {
|
|
161
|
-
pending,
|
|
162
|
-
url: request.url,
|
|
163
|
-
})
|
|
164
|
-
```
|
|
165
|
-
|
|
166
|
-
### A CLI
|
|
167
|
-
|
|
168
|
-
For a command-line tool. The user approves a code in any browser, and the CLI waits.
|
|
50
|
+
import { Auth, tempoAuth } from '@tempoxyz/auth'
|
|
51
|
+
import { createClient, Expiry, Scopes } from 'viem/tempo'
|
|
52
|
+
import * as Tokens from 'viem/tokens'
|
|
169
53
|
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
device: os.hostname(),
|
|
173
|
-
})
|
|
174
|
-
console.log(
|
|
175
|
-
`Open ${device.verificationUriComplete} and confirm ${device.userCode}.`,
|
|
54
|
+
const client = createClient({ account: Auth.account() }).extend(
|
|
55
|
+
tempoAuth({ clientId: 'client_…' }),
|
|
176
56
|
)
|
|
177
|
-
const
|
|
178
|
-
```
|
|
179
|
-
|
|
180
|
-
## More Tempo Accounts
|
|
181
|
-
|
|
182
|
-
A user starts with one Tempo Account and can have more. Add one, then name it in the `access` of the key the app authorizes:
|
|
183
|
-
|
|
184
|
-
```ts
|
|
185
|
-
const tempoAccount = await Actions.tempoAccount.create(auth)
|
|
186
|
-
const accounts = await Actions.tempoAccount.list(auth) // the default first
|
|
57
|
+
const ousd = Tokens.ousd(client.chain.id).address
|
|
187
58
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
59
|
+
// 1. Sign in, and authorize an access key, which becomes the client's account.
|
|
60
|
+
await client.auth.email.signIn({
|
|
61
|
+
code: '123456',
|
|
62
|
+
email: 'user@example.com',
|
|
63
|
+
permissions: {
|
|
64
|
+
expiry: Expiry.days(1),
|
|
65
|
+
limits: [{ limit: 10_000_000n, token: ousd }],
|
|
66
|
+
scopes: [Scopes.tip20(ousd).transfer()],
|
|
67
|
+
},
|
|
192
68
|
})
|
|
193
|
-
```
|
|
194
|
-
|
|
195
|
-
## Keep a sign-in
|
|
196
|
-
|
|
197
|
-
`Authorization.key` is the app's key as data: the Tempo Account, the signed authorization, and the key the library created. Store it with the tokens, and rebuild the account later with `Key.toAccount`.
|
|
198
|
-
|
|
199
|
-
- **Browser:** the key is a `CryptoKey` pair, which IndexedDB stores without exporting it.
|
|
200
|
-
- **Elsewhere:** the key is a secp256k1 private key, as JSON. Store it as securely as a session.
|
|
201
|
-
- **Supplied key:** the key holds no material. Pass the `accessKey` again, as `Key.toAccount(saved.key, { accessKey })`.
|
|
202
|
-
|
|
203
|
-
```ts
|
|
204
|
-
import { Actions, Key } from '@tempoxyz/auth'
|
|
205
|
-
|
|
206
|
-
if (Key.expired(saved.key)) signInAgain()
|
|
207
69
|
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
70
|
+
// 2. Send a transaction with the access key.
|
|
71
|
+
await client.token.transferSync({
|
|
72
|
+
amount: 10_000n,
|
|
73
|
+
to: '0x…',
|
|
74
|
+
token: ousd,
|
|
211
75
|
})
|
|
212
76
|
```
|
|
213
77
|
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
## Errors
|
|
217
|
-
|
|
218
|
-
A request Tempo Auth refuses throws `Auth.RequestError`. It carries the OAuth error code as `code`, and `message` states the fault and its fix. `reason`, `field`, and `meta` let an app branch on the refusal and read the values it compared. [Errors](../../docs/errors.md) lists every `reason`.
|
|
219
|
-
|
|
220
|
-
```ts
|
|
221
|
-
try {
|
|
222
|
-
await Actions.authorize(auth, { permissions })
|
|
223
|
-
} catch (error) {
|
|
224
|
-
if (error instanceof Auth.RequestError && error.reason === 'limit_exceeds_maximum')
|
|
225
|
-
showMaximum(error.meta?.maximum) // in base units
|
|
226
|
-
}
|
|
227
|
-
```
|
|
228
|
-
|
|
229
|
-
## Reference
|
|
230
|
-
|
|
231
|
-
### `Auth.create(options)`
|
|
232
|
-
|
|
233
|
-
| Option | Meaning |
|
|
234
|
-
| -------------- | ----------------------------------------------------------------------------------------------- |
|
|
235
|
-
| `clientId` | The app's Connect client ID. Required |
|
|
236
|
-
| `chainId` | The chain the app's keys act on, such as `tempoTestnet.id`. Defaults to Tempo mainnet |
|
|
237
|
-
| `url` | Where Tempo Auth runs, such as `https://auth.tempo.xyz`. Required |
|
|
238
|
-
| `clientSecret` | The app's client secret. Only a backend holds one |
|
|
239
|
-
| `redirectUri` | Where Tempo Auth redirects back to. Defaults to the page's origin |
|
|
240
|
-
| `scope` | Scopes beyond `openid` and `email`, which every instance includes. Defaults to `offline_access` |
|
|
241
|
-
| `storage` | Where `redirect.start` keeps its state, without serializing it. Defaults to IndexedDB |
|
|
242
|
-
| `fetch` | Calls Tempo Auth. Defaults to the global `fetch` |
|
|
243
|
-
|
|
244
|
-
The actions call version 1 of Tempo Auth's JSON API, under `/v1`. [API versions](../../docs/versioning.md) describes versions and compatibility.
|
|
245
|
-
|
|
246
|
-
### `Actions`
|
|
247
|
-
|
|
248
|
-
Each sign-in flow has its own namespace. The actions every flow shares sit at the top level. Every action that creates a key also takes optional `accessKey` and `permissions`.
|
|
249
|
-
|
|
250
|
-
| Action | Returns |
|
|
251
|
-
| -------------------------------------------------------- | -------------------------------------------------------------------------- |
|
|
252
|
-
| `authorize(auth)` | An `Authorization` for a user already signed in, or `undefined` |
|
|
253
|
-
| `refresh(auth, { refreshToken })` | New `Tokens` |
|
|
254
|
-
| `signOut(auth)` | Nothing. The user's Tempo Auth session ends |
|
|
255
|
-
| `device.authorize(auth, { device })` | The code to show and a `wait()` that resolves with an `Authorization` |
|
|
256
|
-
| `email.sendCode(auth, { email })` | Nothing. Tempo Auth emails a code |
|
|
257
|
-
| `email.signIn(auth, { code, email })` | An `Authorization`, or the `step` the address requires first |
|
|
258
|
-
| `provider.list(auth)` | The OAuth providers Tempo Auth offers, as `{ id, name }` |
|
|
259
|
-
| `provider.signIn(auth, { provider } \| { url })` | Nothing. The browser leaves for the provider or SSO connection |
|
|
260
|
-
| `redirect.complete(auth, { accessKey })` | The `Authorization` when the page is a redirect's callback, or `undefined` |
|
|
261
|
-
| `redirect.createUrl(auth, { redirectUri })` | The `url` to send the browser to, and the `pending` state to keep |
|
|
262
|
-
| `redirect.handleCallback(auth, { pending, url })` | The `Authorization` for a redirect's callback |
|
|
263
|
-
| `redirect.start(auth)` | Nothing. The browser leaves for Tempo Auth's pages |
|
|
264
|
-
| `tempoAccount.create(auth)` | Another Tempo Account for the signed-in user |
|
|
265
|
-
| `tempoAccount.list(auth)` | The signed-in user's Tempo Accounts, the default first |
|
|
266
|
-
|
|
267
|
-
### `Authorization`
|
|
268
|
-
|
|
269
|
-
| Field | Meaning |
|
|
270
|
-
| -------------- | -------------------------------------------------------------------------------- |
|
|
271
|
-
| `account` | The user's Tempo Account, as a viem account that signs with the app's access key |
|
|
272
|
-
| `key` | The same key as data, for `Key.toAccount` |
|
|
273
|
-
| `accessToken` | The Bearer token Tempo APIs accept |
|
|
274
|
-
| `expiresAt` | When the access token expires, in milliseconds |
|
|
275
|
-
| `idToken` | Optional: the OIDC ID token, which names the user |
|
|
276
|
-
| `refreshToken` | Optional: exchanges for new tokens through `refresh`. It rotates each time |
|
|
277
|
-
| `user` | The user's `email` and `id`, the `sub` of every token |
|
|
278
|
-
|
|
279
|
-
### Types
|
|
280
|
-
|
|
281
|
-
Each type an action takes or returns lives in its own module, exported beside `Actions`: `Authorization`, `Key`, `Pending`, `Permissions`, `Provider`, `Step`, `Storage`, `TempoAccount`, `Tokens`, and `User`. Each module's `from` builds a value of its type:
|
|
282
|
-
|
|
283
|
-
```ts
|
|
284
|
-
import { Permissions } from '@tempoxyz/auth'
|
|
78
|
+
## Install
|
|
285
79
|
|
|
286
|
-
|
|
80
|
+
```bash
|
|
81
|
+
pnpm add @tempoxyz/auth viem
|
|
287
82
|
```
|
|
288
83
|
|
|
289
|
-
|
|
84
|
+
## Documentation
|
|
290
85
|
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
| `expired(key)` | Whether the key's authorization has expired |
|
|
86
|
+
- [Guides](docs/guides/README.md): each sign-in flow, access keys, sessions, errors, and Viem.
|
|
87
|
+
- [API reference](docs/api/README.md): every action, function, and type.
|
|
88
|
+
- [Tempo Auth](../../docs/README.md): the service the package calls, over HTTP.
|
package/dist/Actions.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Tempo Auth's actions. Each takes an instance from `Auth.create`. Each sign-in flow has its own
|
|
3
|
-
* namespace,
|
|
3
|
+
* namespace, `oauth` holds the standard OAuth endpoints for tokens an app holds, and the actions
|
|
4
|
+
* every flow shares sit at the top level.
|
|
4
5
|
* @module
|
|
5
6
|
*/
|
|
6
7
|
import type { Account } from 'viem/tempo';
|
|
@@ -10,6 +11,7 @@ import type { Permissions } from './Permissions.ts';
|
|
|
10
11
|
import type { Tokens } from './Tokens.ts';
|
|
11
12
|
export * as device from './actions/device.ts';
|
|
12
13
|
export * as email from './actions/email.ts';
|
|
14
|
+
export * as oauth from './actions/oauth.ts';
|
|
13
15
|
export * as provider from './actions/provider.ts';
|
|
14
16
|
export * as redirect from './actions/redirect.ts';
|
|
15
17
|
export * as tempoAccount from './actions/tempoAccount.ts';
|
|
@@ -24,19 +26,19 @@ export * as tempoAccount from './actions/tempoAccount.ts';
|
|
|
24
26
|
* if (!authorization) showSignIn()
|
|
25
27
|
* ```
|
|
26
28
|
*/
|
|
27
|
-
export declare function authorize(auth: Auth, options?: authorize.Options): Promise<authorize.ReturnType
|
|
29
|
+
export declare function authorize<const chainId extends number = number>(auth: Auth, options?: authorize.Options<chainId>): Promise<authorize.ReturnType<chainId>>;
|
|
28
30
|
export declare namespace authorize {
|
|
29
|
-
type Options = {
|
|
31
|
+
type Options<chainId extends number = number> = {
|
|
30
32
|
/**
|
|
31
|
-
* The key to authorize, as a
|
|
33
|
+
* The key to authorize, as a Viem account. Its `access` names the Tempo Account, which
|
|
32
34
|
* defaults to the user's first. Defaults to a new key.
|
|
33
35
|
*/
|
|
34
36
|
accessKey?: Account.Account | undefined;
|
|
35
37
|
/** What the key may do. It must fit the headless ceiling, which fills what it leaves out. */
|
|
36
|
-
permissions?: Permissions | undefined;
|
|
38
|
+
permissions?: Permissions<chainId> | undefined;
|
|
37
39
|
};
|
|
38
40
|
/** The authorization, or `undefined` without a session. */
|
|
39
|
-
type ReturnType = Authorization | undefined;
|
|
41
|
+
type ReturnType<chainId extends number = number> = Authorization<chainId> | undefined;
|
|
40
42
|
}
|
|
41
43
|
/**
|
|
42
44
|
* Exchanges a refresh token for new tokens. Refresh tokens rotate, so store the new one.
|
package/dist/Actions.js
CHANGED
|
@@ -3,6 +3,7 @@ import * as Api from './internal/api.js';
|
|
|
3
3
|
import { request } from './internal/request.js';
|
|
4
4
|
export * as device from './actions/device.js';
|
|
5
5
|
export * as email from './actions/email.js';
|
|
6
|
+
export * as oauth from './actions/oauth.js';
|
|
6
7
|
export * as provider from './actions/provider.js';
|
|
7
8
|
export * as redirect from './actions/redirect.js';
|
|
8
9
|
export * as tempoAccount from './actions/tempoAccount.js';
|
package/dist/Auth.d.ts
CHANGED
|
@@ -1,4 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A Tempo Auth instance, which every action takes. Create one with {@link create}, then pass it
|
|
3
|
+
* to each action in `Actions`. Every sign-in returns an `Authorization`: the user's tokens and a
|
|
4
|
+
* Viem access key account.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* import { Actions, Auth } from '@tempoxyz/auth'
|
|
9
|
+
* import { Expiry, Scopes } from 'viem/tempo'
|
|
10
|
+
* import { tempo } from 'viem/tempo/chains'
|
|
11
|
+
* import * as Tokens from 'viem/tokens'
|
|
12
|
+
*
|
|
13
|
+
* // OUSD has its mainnet address on every Tempo chain.
|
|
14
|
+
* const ousd = Tokens.ousd(tempo.id).address
|
|
15
|
+
*
|
|
16
|
+
* const auth = Auth.create({ clientId: 'client_…', dev: true })
|
|
17
|
+
*
|
|
18
|
+
* const authorization = await Actions.authorize(auth, {
|
|
19
|
+
* permissions: {
|
|
20
|
+
* expiry: Expiry.days(1),
|
|
21
|
+
* limits: [{ limit: 10_000_000n, token: ousd }],
|
|
22
|
+
* scopes: [Scopes.tip20(ousd).transfer()],
|
|
23
|
+
* },
|
|
24
|
+
* })
|
|
25
|
+
* ```
|
|
26
|
+
* @module
|
|
27
|
+
*/
|
|
28
|
+
import type { Account as ViemAccount } from 'viem/tempo';
|
|
29
|
+
import * as AccountState from './internal/account.ts';
|
|
1
30
|
import type { Storage } from './Storage.ts';
|
|
31
|
+
/**
|
|
32
|
+
* The account of a client extended with `tempoAuth`: an access key account that each sign-in sets
|
|
33
|
+
* and sign-out empties. Reading it while signed out throws {@link SignedOutError}.
|
|
34
|
+
*/
|
|
35
|
+
export type Account = ViemAccount.AccessKeyAccount & {
|
|
36
|
+
/** What the account signs as, which only the `tempoAuth` extension reads. */
|
|
37
|
+
readonly [AccountState.key]: AccountState.State;
|
|
38
|
+
};
|
|
2
39
|
/** A Tempo Auth instance, which every action takes. */
|
|
3
40
|
export type Auth = {
|
|
4
41
|
/** The chain the app's keys act on, unless an action's `permissions` names another. */
|
|
@@ -51,23 +88,52 @@ export declare namespace RequestError {
|
|
|
51
88
|
/** Each refusal's stable name. `docs/errors.md` explains each one. */
|
|
52
89
|
type Reason = 'already_decided' | 'approval_pending' | 'body_too_large' | 'cross_site_request' | 'device_code_expired' | 'duplicate_limit' | 'empty_limits' | 'empty_scopes' | 'expiry_exceeds_maximum' | 'expiry_passed' | 'foreign_account' | 'foreign_origin' | 'invalid_chain' | 'invalid_code' | 'invalid_expiry' | 'invalid_field' | 'invalid_origin' | 'invalid_public_key' | 'limit_exceeds_maximum' | 'malformed_body' | 'missing_code_challenge' | 'missing_key' | 'missing_recipients' | 'periodic_limit' | 'polling_too_fast' | 'rate_limited' | 'rejected' | 'request_denied' | 'scope_without_limit' | 'session_required' | 'unexpected' | 'unknown_code' | 'unknown_endpoint' | 'unsupported_call' | 'unsupported_step' | 'unsupported_token' | 'upstream_unavailable' | 'webauthn_rejected';
|
|
53
90
|
}
|
|
91
|
+
/** A client's {@link Account} read before a sign-in set it, or after sign-out emptied it. */
|
|
92
|
+
export declare class SignedOutError extends Error {
|
|
93
|
+
readonly name = "Auth.SignedOutError";
|
|
94
|
+
constructor();
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Creates the account of a client extended with `tempoAuth`. It holds no key until a sign-in sets
|
|
98
|
+
* it, and reading it before then throws {@link SignedOutError}, so a transaction fails before it
|
|
99
|
+
* reaches the node. Each client needs its own, since the account signs as one user.
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```ts
|
|
103
|
+
* const client = createClient({
|
|
104
|
+
* account: Auth.account(),
|
|
105
|
+
* chain: tempoTestnet,
|
|
106
|
+
* }).extend(tempoAuth({ clientId: 'client_…' }))
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
109
|
+
export declare function account(): Account;
|
|
54
110
|
/**
|
|
55
111
|
* Creates a Tempo Auth instance.
|
|
56
112
|
*
|
|
57
113
|
* @example
|
|
58
114
|
* ```ts
|
|
59
|
-
* const auth = Auth.create({ clientId: 'client_…'
|
|
115
|
+
* const auth = Auth.create({ clientId: 'client_…' })
|
|
60
116
|
* ```
|
|
61
117
|
*/
|
|
62
118
|
export declare function create(options: create.Options): Auth;
|
|
63
119
|
export declare namespace create {
|
|
64
|
-
type Options = Omit<Auth, 'chainId' | 'scope'> & {
|
|
65
|
-
/**
|
|
120
|
+
type Options = Omit<Auth, 'chainId' | 'scope' | 'url'> & {
|
|
121
|
+
/**
|
|
122
|
+
* The chain the app's keys act on, such as `client.chain.id`. Defaults to Tempo mainnet, or to
|
|
123
|
+
* Tempo testnet with `dev`.
|
|
124
|
+
*/
|
|
66
125
|
chainId?: number | undefined;
|
|
126
|
+
/**
|
|
127
|
+
* Whether to use Tempo Auth's development deployment, `https://auth.tehq.dev`, with keys on
|
|
128
|
+
* Tempo testnet unless `chainId` names another chain. Defaults to `false`.
|
|
129
|
+
*/
|
|
130
|
+
dev?: boolean | undefined;
|
|
67
131
|
/**
|
|
68
132
|
* Scopes the app's tokens carry beyond `openid` and `email`, which every instance includes.
|
|
69
133
|
* Defaults to `offline_access`, for a refresh token.
|
|
70
134
|
*/
|
|
71
135
|
scope?: string | undefined;
|
|
136
|
+
/** Where Tempo Auth runs. Defaults to `https://auth.tempo.xyz`, or `https://auth.tehq.dev` with `dev`. */
|
|
137
|
+
url?: string | undefined;
|
|
72
138
|
};
|
|
73
139
|
}
|
package/dist/Auth.js
CHANGED
|
@@ -1,34 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
*
|
|
3
|
-
* to each action in `Actions`. Every sign-in returns an `Authorization`: the user's tokens and a
|
|
4
|
-
* viem access key account.
|
|
5
|
-
*
|
|
6
|
-
* @example
|
|
7
|
-
* ```ts
|
|
8
|
-
* import { Actions, Auth } from '@tempoxyz/auth'
|
|
9
|
-
* import { Expiry, Scopes } from 'viem/tempo'
|
|
10
|
-
* import { tempo, tempoTestnet } from 'viem/tempo/chains'
|
|
11
|
-
* import * as Tokens from 'viem/tokens'
|
|
12
|
-
*
|
|
13
|
-
* const ousd = Tokens.ousd(tempo.id).address
|
|
14
|
-
*
|
|
15
|
-
* const auth = Auth.create({
|
|
16
|
-
* chainId: tempoTestnet.id,
|
|
17
|
-
* clientId: 'client_…',
|
|
18
|
-
* url: 'https://auth.tempo.xyz',
|
|
19
|
-
* })
|
|
20
|
-
*
|
|
21
|
-
* const authorization = await Actions.authorize(auth, {
|
|
22
|
-
* permissions: {
|
|
23
|
-
* expiry: Expiry.days(1),
|
|
24
|
-
* limits: [{ limit: 10_000_000n, token: ousd }],
|
|
25
|
-
* scopes: [Scopes.tip20(ousd).transfer()],
|
|
26
|
-
* },
|
|
27
|
-
* })
|
|
28
|
-
* ```
|
|
29
|
-
* @module
|
|
30
|
-
*/
|
|
31
|
-
import { tempo } from 'viem/tempo/chains';
|
|
1
|
+
import { tempo, tempoTestnet } from 'viem/tempo/chains';
|
|
2
|
+
import * as AccountState from './internal/account.js';
|
|
32
3
|
/** A request Tempo Auth refused, with its status and message. */
|
|
33
4
|
export class RequestError extends Error {
|
|
34
5
|
name = 'Auth.RequestError';
|
|
@@ -54,20 +25,75 @@ export class RequestError extends Error {
|
|
|
54
25
|
this.uri = options.uri;
|
|
55
26
|
}
|
|
56
27
|
}
|
|
28
|
+
/** A client's {@link Account} read before a sign-in set it, or after sign-out emptied it. */
|
|
29
|
+
export class SignedOutError extends Error {
|
|
30
|
+
name = 'Auth.SignedOutError';
|
|
31
|
+
constructor() {
|
|
32
|
+
super("The client's account is signed out. Sign in through `client.auth` before sending.");
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Creates the account of a client extended with `tempoAuth`. It holds no key until a sign-in sets
|
|
37
|
+
* it, and reading it before then throws {@link SignedOutError}, so a transaction fails before it
|
|
38
|
+
* reaches the node. Each client needs its own, since the account signs as one user.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```ts
|
|
42
|
+
* const client = createClient({
|
|
43
|
+
* account: Auth.account(),
|
|
44
|
+
* chain: tempoTestnet,
|
|
45
|
+
* }).extend(tempoAuth({ clientId: 'client_…' }))
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
export function account() {
|
|
49
|
+
const state = { account: undefined };
|
|
50
|
+
// Viem reads a client's account only when an action runs, through property reads, `in` checks,
|
|
51
|
+
// and spreads, so each trap reads through to the account the latest sign-in set.
|
|
52
|
+
const proxy = new Proxy({}, {
|
|
53
|
+
get(_, key) {
|
|
54
|
+
if (key === AccountState.key)
|
|
55
|
+
return state;
|
|
56
|
+
if (state.account)
|
|
57
|
+
return Reflect.get(state.account, key);
|
|
58
|
+
// Promise resolution, serialization, and inspection probe these, which must not throw.
|
|
59
|
+
if (typeof key === 'symbol' || key === 'then' || key === 'toJSON')
|
|
60
|
+
return undefined;
|
|
61
|
+
throw new SignedOutError();
|
|
62
|
+
},
|
|
63
|
+
getOwnPropertyDescriptor(_, key) {
|
|
64
|
+
if (!state.account)
|
|
65
|
+
return undefined;
|
|
66
|
+
const descriptor = Reflect.getOwnPropertyDescriptor(state.account, key);
|
|
67
|
+
// A proxy may report a property its empty target lacks only as configurable.
|
|
68
|
+
return descriptor && { ...descriptor, configurable: true };
|
|
69
|
+
},
|
|
70
|
+
has: (_, key) => key === AccountState.key ||
|
|
71
|
+
(state.account !== undefined && Reflect.has(state.account, key)),
|
|
72
|
+
ownKeys: () => (state.account ? Reflect.ownKeys(state.account) : []),
|
|
73
|
+
});
|
|
74
|
+
return proxy;
|
|
75
|
+
}
|
|
57
76
|
/**
|
|
58
77
|
* Creates a Tempo Auth instance.
|
|
59
78
|
*
|
|
60
79
|
* @example
|
|
61
80
|
* ```ts
|
|
62
|
-
* const auth = Auth.create({ clientId: 'client_…'
|
|
81
|
+
* const auth = Auth.create({ clientId: 'client_…' })
|
|
63
82
|
* ```
|
|
64
83
|
*/
|
|
65
84
|
export function create(options) {
|
|
85
|
+
const { chainId, dev, scope = 'offline_access', url, ...rest } = options;
|
|
66
86
|
// Every authorization reads the user from the ID token, which needs `openid` and `email`.
|
|
67
|
-
const scopes =
|
|
87
|
+
const scopes = scope.split(' ').filter(Boolean);
|
|
68
88
|
return {
|
|
69
|
-
...
|
|
70
|
-
chainId:
|
|
89
|
+
...rest,
|
|
90
|
+
chainId: chainId ?? (dev ? tempoTestnet.id : tempo.id),
|
|
71
91
|
scope: [...new Set(['openid', 'email', ...scopes])].join(' '),
|
|
92
|
+
url: url ?? (dev ? urls.dev : urls.production),
|
|
72
93
|
};
|
|
73
94
|
}
|
|
95
|
+
/** Where Tempo Auth runs unless `create` receives a `url`. */
|
|
96
|
+
const urls = {
|
|
97
|
+
dev: 'https://auth.tehq.dev',
|
|
98
|
+
production: 'https://auth.tempo.xyz',
|
|
99
|
+
};
|
package/dist/Authorization.d.ts
CHANGED
|
@@ -5,12 +5,15 @@
|
|
|
5
5
|
import type * as Key from './Key.ts';
|
|
6
6
|
import type { Tokens } from './Tokens.ts';
|
|
7
7
|
import type { User } from './User.ts';
|
|
8
|
-
/**
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
/**
|
|
9
|
+
* What every sign-in returns: the app's tokens for Tempo APIs, with its access key and the user.
|
|
10
|
+
* `chainId` is the key's chain, as a literal type when the sign-in's `permissions` named one.
|
|
11
|
+
*/
|
|
12
|
+
export type Authorization<chainId extends number = number> = Tokens & {
|
|
13
|
+
/** The user's Tempo Account, as a Viem account that signs with the app's access key. */
|
|
11
14
|
account: ReturnType<typeof Key.toAccount>;
|
|
12
15
|
/** The same key as data, for apps that store it and rebuild it with `Key.toAccount`. */
|
|
13
|
-
key: Key.Key
|
|
16
|
+
key: Key.Key<chainId>;
|
|
14
17
|
/** The signed-in user, from the ID token. */
|
|
15
18
|
user: User;
|
|
16
19
|
};
|
|
@@ -22,4 +25,4 @@ export type Authorization = Tokens & {
|
|
|
22
25
|
* const authorization = Authorization.from({ ...saved, account: Key.toAccount(saved.key) })
|
|
23
26
|
* ```
|
|
24
27
|
*/
|
|
25
|
-
export declare function from(value: Authorization): Authorization
|
|
28
|
+
export declare function from<chainId extends number = number>(value: Authorization<chainId>): Authorization<chainId>;
|