freechat/rtc-server/auth.js
shockrah 715f334619 - Removing peers module for simplicity and reduction of data complexity
Basically the peer map system that we had before isn't going to work simply because
succesful client connections already have the data we need inside them.
Adding a whole wrapper around this just complicates things in a way that doesn't
really give any benefit.

For now every message is echo'd to all connections but that is easy enough to change
2021-04-01 17:21:57 -07:00

53 lines
1.4 KiB
JavaScript

const jsonwebtoken = require('jsonwebtoken')
const fs = require('fs')
const SERVER_HMAC = fs.readFileSync('wss-hmac.secret')
const USER_HMAC = fs.readFileSync('hmac.secret')
exports.verify = function(token) {
/**
* @param {String} token
* @returns 'user' on user connection
* @returns 'server' on server connection
* @retusn false on failure
*/
console.log('given token: ', token)
const vconfig = {
clockTolerance: 3,
}
try {
const decoded = jsonwebtoken.verify(token, USER_HMAC, vconfig);
return 'user'
} catch (err) {
try {
const decoded = jsonwebtoken.verify(token, SERVER_HMAC, {ignoreNotBefore: true})
return 'server'
} catch (err) {
return false
}
}
}
exports.prepare_auth = function(req) {
// NOTE: Why? because setting headers from the server is completely undocumented and I've ran
// through basically every library under the sun I literally con't be fucked to
// read people's code for a feature that could have a fucking tweet as documentation
// Typical User connections are setup with authentication in the headers
// Requested channel is the path
let header_auth = req.headers['authentication'] || req.headers['jwt']
if(!header_auth) {
let path = req.url
let uri = '/jwt/'
if(req.url.startsWith(uri)) {
let jwt = req.url.slice(uri.length)
return [jwt,null]
}
}
else {
return [header_auth, req.url]
}
}