@datacules/agent-identity-express 0.2.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/package.json +23 -0
- package/src/index.ts +85 -0
package/package.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@datacules/agent-identity-express",
|
|
3
|
+
"version": "0.2.1",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "Express middleware for @datacules/agent-identity",
|
|
6
|
+
"main": "./dist/cjs/index.js",
|
|
7
|
+
"module": "./dist/esm/index.js",
|
|
8
|
+
"types": "./dist/types/index.d.ts",
|
|
9
|
+
"scripts": {
|
|
10
|
+
"build": "tsc -p tsconfig.build.json",
|
|
11
|
+
"type-check": "tsc --noEmit"
|
|
12
|
+
},
|
|
13
|
+
"peerDependencies": {
|
|
14
|
+
"@datacules/agent-identity": "^0.1.0",
|
|
15
|
+
"express": ">=4.0.0"
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"@datacules/agent-identity": "*",
|
|
19
|
+
"@types/express": "^4",
|
|
20
|
+
"express": "^4",
|
|
21
|
+
"typescript": "^5"
|
|
22
|
+
}
|
|
23
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Express middleware for @datacules/agent-identity.
|
|
3
|
+
*
|
|
4
|
+
* Resolves credentials before any downstream route handler runs.
|
|
5
|
+
* The resolved credential is attached to req.resolvedCredential.
|
|
6
|
+
*
|
|
7
|
+
* Usage:
|
|
8
|
+
* import express from 'express';
|
|
9
|
+
* import { agentIdentityMiddleware } from '@datacules/agent-identity-express';
|
|
10
|
+
*
|
|
11
|
+
* const app = express();
|
|
12
|
+
* app.use(express.json());
|
|
13
|
+
* app.use(agentIdentityMiddleware({ credentials, rules, logger }));
|
|
14
|
+
*
|
|
15
|
+
* app.post('/ai/complete', (req, res) => {
|
|
16
|
+
* const cred = req.resolvedCredential; // already resolved
|
|
17
|
+
* });
|
|
18
|
+
*/
|
|
19
|
+
import { createRouter } from '@datacules/agent-identity';
|
|
20
|
+
import type {
|
|
21
|
+
AgentRequestContext,
|
|
22
|
+
AuditLogger,
|
|
23
|
+
Credential,
|
|
24
|
+
ResolvedCredential,
|
|
25
|
+
RoutingRule,
|
|
26
|
+
} from '@datacules/agent-identity';
|
|
27
|
+
import type { Request, Response, NextFunction } from 'express';
|
|
28
|
+
|
|
29
|
+
// Extend Express Request type
|
|
30
|
+
declare global {
|
|
31
|
+
namespace Express {
|
|
32
|
+
interface Request {
|
|
33
|
+
resolvedCredential?: ResolvedCredential;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface AgentIdentityMiddlewareOptions {
|
|
39
|
+
credentials: Credential[];
|
|
40
|
+
rules: RoutingRule[];
|
|
41
|
+
logger?: AuditLogger;
|
|
42
|
+
/**
|
|
43
|
+
* Key in req.body that holds the AgentRequestContext.
|
|
44
|
+
* Default: 'agentContext'
|
|
45
|
+
*/
|
|
46
|
+
contextKey?: string;
|
|
47
|
+
/**
|
|
48
|
+
* If true, the middleware passes through when no agentContext is found
|
|
49
|
+
* rather than returning 400. Use when the middleware is global and only
|
|
50
|
+
* some routes are agent-identity-aware.
|
|
51
|
+
* Default: true
|
|
52
|
+
*/
|
|
53
|
+
passThrough?: boolean;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function agentIdentityMiddleware(options: AgentIdentityMiddlewareOptions) {
|
|
57
|
+
const {
|
|
58
|
+
credentials,
|
|
59
|
+
rules,
|
|
60
|
+
logger,
|
|
61
|
+
contextKey = 'agentContext',
|
|
62
|
+
passThrough = true,
|
|
63
|
+
} = options;
|
|
64
|
+
|
|
65
|
+
const router = createRouter(credentials, rules, logger);
|
|
66
|
+
|
|
67
|
+
return (req: Request, res: Response, next: NextFunction): void => {
|
|
68
|
+
const ctx = req.body?.[contextKey] as AgentRequestContext | undefined;
|
|
69
|
+
|
|
70
|
+
if (!ctx) {
|
|
71
|
+
if (passThrough) return next();
|
|
72
|
+
res.status(400).json({ error: `Missing required field: ${contextKey}` });
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const resolved = router.resolve(ctx);
|
|
77
|
+
if (!resolved) {
|
|
78
|
+
res.status(403).json({ error: 'No credential resolved for this context' });
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
req.resolvedCredential = resolved;
|
|
83
|
+
next();
|
|
84
|
+
};
|
|
85
|
+
}
|