atp-sdk 1.0.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/CHANGELOG.md +111 -0
- package/LICENSE +201 -0
- package/README.md +633 -0
- package/dist/__tests__/setup.d.ts.map +1 -0
- package/dist/__tests__/setup.js +55 -0
- package/dist/__tests__/setup.js.map +1 -0
- package/dist/client/atp.d.ts.map +1 -0
- package/dist/client/atp.js +90 -0
- package/dist/client/atp.js.map +1 -0
- package/dist/client/audit.d.ts.map +1 -0
- package/dist/client/audit.js +125 -0
- package/dist/client/audit.js.map +1 -0
- package/dist/client/base.d.ts.map +1 -0
- package/dist/client/base.js +190 -0
- package/dist/client/base.js.map +1 -0
- package/dist/client/credentials.d.ts.map +1 -0
- package/dist/client/credentials.js +112 -0
- package/dist/client/credentials.js.map +1 -0
- package/dist/client/gateway.d.ts.map +1 -0
- package/dist/client/gateway.js +214 -0
- package/dist/client/gateway.js.map +1 -0
- package/dist/client/identity.d.ts.map +1 -0
- package/dist/client/identity.js +94 -0
- package/dist/client/identity.js.map +1 -0
- package/dist/client/permissions.d.ts.map +1 -0
- package/dist/client/permissions.js +132 -0
- package/dist/client/permissions.js.map +1 -0
- package/dist/index.cjs +89 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +72 -0
- package/dist/index.js.map +1 -0
- package/dist/simple-agent.d.ts.map +1 -0
- package/dist/simple-agent.js +261 -0
- package/dist/simple-agent.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +48 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/crypto.d.ts.map +1 -0
- package/dist/utils/crypto.js +100 -0
- package/dist/utils/crypto.js.map +1 -0
- package/dist/utils/did.d.ts.map +1 -0
- package/dist/utils/did.js +225 -0
- package/dist/utils/did.js.map +1 -0
- package/dist/utils/jwt.d.ts.map +1 -0
- package/dist/utils/jwt.js +235 -0
- package/dist/utils/jwt.js.map +1 -0
- package/docs/README.md +362 -0
- package/docs/api/README.md +1077 -0
- package/docs/guides/authentication.md +667 -0
- package/docs/guides/best-practices.md +1004 -0
- package/docs/guides/configuration.md +588 -0
- package/docs/guides/error-handling.md +1073 -0
- package/docs/guides/troubleshooting.md +850 -0
- package/examples/01-basic-setup.js +53 -0
- package/examples/02-identity-management.js +130 -0
- package/examples/03-verifiable-credentials.js +234 -0
- package/examples/04-permissions-and-access-control.js +326 -0
- package/examples/05-audit-logging.js +310 -0
- package/examples/06-real-time-monitoring.js +302 -0
- package/examples/07-advanced-use-cases.js +584 -0
- package/examples/README.md +211 -0
- package/examples/index.js +135 -0
- package/examples/simple-3-line.ts +51 -0
- package/package.json +108 -0
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Real-time Monitoring Example
|
|
3
|
+
*
|
|
4
|
+
* This example demonstrates how to:
|
|
5
|
+
* - Connect to real-time event streams
|
|
6
|
+
* - Monitor security events and alerts
|
|
7
|
+
* - Handle different types of real-time notifications
|
|
8
|
+
* - Implement event filtering and processing
|
|
9
|
+
* - Manage WebSocket connections and reconnection
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { ATPClient, createQuickConfig, DIDUtils } from '@atp/sdk';
|
|
13
|
+
|
|
14
|
+
async function realTimeMonitoringExample() {
|
|
15
|
+
console.log('β‘ ATPβ’ SDK Real-time Monitoring Example\n');
|
|
16
|
+
|
|
17
|
+
// Setup client
|
|
18
|
+
const config = createQuickConfig('http://localhost');
|
|
19
|
+
const client = new ATPClient(config);
|
|
20
|
+
|
|
21
|
+
try {
|
|
22
|
+
// Setup: Create monitoring identity
|
|
23
|
+
console.log('ποΈ Setup: Creating monitoring identity...');
|
|
24
|
+
|
|
25
|
+
const monitorData = await DIDUtils.generateDID({ network: 'testnet' });
|
|
26
|
+
const monitorDID = monitorData.did;
|
|
27
|
+
const monitorKey = monitorData.keyPair.privateKey;
|
|
28
|
+
|
|
29
|
+
console.log(`π‘ Monitor DID: ${monitorDID}`);
|
|
30
|
+
console.log();
|
|
31
|
+
|
|
32
|
+
// Authenticate as monitor
|
|
33
|
+
client.setAuthentication({
|
|
34
|
+
did: monitorDID,
|
|
35
|
+
privateKey: monitorKey
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
// Step 1: Set up event handlers
|
|
39
|
+
console.log('π§ Step 1: Setting up real-time event handlers...');
|
|
40
|
+
|
|
41
|
+
// Connection status handlers
|
|
42
|
+
client.gateway.on('connected', () => {
|
|
43
|
+
console.log('β
Connected to real-time event stream');
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
client.gateway.on('disconnected', () => {
|
|
47
|
+
console.log('β Disconnected from event stream');
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
client.gateway.on('error', (error) => {
|
|
51
|
+
console.error('π¨ WebSocket error:', error.message);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// General event handler
|
|
55
|
+
client.gateway.on('event', (event) => {
|
|
56
|
+
console.log(`π¨ Received event: ${event.type} from ${event.source}`);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
// Specific event type handlers
|
|
60
|
+
client.gateway.on('identity.login', (event) => {
|
|
61
|
+
console.log(`π€ Login Event: ${event.data.actor} from ${event.data.details?.ipAddress}`);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
client.gateway.on('identity.mfa_failure', (event) => {
|
|
65
|
+
console.log(`π¨ MFA Failure: ${event.data.actor} - ${event.data.details?.reason}`);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
client.gateway.on('permission.access_denied', (event) => {
|
|
69
|
+
console.log(`π« Access Denied: ${event.data.actor} -> ${event.data.resource}`);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
client.gateway.on('audit.integrity_violation', (event) => {
|
|
73
|
+
console.log(`β οΈ Integrity Violation: ${event.data.details?.violation}`);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
client.gateway.on('system.alert', (event) => {
|
|
77
|
+
console.log(`π System Alert: ${event.data.severity} - ${event.data.message}`);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
console.log('π Event handlers configured');
|
|
81
|
+
console.log();
|
|
82
|
+
|
|
83
|
+
// Step 2: Connect to event stream with filters
|
|
84
|
+
console.log('π Step 2: Connecting to filtered event stream...');
|
|
85
|
+
|
|
86
|
+
await client.gateway.connectEventStream({
|
|
87
|
+
filters: {
|
|
88
|
+
eventTypes: [
|
|
89
|
+
'identity.login',
|
|
90
|
+
'identity.mfa_failure',
|
|
91
|
+
'permission.access_denied',
|
|
92
|
+
'audit.integrity_violation',
|
|
93
|
+
'system.alert'
|
|
94
|
+
],
|
|
95
|
+
severities: ['medium', 'high', 'critical']
|
|
96
|
+
},
|
|
97
|
+
autoReconnect: true
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
console.log(`π Connection status: ${client.gateway.connectionStatus}`);
|
|
101
|
+
console.log();
|
|
102
|
+
|
|
103
|
+
// Step 3: Simulate some activity and monitor events
|
|
104
|
+
console.log('π Step 3: Simulating activity to generate events...');
|
|
105
|
+
|
|
106
|
+
// Simulate login attempts (these would trigger real-time events)
|
|
107
|
+
await simulateActivity(client);
|
|
108
|
+
|
|
109
|
+
// Wait for events to be processed
|
|
110
|
+
await new Promise(resolve => setTimeout(resolve, 2000));
|
|
111
|
+
console.log();
|
|
112
|
+
|
|
113
|
+
// Step 4: Get security events from gateway
|
|
114
|
+
console.log('π‘οΈ Step 4: Retrieving security events...');
|
|
115
|
+
|
|
116
|
+
const securityEvents = await client.gateway.getSecurityEvents({
|
|
117
|
+
type: 'authentication_failure',
|
|
118
|
+
severity: 'high',
|
|
119
|
+
limit: 5
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
console.log(`π¨ Security Events (${securityEvents.data.total} total):`);
|
|
123
|
+
securityEvents.data.events.forEach((event, index) => {
|
|
124
|
+
console.log(` ${index + 1}. [${event.severity}] ${event.type} - ${event.source}`);
|
|
125
|
+
console.log(` Time: ${event.timestamp}`);
|
|
126
|
+
console.log(` Handled: ${event.handled ? 'β
' : 'β'}`);
|
|
127
|
+
});
|
|
128
|
+
console.log();
|
|
129
|
+
|
|
130
|
+
// Step 5: Send commands through WebSocket
|
|
131
|
+
console.log('π€ Step 5: Sending commands through WebSocket...');
|
|
132
|
+
|
|
133
|
+
try {
|
|
134
|
+
await client.gateway.sendCommand({
|
|
135
|
+
type: 'subscribe_alerts',
|
|
136
|
+
data: {
|
|
137
|
+
severities: ['critical'],
|
|
138
|
+
immediate: true
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
console.log('β
Subscribed to critical alerts');
|
|
143
|
+
|
|
144
|
+
await client.gateway.sendCommand({
|
|
145
|
+
type: 'ping',
|
|
146
|
+
data: { timestamp: new Date().toISOString() }
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
console.log('β
Ping command sent');
|
|
150
|
+
} catch (error) {
|
|
151
|
+
console.log('βΉοΈ Command sending not available (demo mode)');
|
|
152
|
+
}
|
|
153
|
+
console.log();
|
|
154
|
+
|
|
155
|
+
// Step 6: Monitor audit notifications
|
|
156
|
+
console.log('π Step 6: Checking audit notifications...');
|
|
157
|
+
|
|
158
|
+
const notifications = await client.audit.getNotifications({
|
|
159
|
+
severity: 'high',
|
|
160
|
+
acknowledged: false,
|
|
161
|
+
limit: 5
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
console.log(`π’ Audit Notifications (${notifications.data.total} unacknowledged):`);
|
|
165
|
+
notifications.data.notifications.forEach((notification, index) => {
|
|
166
|
+
console.log(` ${index + 1}. [${notification.severity}] ${notification.message}`);
|
|
167
|
+
console.log(` Event ID: ${notification.eventId}`);
|
|
168
|
+
console.log(` Created: ${notification.createdAt}`);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
// Acknowledge notifications
|
|
172
|
+
if (notifications.data.notifications.length > 0) {
|
|
173
|
+
const firstNotification = notifications.data.notifications[0];
|
|
174
|
+
await client.audit.acknowledgeNotification(firstNotification.id);
|
|
175
|
+
console.log(`β
Acknowledged notification: ${firstNotification.id}`);
|
|
176
|
+
}
|
|
177
|
+
console.log();
|
|
178
|
+
|
|
179
|
+
// Step 7: Get real-time gateway status
|
|
180
|
+
console.log('π Step 7: Monitoring gateway status...');
|
|
181
|
+
|
|
182
|
+
const gatewayStatus = await client.gateway.getStatus();
|
|
183
|
+
|
|
184
|
+
console.log(`π¦ Gateway Status: ${gatewayStatus.data.status}`);
|
|
185
|
+
console.log(`π Load: CPU ${gatewayStatus.data.load.cpu}%, Memory ${gatewayStatus.data.load.memory}%`);
|
|
186
|
+
console.log(`π Connections: ${gatewayStatus.data.load.connections}`);
|
|
187
|
+
console.log(`π Services:`);
|
|
188
|
+
|
|
189
|
+
Object.entries(gatewayStatus.data.services).forEach(([service, info]) => {
|
|
190
|
+
const statusIcon = info.status === 'up' ? 'β
' : info.status === 'degraded' ? 'β οΈ' : 'β';
|
|
191
|
+
console.log(` ${statusIcon} ${service}: ${info.responseTime}ms (${info.lastCheck})`);
|
|
192
|
+
});
|
|
193
|
+
console.log();
|
|
194
|
+
|
|
195
|
+
// Step 8: Monitor connection statistics
|
|
196
|
+
console.log('π‘ Step 8: Monitoring connection statistics...');
|
|
197
|
+
|
|
198
|
+
const connectionStats = await client.gateway.getConnectionStats();
|
|
199
|
+
|
|
200
|
+
console.log(`π Connection Statistics:`);
|
|
201
|
+
console.log(` Total: ${connectionStats.data.totalConnections}`);
|
|
202
|
+
console.log(` Active: ${connectionStats.data.activeConnections}`);
|
|
203
|
+
console.log(` HTTP: ${connectionStats.data.httpConnections}`);
|
|
204
|
+
console.log(` WebSocket: ${connectionStats.data.wsConnections}`);
|
|
205
|
+
console.log(` TLS: ${connectionStats.data.tlsConnections}`);
|
|
206
|
+
console.log(` By service:`, connectionStats.data.connectionsByService);
|
|
207
|
+
console.log();
|
|
208
|
+
|
|
209
|
+
// Step 9: Test rate limiting status
|
|
210
|
+
console.log('β±οΈ Step 9: Checking rate limiting status...');
|
|
211
|
+
|
|
212
|
+
const rateLimit = await client.gateway.getRateLimit();
|
|
213
|
+
|
|
214
|
+
console.log(`β° Rate Limit Status:`);
|
|
215
|
+
console.log(` Remaining: ${rateLimit.data.remaining}/${rateLimit.data.limit}`);
|
|
216
|
+
console.log(` Reset time: ${rateLimit.data.resetTime}`);
|
|
217
|
+
console.log(` Window start: ${rateLimit.data.windowStart}`);
|
|
218
|
+
console.log();
|
|
219
|
+
|
|
220
|
+
// Step 10: Set up continuous monitoring
|
|
221
|
+
console.log('π Step 10: Setting up continuous monitoring...');
|
|
222
|
+
|
|
223
|
+
let eventCount = 0;
|
|
224
|
+
const monitoringDuration = 5000; // 5 seconds
|
|
225
|
+
|
|
226
|
+
console.log(`β±οΈ Monitoring for ${monitoringDuration/1000} seconds...`);
|
|
227
|
+
|
|
228
|
+
// Count all events during monitoring period
|
|
229
|
+
const eventCounter = (event) => {
|
|
230
|
+
eventCount++;
|
|
231
|
+
console.log(`π Event #${eventCount}: ${event.type} (${new Date().toLocaleTimeString()})`);
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
client.gateway.on('event', eventCounter);
|
|
235
|
+
|
|
236
|
+
// Monitor for specified duration
|
|
237
|
+
await new Promise(resolve => {
|
|
238
|
+
setTimeout(() => {
|
|
239
|
+
client.gateway.off('event', eventCounter);
|
|
240
|
+
console.log(`β
Monitoring completed. Received ${eventCount} events.`);
|
|
241
|
+
resolve();
|
|
242
|
+
}, monitoringDuration);
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
} catch (error) {
|
|
246
|
+
console.error('β Real-time monitoring example failed:', error.message);
|
|
247
|
+
if (error.response) {
|
|
248
|
+
console.error('Response:', error.response.data);
|
|
249
|
+
}
|
|
250
|
+
} finally {
|
|
251
|
+
// Clean up WebSocket connection
|
|
252
|
+
client.gateway.disconnectEventStream();
|
|
253
|
+
client.cleanup();
|
|
254
|
+
console.log('\n⨠Real-time monitoring example completed!');
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// Helper function to simulate activity
|
|
259
|
+
async function simulateActivity(client) {
|
|
260
|
+
console.log('π¬ Simulating user activities...');
|
|
261
|
+
|
|
262
|
+
try {
|
|
263
|
+
// Simulate some audit events that might trigger real-time notifications
|
|
264
|
+
await client.audit.logEvent({
|
|
265
|
+
source: 'demo-simulator',
|
|
266
|
+
action: 'user_login_attempt',
|
|
267
|
+
resource: 'auth:system',
|
|
268
|
+
actor: 'did:atp:testnet:demo-user',
|
|
269
|
+
details: {
|
|
270
|
+
ipAddress: '203.0.113.10',
|
|
271
|
+
userAgent: 'Demo Browser',
|
|
272
|
+
success: true,
|
|
273
|
+
mfaRequired: true
|
|
274
|
+
}
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
await client.audit.logEvent({
|
|
278
|
+
source: 'demo-simulator',
|
|
279
|
+
action: 'suspicious_activity_detected',
|
|
280
|
+
resource: 'security:monitor',
|
|
281
|
+
actor: 'did:atp:testnet:suspicious-user',
|
|
282
|
+
details: {
|
|
283
|
+
activityType: 'multiple_failed_logins',
|
|
284
|
+
attempts: 5,
|
|
285
|
+
timeWindow: '60 seconds',
|
|
286
|
+
blocked: true
|
|
287
|
+
}
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
console.log('π Demo events logged');
|
|
291
|
+
|
|
292
|
+
} catch (error) {
|
|
293
|
+
console.log('βΉοΈ Activity simulation not available (demo mode)');
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// Run the example
|
|
298
|
+
if (import.meta.url === `file://${process.argv[1]}`) {
|
|
299
|
+
realTimeMonitoringExample().catch(console.error);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
export { realTimeMonitoringExample };
|