5
0
Fork 0

backend-auth integration

master
UnicodingUnicorn 2019-02-24 04:02:47 +08:00
parent dfb346dbca
commit a5540dff22
2 changed files with 43 additions and 26 deletions

View File

@ -2,6 +2,8 @@
Beep backend handling WebRTC signaling. Beep backend handling WebRTC signaling.
**To run this service securely means to run it behind traefik forwarding auth to `backend-auth`**
## Quickstart ## Quickstart
Docker: Docker:
@ -32,7 +34,7 @@ Unless otherwise noted, bodies and responses are with ```Content-Type: applicati
### Subscribe to SSE ### Subscribe to SSE
``` ```
GET /subscribe/:user/device/:device GET /subscribe
``` ```
Subscribe a user's device to the signaling service. Recommended usage: Subscribe a user's device to the signaling service. Recommended usage:
@ -45,17 +47,22 @@ es.onmessage = (e) => {
}; };
``` ```
#### URL Params #### Required headers
| Name | Type | Description | Required | | Name | Description |
| ---- | ---- | ----------- | -------- | | ---- | ----------- |
| user | String | User's ID. | ✓ | | X-User-Claim | Stringified user claim, populated by `backend-auth` called by `traefik` |
| device | String | Device's ID. Must be unique to the device. I suggest something based on MAC address. | ✓ |
#### Success Response (200 OK) #### Success Response (200 OK)
An [EventSource](https://developer.mozilla.org/en-US/docs/Web/API/EventSource) stream. An [EventSource](https://developer.mozilla.org/en-US/docs/Web/API/EventSource) stream.
#### Errors
| Code | Description |
| ---- | ----------- |
| 401| Invalid user claims header. |
--- ---
### Get a user's devices ### Get a user's devices

View File

@ -14,31 +14,41 @@ app.use((req, res, next) => {
let connections = {}; let connections = {};
app.get('/subscribe/:user/device/:device', (req, res) => { app.get('/subscribe', (req, res) => {
if (!connections[req.params.user]) { if (!req.get("X-User-Claim")) {
connections[req.params.user] = {}; res.sendStatus(401);
return;
} }
console.log(`Someone subscribed to ${req.params.user}, ${req.params.device}`); try {
const user_claim = JSON.parse(req.get("X-User-Claim"));
if (!connections[user_claim.userid]) {
connections[user_claim.userid] = {};
}
connections[req.params.user][req.params.device] = res; console.log(`Someone subscribed to ${user_claim.userid}, ${user_claim.clientid}`);
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache'
});
// So the connection doesn't timeout and die connections[user_claim.userid][user_claim.clientid] = res;
let timeoutID = 0; res.writeHead(200, {
const refresh = () => { 'Content-Type': 'text/event-stream',
res.write(':\n\n'); 'Cache-Control': 'no-cache'
timeoutID = setTimeout(refresh, 25000); });
};
refresh();
res.on('close', () => { // So the connection doesn't timeout and die
connections[req.params.user][req.params.device] = null; let timeoutID = 0;
clearTimeout(timeoutID); const refresh = () => {
}); res.write(':\n\n');
timeoutID = setTimeout(refresh, 25000);
};
refresh();
res.on('close', () => {
connections[user_claim.userid][user_claim.clientid] = null;
clearTimeout(timeoutID);
});
} catch(e) {
res.sendStatus(401)
}
}); });
app.get('/user/:user/devices', (req, res) => { app.get('/user/:user/devices', (req, res) => {