mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
prettier 4 tabwidth
This commit is contained in:
parent
00ec614f1d
commit
96b413a365
42 changed files with 7034 additions and 6905 deletions
|
@ -27,148 +27,148 @@ import utils from '../utils/utils'
|
|||
import dbtools from '../utils/dbtools'
|
||||
|
||||
interface Options {
|
||||
userDB: Database
|
||||
jsonResponse: boolean
|
||||
exceptions: Array<string>
|
||||
userDB: Database
|
||||
jsonResponse: boolean
|
||||
exceptions: Array<string>
|
||||
}
|
||||
|
||||
export const testUser = {
|
||||
id: 19,
|
||||
avaiblePWRequests: 645,
|
||||
pwRequestCount: 19,
|
||||
created: new Date(),
|
||||
pw: 'secret',
|
||||
loginCount: 3,
|
||||
createdBy: 1,
|
||||
id: 19,
|
||||
avaiblePWRequests: 645,
|
||||
pwRequestCount: 19,
|
||||
created: new Date(),
|
||||
pw: 'secret',
|
||||
loginCount: 3,
|
||||
createdBy: 1,
|
||||
}
|
||||
|
||||
function renderLogin(_req: Request, res: Response, jsonResponse: boolean) {
|
||||
res.status(401) // Unauthorized
|
||||
if (jsonResponse) {
|
||||
res.json({
|
||||
result: 'nouser',
|
||||
msg: 'You are not logged in',
|
||||
})
|
||||
} else {
|
||||
res.render('login', {
|
||||
devel: process.env.NS_DEVEL,
|
||||
})
|
||||
}
|
||||
res.status(401) // Unauthorized
|
||||
if (jsonResponse) {
|
||||
res.json({
|
||||
result: 'nouser',
|
||||
msg: 'You are not logged in',
|
||||
})
|
||||
} else {
|
||||
res.render('login', {
|
||||
devel: process.env.NS_DEVEL,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export default function (options: Options): RequestHandler {
|
||||
const {
|
||||
userDB,
|
||||
jsonResponse,
|
||||
exceptions,
|
||||
}: {
|
||||
userDB: Database
|
||||
jsonResponse: boolean
|
||||
exceptions: string[]
|
||||
} = options
|
||||
const {
|
||||
userDB,
|
||||
jsonResponse,
|
||||
exceptions,
|
||||
}: {
|
||||
userDB: Database
|
||||
jsonResponse: boolean
|
||||
exceptions: string[]
|
||||
} = options
|
||||
|
||||
return function (req: Request, res: Response, next: NextFunction) {
|
||||
const sessionID = req.cookies.sessionID
|
||||
const isException = exceptions.some((exc) => {
|
||||
return req.url.split('?')[0] === exc
|
||||
})
|
||||
return function (req: Request, res: Response, next: NextFunction) {
|
||||
const sessionID = req.cookies.sessionID
|
||||
const isException = exceptions.some((exc) => {
|
||||
return req.url.split('?')[0] === exc
|
||||
})
|
||||
|
||||
if (process.env.NS_NOUSER) {
|
||||
req.session = {
|
||||
user: testUser,
|
||||
sessionID: sessionID || 11111111111,
|
||||
isException: false,
|
||||
}
|
||||
next()
|
||||
return
|
||||
}
|
||||
if (process.env.NS_NOUSER) {
|
||||
req.session = {
|
||||
user: testUser,
|
||||
sessionID: sessionID || 11111111111,
|
||||
isException: false,
|
||||
}
|
||||
next()
|
||||
return
|
||||
}
|
||||
|
||||
// FIXME Allowing all urls with _next in it, but not in params
|
||||
if (
|
||||
req.url.split('?')[0].includes('_next') ||
|
||||
req.url.split('?')[0].includes('well-known/acme-challenge')
|
||||
) {
|
||||
req.session = { isException: true }
|
||||
next()
|
||||
return
|
||||
}
|
||||
// FIXME Allowing all urls with _next in it, but not in params
|
||||
if (
|
||||
req.url.split('?')[0].includes('_next') ||
|
||||
req.url.split('?')[0].includes('well-known/acme-challenge')
|
||||
) {
|
||||
req.session = { isException: true }
|
||||
next()
|
||||
return
|
||||
}
|
||||
|
||||
if (!sessionID) {
|
||||
if (isException) {
|
||||
logger.DebugLog(`EXCEPTION: ${req.url}`, 'auth', 1)
|
||||
req.session = { isException: true }
|
||||
next()
|
||||
return
|
||||
}
|
||||
logger.DebugLog(`No session ID: ${req.url}`, 'auth', 1)
|
||||
renderLogin(req, res, jsonResponse)
|
||||
return
|
||||
}
|
||||
|
||||
const user = GetUserBySessionID(userDB, sessionID)
|
||||
|
||||
if (!user) {
|
||||
if (isException) {
|
||||
logger.DebugLog(`EXCEPTION: ${req.url}`, 'auth', 1)
|
||||
req.session = { isException: true }
|
||||
next()
|
||||
return
|
||||
}
|
||||
logger.DebugLog(`No user:${req.url}`, 'auth', 1)
|
||||
renderLogin(req, res, jsonResponse)
|
||||
return
|
||||
}
|
||||
|
||||
req.session = {
|
||||
user: user,
|
||||
sessionID: sessionID,
|
||||
isException: isException,
|
||||
}
|
||||
|
||||
logger.DebugLog(`ID #${user.id}: ${req.url}`, 'auth', 1)
|
||||
|
||||
dbtools.Update(
|
||||
userDB,
|
||||
'sessions',
|
||||
{
|
||||
lastAccess: utils.GetDateString(),
|
||||
},
|
||||
{
|
||||
id: sessionID,
|
||||
}
|
||||
)
|
||||
|
||||
dbtools.Update(
|
||||
userDB,
|
||||
'users',
|
||||
{
|
||||
lastAccess: utils.GetDateString(),
|
||||
},
|
||||
{
|
||||
id: user.id,
|
||||
}
|
||||
)
|
||||
|
||||
if (!sessionID) {
|
||||
if (isException) {
|
||||
logger.DebugLog(`EXCEPTION: ${req.url}`, 'auth', 1)
|
||||
req.session = { isException: true }
|
||||
next()
|
||||
return
|
||||
}
|
||||
logger.DebugLog(`No session ID: ${req.url}`, 'auth', 1)
|
||||
renderLogin(req, res, jsonResponse)
|
||||
return
|
||||
}
|
||||
|
||||
const user = GetUserBySessionID(userDB, sessionID)
|
||||
|
||||
if (!user) {
|
||||
if (isException) {
|
||||
logger.DebugLog(`EXCEPTION: ${req.url}`, 'auth', 1)
|
||||
req.session = { isException: true }
|
||||
next()
|
||||
return
|
||||
}
|
||||
logger.DebugLog(`No user:${req.url}`, 'auth', 1)
|
||||
renderLogin(req, res, jsonResponse)
|
||||
return
|
||||
}
|
||||
|
||||
req.session = {
|
||||
user: user,
|
||||
sessionID: sessionID,
|
||||
isException: isException,
|
||||
}
|
||||
|
||||
logger.DebugLog(`ID #${user.id}: ${req.url}`, 'auth', 1)
|
||||
|
||||
dbtools.Update(
|
||||
userDB,
|
||||
'sessions',
|
||||
{
|
||||
lastAccess: utils.GetDateString(),
|
||||
},
|
||||
{
|
||||
id: sessionID,
|
||||
}
|
||||
)
|
||||
|
||||
dbtools.Update(
|
||||
userDB,
|
||||
'users',
|
||||
{
|
||||
lastAccess: utils.GetDateString(),
|
||||
},
|
||||
{
|
||||
id: user.id,
|
||||
}
|
||||
)
|
||||
|
||||
next()
|
||||
}
|
||||
}
|
||||
|
||||
function GetUserBySessionID(db: Database, sessionID: string) {
|
||||
logger.DebugLog(`Getting user from db`, 'auth', 2)
|
||||
logger.DebugLog(`Getting user from db`, 'auth', 2)
|
||||
|
||||
const session = dbtools.Select(db, 'sessions', {
|
||||
id: sessionID,
|
||||
})[0]
|
||||
const session = dbtools.Select(db, 'sessions', {
|
||||
id: sessionID,
|
||||
})[0]
|
||||
|
||||
if (!session) {
|
||||
return
|
||||
}
|
||||
if (!session) {
|
||||
return
|
||||
}
|
||||
|
||||
const user = dbtools.Select(db, 'users', {
|
||||
id: session.userID,
|
||||
})[0]
|
||||
const user = dbtools.Select(db, 'users', {
|
||||
id: session.userID,
|
||||
})[0]
|
||||
|
||||
if (user) {
|
||||
return user
|
||||
}
|
||||
if (user) {
|
||||
return user
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,68 +23,70 @@ import type { Response, NextFunction } from 'express'
|
|||
import type { Request } from '../types/basicTypes'
|
||||
|
||||
interface Options {
|
||||
loggableKeywords: Array<string>
|
||||
loggableModules: Array<string>
|
||||
exceptions: Array<string>
|
||||
excludeFromStats: Array<string>
|
||||
loggableKeywords: Array<string>
|
||||
loggableModules: Array<string>
|
||||
exceptions: Array<string>
|
||||
excludeFromStats: Array<string>
|
||||
}
|
||||
|
||||
export default function (options: Options): any {
|
||||
const loggableKeywords = options ? options.loggableKeywords : undefined
|
||||
const loggableModules = options ? options.loggableModules : undefined
|
||||
const exceptions = options.exceptions || []
|
||||
const excludeFromStats = options.excludeFromStats || []
|
||||
const loggableKeywords = options ? options.loggableKeywords : undefined
|
||||
const loggableModules = options ? options.loggableModules : undefined
|
||||
const exceptions = options.exceptions || []
|
||||
const excludeFromStats = options.excludeFromStats || []
|
||||
|
||||
return function (req: Request, res: Response, next: NextFunction) {
|
||||
res.on('finish', function () {
|
||||
// TODO: test this
|
||||
const isException = exceptions.some((ex) => {
|
||||
return req.url.includes(ex)
|
||||
})
|
||||
return function (req: Request, res: Response, next: NextFunction) {
|
||||
res.on('finish', function () {
|
||||
// TODO: test this
|
||||
const isException = exceptions.some((ex) => {
|
||||
return req.url.includes(ex)
|
||||
})
|
||||
|
||||
if (isException) {
|
||||
return
|
||||
}
|
||||
if (isException) {
|
||||
return
|
||||
}
|
||||
|
||||
let hostname = 'NOHOST'
|
||||
if (req.hostname) {
|
||||
hostname = req.hostname.replace('www.', '').split('.')[0]
|
||||
} else {
|
||||
logger.Log('Hostname is undefined!', logger.GetColor('redbg'))
|
||||
console.log(req.body)
|
||||
console.log(req.query)
|
||||
console.log(req.headers)
|
||||
}
|
||||
let hostname = 'NOHOST'
|
||||
if (req.hostname) {
|
||||
hostname = req.hostname.replace('www.', '').split('.')[0]
|
||||
} else {
|
||||
logger.Log('Hostname is undefined!', logger.GetColor('redbg'))
|
||||
console.log(req.body)
|
||||
console.log(req.query)
|
||||
console.log(req.headers)
|
||||
}
|
||||
|
||||
const hasLoggableKeyword =
|
||||
loggableKeywords &&
|
||||
loggableKeywords.some((keyword) => {
|
||||
return req.url.includes(keyword)
|
||||
const hasLoggableKeyword =
|
||||
loggableKeywords &&
|
||||
loggableKeywords.some((keyword) => {
|
||||
return req.url.includes(keyword)
|
||||
})
|
||||
const hasLoggableModule =
|
||||
loggableModules &&
|
||||
loggableModules.some((keyword) => {
|
||||
return hostname.includes(keyword)
|
||||
})
|
||||
const toLog = hasLoggableModule || hasLoggableKeyword
|
||||
|
||||
logger.LogReq(req, true, res.statusCode)
|
||||
if (toLog) {
|
||||
logger.LogReq(req)
|
||||
}
|
||||
|
||||
const shouldLogStat = !excludeFromStats.some((ex) => {
|
||||
return req.url.includes(ex)
|
||||
})
|
||||
|
||||
if (res.statusCode !== 404 && shouldLogStat) {
|
||||
logger.LogStat(
|
||||
req.url,
|
||||
hostname,
|
||||
req.session && req.session.user
|
||||
? req.session.user.id
|
||||
: 'NOUSER'
|
||||
)
|
||||
}
|
||||
})
|
||||
const hasLoggableModule =
|
||||
loggableModules &&
|
||||
loggableModules.some((keyword) => {
|
||||
return hostname.includes(keyword)
|
||||
})
|
||||
const toLog = hasLoggableModule || hasLoggableKeyword
|
||||
|
||||
logger.LogReq(req, true, res.statusCode)
|
||||
if (toLog) {
|
||||
logger.LogReq(req)
|
||||
}
|
||||
|
||||
const shouldLogStat = !excludeFromStats.some((ex) => {
|
||||
return req.url.includes(ex)
|
||||
})
|
||||
|
||||
if (res.statusCode !== 404 && shouldLogStat) {
|
||||
logger.LogStat(
|
||||
req.url,
|
||||
hostname,
|
||||
req.session && req.session.user ? req.session.user.id : 'NOUSER'
|
||||
)
|
||||
}
|
||||
})
|
||||
next()
|
||||
}
|
||||
next()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,60 +27,60 @@ import { Socket } from '../types/basicTypes'
|
|||
import { testUser } from './auth.middleware'
|
||||
|
||||
interface Options {
|
||||
userDB: any
|
||||
userDB: any
|
||||
}
|
||||
|
||||
export default function SocketAuth(options: Options): any {
|
||||
const { userDB } = options
|
||||
const { userDB } = options
|
||||
|
||||
return (socket: Socket, next: (arg0?: any) => void) => {
|
||||
try {
|
||||
const cookies = cookie.parse(socket.handshake.headers.cookie || '')
|
||||
const sessionID = cookies.sessionID
|
||||
return (socket: Socket, next: (arg0?: any) => void) => {
|
||||
try {
|
||||
const cookies = cookie.parse(socket.handshake.headers.cookie || '')
|
||||
const sessionID = cookies.sessionID
|
||||
|
||||
if (process.env.NS_NOUSER) {
|
||||
socket.user = testUser
|
||||
next()
|
||||
return
|
||||
}
|
||||
if (process.env.NS_NOUSER) {
|
||||
socket.user = testUser
|
||||
next()
|
||||
return
|
||||
}
|
||||
|
||||
if (!sessionID) {
|
||||
next(new Error('Not authenticated, please log in'))
|
||||
return
|
||||
}
|
||||
if (!sessionID) {
|
||||
next(new Error('Not authenticated, please log in'))
|
||||
return
|
||||
}
|
||||
|
||||
const user = GetUserBySessionID(userDB, sessionID)
|
||||
const user = GetUserBySessionID(userDB, sessionID)
|
||||
|
||||
if (!user) {
|
||||
next(new Error('Not authenticated, please log in'))
|
||||
return
|
||||
}
|
||||
socket.user = user
|
||||
next()
|
||||
} catch (e) {
|
||||
next(new Error('Authentication server error'))
|
||||
console.error('Authentication server error')
|
||||
console.error(e)
|
||||
if (!user) {
|
||||
next(new Error('Not authenticated, please log in'))
|
||||
return
|
||||
}
|
||||
socket.user = user
|
||||
next()
|
||||
} catch (e) {
|
||||
next(new Error('Authentication server error'))
|
||||
console.error('Authentication server error')
|
||||
console.error(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function GetUserBySessionID(db: any, sessionID: string) {
|
||||
logger.DebugLog(`Getting user from db`, 'auth', 2)
|
||||
logger.DebugLog(`Getting user from db`, 'auth', 2)
|
||||
|
||||
const session = dbtools.Select(db, 'sessions', {
|
||||
id: sessionID,
|
||||
})[0]
|
||||
const session = dbtools.Select(db, 'sessions', {
|
||||
id: sessionID,
|
||||
})[0]
|
||||
|
||||
if (!session) {
|
||||
return
|
||||
}
|
||||
if (!session) {
|
||||
return
|
||||
}
|
||||
|
||||
const user = dbtools.Select(db, 'users', {
|
||||
id: session.userID,
|
||||
})[0]
|
||||
const user = dbtools.Select(db, 'users', {
|
||||
id: session.userID,
|
||||
})[0]
|
||||
|
||||
if (user) {
|
||||
return user
|
||||
}
|
||||
if (user) {
|
||||
return user
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue