@tempoxyz/auth 0.0.0-pr-69.sha-6003f39 → 0.0.0-pr-74.sha-12bf7d4
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 +7 -0
- package/dist/Actions.js +2 -0
- package/dist/Auth.d.ts +4 -3
- package/dist/Decorator.d.ts +11 -1
- package/dist/Decorator.js +68 -25
- package/dist/actions/device.d.ts +6 -0
- package/dist/actions/device.js +3 -0
- package/dist/actions/email.d.ts +13 -0
- package/dist/actions/email.js +17 -3
- package/dist/actions/passkey.d.ts +70 -0
- package/dist/actions/passkey.js +60 -0
- package/dist/actions/redirect.d.ts +12 -0
- package/dist/actions/redirect.js +7 -10
- package/dist/internal/accessKey.d.ts +5 -0
- package/dist/internal/accessKey.js +11 -0
- package/dist/internal/link.d.ts +40 -0
- package/dist/internal/link.js +28 -0
- package/dist/internal/request.d.ts +3 -0
- package/dist/internal/request.js +2 -1
- package/dist/internal/store.d.ts +11 -0
- package/dist/internal/store.js +14 -0
- package/package.json +1 -1
package/dist/Actions.d.ts
CHANGED
|
@@ -12,6 +12,7 @@ import type { Tokens } from './Tokens.ts';
|
|
|
12
12
|
export * as device from './actions/device.ts';
|
|
13
13
|
export * as email from './actions/email.ts';
|
|
14
14
|
export * as oauth from './actions/oauth.ts';
|
|
15
|
+
export * as passkey from './actions/passkey.ts';
|
|
15
16
|
export * as provider from './actions/provider.ts';
|
|
16
17
|
export * as redirect from './actions/redirect.ts';
|
|
17
18
|
export * as tempoAccount from './actions/tempoAccount.ts';
|
|
@@ -34,6 +35,12 @@ export declare namespace authorize {
|
|
|
34
35
|
* defaults to the user's first. Defaults to a new key.
|
|
35
36
|
*/
|
|
36
37
|
accessKey?: Account.Account | undefined;
|
|
38
|
+
/**
|
|
39
|
+
* A message the owner's signature binds, such as a sign-in challenge the app's backend issued.
|
|
40
|
+
* Tempo Auth binds its EIP-191 hash as the key authorization's `witness`. Only an app with the
|
|
41
|
+
* `tempo-auth:sign` scope may send one.
|
|
42
|
+
*/
|
|
43
|
+
challenge?: string | undefined;
|
|
37
44
|
/** What the key may do. It must fit the headless ceiling, which fills what it leaves out. */
|
|
38
45
|
permissions?: Permissions<chainId> | undefined;
|
|
39
46
|
};
|
package/dist/Actions.js
CHANGED
|
@@ -4,6 +4,7 @@ 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
6
|
export * as oauth from './actions/oauth.js';
|
|
7
|
+
export * as passkey from './actions/passkey.js';
|
|
7
8
|
export * as provider from './actions/provider.js';
|
|
8
9
|
export * as redirect from './actions/redirect.js';
|
|
9
10
|
export * as tempoAccount from './actions/tempoAccount.js';
|
|
@@ -29,6 +30,7 @@ export async function authorize(auth, options = {}) {
|
|
|
29
30
|
const response = await Api.post(auth, '/v1/authorize', {
|
|
30
31
|
json: request(auth, {
|
|
31
32
|
accessKey: key.accessKey,
|
|
33
|
+
challenge: options.challenge,
|
|
32
34
|
permissions: options.permissions,
|
|
33
35
|
}),
|
|
34
36
|
});
|
package/dist/Auth.d.ts
CHANGED
|
@@ -51,8 +51,9 @@ export type Auth = {
|
|
|
51
51
|
/** The scopes the app's tokens carry. */
|
|
52
52
|
scope: string;
|
|
53
53
|
/**
|
|
54
|
-
* Where `Actions.redirect.start` keeps its state until Tempo Auth redirects back
|
|
55
|
-
*
|
|
54
|
+
* Where `Actions.redirect.start` keeps its state until Tempo Auth redirects back, and
|
|
55
|
+
* `Actions.passkey.signIn` keeps a new key its email step signed. It must keep objects as they
|
|
56
|
+
* are, since a browser key is a `CryptoKey`. Defaults to IndexedDB.
|
|
56
57
|
*/
|
|
57
58
|
storage?: Storage | undefined;
|
|
58
59
|
/** Where Tempo Auth runs, such as `https://auth.tempo.xyz`. */
|
|
@@ -86,7 +87,7 @@ export declare namespace RequestError {
|
|
|
86
87
|
uri?: string | undefined;
|
|
87
88
|
};
|
|
88
89
|
/** Each refusal's stable name. `docs/errors.md` explains each one. */
|
|
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_period' | 'invalid_public_key' | 'limit_exceeds_maximum' | 'malformed_body' | 'missing_code_challenge' | 'missing_key' | 'missing_recipients' | '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' | 'webauthn_required';
|
|
90
|
+
type Reason = 'already_decided' | 'approval_pending' | 'body_too_large' | 'challenge_not_permitted' | '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_period' | 'invalid_public_key' | 'limit_exceeds_maximum' | 'malformed_body' | 'missing_code_challenge' | 'missing_key' | 'missing_recipients' | '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' | 'webauthn_required';
|
|
90
91
|
}
|
|
91
92
|
/** A client's {@link Account} read before a sign-in set it, or after sign-out emptied it. */
|
|
92
93
|
export declare class SignedOutError extends Error {
|
package/dist/Decorator.d.ts
CHANGED
|
@@ -82,7 +82,10 @@ export declare namespace tempoAuth {
|
|
|
82
82
|
email: {
|
|
83
83
|
/** `Actions.email.sendCode`. */
|
|
84
84
|
sendCode: (options: Actions.email.sendCode.Options) => Promise<void>;
|
|
85
|
-
/**
|
|
85
|
+
/**
|
|
86
|
+
* `Actions.email.signIn`, which sets the client's account unless it returns a step. A step
|
|
87
|
+
* that another sign-in or a sign-out overtook throws {@link SupersededError}.
|
|
88
|
+
*/
|
|
86
89
|
signIn: <const id extends number = chainId>(options: Actions.email.signIn.Options<id>) => Promise<Actions.email.signIn.ReturnType<id>>;
|
|
87
90
|
};
|
|
88
91
|
/** The instance the members call `Actions` with. */
|
|
@@ -95,6 +98,13 @@ export declare namespace tempoAuth {
|
|
|
95
98
|
/** `Actions.oauth.userInfo`. */
|
|
96
99
|
userInfo: (options: Actions.oauth.userInfo.Options) => Promise<User>;
|
|
97
100
|
};
|
|
101
|
+
passkey: {
|
|
102
|
+
/**
|
|
103
|
+
* `Actions.passkey.signIn`, which sets the client's account unless it returns a step. A step
|
|
104
|
+
* that another sign-in or a sign-out overtook throws {@link SupersededError}.
|
|
105
|
+
*/
|
|
106
|
+
signIn: <const id extends number = chainId>(options?: Actions.passkey.signIn.Options<id>) => Promise<Actions.passkey.signIn.ReturnType<id>>;
|
|
107
|
+
};
|
|
98
108
|
provider: {
|
|
99
109
|
/** `Actions.provider.list`. */
|
|
100
110
|
list: () => Promise<Actions.provider.list.ReturnType>;
|
package/dist/Decorator.js
CHANGED
|
@@ -54,42 +54,66 @@ export function tempoAuth(options) {
|
|
|
54
54
|
// sign-in or sign-out completed after it began, so a stale result never replaces a newer one,
|
|
55
55
|
// and a sign-in still pending at sign-out never signs the client back in.
|
|
56
56
|
let version = 0;
|
|
57
|
+
// The version the latest sign-out produced, while no change has followed it.
|
|
58
|
+
let signedOut;
|
|
57
59
|
/**
|
|
58
|
-
* Begins an operation that changes the client's account
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
60
|
+
* Begins an operation that changes the client's account. `apply` applies its result: the
|
|
61
|
+
* account to sign with, or `undefined` to stop signing. `check` accepts a result that changes
|
|
62
|
+
* no account, such as a step. Applying an account, or checking a step, that a newer change
|
|
63
|
+
* overtook throws {@link SupersededError}, so the caller never holds a result the client's
|
|
64
|
+
* newer state contradicts. `fail` rethrows the operation's error. A sign-in refused after its
|
|
65
|
+
* code or passkey verified already set a server session, which `fail` ends when a sign-out
|
|
66
|
+
* overtook it.
|
|
62
67
|
*/
|
|
63
68
|
const begin = () => {
|
|
64
69
|
const started = version;
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
70
|
+
// A sign-in that finishes after sign-out may have set a new server session, so it ends that
|
|
71
|
+
// session again, unless a newer change already followed the sign-out.
|
|
72
|
+
const superseded = async () => {
|
|
73
|
+
if (signedOut === version)
|
|
74
|
+
await Actions.signOut(auth);
|
|
75
|
+
return new SupersededError();
|
|
76
|
+
};
|
|
77
|
+
return {
|
|
78
|
+
async apply(account) {
|
|
79
|
+
if (version !== started) {
|
|
80
|
+
if (account)
|
|
81
|
+
throw await superseded();
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
state.account = account;
|
|
85
|
+
version++;
|
|
86
|
+
},
|
|
87
|
+
async check() {
|
|
88
|
+
if (version !== started)
|
|
89
|
+
throw await superseded();
|
|
90
|
+
},
|
|
91
|
+
// An arrow function, which `catch` takes unbound.
|
|
92
|
+
fail: async (error) => {
|
|
93
|
+
if (version !== started)
|
|
94
|
+
await superseded();
|
|
95
|
+
throw error;
|
|
96
|
+
},
|
|
73
97
|
};
|
|
74
98
|
};
|
|
75
99
|
return {
|
|
76
100
|
auth: {
|
|
77
101
|
async authorize(options = {}) {
|
|
78
|
-
const
|
|
102
|
+
const change = begin();
|
|
79
103
|
const authorization = await Actions.authorize(auth, options);
|
|
80
104
|
// Without a session, the client stops signing as the user it last authorized.
|
|
81
|
-
apply(authorization?.account);
|
|
105
|
+
await change.apply(authorization?.account);
|
|
82
106
|
return authorization;
|
|
83
107
|
},
|
|
84
108
|
device: {
|
|
85
109
|
async authorize(options = {}) {
|
|
86
|
-
const
|
|
110
|
+
const change = begin();
|
|
87
111
|
const device = await Actions.device.authorize(auth, options);
|
|
88
112
|
return {
|
|
89
113
|
...device,
|
|
90
114
|
async wait() {
|
|
91
115
|
const authorization = await device.wait();
|
|
92
|
-
apply(authorization.account);
|
|
116
|
+
await change.apply(authorization.account);
|
|
93
117
|
return authorization;
|
|
94
118
|
},
|
|
95
119
|
};
|
|
@@ -98,10 +122,14 @@ export function tempoAuth(options) {
|
|
|
98
122
|
email: {
|
|
99
123
|
sendCode: (options) => Actions.email.sendCode(auth, options),
|
|
100
124
|
async signIn(options) {
|
|
101
|
-
const
|
|
102
|
-
const result = await Actions.email
|
|
103
|
-
|
|
104
|
-
|
|
125
|
+
const change = begin();
|
|
126
|
+
const result = await Actions.email
|
|
127
|
+
.signIn(auth, options)
|
|
128
|
+
.catch(change.fail);
|
|
129
|
+
if ('step' in result)
|
|
130
|
+
await change.check();
|
|
131
|
+
else
|
|
132
|
+
await change.apply(result.account);
|
|
105
133
|
return result;
|
|
106
134
|
},
|
|
107
135
|
},
|
|
@@ -111,24 +139,37 @@ export function tempoAuth(options) {
|
|
|
111
139
|
jwks: () => Actions.oauth.jwks(auth),
|
|
112
140
|
userInfo: (options) => Actions.oauth.userInfo(auth, options),
|
|
113
141
|
},
|
|
142
|
+
passkey: {
|
|
143
|
+
async signIn(options = {}) {
|
|
144
|
+
const change = begin();
|
|
145
|
+
const result = await Actions.passkey
|
|
146
|
+
.signIn(auth, options)
|
|
147
|
+
.catch(change.fail);
|
|
148
|
+
if ('step' in result)
|
|
149
|
+
await change.check();
|
|
150
|
+
else
|
|
151
|
+
await change.apply(result.account);
|
|
152
|
+
return result;
|
|
153
|
+
},
|
|
154
|
+
},
|
|
114
155
|
provider: {
|
|
115
156
|
list: () => Actions.provider.list(auth),
|
|
116
157
|
signIn: (options) => Actions.provider.signIn(auth, options),
|
|
117
158
|
},
|
|
118
159
|
redirect: {
|
|
119
160
|
async complete(options = {}) {
|
|
120
|
-
const
|
|
161
|
+
const change = begin();
|
|
121
162
|
const authorization = await Actions.redirect.complete(auth, options);
|
|
122
163
|
// `undefined` means the page is not a callback, which says nothing about the session.
|
|
123
164
|
if (authorization)
|
|
124
|
-
apply(authorization.account);
|
|
165
|
+
await change.apply(authorization.account);
|
|
125
166
|
return authorization;
|
|
126
167
|
},
|
|
127
168
|
createUrl: (options = {}) => Actions.redirect.createUrl(auth, options),
|
|
128
169
|
async handleCallback(options) {
|
|
129
|
-
const
|
|
170
|
+
const change = begin();
|
|
130
171
|
const authorization = await Actions.redirect.handleCallback(auth, options);
|
|
131
|
-
apply(authorization.account);
|
|
172
|
+
await change.apply(authorization.account);
|
|
132
173
|
return authorization;
|
|
133
174
|
},
|
|
134
175
|
start: (options = {}) => Actions.redirect.start(auth, options),
|
|
@@ -138,7 +179,8 @@ export function tempoAuth(options) {
|
|
|
138
179
|
// A sign-in stored before `Key.chainId` existed gains it here.
|
|
139
180
|
const key = Key.from(authorization.key);
|
|
140
181
|
const account = Key.toAccount(key, options);
|
|
141
|
-
|
|
182
|
+
state.account = account;
|
|
183
|
+
version++;
|
|
142
184
|
return { ...authorization, account, key };
|
|
143
185
|
},
|
|
144
186
|
async signOut() {
|
|
@@ -146,6 +188,7 @@ export function tempoAuth(options) {
|
|
|
146
188
|
// pending is discarded, since the user asked to leave.
|
|
147
189
|
state.account = undefined;
|
|
148
190
|
version++;
|
|
191
|
+
signedOut = version;
|
|
149
192
|
await Actions.signOut(auth);
|
|
150
193
|
},
|
|
151
194
|
tempoAccount: {
|
package/dist/actions/device.d.ts
CHANGED
|
@@ -21,6 +21,12 @@ export declare namespace authorize {
|
|
|
21
21
|
* defaults to the user's first. Defaults to a new key.
|
|
22
22
|
*/
|
|
23
23
|
accessKey?: Account.Account | undefined;
|
|
24
|
+
/**
|
|
25
|
+
* A message the owner's signature binds, such as a sign-in challenge the app's backend issued.
|
|
26
|
+
* Tempo Auth binds its EIP-191 hash as the key authorization's `witness`. Only an app with the
|
|
27
|
+
* `tempo-auth:sign` scope may send one.
|
|
28
|
+
*/
|
|
29
|
+
challenge?: string | undefined;
|
|
24
30
|
/**
|
|
25
31
|
* The name the approval page shows. Defaults to the machine's hostname in Node, Bun, and Deno,
|
|
26
32
|
* and to `Unknown device` elsewhere.
|
package/dist/actions/device.js
CHANGED
|
@@ -17,6 +17,9 @@ export async function authorize(auth, options = {}) {
|
|
|
17
17
|
const key = await AccessKey.resolve(options.accessKey);
|
|
18
18
|
const code = await Api.json(await Api.post(auth, '/v1/device/authorize', {
|
|
19
19
|
json: {
|
|
20
|
+
...(options.challenge === undefined
|
|
21
|
+
? {}
|
|
22
|
+
: { challenge: options.challenge }),
|
|
20
23
|
device: options.device ?? hostname(),
|
|
21
24
|
key_authorization: request(auth, {
|
|
22
25
|
accessKey: key.accessKey,
|
package/dist/actions/email.d.ts
CHANGED
|
@@ -40,10 +40,23 @@ export declare namespace signIn {
|
|
|
40
40
|
* defaults to the user's first. Defaults to a new key.
|
|
41
41
|
*/
|
|
42
42
|
accessKey?: Account.Account | undefined;
|
|
43
|
+
/**
|
|
44
|
+
* A message the owner's signature binds, such as a sign-in challenge the app's backend issued.
|
|
45
|
+
* Tempo Auth binds its EIP-191 hash as the key authorization's `witness`. Only an app with the
|
|
46
|
+
* `tempo-auth:sign` scope may send one.
|
|
47
|
+
*/
|
|
48
|
+
challenge?: string | undefined;
|
|
43
49
|
/** The 6-digit code the user was emailed. */
|
|
44
50
|
code: string;
|
|
45
51
|
/** The address the code was sent to. */
|
|
46
52
|
email: string;
|
|
53
|
+
/**
|
|
54
|
+
* The `link` from `Actions.passkey.signIn`'s email step, which links that wallet.tempo passkey
|
|
55
|
+
* to the user. When the step is `signed`, the call returns the key that passkey sign-in
|
|
56
|
+
* authorized with its assertion, given the same `accessKey`, `challenge`, and `permissions`.
|
|
57
|
+
* Another `accessKey` replaces the key that sign-in generated.
|
|
58
|
+
*/
|
|
59
|
+
link?: string | undefined;
|
|
47
60
|
/** What the key may do. It must fit the headless ceiling, which fills what it leaves out. */
|
|
48
61
|
permissions?: Permissions<chainId> | undefined;
|
|
49
62
|
};
|
package/dist/actions/email.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import * as AccessKey from '../internal/accessKey.js';
|
|
2
2
|
import * as Api from '../internal/api.js';
|
|
3
|
+
import * as Link from '../internal/link.js';
|
|
3
4
|
import { request } from '../internal/request.js';
|
|
4
5
|
/**
|
|
5
6
|
* Emails the user a sign-in code. The response is the same for every address.
|
|
@@ -23,15 +24,28 @@ export async function sendCode(auth, options) {
|
|
|
23
24
|
* ```
|
|
24
25
|
*/
|
|
25
26
|
export async function signIn(auth, options) {
|
|
26
|
-
const { code, email, permissions } = options;
|
|
27
|
-
|
|
27
|
+
const { challenge, code, email, link, permissions } = options;
|
|
28
|
+
// A one-tap passkey sign-in's email step already signed the key it generated, which Tempo Auth
|
|
29
|
+
// returns once this request names it again. A key the app supplies replaces it.
|
|
30
|
+
const kept = link && !options.accessKey ? await Link.get(auth, { link }) : undefined;
|
|
31
|
+
const key = kept ?? (await AccessKey.resolve(options.accessKey));
|
|
28
32
|
const body = await Api.json(await Api.post(auth, '/v1/auth/code/verify', {
|
|
29
33
|
json: {
|
|
30
|
-
authorize: request(auth, {
|
|
34
|
+
authorize: request(auth, {
|
|
35
|
+
accessKey: key.accessKey,
|
|
36
|
+
challenge,
|
|
37
|
+
permissions,
|
|
38
|
+
}),
|
|
31
39
|
code,
|
|
32
40
|
email,
|
|
41
|
+
...(link ? { link } : {}),
|
|
33
42
|
},
|
|
34
43
|
}));
|
|
44
|
+
// A refusal throws above, which keeps the key for a retry with a new code. Tempo Auth already took
|
|
45
|
+
// the parked request, so a failed removal keeps the stale key, which the next email step replaces,
|
|
46
|
+
// rather than discard the authorization.
|
|
47
|
+
if (kept && link)
|
|
48
|
+
await Link.release(auth, { link }).catch(() => undefined);
|
|
35
49
|
if ('step' in body)
|
|
36
50
|
return {
|
|
37
51
|
step: { ...body.step, url: new URL(body.step.url, auth.url).href },
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Signs the user in with a passkey from the app's own sign-in form, and signs the app's key with the
|
|
3
|
+
* same assertion. The browser offers every passkey it holds for Tempo's domain, so the app needs no
|
|
4
|
+
* email first.
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
import { Authentication } from 'ox/webauthn';
|
|
8
|
+
import type { Account } from 'viem/tempo';
|
|
9
|
+
import type { Auth } from '../Auth.ts';
|
|
10
|
+
import type { Authorization } from '../Authorization.ts';
|
|
11
|
+
import type { Permissions } from '../Permissions.ts';
|
|
12
|
+
/**
|
|
13
|
+
* Signs the user in with a passkey, and authorizes the app in the same call. On a page, the
|
|
14
|
+
* passkey's one assertion also signs the app's key when that passkey owns the Tempo Account, since
|
|
15
|
+
* the ceremony's challenge is the key's digest. A native app, which sends no `Origin`, signs in
|
|
16
|
+
* with a random challenge and authorizes as a headless sign-in does. Returns the email step for a
|
|
17
|
+
* wallet.tempo passkey whose account has no email: the app signs the user in with
|
|
18
|
+
* `Actions.email.signIn`, passing the step's `link`, which links the passkey, and the same
|
|
19
|
+
* `accessKey`, `challenge`, and `permissions`. A `signed` step's assertion already signed the key,
|
|
20
|
+
* which that call returns without another tap, so a new key waits in the instance's `storage`.
|
|
21
|
+
* Throws a `RequestError` with reason `webauthn_required` when another passkey owns
|
|
22
|
+
* the account, which signs only on Tempo Auth's pages, where `Actions.redirect.start` authorizes it
|
|
23
|
+
* for the signed-in user.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* const result = await Actions.passkey.signIn(auth)
|
|
28
|
+
* if ('step' in result) showEmailForm()
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export declare function signIn<const chainId extends number = number>(auth: Auth, options?: signIn.Options<chainId>): Promise<signIn.ReturnType<chainId>>;
|
|
32
|
+
export declare namespace signIn {
|
|
33
|
+
type Options<chainId extends number = number> = {
|
|
34
|
+
/**
|
|
35
|
+
* The key to authorize, as a Viem account. Its `access` names the Tempo Account, which
|
|
36
|
+
* defaults to the user's first. Defaults to a new key.
|
|
37
|
+
*/
|
|
38
|
+
accessKey?: Account.Account | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* A message the owner's signature binds, such as a sign-in challenge the app's backend issued.
|
|
41
|
+
* Tempo Auth binds its EIP-191 hash as the key authorization's `witness`. Only an app with the
|
|
42
|
+
* `tempo-auth:sign` scope may send one.
|
|
43
|
+
*/
|
|
44
|
+
challenge?: string | undefined;
|
|
45
|
+
/**
|
|
46
|
+
* Runs the WebAuthn ceremony in place of `navigator.credentials.get`, such as a native passkey
|
|
47
|
+
* module. Its assertion must report a web origin on the relying party's domain that the app
|
|
48
|
+
* registered, so an Android app's facet origin is refused.
|
|
49
|
+
*/
|
|
50
|
+
getFn?: Authentication.sign.Options['getFn'] | undefined;
|
|
51
|
+
/** What the key may do. It must fit the headless ceiling, which fills what it leaves out. */
|
|
52
|
+
permissions?: Permissions<chainId> | undefined;
|
|
53
|
+
};
|
|
54
|
+
/** The step a wallet.tempo passkey without an email needs: an emailed code, which links it. */
|
|
55
|
+
type EmailStep = {
|
|
56
|
+
/** Passed as `link` to `Actions.email.signIn`, which links the passkey to that user. */
|
|
57
|
+
link: string;
|
|
58
|
+
/**
|
|
59
|
+
* Whether the assertion already signed the key, which `Actions.email.signIn` returns with
|
|
60
|
+
* `link`. Otherwise that call authorizes the key itself.
|
|
61
|
+
*/
|
|
62
|
+
signed: boolean;
|
|
63
|
+
/** The kind of step. */
|
|
64
|
+
type: 'email';
|
|
65
|
+
};
|
|
66
|
+
/** The authorization, or the email step. */
|
|
67
|
+
type ReturnType<chainId extends number = number> = Authorization<chainId> | {
|
|
68
|
+
step: EmailStep;
|
|
69
|
+
};
|
|
70
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Signs the user in with a passkey from the app's own sign-in form, and signs the app's key with the
|
|
3
|
+
* same assertion. The browser offers every passkey it holds for Tempo's domain, so the app needs no
|
|
4
|
+
* email first.
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
import { Authentication } from 'ox/webauthn';
|
|
8
|
+
import * as AccessKey from '../internal/accessKey.js';
|
|
9
|
+
import * as Api from '../internal/api.js';
|
|
10
|
+
import * as Link from '../internal/link.js';
|
|
11
|
+
import { request } from '../internal/request.js';
|
|
12
|
+
/**
|
|
13
|
+
* Signs the user in with a passkey, and authorizes the app in the same call. On a page, the
|
|
14
|
+
* passkey's one assertion also signs the app's key when that passkey owns the Tempo Account, since
|
|
15
|
+
* the ceremony's challenge is the key's digest. A native app, which sends no `Origin`, signs in
|
|
16
|
+
* with a random challenge and authorizes as a headless sign-in does. Returns the email step for a
|
|
17
|
+
* wallet.tempo passkey whose account has no email: the app signs the user in with
|
|
18
|
+
* `Actions.email.signIn`, passing the step's `link`, which links the passkey, and the same
|
|
19
|
+
* `accessKey`, `challenge`, and `permissions`. A `signed` step's assertion already signed the key,
|
|
20
|
+
* which that call returns without another tap, so a new key waits in the instance's `storage`.
|
|
21
|
+
* Throws a `RequestError` with reason `webauthn_required` when another passkey owns
|
|
22
|
+
* the account, which signs only on Tempo Auth's pages, where `Actions.redirect.start` authorizes it
|
|
23
|
+
* for the signed-in user.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* const result = await Actions.passkey.signIn(auth)
|
|
28
|
+
* if ('step' in result) showEmailForm()
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export async function signIn(auth, options = {}) {
|
|
32
|
+
const { challenge, getFn, permissions } = options;
|
|
33
|
+
const key = await AccessKey.resolve(options.accessKey);
|
|
34
|
+
const authorize = request(auth, {
|
|
35
|
+
accessKey: key.accessKey,
|
|
36
|
+
challenge,
|
|
37
|
+
permissions,
|
|
38
|
+
});
|
|
39
|
+
// A page sends the request with the options, so the challenge is its key's digest. A native app
|
|
40
|
+
// sends no `Origin` to bind one-tap to, so it sends the request with the assertion instead.
|
|
41
|
+
const native = !/^https?:/i.test(authorize.redirect_uri);
|
|
42
|
+
const requested = await Api.json(await Api.post(auth, '/v1/auth/passkey/options', {
|
|
43
|
+
json: native ? {} : { authorize },
|
|
44
|
+
}));
|
|
45
|
+
const deserialized = Authentication.deserializeOptions(requested.options);
|
|
46
|
+
const assertion = Authentication.serializeResponse(await Authentication.sign(
|
|
47
|
+
// ox reads `getFn` beside request options at runtime, which its types admit only beside a challenge.
|
|
48
|
+
getFn ? { ...deserialized, getFn } : deserialized));
|
|
49
|
+
const body = await Api.json(await Api.post(auth, '/v1/auth/passkey/verify', {
|
|
50
|
+
json: native ? { assertion, authorize } : { assertion },
|
|
51
|
+
}));
|
|
52
|
+
if ('step' in body) {
|
|
53
|
+
// The emailed-code sign-in that sends the link returns this key's authorization, so a key the
|
|
54
|
+
// library generated waits for it in `storage`.
|
|
55
|
+
if (body.step.signed && !options.accessKey)
|
|
56
|
+
await Link.keep(auth, { link: body.step.link, material: key.material });
|
|
57
|
+
return { step: body.step };
|
|
58
|
+
}
|
|
59
|
+
return Api.authorization(body.authorization, key);
|
|
60
|
+
}
|
|
@@ -45,6 +45,12 @@ export declare namespace createUrl {
|
|
|
45
45
|
* defaults to the user's first. Defaults to a new key, whose material `pending` keeps.
|
|
46
46
|
*/
|
|
47
47
|
accessKey?: Account.Account | undefined;
|
|
48
|
+
/**
|
|
49
|
+
* A message the owner's signature binds, such as a sign-in challenge the app's backend issued.
|
|
50
|
+
* Tempo Auth binds its EIP-191 hash as the key authorization's `witness`. Only an app with the
|
|
51
|
+
* `tempo-auth:sign` scope may send one.
|
|
52
|
+
*/
|
|
53
|
+
challenge?: string | undefined;
|
|
48
54
|
/** What the key may do. Tempo Auth's pages show it, and the headless ceiling fills what it leaves out. */
|
|
49
55
|
permissions?: Permissions | undefined;
|
|
50
56
|
/** Where Tempo Auth redirects back to. Defaults to the instance's `redirectUri`. */
|
|
@@ -97,6 +103,12 @@ export declare namespace start {
|
|
|
97
103
|
* key, which `storage` keeps until the browser returns.
|
|
98
104
|
*/
|
|
99
105
|
accessKey?: Account.Account | undefined;
|
|
106
|
+
/**
|
|
107
|
+
* A message the owner's signature binds, such as a sign-in challenge the app's backend issued.
|
|
108
|
+
* Tempo Auth binds its EIP-191 hash as the key authorization's `witness`. Only an app with the
|
|
109
|
+
* `tempo-auth:sign` scope may send one.
|
|
110
|
+
*/
|
|
111
|
+
challenge?: string | undefined;
|
|
100
112
|
/** What the key may do. Tempo Auth's pages show it, and the headless ceiling fills what it leaves out. */
|
|
101
113
|
permissions?: Permissions | undefined;
|
|
102
114
|
};
|
package/dist/actions/redirect.js
CHANGED
|
@@ -8,8 +8,8 @@ import { Base64, Bytes, Hash } from 'ox';
|
|
|
8
8
|
import { RequestError } from '../Auth.js';
|
|
9
9
|
import * as AccessKey from '../internal/accessKey.js';
|
|
10
10
|
import * as Api from '../internal/api.js';
|
|
11
|
-
import * as IndexedDb from '../internal/indexedDb.js';
|
|
12
11
|
import { request } from '../internal/request.js';
|
|
12
|
+
import * as Store from '../internal/store.js';
|
|
13
13
|
import * as Pending from '../Pending.js';
|
|
14
14
|
/**
|
|
15
15
|
* Finishes a {@link start} when the page loads with Tempo Auth's callback, and returns `undefined`
|
|
@@ -24,8 +24,8 @@ import * as Pending from '../Pending.js';
|
|
|
24
24
|
*/
|
|
25
25
|
export function complete(auth, options = {}) {
|
|
26
26
|
completed ??= (async () => {
|
|
27
|
-
const stored = (await
|
|
28
|
-
await
|
|
27
|
+
const stored = (await Store.get(auth).getItem(storageKey));
|
|
28
|
+
await Store.get(auth).removeItem(storageKey);
|
|
29
29
|
const url = new URL(window.location.href);
|
|
30
30
|
if (!url.searchParams.has('code'))
|
|
31
31
|
return undefined;
|
|
@@ -75,6 +75,9 @@ export async function createUrl(auth, options = {}) {
|
|
|
75
75
|
});
|
|
76
76
|
const url = new URL('/authorize', auth.url);
|
|
77
77
|
url.search = new URLSearchParams({
|
|
78
|
+
...(options.challenge === undefined
|
|
79
|
+
? {}
|
|
80
|
+
: { challenge: options.challenge }),
|
|
78
81
|
client_id,
|
|
79
82
|
code_challenge: Base64.fromBytes(Hash.sha256(Bytes.fromString(pending.verifier)), base64url),
|
|
80
83
|
key_authorization: Base64.fromString(JSON.stringify(key_authorization), base64url),
|
|
@@ -145,18 +148,12 @@ export async function handleCallback(auth, options) {
|
|
|
145
148
|
*/
|
|
146
149
|
export async function start(auth, options = {}) {
|
|
147
150
|
const { pending, url } = await createUrl(auth, options);
|
|
148
|
-
await
|
|
151
|
+
await Store.get(auth).setItem(storageKey, pending);
|
|
149
152
|
window.location.assign(url);
|
|
150
153
|
}
|
|
151
154
|
/** Unpadded URL-safe Base64, as PKCE and `key_authorization` use. */
|
|
152
155
|
const base64url = { pad: false, url: true };
|
|
153
156
|
/** The redirect this page load returns from, so its code is redeemed once. */
|
|
154
157
|
let completed;
|
|
155
|
-
/** The default `storage`, opened on first use. */
|
|
156
|
-
let indexedDb;
|
|
157
158
|
/** Where a redirect keeps its state in `storage`. */
|
|
158
159
|
const storageKey = 'tempo-auth:redirect';
|
|
159
|
-
/** Where a redirect keeps its state: the instance's `storage`, or IndexedDB. */
|
|
160
|
-
function storage(auth) {
|
|
161
|
-
return auth.storage ?? (indexedDb ??= IndexedDb.create());
|
|
162
|
-
}
|
|
@@ -5,6 +5,11 @@ import type * as Key from '../Key.ts';
|
|
|
5
5
|
* a `CryptoKey` across page loads. Elsewhere a CLI or native app must store the key as a string.
|
|
6
6
|
*/
|
|
7
7
|
export declare function resolve(accessKey: Account.Account | undefined): Promise<resolve.ReturnType>;
|
|
8
|
+
/**
|
|
9
|
+
* Rebuilds a key that {@link resolve} generated from its material. Returns `undefined` for the
|
|
10
|
+
* empty material of a key the app supplied.
|
|
11
|
+
*/
|
|
12
|
+
export declare function restore(material: Key.Material): resolve.ReturnType | undefined;
|
|
8
13
|
export declare namespace resolve {
|
|
9
14
|
type ReturnType = {
|
|
10
15
|
/** The key, as a Viem account that signs with it. */
|
|
@@ -24,3 +24,14 @@ export async function resolve(accessKey) {
|
|
|
24
24
|
material: { privateKey },
|
|
25
25
|
};
|
|
26
26
|
}
|
|
27
|
+
/**
|
|
28
|
+
* Rebuilds a key that {@link resolve} generated from its material. Returns `undefined` for the
|
|
29
|
+
* empty material of a key the app supplied.
|
|
30
|
+
*/
|
|
31
|
+
export function restore(material) {
|
|
32
|
+
if (material.keyPair)
|
|
33
|
+
return { accessKey: Account.fromWebCryptoP256(material.keyPair), material };
|
|
34
|
+
if (material.privateKey)
|
|
35
|
+
return { accessKey: Account.fromSecp256k1(material.privateKey), material };
|
|
36
|
+
return undefined;
|
|
37
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keeps a new key that a one-tap passkey sign-in signed, until the emailed-code sign-in that sends
|
|
3
|
+
* its email step's `link` returns the key's authorization.
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import type { Auth } from '../Auth.ts';
|
|
7
|
+
import type * as Key from '../Key.ts';
|
|
8
|
+
import * as AccessKey from './accessKey.ts';
|
|
9
|
+
/** Keeps the key's material for `link`. Keeps nothing outside a browser without `storage`. */
|
|
10
|
+
export declare function keep(auth: Auth, options: keep.Options): Promise<void>;
|
|
11
|
+
export declare namespace keep {
|
|
12
|
+
type Options = {
|
|
13
|
+
/** The email step's `link`. */
|
|
14
|
+
link: string;
|
|
15
|
+
/** The key `AccessKey.resolve` generated. */
|
|
16
|
+
material: Key.Material;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Returns the key kept for `link`, or `undefined` when none is. Leaves it in place, so a refused
|
|
21
|
+
* code retries with the same key, until {@link release} removes it.
|
|
22
|
+
*/
|
|
23
|
+
export declare function get(auth: Auth, options: get.Options): Promise<AccessKey.resolve.ReturnType | undefined>;
|
|
24
|
+
export declare namespace get {
|
|
25
|
+
type Options = {
|
|
26
|
+
/** The `link` the emailed-code sign-in sends. */
|
|
27
|
+
link: string;
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Removes the key kept for `link`, once the emailed-code sign-in that used it finished. A key a newer
|
|
32
|
+
* email step kept stays, so a sign-in that finishes late cannot remove it.
|
|
33
|
+
*/
|
|
34
|
+
export declare function release(auth: Auth, options: release.Options): Promise<void>;
|
|
35
|
+
export declare namespace release {
|
|
36
|
+
type Options = {
|
|
37
|
+
/** The `link` the finished sign-in sent. */
|
|
38
|
+
link: string;
|
|
39
|
+
};
|
|
40
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import * as AccessKey from './accessKey.js';
|
|
2
|
+
import * as Store from './store.js';
|
|
3
|
+
/** Where the key waits in `storage`. A later email step replaces it. */
|
|
4
|
+
const storageKey = 'tempo-auth:passkey-link';
|
|
5
|
+
/** Keeps the key's material for `link`. Keeps nothing outside a browser without `storage`. */
|
|
6
|
+
export async function keep(auth, options) {
|
|
7
|
+
await Store.get(auth)?.setItem(storageKey, options);
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Returns the key kept for `link`, or `undefined` when none is. Leaves it in place, so a refused
|
|
11
|
+
* code retries with the same key, until {@link release} removes it.
|
|
12
|
+
*/
|
|
13
|
+
export async function get(auth, options) {
|
|
14
|
+
const kept = (await Store.get(auth)?.getItem(storageKey));
|
|
15
|
+
if (kept?.link !== options.link)
|
|
16
|
+
return undefined;
|
|
17
|
+
return AccessKey.restore(kept.material);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Removes the key kept for `link`, once the emailed-code sign-in that used it finished. A key a newer
|
|
21
|
+
* email step kept stays, so a sign-in that finishes late cannot remove it.
|
|
22
|
+
*/
|
|
23
|
+
export async function release(auth, options) {
|
|
24
|
+
const store = Store.get(auth);
|
|
25
|
+
const kept = (await store?.getItem(storageKey));
|
|
26
|
+
if (kept?.link === options.link)
|
|
27
|
+
await store?.removeItem(storageKey);
|
|
28
|
+
}
|
|
@@ -7,8 +7,11 @@ import type { Permissions } from '../Permissions.ts';
|
|
|
7
7
|
*/
|
|
8
8
|
export declare function request(auth: Auth, options: {
|
|
9
9
|
accessKey: Account.Account;
|
|
10
|
+
/** A message the key's witness binds. */
|
|
11
|
+
challenge?: string | undefined;
|
|
10
12
|
permissions?: Permissions | undefined;
|
|
11
13
|
}): {
|
|
14
|
+
challenge?: string;
|
|
12
15
|
client_id: string;
|
|
13
16
|
key_authorization: {
|
|
14
17
|
account?: `0x${string}`;
|
package/dist/internal/request.js
CHANGED
|
@@ -9,8 +9,9 @@ import { Hex } from 'ox';
|
|
|
9
9
|
* key account names its Tempo Account through `access`, and a plain key leaves it to Tempo Auth.
|
|
10
10
|
*/
|
|
11
11
|
export function request(auth, options) {
|
|
12
|
-
const { accessKey, permissions = {} } = options;
|
|
12
|
+
const { accessKey, challenge, permissions = {} } = options;
|
|
13
13
|
return {
|
|
14
|
+
...(challenge === undefined ? {} : { challenge }),
|
|
14
15
|
client_id: auth.clientId,
|
|
15
16
|
key_authorization: {
|
|
16
17
|
...(accessKey.accessKeyAddress ? { account: accessKey.address } : {}),
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolves where actions keep state across page loads: the instance's `storage`, or IndexedDB.
|
|
3
|
+
* @module
|
|
4
|
+
*/
|
|
5
|
+
import type { Auth } from '../Auth.ts';
|
|
6
|
+
import type { Storage } from '../Storage.ts';
|
|
7
|
+
/**
|
|
8
|
+
* Returns the instance's `storage`, or the page's IndexedDB. Returns `undefined` outside a browser
|
|
9
|
+
* when the instance names no `storage`.
|
|
10
|
+
*/
|
|
11
|
+
export declare function get(auth: Auth): Storage | undefined;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import * as IndexedDb from './indexedDb.js';
|
|
2
|
+
/** The default `storage`, opened on first use. */
|
|
3
|
+
let indexedDb;
|
|
4
|
+
/**
|
|
5
|
+
* Returns the instance's `storage`, or the page's IndexedDB. Returns `undefined` outside a browser
|
|
6
|
+
* when the instance names no `storage`.
|
|
7
|
+
*/
|
|
8
|
+
export function get(auth) {
|
|
9
|
+
if (auth.storage)
|
|
10
|
+
return auth.storage;
|
|
11
|
+
if (typeof indexedDB === 'undefined')
|
|
12
|
+
return undefined;
|
|
13
|
+
return (indexedDb ??= IndexedDb.create());
|
|
14
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tempoxyz/auth",
|
|
3
|
-
"version": "0.0.0-pr-
|
|
3
|
+
"version": "0.0.0-pr-74.sha-12bf7d4",
|
|
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": {
|