import express from 'express' ; import { LogInstance } from 'log/loginstance' ; import { User } from 'entities/user' ; import { SessionCounter } from 'sessioncounter' ; /**
 * handles requests to /users
 */ export class UsersRoute { public static InstallRoutes ( app ) { app . route ( '/users' ) . get ( async ( req , res ) => await UsersRoute . onGetAllUsers ( req , res ) ) . post ( async ( req , res ) => await UsersRoute . onPostUsers ( req , res ) ) ; app . route ( '/users/:userId' ) . get ( async ( req , res ) => await UsersRoute . onGetUsersById ( req , res ) ) . put ( async ( req , res ) => await UsersRoute . onPutUsersById ( req , res ) ) . delete ( async ( req , res ) => await UsersRoute . onDeleteUserById ( req , res ) ) ; app . route ( '/users/auth/login' ) . post ( async ( req , res ) => await UsersRoute . onPostLogin ( req , res ) ) ; app . route ( '/users/auth/logout' ) . post ( ( req , res ) => UsersRoute . onPostLogout ( req , res ) ) ; app . route ( '/users/auth/validate' ) . post ( async ( req , res ) => await UsersRoute . onPostValidate ( req , res ) ) ; app . route ( '/users/auth/validate_security' ) . post ( async ( req , res ) => await UsersRoute . onPostValidateSecurity ( req , res ) ) ; app . route ( '/users/byname/:username' ) . get ( async ( req , res ) => await UsersRoute . onGetUsersByName ( req , res ) ) ; } param req param res private static async onGetAllUsers ( req , res ) { type getUsersQuery = { offset : string ; length : string ; } ; const query = req . query as getUsersQuery ; const colOffset = Number ( query . offset ) ; const colLength = Number ( query . length ) ; if ( isNaN ( colOffset ) === true || isNaN ( colLength ) === true ) { return res . status ( 400 ) . end ( ) ; } const MAX_COLUMN_LENGTH = 100 ; if ( colLength > MAX_COLUMN_LENGTH ) { return res . status ( 413 ) . end ( ) ; } try { const users = await User . getAll ( colOffset , colLength ) ; return res . status ( 200 ) . json ( users ) . end ( ) ; } catch ( error ) { LogInstance . error ( error ) ; return res . status ( 500 ) . end ( ) ; } } param req param res private static async onPostUsers ( req , res ) { type postUsersBody = { username : string ; playername : string ; password : string ; security_question : number ; security_answer : string ; } ; const body = req . body as postUsersBody ; const userName = body . username ; const playerName = body . playername ; const password = body . password ; const securityQuestion = body . security_question ; const securityAnswer = body . security_answer ; if ( userName == null || playerName == null || password == null || isNaN ( securityQuestion ) === true || securityAnswer == null ) { return res . status ( 400 ) . end ( ) ; } try { const userExists = await User . isTaken ( userName , playerName ) ; if ( userExists === true ) { LogInstance . warn ( 'Tried to create an existing user ' + userName + ' (' + playerName + ')' ) ; return res . status ( 409 ) . end ( ) ; } const newUser = await User . create ( userName , playerName , password , securityQuestion , securityAnswer ) ; return res . status ( 201 ) . json ( newUser ) . end ( ) ; } catch ( error ) { LogInstance . error ( error ) ; return res . status ( 500 ) . end ( ) ; } } param req param res private static async onGetUsersById ( req , res ) { const reqUserId = Number ( req . params . userId ) ; if ( isNaN ( reqUserId ) ) { return res . status ( 400 ) . end ( ) ; } try { const user = await User . getById ( reqUserId ) ; if ( user == null ) { return res . status ( 404 ) . end ( ) ; } return res . status ( 200 ) . json ( user ) . end ( ) ; } catch ( error ) { LogInstance . error ( error ) ; return res . status ( 500 ) . end ( ) ; } } param req param res private static async onPutUsersById ( req , res ) { const reqUserId = Number ( req . params . userId ) ; if ( isNaN ( reqUserId ) || req . body == null ) { return res . status ( 400 ) . end ( ) ; } try { const wasUpdated = await User . set ( reqUserId , req . body ) ; if ( wasUpdated ) { return res . status ( 200 ) . end ( ) ; } else { return res . status ( 404 ) . end ( ) ; } } catch ( error ) { LogInstance . error ( error ) ; return res . status ( 500 ) . end ( ) ; } } param req param res private static async onDeleteUserById ( req , res ) { const reqUserId = Number ( req . params . userId ) ; if ( isNaN ( reqUserId ) ) { return res . status ( 400 ) . end ( ) ; } try { const deleted = await User . removeById ( reqUserId ) ; if ( deleted === true ) { return res . status ( 200 ) . end ( ) ; } else { return res . status ( 404 ) . end ( ) ; } } catch ( error ) { LogInstance . error ( error ) ; return res . status ( 500 ) . end ( ) ; } } param req param res private static async onPostLogin ( req , res ) { type postUsersBody = { username : string ; password : string ; } ; const body = req . body as postUsersBody ; const userName = body . username ; const password = body . password ; if ( userName == null || password == null ) { return res . status ( 400 ) . end ( ) ; } try { const loggedUserId = await User . validateCredentials ( userName , password ) ; if ( loggedUserId != null ) { SessionCounter . Increment ( ) ; return res . status ( 200 ) . json ( { userId : loggedUserId } ) . end ( ) ; } else { return res . status ( 401 ) . end ( ) ; } } catch ( error ) { LogInstance . error ( error ) ; return res . status ( 500 ) . end ( ) ; } } param req param res private static onPostLogout ( req , res ) { type postLogoutBody = { userId : string ; } ; const body = req . body as postLogoutBody ; const userId = body . userId ; if ( userId == null ) { return res . status ( 400 ) . end ( ) ; } try { SessionCounter . Decrement ( ) ; return res . status ( 200 ) . json ( ) . end ( ) ; } catch ( error ) { LogInstance . error ( error ) ; return res . status ( 500 ) . end ( ) ; } } param req param res private static async onPostValidate ( req , res ) { type validateBody = { username : string ; password : string ; } ; const body = req . body as validateBody ; const username = body . username ; const password = body . password ; if ( username == null || password == null ) { return res . status ( 400 ) . end ( ) ; } try { const foundUserId = await User . validateCredentials ( username , password ) ; if ( foundUserId == null ) { return res . status ( 401 ) . json ( ) . end ( ) ; } return res . status ( 200 ) . json ( { userId : foundUserId } ) . end ( ) ; } catch ( error ) { LogInstance . error ( error ) ; return res . status ( 500 ) . end ( ) ; } } param req param res private static async onPostValidateSecurity ( req , res ) { type validateBody = { username : string ; security_answer : string ; } ; const body = req . body as validateBody ; const username = body . username ; const securityAnswer = body . security_answer ; if ( username == null || securityAnswer == null ) { return res . status ( 400 ) . end ( ) ; } try { const foundUserId = await User . validateSecurityAnswer ( username , securityAnswer ) ; if ( foundUserId == null ) { return res . status ( 401 ) . json ( ) . end ( ) ; } return res . status ( 200 ) . json ( { userId : foundUserId } ) . end ( ) ; } catch ( error ) { LogInstance . error ( error ) ; return res . status ( 500 ) . end ( ) ; } } param req param res private static async onGetUsersByName ( req , res ) { const reqUsername = req . params . username ; if ( reqUsername == null ) { return res . status ( 400 ) . end ( ) ; } try { const user = await User . getByName ( reqUsername ) ; if ( user == null ) { return res . status ( 404 ) . end ( ) ; } return res . status ( 200 ) . json ( user ) . end ( ) ; } catch ( error ) { LogInstance . error ( error ) ; return res . status ( 500 ) . end ( ) ; } } }