-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathserver.js
More file actions
115 lines (102 loc) · 2.7 KB
/
Copy pathserver.js
File metadata and controls
115 lines (102 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
const express = require("express");
const { createAPortMiddleware } = require("../src");
const app = express();
const port = process.env.PORT || 3000;
// Middleware
app.use(express.json());
// Initialize APort middleware
const aportMiddleware = createAPortMiddleware({
apiKey: process.env.APORT_API_KEY,
baseUrl: process.env.APORT_BASE_URL,
});
// Routes
app.get("/", (req, res) => {
res.json({
message: "APort Express Middleware Example",
version: "1.0.0",
endpoints: {
"GET /public": "Public endpoint (no verification)",
"POST /refund":
"Refund endpoint (requires finance.payment.refund.v1 policy)",
"GET /admin": "Admin endpoint (requires admin.access policy)",
},
});
});
// Public endpoint (no verification required)
app.get("/public", (req, res) => {
res.json({
message: "This is a public endpoint",
timestamp: new Date().toISOString(),
});
});
// Refund endpoint (requires verification)
app.post(
"/refund",
aportMiddleware("finance.payment.refund.v1", {
context: {
endpoint: "refund",
action: "process_refund",
},
}),
(req, res) => {
const { amount, order_id } = req.body;
// Access verification result
const { passport, agentId } = req.aport;
// Check specific limits
if (amount > passport.limits.refund_amount_max_per_tx) {
return res.status(403).json({
error: "Refund amount exceeds limit",
requested: amount,
limit: passport.limits.refund_amount_max_per_tx,
});
}
// Process refund
res.json({
success: true,
message: "Refund processed successfully",
refund: {
id: `refund_${Date.now()}`,
amount: amount,
order_id: order_id,
agent_id: agentId,
timestamp: new Date().toISOString(),
},
});
}
);
// Admin endpoint (requires admin access)
app.get(
"/admin",
aportMiddleware("admin.access", {
context: {
endpoint: "admin",
action: "view_dashboard",
},
}),
(req, res) => {
const { passport } = req.aport;
res.json({
message: "Admin dashboard",
user: {
agent_id: req.aport.agentId,
capabilities: passport.capabilities,
limits: passport.limits,
},
timestamp: new Date().toISOString(),
});
}
);
// Error handling
app.use((err, req, res, next) => {
console.error("Error:", err);
res.status(500).json({
error: "Internal server error",
message: err.message,
});
});
// Start server
app.listen(port, () => {
console.log(`🚀 Server running on http://localhost:${port}`);
console.log(`📚 API Documentation: http://localhost:${port}`);
console.log(`🔑 Make sure to set APORT_API_KEY environment variable`);
});