@zeroae/countersign-widget 0.0.0-stage → 0.1.1
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 +21 -0
- package/README.md +10 -2
- package/package.json +27 -4
- package/src/hints.js +10 -0
- package/src/service-worker.js +34 -0
- package/src/widget.js +122 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Zero A.E., LLC
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @zeroae/countersign-widget
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Add pairing and certificate-approval controls to an authenticated web page in a Countersign integration.
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
npm install @zeroae/countersign-widget
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
Import `@zeroae/countersign-widget` to register the `<countersign-widget>` element. Set its `adapter` property to connect it to your backend. The service worker entry point exports `installPushHandlers` for Web Push notifications.
|
|
10
|
+
|
|
11
|
+
[Widget and adapter reference](https://zeroae.github.io/countersign/reference/widget/) · [Demo Integrator](https://github.com/zeroae/countersign/tree/main/examples/demo-integrator)
|
package/package.json
CHANGED
|
@@ -1,6 +1,29 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zeroae/countersign-widget",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"exports": {
|
|
6
|
+
".": "./src/widget.js",
|
|
7
|
+
"./service-worker": "./src/service-worker.js"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"lint": "eslint src test",
|
|
11
|
+
"test": "node --test"
|
|
12
|
+
},
|
|
13
|
+
"devDependencies": {
|
|
14
|
+
"happy-dom": "^21.0.4"
|
|
15
|
+
},
|
|
16
|
+
"description": "Web component and service worker helpers for Countersign pairing and approvals",
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/zeroae/countersign.git",
|
|
21
|
+
"directory": "packages/widget"
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"src"
|
|
25
|
+
],
|
|
26
|
+
"publishConfig": {
|
|
27
|
+
"access": "public"
|
|
28
|
+
}
|
|
29
|
+
}
|
package/src/hints.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// IDs are randomly generated by the Broker. Limit the alphabet before using
|
|
2
|
+
// an ID as a path segment or delivering it to the OS protocol handler.
|
|
3
|
+
export function isRequestId(value) {
|
|
4
|
+
return typeof value === 'string' && /^[A-Za-z0-9_-]{22,128}$/.test(value);
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function approvalLink(id) {
|
|
8
|
+
if (!isRequestId(id)) throw new TypeError('Invalid Countersign request ID');
|
|
9
|
+
return `countersign://approve/${id}`;
|
|
10
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { isRequestId } from './hints.js';
|
|
2
|
+
|
|
3
|
+
// Register in the Integrator's service worker. The server sends a JSON string
|
|
4
|
+
// containing only the request ID. The target page does its own session check.
|
|
5
|
+
export function installPushHandlers(worker, page) {
|
|
6
|
+
const url = new URL(page, worker.location.href);
|
|
7
|
+
if (url.protocol !== 'https:' || url.origin !== worker.location.origin ||
|
|
8
|
+
url.username || url.password) {
|
|
9
|
+
throw new TypeError('Expected an HTTPS page on the Integrator origin');
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
worker.addEventListener('push', event => {
|
|
13
|
+
let id;
|
|
14
|
+
try {
|
|
15
|
+
id = event.data?.json();
|
|
16
|
+
} catch {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
if (!isRequestId(id)) return;
|
|
20
|
+
event.waitUntil(worker.registration.showNotification('Countersign approval requested', {
|
|
21
|
+
tag: `countersign-${id}`,
|
|
22
|
+
data: id,
|
|
23
|
+
}));
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
worker.addEventListener('notificationclick', event => {
|
|
27
|
+
const id = event.notification.data;
|
|
28
|
+
if (!isRequestId(id) || event.notification.tag !== `countersign-${id}`) return;
|
|
29
|
+
const target = new URL(url);
|
|
30
|
+
target.hash = new URLSearchParams({ countersign: id }).toString();
|
|
31
|
+
event.notification.close();
|
|
32
|
+
event.waitUntil(worker.clients.openWindow(target.href));
|
|
33
|
+
});
|
|
34
|
+
}
|
package/src/widget.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { approvalLink, isRequestId } from './hints.js';
|
|
2
|
+
|
|
3
|
+
// The Integrator supplies a session-bound backend adapter. It owns the user,
|
|
4
|
+
// Context, CSRF protection and any explanation of a denied request.
|
|
5
|
+
export class CountersignWidget extends HTMLElement {
|
|
6
|
+
#adapter;
|
|
7
|
+
#generation = 0;
|
|
8
|
+
#busy = false;
|
|
9
|
+
|
|
10
|
+
constructor() {
|
|
11
|
+
super();
|
|
12
|
+
const root = this.attachShadow({ mode: 'open' });
|
|
13
|
+
root.innerHTML = `
|
|
14
|
+
<style>
|
|
15
|
+
:host { display: inline-block; font-family: var(--countersign-font, system-ui, sans-serif); }
|
|
16
|
+
[part=actions] { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; }
|
|
17
|
+
button, a { font: inherit; }
|
|
18
|
+
a { color: var(--countersign-accent, LinkText); }
|
|
19
|
+
[role=status]:empty { display: none; }
|
|
20
|
+
</style>
|
|
21
|
+
<div part="actions">
|
|
22
|
+
<button type="button" data-action="pair">Pair Companion</button>
|
|
23
|
+
<button type="button" data-action="connect">Request access</button>
|
|
24
|
+
<a part="launch" hidden>Open Companion</a>
|
|
25
|
+
<button type="button" data-action="push" hidden>Enable notifications</button>
|
|
26
|
+
</div>
|
|
27
|
+
<p role="status" part="status"></p>`;
|
|
28
|
+
root.addEventListener('click', event => {
|
|
29
|
+
const action = event.target.dataset?.action;
|
|
30
|
+
if (action) void this.#run(action);
|
|
31
|
+
});
|
|
32
|
+
this.#renderControls();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
set adapter(value) {
|
|
36
|
+
if (value != null &&
|
|
37
|
+
(typeof value.pair !== 'function' || typeof value.connect !== 'function')) {
|
|
38
|
+
throw new TypeError('The adapter must implement pair() and connect()');
|
|
39
|
+
}
|
|
40
|
+
this.#adapter = value;
|
|
41
|
+
this.#reset();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
get adapter() {
|
|
45
|
+
return this.#adapter;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
disconnectedCallback() {
|
|
49
|
+
this.#reset();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Call with an owned ID returned by the Integrator backend after the user
|
|
53
|
+
// signs in, including when arriving from a notification click.
|
|
54
|
+
showRequest(id) {
|
|
55
|
+
const link = this.shadowRoot.querySelector('a');
|
|
56
|
+
link.href = approvalLink(id);
|
|
57
|
+
link.hidden = false;
|
|
58
|
+
this.#status('Review this request in your Companion.');
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
#reset() {
|
|
62
|
+
this.#generation++;
|
|
63
|
+
this.#busy = false;
|
|
64
|
+
this.shadowRoot.querySelector('a').hidden = true;
|
|
65
|
+
this.shadowRoot.querySelector('a').removeAttribute('href');
|
|
66
|
+
this.#status('');
|
|
67
|
+
this.#renderControls();
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
#status(message) {
|
|
71
|
+
this.shadowRoot.querySelector('[role=status]').textContent = message;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
#renderControls() {
|
|
75
|
+
for (const button of this.shadowRoot.querySelectorAll('button')) {
|
|
76
|
+
button.disabled = !this.#adapter || this.#busy;
|
|
77
|
+
if (button.dataset.action === 'push') {
|
|
78
|
+
button.hidden = typeof this.#adapter?.subscribe !== 'function';
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async #run(action) {
|
|
84
|
+
if (!this.#adapter || this.#busy) return;
|
|
85
|
+
const generation = this.#generation;
|
|
86
|
+
this.#busy = true;
|
|
87
|
+
this.#renderControls();
|
|
88
|
+
try {
|
|
89
|
+
if (action === 'pair') {
|
|
90
|
+
await this.#adapter.pair();
|
|
91
|
+
if (generation === this.#generation) this.#status('Continue pairing in your Companion.');
|
|
92
|
+
} else if (action === 'connect') {
|
|
93
|
+
this.shadowRoot.querySelector('a').hidden = true;
|
|
94
|
+
const id = await this.#adapter.connect();
|
|
95
|
+
if (generation !== this.#generation) return;
|
|
96
|
+
if (!isRequestId(id)) throw new TypeError('Invalid request ID');
|
|
97
|
+
this.showRequest(id);
|
|
98
|
+
} else if (action === 'push') {
|
|
99
|
+
await this.#adapter.subscribe();
|
|
100
|
+
if (generation === this.#generation) this.#status('Notifications enabled.');
|
|
101
|
+
}
|
|
102
|
+
} catch {
|
|
103
|
+
if (generation === this.#generation) this.#status('This action could not be completed. Try again.');
|
|
104
|
+
} finally {
|
|
105
|
+
if (generation === this.#generation) {
|
|
106
|
+
this.#busy = false;
|
|
107
|
+
this.#renderControls();
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Call from the subscription adapter on a user gesture. Hand the result to
|
|
114
|
+
// the Integrator backend before reporting successful registration.
|
|
115
|
+
export async function subscribeForPush(registration, applicationServerKey, save) {
|
|
116
|
+
const manager = registration.pushManager;
|
|
117
|
+
const subscription = await manager.getSubscription() ??
|
|
118
|
+
await manager.subscribe({ userVisibleOnly: true, applicationServerKey });
|
|
119
|
+
await save(subscription.toJSON());
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
customElements.define('countersign-widget', CountersignWidget);
|