@nevermined-io/ui-widgets-server 0.3.1 → 0.4.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/README.md +89 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# @nevermined-io/ui-widgets-server
|
|
2
|
+
|
|
3
|
+
Server-side helper for the Nevermined UI Widgets. Mints short-lived **init tokens** that the browser SDK ([`@nevermined-io/ui-widgets`](https://www.npmjs.com/package/@nevermined-io/ui-widgets)) exchanges for an authenticated widget session.
|
|
4
|
+
|
|
5
|
+
This package is intended to run **only on your backend**. It signs JWTs with your widget key secret, which must never be exposed to the browser.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @nevermined-io/ui-widgets-server
|
|
11
|
+
# or
|
|
12
|
+
pnpm add @nevermined-io/ui-widgets-server
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Requires Node 18+ (uses Web Crypto via [`jose`](https://www.npmjs.com/package/jose)).
|
|
16
|
+
|
|
17
|
+
## Quick start
|
|
18
|
+
|
|
19
|
+
In your organization's backend, create an endpoint that returns a fresh init token for the currently logged-in user:
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import express from 'express'
|
|
23
|
+
import { createInitToken } from '@nevermined-io/ui-widgets-server'
|
|
24
|
+
|
|
25
|
+
const app = express()
|
|
26
|
+
|
|
27
|
+
app.get('/api/widget-init-token', async (req, res) => {
|
|
28
|
+
// Authenticate the request your normal way and resolve the user.
|
|
29
|
+
const userId = req.user.id
|
|
30
|
+
|
|
31
|
+
const initToken = await createInitToken({
|
|
32
|
+
userId,
|
|
33
|
+
orgId: process.env.NVM_ORG_ID!,
|
|
34
|
+
secretKey: process.env.NVM_WIDGET_KEY_SECRET!, // never expose to the browser
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
res.json({ initToken })
|
|
38
|
+
})
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Then, in the browser:
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
import { NeverminedWidgets } from '@nevermined-io/ui-widgets'
|
|
45
|
+
|
|
46
|
+
const { initToken } = await fetch('/api/widget-init-token').then(r => r.json())
|
|
47
|
+
|
|
48
|
+
const nvm = await NeverminedWidgets.initialize({
|
|
49
|
+
initToken,
|
|
50
|
+
environment: 'sandbox',
|
|
51
|
+
})
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## API
|
|
55
|
+
|
|
56
|
+
### `createInitToken(options): Promise<string>`
|
|
57
|
+
|
|
58
|
+
Returns a signed JWT (HS256) that the browser SDK passes to `POST /api/v1/widgets/session`. The token's audience is `nvm:widgets` and it carries the user identity (`sub`) and organization id (`orgId`) as claims.
|
|
59
|
+
|
|
60
|
+
#### Options
|
|
61
|
+
|
|
62
|
+
| Field | Type | Required | Default | Description |
|
|
63
|
+
| ------------ | -------- | -------- | ------- | ----------- |
|
|
64
|
+
| `userId` | `string` | yes | — | Stable identifier for the end user in your system. Becomes the JWT `sub` claim. |
|
|
65
|
+
| `orgId` | `string` | yes | — | Your Nevermined organization id. |
|
|
66
|
+
| `secretKey` | `string` | yes | — | The widget key secret (`wk_...`) issued in the Nevermined dashboard. **Server-side only.** |
|
|
67
|
+
| `expiresIn` | `string` | no | `'5m'` | JWT lifetime — any [`jose`-compatible](https://github.com/panva/jose) duration string. The default is intentionally short: the browser exchanges the token immediately for a long-lived session. |
|
|
68
|
+
|
|
69
|
+
#### Token shape
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
interface InitTokenPayload {
|
|
73
|
+
sub: string // userId
|
|
74
|
+
orgId: string
|
|
75
|
+
aud: 'nvm:widgets'
|
|
76
|
+
iat: number
|
|
77
|
+
exp: number
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## Security notes
|
|
82
|
+
|
|
83
|
+
- Treat `secretKey` like a database password: never inline it in client code, commit it, or send it through an unencrypted channel. If it leaks, rotate the widget key in the dashboard immediately.
|
|
84
|
+
- Issue a new init token per session — they are short-lived by design. Do not cache them across users.
|
|
85
|
+
- Pair this with `allowedOrigins` on the widget key so even a leaked secret can't mint sessions for arbitrary origins.
|
|
86
|
+
|
|
87
|
+
## License
|
|
88
|
+
|
|
89
|
+
Apache-2.0 © Nevermined
|