-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathpersistent-events.ts
More file actions
58 lines (48 loc) · 1.78 KB
/
persistent-events.ts
File metadata and controls
58 lines (48 loc) · 1.78 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
/**
* Persistent Event Listener Example
*
* Demonstrates the persistent listener that automatically reconnects
* and refreshes tokens when they expire.
* Requires: npm install @microsoft/signalr
*/
import { PasswordAuth, NodeHttpClient, MemoryStorage } from '@oneidentity/safeguard';
import { PersistentSafeguardEventListener } from '@oneidentity/safeguard/events';
import * as signalR from '@microsoft/signalr';
const host = 'safeguard.sample.corp';
const username = 'MyUser';
const password = 'MyPassword';
const provider = 'Local';
async function main() {
const auth = new PasswordAuth({ username, password, provider });
const httpClient = new NodeHttpClient();
// To disable TLS verification for self-signed certs (dev only):
// const httpClient = new NodeHttpClient({ rejectUnauthorized: false });
const storage = new MemoryStorage();
// Authenticate to get initial token
const tokenSet = await auth.authenticate(host, httpClient, storage);
// Build SignalR connection
const connection = new signalR.HubConnectionBuilder()
.withUrl(`https://${host}/service/event/signalr`, {
accessTokenFactory: () => tokenSet.accessToken.expose(),
})
.withAutomaticReconnect()
.build();
// Wrap in persistent listener (handles token refresh on reconnect)
const listener = new PersistentSafeguardEventListener(
connection, auth, host, httpClient, storage,
);
listener.onStateChange((state) => {
console.log('State:', state);
});
listener.on('NotifyEventAsync', (event) => {
console.log('Event:', event);
});
await listener.start();
console.log('Persistent listener running... (Ctrl+C to stop)');
process.on('SIGINT', async () => {
await listener.stop();
httpClient.dispose?.();
process.exit(0);
});
}
main().catch(console.error);