@tensile-tools/auth 0.0.0-stage → 0.4.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 +202 -0
- package/README.md +142 -2
- package/dist/adapt.d.ts +54 -0
- package/dist/adapt.js +72 -0
- package/dist/api.d.ts +63 -0
- package/dist/api.js +8 -0
- package/dist/body.d.ts +18 -0
- package/dist/body.js +55 -0
- package/dist/cache.d.ts +73 -0
- package/dist/cache.js +222 -0
- package/dist/clientip.d.ts +43 -0
- package/dist/clientip.js +151 -0
- package/dist/core.d.ts +219 -0
- package/dist/core.js +420 -0
- package/dist/express.d.ts +60 -0
- package/dist/express.js +110 -0
- package/dist/gen/api.d.ts +5368 -0
- package/dist/gen/api.js +5 -0
- package/dist/hono.d.ts +49 -0
- package/dist/hono.js +43 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +8 -0
- package/dist/next.d.ts +106 -0
- package/dist/next.js +205 -0
- package/dist/problem.d.ts +36 -0
- package/dist/problem.js +79 -0
- package/dist/proxy.d.ts +57 -0
- package/dist/proxy.js +137 -0
- package/dist/rebuild.d.ts +19 -0
- package/dist/rebuild.js +58 -0
- package/dist/transport.d.ts +6 -0
- package/dist/transport.js +39 -0
- package/dist/webhooks.d.ts +53 -0
- package/dist/webhooks.js +124 -0
- package/express/README.md +62 -0
- package/hono/README.md +58 -0
- package/next/README.md +89 -0
- package/package.json +100 -4
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { Request as ExpressRequest, RequestHandler, Response as ExpressResponse } from 'express';
|
|
2
|
+
import { type ApprovableOptions } from './adapt.ts';
|
|
3
|
+
import { type TrustProxy } from './clientip.ts';
|
|
4
|
+
import { type Action, type Need, type Principal, type Subject, type Tensile, type TensileOptions } from './core.ts';
|
|
5
|
+
import { type ProxyRoutes } from './proxy.ts';
|
|
6
|
+
declare global {
|
|
7
|
+
namespace Express {
|
|
8
|
+
interface Request {
|
|
9
|
+
/** The authenticated caller, set by `authenticate`, `need`, and `approvable`. `undefined` when signed out. */
|
|
10
|
+
principal?: Principal;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export type { ApprovableOptions } from './adapt.ts';
|
|
15
|
+
/** Options of the Express adapter. */
|
|
16
|
+
export interface ExpressOptions extends TensileOptions {
|
|
17
|
+
/**
|
|
18
|
+
* The forwarded header to read for the client's address. The adapter
|
|
19
|
+
* reads it only when the socket's address is in `trustedProxies`, and
|
|
20
|
+
* uses the socket's address otherwise. Express's own `trust proxy`
|
|
21
|
+
* setting isn't consulted.
|
|
22
|
+
*/
|
|
23
|
+
trustProxy?: TrustProxy;
|
|
24
|
+
/** The proxies' own addresses, as CIDRs. Defaults to none. */
|
|
25
|
+
trustedProxies?: readonly string[];
|
|
26
|
+
/** Reads the client's address another way. Overrides `trustProxy`. */
|
|
27
|
+
clientIp?: (req: ExpressRequest) => string | undefined;
|
|
28
|
+
}
|
|
29
|
+
/** A request whose principal is set. */
|
|
30
|
+
export type AuthedRequest = ExpressRequest & {
|
|
31
|
+
principal: Principal;
|
|
32
|
+
};
|
|
33
|
+
/** The Express adapter. */
|
|
34
|
+
export interface ExpressAuth {
|
|
35
|
+
/** The core client. */
|
|
36
|
+
readonly client: Tensile;
|
|
37
|
+
/** Forwards the browser routes to the server. Mount it before body parsers: `app.use(auth.proxy())`. */
|
|
38
|
+
proxy(routes?: ProxyRoutes): RequestHandler;
|
|
39
|
+
/** Sets `req.principal`, or leaves it `undefined` when signed out. Refuses a bad bearer token with 401. */
|
|
40
|
+
authenticate(): RequestHandler;
|
|
41
|
+
/** Lets the request through only when the principal meets `need`. */
|
|
42
|
+
need(need: Need): RequestHandler;
|
|
43
|
+
/**
|
|
44
|
+
* Like `need`, and an agent that meets a human-only need may ask its
|
|
45
|
+
* human to approve the action `describe` returns. `options.wait`
|
|
46
|
+
* replaces `approvalWait` for this route.
|
|
47
|
+
*/
|
|
48
|
+
approvable(need: Need, describe: (req: ExpressRequest) => Action, options?: ApprovableOptions): RequestHandler;
|
|
49
|
+
/** Calls on the person's behalf, with the request's own credential. */
|
|
50
|
+
as(req: ExpressRequest): Subject;
|
|
51
|
+
}
|
|
52
|
+
/** Creates the Express adapter. */
|
|
53
|
+
export declare function tensile(options: ExpressOptions): ExpressAuth;
|
|
54
|
+
/**
|
|
55
|
+
* Turns an Express request into a Fetch API request, with its body as a
|
|
56
|
+
* stream when `body` is set, and `signal` as its signal.
|
|
57
|
+
*/
|
|
58
|
+
export declare function toFetchRequest(req: ExpressRequest, body: boolean, signal?: AbortSignal): Request;
|
|
59
|
+
/** Writes a Fetch API response to Express, streaming its body. */
|
|
60
|
+
export declare function send(res: ExpressResponse, out: Response): Promise<void>;
|
package/dist/express.js
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
// The Express 5 adapter.
|
|
2
|
+
import { Readable } from 'node:stream';
|
|
3
|
+
import { applyTo, decide } from "./adapt.js";
|
|
4
|
+
import { clientIpReader } from "./clientip.js";
|
|
5
|
+
import { createTensile, waitSeconds } from "./core.js";
|
|
6
|
+
import { isProxied } from "./proxy.js";
|
|
7
|
+
/** Creates the Express adapter. */
|
|
8
|
+
export function tensile(options) {
|
|
9
|
+
const client = createTensile(options);
|
|
10
|
+
const read = clientIpReader(options);
|
|
11
|
+
const clientIp = options.clientIp ?? ((req) => read(toHeaders(req), { address: req.socket.remoteAddress }));
|
|
12
|
+
const gate = (g) => async (req, res, next) => {
|
|
13
|
+
// The check ends when the client disconnects, so a waiting
|
|
14
|
+
// approval check doesn't outlive the agent's request.
|
|
15
|
+
const ac = new AbortController();
|
|
16
|
+
const gone = () => { if (!res.writableFinished)
|
|
17
|
+
ac.abort(); };
|
|
18
|
+
res.on('close', gone);
|
|
19
|
+
try {
|
|
20
|
+
const d = await decide(client, toFetchRequest(req, false, ac.signal), { ...g(req), clientIp: clientIp(req) });
|
|
21
|
+
res.off('close', gone);
|
|
22
|
+
if (ac.signal.aborted)
|
|
23
|
+
return;
|
|
24
|
+
if (d.kind === 'refused')
|
|
25
|
+
return void (await send(res, d.response));
|
|
26
|
+
apply(res, d);
|
|
27
|
+
req.principal = d.kind === 'allowed' ? d.principal : undefined;
|
|
28
|
+
next();
|
|
29
|
+
}
|
|
30
|
+
catch (err) {
|
|
31
|
+
res.off('close', gone);
|
|
32
|
+
next(err);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
return {
|
|
36
|
+
client,
|
|
37
|
+
proxy(routes = {}) {
|
|
38
|
+
const handler = client.proxy(routes);
|
|
39
|
+
return async (req, res, next) => {
|
|
40
|
+
if (!isProxied(rawPath(req), routes))
|
|
41
|
+
return next();
|
|
42
|
+
try {
|
|
43
|
+
await send(res, await handler(toFetchRequest(req, true), { clientIp: clientIp(req) }));
|
|
44
|
+
}
|
|
45
|
+
catch (err) {
|
|
46
|
+
next(err);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
},
|
|
50
|
+
authenticate: () => gate(() => ({})),
|
|
51
|
+
need: need => gate(() => ({ need })),
|
|
52
|
+
approvable(need, describe, o = {}) {
|
|
53
|
+
const wait = o.wait === undefined ? undefined : waitSeconds(o.wait, 'wait');
|
|
54
|
+
return gate(req => ({ need, action: describe(req), wait }));
|
|
55
|
+
},
|
|
56
|
+
as: req => client.as(toFetchRequest(req, false)),
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
// rawPath is the path as the client sent it, still encoded.
|
|
60
|
+
function rawPath(req) {
|
|
61
|
+
const u = req.originalUrl;
|
|
62
|
+
const q = u.indexOf('?');
|
|
63
|
+
return q < 0 ? u : u.slice(0, q);
|
|
64
|
+
}
|
|
65
|
+
function toHeaders(req) {
|
|
66
|
+
const h = new Headers();
|
|
67
|
+
for (const [k, v] of Object.entries(req.headers)) {
|
|
68
|
+
if (Array.isArray(v))
|
|
69
|
+
for (const x of v)
|
|
70
|
+
h.append(k, x);
|
|
71
|
+
else if (v !== undefined)
|
|
72
|
+
h.set(k, v);
|
|
73
|
+
}
|
|
74
|
+
return h;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Turns an Express request into a Fetch API request, with its body as a
|
|
78
|
+
* stream when `body` is set, and `signal` as its signal.
|
|
79
|
+
*/
|
|
80
|
+
export function toFetchRequest(req, body, signal) {
|
|
81
|
+
const url = `${req.protocol}://${req.get('host')}${req.originalUrl}`;
|
|
82
|
+
const withBody = body && req.method !== 'GET' && req.method !== 'HEAD';
|
|
83
|
+
return new Request(url, {
|
|
84
|
+
method: req.method,
|
|
85
|
+
headers: toHeaders(req),
|
|
86
|
+
...(signal ? { signal } : {}),
|
|
87
|
+
...(withBody ? { body: Readable.toWeb(req), duplex: 'half' } : {}),
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
function apply(res, d) {
|
|
91
|
+
const h = new Headers();
|
|
92
|
+
applyTo(h, d);
|
|
93
|
+
h.forEach((v, k) => { if (k !== 'set-cookie')
|
|
94
|
+
res.setHeader(k, v); });
|
|
95
|
+
for (const c of h.getSetCookie())
|
|
96
|
+
res.append('set-cookie', c);
|
|
97
|
+
}
|
|
98
|
+
/** Writes a Fetch API response to Express, streaming its body. */
|
|
99
|
+
export async function send(res, out) {
|
|
100
|
+
res.status(out.status);
|
|
101
|
+
out.headers.forEach((v, k) => { if (k !== 'set-cookie')
|
|
102
|
+
res.setHeader(k, v); });
|
|
103
|
+
for (const c of out.headers.getSetCookie())
|
|
104
|
+
res.append('set-cookie', c);
|
|
105
|
+
if (!out.body)
|
|
106
|
+
return void res.end();
|
|
107
|
+
await new Promise((resolve, reject) => {
|
|
108
|
+
Readable.fromWeb(out.body).on('error', reject).pipe(res).on('finish', resolve).on('error', reject);
|
|
109
|
+
});
|
|
110
|
+
}
|