@tensile-tools/auth-client 0.0.0-stage → 0.3.0
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/LICENSE +202 -0
- package/README.md +62 -2
- package/dist/client.d.ts +569 -0
- package/dist/client.js +637 -0
- package/dist/devtools.d.ts +12 -0
- package/dist/devtools.js +44 -0
- package/dist/elements/account.d.ts +63 -0
- package/dist/elements/account.js +331 -0
- package/dist/elements/approvals.d.ts +61 -0
- package/dist/elements/approvals.js +252 -0
- package/dist/elements/auth.d.ts +48 -0
- package/dist/elements/auth.js +238 -0
- package/dist/elements/base.d.ts +176 -0
- package/dist/elements/base.js +540 -0
- package/dist/elements/config.d.ts +70 -0
- package/dist/elements/config.js +119 -0
- package/dist/elements/format.d.ts +32 -0
- package/dist/elements/format.js +67 -0
- package/dist/elements/index.d.ts +21 -0
- package/dist/elements/index.js +31 -0
- package/dist/elements/qr.d.ts +9 -0
- package/dist/elements/qr.js +302 -0
- package/dist/elements/returnto.d.ts +17 -0
- package/dist/elements/returnto.js +63 -0
- package/dist/elements/setup.d.ts +70 -0
- package/dist/elements/setup.js +316 -0
- package/dist/elements/styles.d.ts +3 -0
- package/dist/elements/styles.js +194 -0
- package/dist/elements/text.d.ts +21 -0
- package/dist/elements/text.js +302 -0
- package/dist/fonts/OFL-geist-mono.txt +93 -0
- package/dist/fonts/OFL-geist.txt +93 -0
- package/dist/fonts/OFL-inter.txt +93 -0
- package/dist/fonts/README.md +18 -0
- package/dist/fonts/geist-mono.woff2 +0 -0
- package/dist/fonts/geist.woff2 +0 -0
- package/dist/fonts/inter.woff2 +0 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +5 -0
- package/dist/problem.d.ts +54 -0
- package/dist/problem.js +40 -0
- package/dist/webauthn.d.ts +46 -0
- package/dist/webauthn.js +102 -0
- package/package.json +51 -4
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
// The approval elements: one agent's request, and the person's inbox of
|
|
2
|
+
// requests.
|
|
3
|
+
import { html, TensileElement } from "./base.js";
|
|
4
|
+
import { amount, asAction, clock } from "./format.js";
|
|
5
|
+
/** The agent's name, in the violet that marks agents. */
|
|
6
|
+
function agentToken(a, fallback) {
|
|
7
|
+
return html `<span class="agent">${a.agent?.name || fallback}</span>`;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* `<tensile-approval approval-id="apr_…">`: what an agent asks to do, and
|
|
11
|
+
* Approve, which runs a passkey ceremony whose signature covers the exact
|
|
12
|
+
* action, or Decline. An action whose params hold `amount` (a decimal),
|
|
13
|
+
* `currency` (ISO 4217, default USD), `payee`, and `for` shows as a
|
|
14
|
+
* payment, with the amount large.
|
|
15
|
+
*
|
|
16
|
+
* Signed out, it says "Sign in to review this request" and offers a
|
|
17
|
+
* passkey sign-in, then shows the request. With `sign-in-href`, it also
|
|
18
|
+
* links to that sign-in page, with this page as `return_to`.
|
|
19
|
+
*
|
|
20
|
+
* Events: `approved`, `declined`, `problem`.
|
|
21
|
+
*/
|
|
22
|
+
export class Approval extends TensileElement {
|
|
23
|
+
followsSession = true;
|
|
24
|
+
static attrs = ['approval-id', 'sign-in-href'];
|
|
25
|
+
item;
|
|
26
|
+
decidedAt = '';
|
|
27
|
+
/** The approval to show, instead of loading `approval-id`. */
|
|
28
|
+
get approval() {
|
|
29
|
+
return this.item;
|
|
30
|
+
}
|
|
31
|
+
set approval(a) {
|
|
32
|
+
this.item = a;
|
|
33
|
+
this.state = a ? (a.state === 'pending' ? 'ask' : a.state) : 'start';
|
|
34
|
+
this.update();
|
|
35
|
+
}
|
|
36
|
+
on(o) {
|
|
37
|
+
return o.agents;
|
|
38
|
+
}
|
|
39
|
+
async load() {
|
|
40
|
+
const id = this.getAttribute('approval-id');
|
|
41
|
+
if (!id)
|
|
42
|
+
return;
|
|
43
|
+
this.state = 'start';
|
|
44
|
+
this.update();
|
|
45
|
+
const g = this.generation;
|
|
46
|
+
const r = await this.client.approval(id);
|
|
47
|
+
if (this.stale(g))
|
|
48
|
+
return;
|
|
49
|
+
if (r.outcome === 'problem' && r.problem.status === 401) {
|
|
50
|
+
this.item = undefined;
|
|
51
|
+
this.state = 'signin';
|
|
52
|
+
return this.update();
|
|
53
|
+
}
|
|
54
|
+
if (r.outcome !== 'ok') {
|
|
55
|
+
this.state = 'gone';
|
|
56
|
+
this.error = r.outcome === 'problem' && r.problem.status === 404 ? '' : this.explain(r);
|
|
57
|
+
return this.update();
|
|
58
|
+
}
|
|
59
|
+
this.approval = r.data;
|
|
60
|
+
}
|
|
61
|
+
sized;
|
|
62
|
+
rendered() {
|
|
63
|
+
this.fit();
|
|
64
|
+
// The display font can arrive after the first render, and the frame
|
|
65
|
+
// can change width: fit the amount again then.
|
|
66
|
+
if (!this.sized && typeof ResizeObserver !== 'undefined') {
|
|
67
|
+
this.sized = new ResizeObserver(() => this.fit());
|
|
68
|
+
this.sized.observe(this);
|
|
69
|
+
void document.fonts?.ready.then(() => this.fit());
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/** Shrinks the amount until it fits its line. */
|
|
73
|
+
fit() {
|
|
74
|
+
const el = this.root.querySelector('.amt');
|
|
75
|
+
if (!el)
|
|
76
|
+
return;
|
|
77
|
+
el.style.fontSize = '';
|
|
78
|
+
let size = parseFloat(getComputedStyle(el).fontSize);
|
|
79
|
+
while (el.scrollWidth > el.clientWidth + 1 && size > 20) {
|
|
80
|
+
size -= 2;
|
|
81
|
+
el.style.fontSize = size + 'px';
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
disconnectedCallback() {
|
|
85
|
+
super.disconnectedCallback();
|
|
86
|
+
this.sized?.disconnect();
|
|
87
|
+
this.sized = undefined;
|
|
88
|
+
}
|
|
89
|
+
body(o) {
|
|
90
|
+
const a = this.item;
|
|
91
|
+
if (!a && this.state === 'signin')
|
|
92
|
+
return this.signedOutBody(o);
|
|
93
|
+
if (!a && this.state === 'gone')
|
|
94
|
+
return html `${this.heading(o, this.r('approval.goneHeadline'), this.t('approval.goneTitle'))}${this.gone()}`;
|
|
95
|
+
if (!a)
|
|
96
|
+
return this.loadingBody(o, this.t('approval.loading'));
|
|
97
|
+
const who = agentToken(a, this.t('anAgent'));
|
|
98
|
+
const name = a.agent?.name || this.t('anAgent');
|
|
99
|
+
const p = a.action.params ?? {};
|
|
100
|
+
const money = amount(p.amount, p.currency || 'USD');
|
|
101
|
+
let sentence;
|
|
102
|
+
if (money) {
|
|
103
|
+
sentence = html `<span class="l">${this.r('approval.pay', { agent: who })}</span><span class="amt"><span class="vh">${money.text}</span><span class="cur" aria-hidden="true">${money.sign}</span><span aria-hidden="true">${money.number}</span></span>${p.payee ? html `<span class="l">${this.r('approval.payee', { payee: p.payee, for: p.for ? this.t('approval.payFor', { for: p.for }) : '' })}</span>` : ''}`;
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
sentence = this.r('approval.headline', { agent: who, action: asAction(a.action.summary) });
|
|
107
|
+
}
|
|
108
|
+
const head = this.heading(o, sentence, this.t('approval.title', { agent: name }), a.action.summary);
|
|
109
|
+
switch (this.state) {
|
|
110
|
+
case 'approved':
|
|
111
|
+
return html `${head}${this.done(this.t('approval.approvedChip'), 'green', this.t('approval.approved', { time: this.decidedAt || clock(new Date()), agent: name }))}`;
|
|
112
|
+
case 'denied':
|
|
113
|
+
case 'declined':
|
|
114
|
+
return html `${head}${this.done(this.t('approval.declinedChip'), 'orange', this.t('approval.declined', { agent: name }))}`;
|
|
115
|
+
case 'gone':
|
|
116
|
+
return html `${head}${this.gone()}`;
|
|
117
|
+
}
|
|
118
|
+
return html `${head}<div class="body">
|
|
119
|
+
${money || o.headline === 'title' ? html `<p class="fine">${this.t('approval.signed', { summary: a.action.summary })}</p>` : ''}
|
|
120
|
+
<p class="cap">${this.t('approval.fine', { time: clock(a.expiresAt) })}</p>
|
|
121
|
+
${this.err()}
|
|
122
|
+
<div class="acts"><button class="btn fill" type="button" part="button button-primary" data-do="approve" data-key="approve">${this.t('approval.approve')}</button>
|
|
123
|
+
<button class="btn plain" type="button" part="button" data-do="decline" data-key="decline">${this.t('approval.decline')}</button></div></div>`;
|
|
124
|
+
}
|
|
125
|
+
forget() {
|
|
126
|
+
this.item = undefined;
|
|
127
|
+
this.decidedAt = '';
|
|
128
|
+
}
|
|
129
|
+
/** The signed-out state: sign in, then the request shows. */
|
|
130
|
+
signedOutBody(o) {
|
|
131
|
+
return html `${this.heading(o, this.r('approval.signIn'), this.t('approval.signInTitle'))}${this.signInActions(o, this.t('approval.signInFine'))}`;
|
|
132
|
+
}
|
|
133
|
+
gone() {
|
|
134
|
+
return html `${this.done(this.t('approval.goneChip'), '', this.t('approval.gone'))}${this.err()}`;
|
|
135
|
+
}
|
|
136
|
+
async act(name) {
|
|
137
|
+
const a = this.item;
|
|
138
|
+
if (!a)
|
|
139
|
+
return;
|
|
140
|
+
if (name === 'approve') {
|
|
141
|
+
const r = await this.client.approve(a.id);
|
|
142
|
+
if (r.outcome !== 'ok') {
|
|
143
|
+
if (r.outcome === 'problem' && r.problem.status === 404)
|
|
144
|
+
return this.go('gone');
|
|
145
|
+
return this.failWith(r);
|
|
146
|
+
}
|
|
147
|
+
this.decidedAt = clock(new Date());
|
|
148
|
+
this.emit('approved', { id: a.id, action: a.action, agent: a.agent });
|
|
149
|
+
return this.go('approved');
|
|
150
|
+
}
|
|
151
|
+
if (name === 'decline') {
|
|
152
|
+
const r = await this.client.deny(a.id);
|
|
153
|
+
if (r.outcome !== 'ok') {
|
|
154
|
+
if (r.outcome === 'problem' && r.problem.status === 404)
|
|
155
|
+
return this.go('gone');
|
|
156
|
+
return this.failWith(r);
|
|
157
|
+
}
|
|
158
|
+
this.emit('declined', { id: a.id, action: a.action, agent: a.agent });
|
|
159
|
+
this.go('declined');
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* `<tensile-approvals>`: the person's inbox of requests from their agents,
|
|
165
|
+
* newest first, each with Approve and Decline. It checks for new requests
|
|
166
|
+
* every `refresh` seconds (default 15; 0 turns it off) while the page is
|
|
167
|
+
* visible.
|
|
168
|
+
*
|
|
169
|
+
* Events: `approved`, `declined`, `problem`, and `change` when the list
|
|
170
|
+
* changes, with the pending requests as its detail.
|
|
171
|
+
*/
|
|
172
|
+
export class Approvals extends TensileElement {
|
|
173
|
+
followsSession = true;
|
|
174
|
+
static attrs = ['refresh'];
|
|
175
|
+
wide = true;
|
|
176
|
+
items;
|
|
177
|
+
timer;
|
|
178
|
+
on(o) {
|
|
179
|
+
return o.agents;
|
|
180
|
+
}
|
|
181
|
+
connectedCallback() {
|
|
182
|
+
super.connectedCallback();
|
|
183
|
+
const every = Number(this.getAttribute('refresh') ?? 15);
|
|
184
|
+
if (every > 0 && !this.timer) {
|
|
185
|
+
this.timer = setInterval(() => {
|
|
186
|
+
if (document.visibilityState === 'visible' && !this.busy && !this.signedOut && this.on(this.options))
|
|
187
|
+
void this.load();
|
|
188
|
+
}, every * 1000);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
disconnectedCallback() {
|
|
192
|
+
super.disconnectedCallback();
|
|
193
|
+
clearInterval(this.timer);
|
|
194
|
+
this.timer = undefined;
|
|
195
|
+
}
|
|
196
|
+
forget() {
|
|
197
|
+
this.items = undefined;
|
|
198
|
+
}
|
|
199
|
+
async load() {
|
|
200
|
+
const g = this.generation;
|
|
201
|
+
const r = await this.client.approvals();
|
|
202
|
+
if (this.stale(g))
|
|
203
|
+
return;
|
|
204
|
+
if (r.outcome !== 'ok') {
|
|
205
|
+
this.error = this.explain(r);
|
|
206
|
+
return this.update();
|
|
207
|
+
}
|
|
208
|
+
const before = this.items?.map(a => a.id).join();
|
|
209
|
+
const hadError = this.error !== '';
|
|
210
|
+
this.items = r.data;
|
|
211
|
+
this.error = '';
|
|
212
|
+
if (before !== r.data.map(a => a.id).join()) {
|
|
213
|
+
this.emit('change', r.data);
|
|
214
|
+
this.update();
|
|
215
|
+
}
|
|
216
|
+
else if (hadError) {
|
|
217
|
+
this.update();
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
body(o) {
|
|
221
|
+
const items = this.items;
|
|
222
|
+
if (!items)
|
|
223
|
+
return this.loadingBody(o, this.t('approvals.title'));
|
|
224
|
+
const n = items.length;
|
|
225
|
+
const key = n === 0 ? 'approvals.none' : n === 1 ? 'approvals.one' : 'approvals.many';
|
|
226
|
+
const head = this.heading(o, this.r(key, { count: html `<span class="num">${n}</span>` }), this.t('approvals.title'), this.t(key, { count: n }));
|
|
227
|
+
const rows = items.map(a => {
|
|
228
|
+
const name = a.agent?.name || this.t('anAgent');
|
|
229
|
+
return html `<li class="req" part="row"><div><p class="what">${this.r('approvals.item', { agent: html `<b>${name}</b>`, action: asAction(a.action.summary) })}</p>
|
|
230
|
+
<small class="cap">${this.t('approvals.expires', { time: clock(a.expiresAt) })}</small></div>
|
|
231
|
+
<div class="two"><button class="btn fill sm" type="button" part="button button-primary" data-do="approve" data-id="${a.id}" data-key="a-${a.id}" aria-label="${this.t('approvals.approveLabel', { summary: a.action.summary })}">${this.t('approval.approve')}</button>
|
|
232
|
+
<button class="btn plain sm" type="button" part="button" data-do="decline" data-id="${a.id}" data-key="d-${a.id}" aria-label="${this.t('approvals.declineLabel', { summary: a.action.summary })}">${this.t('approval.decline')}</button></div></li>`;
|
|
233
|
+
});
|
|
234
|
+
return html `${head}${this.err()}${n ? html `<ul class="inbox">${rows}</ul>` : ''}`;
|
|
235
|
+
}
|
|
236
|
+
async act(name, from) {
|
|
237
|
+
const a = this.items?.find(x => x.id === from.dataset.id);
|
|
238
|
+
if (!a)
|
|
239
|
+
return;
|
|
240
|
+
const r = name === 'approve' ? await this.client.approve(a.id) : await this.client.deny(a.id);
|
|
241
|
+
if (r.outcome !== 'ok' && !(r.outcome === 'problem' && r.problem.status === 404))
|
|
242
|
+
return this.failWith(r);
|
|
243
|
+
this.items = (this.items ?? []).filter(x => x.id !== a.id);
|
|
244
|
+
if (r.outcome === 'ok') {
|
|
245
|
+
const event = name === 'approve' ? 'approved' : 'declined';
|
|
246
|
+
this.emit(event, { id: a.id, action: a.action, agent: a.agent });
|
|
247
|
+
this.announce(`${this.t(name === 'approve' ? 'approval.approvedChip' : 'approval.declinedChip')}: ${a.action.summary}`);
|
|
248
|
+
}
|
|
249
|
+
this.emit('change', this.items);
|
|
250
|
+
this.go(this.state);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { type Html, TensileElement } from './base.ts';
|
|
2
|
+
import type { Options } from './config.ts';
|
|
3
|
+
/**
|
|
4
|
+
* `<tensile-sign-in>`: "Sign in with passkey", then "or", then email and
|
|
5
|
+
* password, as `methods` and `first` order them. It asks for a code from
|
|
6
|
+
* an authenticator app, or a recovery code, when the account needs one.
|
|
7
|
+
* After sign-in it goes to the page's `return_to` path when the URL
|
|
8
|
+
* has a safe one (a relative path on this origin), and otherwise to
|
|
9
|
+
* `redirect`. The link to `sign-up-href` carries `return_to` on.
|
|
10
|
+
*
|
|
11
|
+
* Events: `signed-in` (cancelable: cancel it to stay on the page instead
|
|
12
|
+
* of following `redirect`), `problem`.
|
|
13
|
+
*/
|
|
14
|
+
export declare class SignIn extends TensileElement {
|
|
15
|
+
static attrs: string[];
|
|
16
|
+
private autofillStarted;
|
|
17
|
+
protected load(): Promise<void>;
|
|
18
|
+
protected body(o: Options): Html;
|
|
19
|
+
private ways;
|
|
20
|
+
protected act(name: string): Promise<void>;
|
|
21
|
+
private settle;
|
|
22
|
+
private signedIn;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* `<tensile-sign-up>`: an email address, then a passkey or a password, in
|
|
26
|
+
* the order `methods` and `first` set. A password sign-up ends with "Check
|
|
27
|
+
* your email": the account exists once the person follows the link.
|
|
28
|
+
*
|
|
29
|
+
* Events: `signed-up` (cancelable, as `signed-in` is), `problem`.
|
|
30
|
+
*/
|
|
31
|
+
export declare class SignUp extends TensileElement {
|
|
32
|
+
static attrs: string[];
|
|
33
|
+
protected body(o: Options): Html;
|
|
34
|
+
protected act(name: string): Promise<void>;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* `<tensile-step-up>`: "Confirm it's you" before a sensitive action. Set
|
|
38
|
+
* `subject` (for example "Ravi Patel"), `action` (for example "to
|
|
39
|
+
* remove"), and `detail` to name the action, and `purpose` and `resource`
|
|
40
|
+
* to step up for that purpose only.
|
|
41
|
+
*
|
|
42
|
+
* Events: `stepped-up`, `cancelled`, `problem`.
|
|
43
|
+
*/
|
|
44
|
+
export declare class StepUp extends TensileElement {
|
|
45
|
+
static attrs: string[];
|
|
46
|
+
protected body(o: Options): Html;
|
|
47
|
+
protected act(name: string): Promise<void>;
|
|
48
|
+
}
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
// The sign-in, sign-up, and step-up elements.
|
|
2
|
+
import { glyph, html, sameOrigin, TensileElement } from "./base.js";
|
|
3
|
+
import { carryReturnTo, returnToHere } from "./returnto.js";
|
|
4
|
+
const hasPasskey = (o) => o.methods !== 'password';
|
|
5
|
+
const hasPassword = (o) => o.methods !== 'passkey';
|
|
6
|
+
const passkeyFirst = (o) => o.methods === 'passkey' || (o.methods === 'both' && o.first === 'passkey');
|
|
7
|
+
/**
|
|
8
|
+
* `<tensile-sign-in>`: "Sign in with passkey", then "or", then email and
|
|
9
|
+
* password, as `methods` and `first` order them. It asks for a code from
|
|
10
|
+
* an authenticator app, or a recovery code, when the account needs one.
|
|
11
|
+
* After sign-in it goes to the page's `return_to` path when the URL
|
|
12
|
+
* has a safe one (a relative path on this origin), and otherwise to
|
|
13
|
+
* `redirect`. The link to `sign-up-href` carries `return_to` on.
|
|
14
|
+
*
|
|
15
|
+
* Events: `signed-in` (cancelable: cancel it to stay on the page instead
|
|
16
|
+
* of following `redirect`), `problem`.
|
|
17
|
+
*/
|
|
18
|
+
export class SignIn extends TensileElement {
|
|
19
|
+
static attrs = ['redirect', 'sign-up-href'];
|
|
20
|
+
autofillStarted = false;
|
|
21
|
+
async load() {
|
|
22
|
+
const o = this.options;
|
|
23
|
+
if (this.autofillStarted || !o.autofill || !hasPasskey(o) || !hasPassword(o))
|
|
24
|
+
return;
|
|
25
|
+
this.autofillStarted = true;
|
|
26
|
+
const r = await this.client.autofill();
|
|
27
|
+
if (r.outcome === 'ok')
|
|
28
|
+
this.signedIn(r);
|
|
29
|
+
}
|
|
30
|
+
body(o) {
|
|
31
|
+
const custom = o.headlineText.trim();
|
|
32
|
+
const head = this.state === 'code' || this.state === 'recovery'
|
|
33
|
+
? this.heading(o, this.r('signIn.code.headline'), this.t('signIn.code.title'))
|
|
34
|
+
: this.heading(o, custom ? html `${custom}` : this.r('signIn.headline'), custom || this.t('signIn.title'));
|
|
35
|
+
return html `${head}${this.ways(o)}`;
|
|
36
|
+
}
|
|
37
|
+
ways(o) {
|
|
38
|
+
const email = this.values.email ?? '';
|
|
39
|
+
switch (this.state) {
|
|
40
|
+
case 'done':
|
|
41
|
+
return this.done(this.t('signIn.doneChip'), 'green', this.t('signIn.done'));
|
|
42
|
+
case 'pw':
|
|
43
|
+
return html `<form class="body" data-do="password" novalidate>
|
|
44
|
+
<p class="as"><span>${email}</span><button class="lnk" type="button" part="link" data-do="change" data-key="change">${this.t('signIn.change')}</button></p>
|
|
45
|
+
<input class="vh" type="email" name="email" autocomplete="username" value="${email}" tabindex="-1" aria-hidden="true">
|
|
46
|
+
${this.field('password', this.t('signIn.passwordLabel'), html `type="password" autocomplete="current-password" required data-autofocus`, { invalid: true })}
|
|
47
|
+
${this.err()}
|
|
48
|
+
<div class="acts"><button class="btn fill" part="button button-primary" data-key="submit">${this.t('signIn.submit')}</button>
|
|
49
|
+
<button class="lnk" type="button" part="link" data-do="forgot" data-key="forgot">${this.t('signIn.forgot')}</button></div></form>`;
|
|
50
|
+
case 'code':
|
|
51
|
+
return html `<form class="body" data-do="code" novalidate>
|
|
52
|
+
${this.field('code', this.t('code.label'), html `class="code" inputmode="numeric" autocomplete="one-time-code" maxlength="6" placeholder="${this.t('code.placeholder')}" required data-autofocus`, { invalid: true })}
|
|
53
|
+
${this.err()}
|
|
54
|
+
<div class="acts"><button class="btn fill" part="button button-primary">${this.t('continue')}</button>
|
|
55
|
+
<button class="lnk" type="button" part="link" data-do="use-recovery">${this.t('signIn.recovery')}</button></div></form>`;
|
|
56
|
+
case 'recovery':
|
|
57
|
+
return html `<form class="body" data-do="recovery" novalidate>
|
|
58
|
+
${this.field('recovery', this.t('signIn.recoveryLabel'), html `class="code" autocomplete="off" autocapitalize="characters" spellcheck="false" required data-autofocus`, { invalid: true })}
|
|
59
|
+
${this.err()}
|
|
60
|
+
<div class="acts"><button class="btn fill" part="button button-primary">${this.t('continue')}</button>
|
|
61
|
+
<button class="lnk" type="button" part="link" data-do="use-code">${this.t('signIn.useCode')}</button></div></form>`;
|
|
62
|
+
}
|
|
63
|
+
const pk = (kind) => html `<div class="acts"><button class="btn ${kind}" type="button" part="button${kind === 'fill' ? ' button-primary' : ''}" data-do="passkey" data-key="passkey">${o.passkeyLabel.trim() || this.t('signIn.passkey')}</button></div>`;
|
|
64
|
+
const af = o.autofill && hasPasskey(o);
|
|
65
|
+
const pw = (kind) => html `<form class="stack" data-do="email" novalidate>
|
|
66
|
+
${this.field('email', this.t('signIn.emailLabel'), html `type="email" autocomplete="${af ? 'username webauthn' : 'username'}" placeholder="${this.t('signIn.emailPlaceholder')}" required`, {
|
|
67
|
+
invalid: true,
|
|
68
|
+
after: af ? html `<span class="afk" title="${this.t('signIn.autofillHint')}">${glyph.key}</span>` : undefined,
|
|
69
|
+
})}
|
|
70
|
+
<button class="btn ${kind}" part="button${kind === 'fill' ? ' button-primary' : ''}" data-key="email">${o.passwordLabel.trim() || this.t('signIn.email')}</button></form>`;
|
|
71
|
+
const or = html `<p class="or" part="divider"><span>${this.t('or')}</span></p>`;
|
|
72
|
+
let methods;
|
|
73
|
+
if (o.methods === 'passkey')
|
|
74
|
+
methods = pk('fill');
|
|
75
|
+
else if (o.methods === 'password')
|
|
76
|
+
methods = pw('fill');
|
|
77
|
+
else if (passkeyFirst(o))
|
|
78
|
+
methods = html `${pk('fill')}${or}${pw('plain')}`;
|
|
79
|
+
else
|
|
80
|
+
methods = html `${pw('fill')}${or}${pk('plain')}`;
|
|
81
|
+
const signUp = sameOrigin(this.getAttribute('sign-up-href'));
|
|
82
|
+
return html `<div class="body">${methods}${this.err()}${signUp ? html `<a class="lnk" part="link" href="${carryReturnTo(signUp)}">${this.t('signIn.signUp')}</a>` : ''}</div>`;
|
|
83
|
+
}
|
|
84
|
+
async act(name) {
|
|
85
|
+
const email = (this.values.email ?? '').trim();
|
|
86
|
+
switch (name) {
|
|
87
|
+
case 'passkey': return this.settle(await this.client.enter());
|
|
88
|
+
case 'email':
|
|
89
|
+
if (!email)
|
|
90
|
+
return this.fail(this.t('signIn.emailMissing'));
|
|
91
|
+
return this.go('pw');
|
|
92
|
+
case 'change': return this.go('start');
|
|
93
|
+
case 'password':
|
|
94
|
+
return this.settle(await this.client.signInWithPassword({ email, password: this.values.password ?? '' }));
|
|
95
|
+
case 'forgot': {
|
|
96
|
+
const r = await this.client.forgotPassword(email);
|
|
97
|
+
if (r.outcome !== 'ok')
|
|
98
|
+
return this.failWith(r);
|
|
99
|
+
this.error = '';
|
|
100
|
+
this.notice = this.t('signIn.forgotSent', { email });
|
|
101
|
+
this.emit('reset-requested', { email });
|
|
102
|
+
return this.update();
|
|
103
|
+
}
|
|
104
|
+
case 'code': {
|
|
105
|
+
const code = (this.values.code ?? '').replace(/\s/g, '');
|
|
106
|
+
if (!/^\d{6}$/.test(code))
|
|
107
|
+
return this.fail(this.t('code.bad'));
|
|
108
|
+
return this.settle(await this.client.verifyTOTP(code));
|
|
109
|
+
}
|
|
110
|
+
case 'recovery': return this.settle(await this.client.signInWithRecoveryCode((this.values.recovery ?? '').trim()));
|
|
111
|
+
case 'use-recovery': return this.go('recovery');
|
|
112
|
+
case 'use-code': return this.go('code');
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
settle(r) {
|
|
116
|
+
if (r.outcome === 'ok')
|
|
117
|
+
return this.signedIn(r);
|
|
118
|
+
if (r.outcome === 'needs_factor') {
|
|
119
|
+
this.emit('needs-factor', { needs: r.needs });
|
|
120
|
+
return this.go(r.needs.includes('totp') ? 'code' : 'recovery');
|
|
121
|
+
}
|
|
122
|
+
this.failWith(r);
|
|
123
|
+
}
|
|
124
|
+
signedIn(r) {
|
|
125
|
+
if (!this.emit('signed-in', r.data, true))
|
|
126
|
+
return;
|
|
127
|
+
const to = returnToHere() ?? sameOrigin(this.getAttribute('redirect'));
|
|
128
|
+
if (to) {
|
|
129
|
+
location.assign(to);
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
this.go('done');
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* `<tensile-sign-up>`: an email address, then a passkey or a password, in
|
|
137
|
+
* the order `methods` and `first` set. A password sign-up ends with "Check
|
|
138
|
+
* your email": the account exists once the person follows the link.
|
|
139
|
+
*
|
|
140
|
+
* Events: `signed-up` (cancelable, as `signed-in` is), `problem`.
|
|
141
|
+
*/
|
|
142
|
+
export class SignUp extends TensileElement {
|
|
143
|
+
static attrs = ['redirect', 'sign-in-href'];
|
|
144
|
+
body(o) {
|
|
145
|
+
const head = this.heading(o, this.r('signUp.headline'), this.t('signUp.title'));
|
|
146
|
+
if (this.state === 'done')
|
|
147
|
+
return html `${head}${this.done(this.t('signUp.doneChip'), 'green', this.t('signUp.done'))}`;
|
|
148
|
+
if (this.state === 'check') {
|
|
149
|
+
return html `${head}${this.done(this.t('signUp.checkEmailChip'), '', this.t('signUp.checkEmail', { email: this.values.email ?? '' }))}`;
|
|
150
|
+
}
|
|
151
|
+
const pkMain = hasPasskey(o) && passkeyFirst(o);
|
|
152
|
+
const mode = (this.state === 'alt') === pkMain ? 'password' : 'passkey';
|
|
153
|
+
const main = this.state !== 'alt';
|
|
154
|
+
const label = main && o.signUpLabel.trim() ? o.signUpLabel.trim() : this.t(mode === 'passkey' ? 'signUp.passkey' : 'signUp.password');
|
|
155
|
+
const other = o.methods === 'both'
|
|
156
|
+
? html `<button class="lnk" type="button" part="link" data-do="switch" data-key="switch">${this.t(mode === 'passkey' ? 'signUp.usePassword' : 'signUp.usePasskey')}</button>`
|
|
157
|
+
: '';
|
|
158
|
+
const signIn = sameOrigin(this.getAttribute('sign-in-href'));
|
|
159
|
+
return html `${head}<form class="body" data-do="${mode}" novalidate>
|
|
160
|
+
${this.field('email', this.t('signUp.emailLabel'), html `type="email" autocomplete="${mode === 'passkey' ? 'username' : 'email'}" placeholder="${this.t('signIn.emailPlaceholder')}" required data-autofocus`, { invalid: !this.values.email })}
|
|
161
|
+
${mode === 'password' ? this.field('password', this.t('signUp.passwordLabel'), html `type="password" autocomplete="new-password" minlength="15" required`, { hint: this.t('signUp.passwordHint'), invalid: !!this.values.email }) : ''}
|
|
162
|
+
${this.err()}
|
|
163
|
+
<div class="acts"><button class="btn fill" part="button button-primary" data-key="submit">${label}</button>${other}
|
|
164
|
+
${signIn ? html `<a class="lnk" part="link" href="${carryReturnTo(signIn)}">${this.t('signUp.signIn')}</a>` : ''}</div></form>`;
|
|
165
|
+
}
|
|
166
|
+
async act(name) {
|
|
167
|
+
const email = (this.values.email ?? '').trim();
|
|
168
|
+
if (name === 'switch')
|
|
169
|
+
return this.go(this.state === 'alt' ? 'start' : 'alt');
|
|
170
|
+
if (!email)
|
|
171
|
+
return this.fail(this.t('signIn.emailMissing'));
|
|
172
|
+
if (name === 'passkey') {
|
|
173
|
+
const r = await this.client.signUp({ name: email });
|
|
174
|
+
if (r.outcome !== 'ok')
|
|
175
|
+
return this.failWith(r);
|
|
176
|
+
if (!this.emit('signed-up', { method: 'passkey', data: r.data }, true))
|
|
177
|
+
return;
|
|
178
|
+
const to = returnToHere() ?? sameOrigin(this.getAttribute('redirect'));
|
|
179
|
+
if (to)
|
|
180
|
+
return location.assign(to);
|
|
181
|
+
return this.go('done');
|
|
182
|
+
}
|
|
183
|
+
if (name === 'password') {
|
|
184
|
+
const r = await this.client.signUpWithPassword({ email, password: this.values.password ?? '' });
|
|
185
|
+
if (r.outcome !== 'ok')
|
|
186
|
+
return this.failWith(r);
|
|
187
|
+
this.emit('signed-up', { method: 'password', confirmed: false });
|
|
188
|
+
this.go('check');
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* `<tensile-step-up>`: "Confirm it's you" before a sensitive action. Set
|
|
194
|
+
* `subject` (for example "Ravi Patel"), `action` (for example "to
|
|
195
|
+
* remove"), and `detail` to name the action, and `purpose` and `resource`
|
|
196
|
+
* to step up for that purpose only.
|
|
197
|
+
*
|
|
198
|
+
* Events: `stepped-up`, `cancelled`, `problem`.
|
|
199
|
+
*/
|
|
200
|
+
export class StepUp extends TensileElement {
|
|
201
|
+
static attrs = ['subject', 'action', 'detail', 'purpose', 'resource'];
|
|
202
|
+
body(o) {
|
|
203
|
+
const subject = this.getAttribute('subject') ?? '';
|
|
204
|
+
const sentence = subject
|
|
205
|
+
? this.r('stepUp.headline', { subject, action: this.getAttribute('action') ?? '' })
|
|
206
|
+
: this.r('stepUp.headlinePlain');
|
|
207
|
+
const head = this.heading(o, sentence, this.t('stepUp.title'), subject ? html `${this.getAttribute('action') ?? ''} <b>${subject}</b>` : undefined);
|
|
208
|
+
if (this.state === 'done')
|
|
209
|
+
return html `${head}${this.done(this.t('stepUp.doneChip'), 'green', this.t('stepUp.done'))}`;
|
|
210
|
+
if (this.state === 'cancelled') {
|
|
211
|
+
return html `${head}${this.done(this.t('stepUp.cancelledChip'), '', this.t('stepUp.cancelled'), { label: this.t('showAgain'), action: 'again' })}`;
|
|
212
|
+
}
|
|
213
|
+
const detail = this.getAttribute('detail');
|
|
214
|
+
const who = subject ? html `<div class="subject">${glyph.you}<span class="nm"><b>${subject}</b>${detail ? html `<small>${detail}</small>` : ''}</span></div>` : '';
|
|
215
|
+
const pk = hasPasskey(o);
|
|
216
|
+
return html `${head}<form class="body" data-do="${pk ? 'passkey' : 'password'}" novalidate>${who}
|
|
217
|
+
${pk ? '' : this.field('password', this.t('stepUp.passwordLabel'), html `type="password" autocomplete="current-password" required`, { invalid: true })}
|
|
218
|
+
${this.err()}
|
|
219
|
+
<div class="acts"><button class="btn fill" part="button button-primary" data-key="confirm">${this.t(pk ? 'stepUp.passkey' : 'stepUp.password')}</button>
|
|
220
|
+
<button class="btn plain" type="button" part="button" data-do="cancel" data-key="cancel">${this.t('cancel')}</button></div></form>`;
|
|
221
|
+
}
|
|
222
|
+
async act(name) {
|
|
223
|
+
if (name === 'cancel') {
|
|
224
|
+
this.emit('cancelled');
|
|
225
|
+
return this.go('cancelled');
|
|
226
|
+
}
|
|
227
|
+
if (name === 'again')
|
|
228
|
+
return this.go('start');
|
|
229
|
+
const purpose = this.getAttribute('purpose');
|
|
230
|
+
const r = name === 'passkey'
|
|
231
|
+
? await this.client.stepUp(purpose ? { for: { name: purpose, resource: this.getAttribute('resource') ?? undefined } } : {})
|
|
232
|
+
: await this.client.stepUpWithPassword(this.values.password ?? '');
|
|
233
|
+
if (r.outcome !== 'ok')
|
|
234
|
+
return this.failWith(r);
|
|
235
|
+
this.emit('stepped-up', r.data);
|
|
236
|
+
this.go('done');
|
|
237
|
+
}
|
|
238
|
+
}
|