This commit is contained in:
mrfry 2021-05-29 21:44:48 +02:00
parent b0fad8d4a2
commit aea8ad22d1
6 changed files with 115 additions and 69 deletions

View file

@ -8,7 +8,7 @@ interface Options {
exceptions: Array<string>
}
const testUser = {
export const testUser = {
id: 19,
avaiblePWRequests: 645,
pwRequestCount: 19,

View file

@ -2,34 +2,44 @@ import logger from '../utils/logger'
import dbtools from '../utils/dbtools'
import cookie from 'cookie'
import { testUser } from './auth.middleware'
interface Options {
userDB: any
}
export default function (options: Options): any {
export default function SocketAuth(options: Options): any {
const { userDB } = options
return function (socket, next) {
const cookies = cookie.parse(socket.handshake.headers.cookie)
const sessionID = cookies.sessionID
return (socket, next) => {
try {
const cookies = cookie.parse(socket.handshake.headers.cookie || '')
const sessionID = cookies.sessionID
if (process.env.NS_NOUSER) {
if (process.env.NS_NOUSER) {
socket.user = testUser
next()
return
}
if (!sessionID) {
next(new Error('Not authenticated, please log in'))
return
}
const user = GetUserBySessionID(userDB, sessionID)
if (!user) {
next(new Error('Not authenticated, please log in'))
return
}
socket.user = user
next()
return
} catch (e) {
next(new Error('Authentication server error'))
console.error('Authentication server error')
console.error(e)
}
if (!sessionID) {
next(new Error('Authentication error'))
return
}
const user = GetUserBySessionID(userDB, sessionID)
if (!user) {
next(new Error('Authentication error'))
return
}
next()
}
}