2013-08-20 09:43:18 +08:00
|
|
|
fs = require("fs")
|
|
|
|
sanitizer = require("sanitizer")
|
|
|
|
util = require("util")
|
|
|
|
|
|
|
|
config = require("../config")
|
2013-08-30 10:14:55 +08:00
|
|
|
RedisKeys = require("../lib/redis_keys")
|
2013-08-20 09:43:18 +08:00
|
|
|
|
2013-09-16 07:10:19 +08:00
|
|
|
moduleDeps = ["App", "RedisAction", "RedisStore"]
|
|
|
|
|
2013-08-20 10:51:26 +08:00
|
|
|
# The main router that registers the routes that can be accessed by the client.
|
2013-08-20 09:43:18 +08:00
|
|
|
module.exports = class MainRouter
|
2013-09-16 07:10:19 +08:00
|
|
|
|
|
|
|
constructor: () ->
|
|
|
|
config.modules.wait moduleDeps, =>
|
|
|
|
@app = config.modules.get("App")
|
|
|
|
@redisAction = config.modules.get("RedisAction")
|
|
|
|
@redisStore = config.modules.get("RedisStore")
|
|
|
|
@_registerRoutes()
|
2013-08-20 09:43:18 +08:00
|
|
|
|
|
|
|
_registerRoutes: () ->
|
|
|
|
@app.get "/", @_index
|
|
|
|
@app.get "/auth", @_getAuth
|
|
|
|
@app.post "/auth", @_postAuth
|
2013-09-16 07:10:19 +08:00
|
|
|
@app.post "/logout", @_requiresLogin, @_logout
|
2013-08-20 09:43:18 +08:00
|
|
|
@app.get "/meetings", @_meetings
|
|
|
|
|
|
|
|
# When requesting the homepage a potential meetingID and sessionID are extracted
|
|
|
|
# from the user's cookie. If they match with a user that is in the database under
|
|
|
|
# the same data, they are instantly redirected to join into the meeting.
|
|
|
|
# If they are not, they will be redirected to the index page where they can enter
|
|
|
|
# their login details.
|
2013-10-29 01:23:15 +08:00
|
|
|
#
|
|
|
|
# This method is registered as a route on express.
|
|
|
|
#
|
|
|
|
# @internal
|
2013-09-16 07:10:19 +08:00
|
|
|
_index: (req, res) =>
|
2013-10-30 04:53:00 +08:00
|
|
|
@redisAction.getMeetings (err, meetings) ->
|
2013-08-20 09:43:18 +08:00
|
|
|
res.render "index",
|
2013-10-29 01:23:15 +08:00
|
|
|
title: config.appName
|
2013-08-20 09:43:18 +08:00
|
|
|
meetings: meetings
|
|
|
|
|
2013-10-29 01:23:15 +08:00
|
|
|
# Upon submitting their login details from the index page via a POST request, a meeting
|
|
|
|
# will be created and joined. If an error occurs, which usually results in using an
|
|
|
|
# invalid username or meetingID, the user receives an error response. Both success and
|
|
|
|
# error responses are in json only.
|
|
|
|
#
|
|
|
|
# This method is registered as a route on express.
|
|
|
|
#
|
|
|
|
# @internal
|
2013-09-16 07:10:19 +08:00
|
|
|
_postAuth: (req, res) =>
|
2013-08-20 09:43:18 +08:00
|
|
|
user = req.body
|
2013-10-29 01:23:15 +08:00
|
|
|
username = user.username = sanitizer.escape(user.username)
|
|
|
|
meetingID = user.meetingID = sanitizer.escape(user.meetingID)
|
2013-08-20 09:43:18 +08:00
|
|
|
sessionID = req.sessionID
|
2013-10-29 01:23:15 +08:00
|
|
|
|
|
|
|
validParameters = @_validateLoginParameters username, meetingID
|
|
|
|
|
|
|
|
if validParameters
|
|
|
|
@redisAction.makeMeeting meetingID, sessionID, username, (result) ->
|
|
|
|
user.loginAccepted = result
|
|
|
|
# save the ids so socketio can get the username and meeting
|
|
|
|
if result
|
|
|
|
res.cookie "sessionid", sessionID
|
|
|
|
res.cookie "meetingid", meetingID
|
|
|
|
res.contentType "json"
|
2013-08-20 09:43:18 +08:00
|
|
|
res.send(user)
|
2013-10-29 01:23:15 +08:00
|
|
|
else
|
|
|
|
user.loginAccepted = false
|
|
|
|
res.send(user)
|
2013-08-20 09:43:18 +08:00
|
|
|
|
|
|
|
# Returns a json informing if there's an authenticated user or not. The meetingID and
|
|
|
|
# sessionID are extracted from the user's cookie. If they match with a user that is
|
|
|
|
# in the database, the user is accepted and his information is included in the response.
|
|
|
|
# If they don't match, the user is not accepted.
|
2013-10-29 01:23:15 +08:00
|
|
|
#
|
|
|
|
# This method is registered as a route on express.
|
|
|
|
#
|
|
|
|
# @internal
|
2013-09-16 07:10:19 +08:00
|
|
|
_getAuth: (req, res) =>
|
2013-10-30 04:53:00 +08:00
|
|
|
@redisAction.isValidSession req.cookies["meetingid"], req.cookies["sessionid"], (err, valid) ->
|
2013-08-20 09:43:18 +08:00
|
|
|
res.contentType "json"
|
|
|
|
user = {}
|
|
|
|
unless valid
|
|
|
|
user.loginAccepted = false
|
|
|
|
res.send user
|
|
|
|
else
|
|
|
|
user.loginAccepted = true
|
|
|
|
user.meetingID = req.cookies.meetingid
|
|
|
|
# user.username = ?? // TODO
|
|
|
|
res.send user
|
|
|
|
|
|
|
|
# When a user logs out, their session is destroyed and their cookies are cleared.
|
|
|
|
# @param {Object} req Request object from the client
|
|
|
|
# @param {Object} res Response object to the client
|
2013-10-29 01:23:15 +08:00
|
|
|
#
|
|
|
|
# This method is registered as a route on express.
|
|
|
|
#
|
|
|
|
# @internal
|
2013-09-16 07:10:19 +08:00
|
|
|
_logout: (req, res) =>
|
2013-08-20 09:43:18 +08:00
|
|
|
req.session.destroy() # end the session
|
|
|
|
res.cookie "sessionid", null # clear the cookie from the client
|
|
|
|
res.cookie "meetingid", null
|
|
|
|
res.redirect "/"
|
|
|
|
|
|
|
|
# @param {Object} req Request object from the client
|
|
|
|
# @param {Object} res Response object to the client
|
2013-10-29 01:23:15 +08:00
|
|
|
#
|
|
|
|
# This method is registered as a route on express.
|
|
|
|
#
|
|
|
|
# @internal
|
2013-09-16 07:10:19 +08:00
|
|
|
_meetings: (req, res) =>
|
2013-10-30 04:53:00 +08:00
|
|
|
@redisAction.getMeetings (err, results) ->
|
2013-08-20 09:43:18 +08:00
|
|
|
res.contentType "json"
|
|
|
|
res.send JSON.stringify(results)
|
|
|
|
|
2013-09-16 07:10:19 +08:00
|
|
|
# If a page requires authentication to view, this function is used to verify that there
|
|
|
|
# is a user logged in.
|
|
|
|
# @param {Object} req Request object from client
|
|
|
|
# @param {Object} res Response object to client
|
|
|
|
# @param {Function} next To be run as a callback if valid
|
2013-10-29 01:23:15 +08:00
|
|
|
#
|
|
|
|
# This method is registered as a route on express.
|
|
|
|
#
|
|
|
|
# @internal
|
2013-09-16 07:10:19 +08:00
|
|
|
_requiresLogin: (req, res, next) =>
|
|
|
|
# check that they have a cookie with valid session id
|
2013-10-30 04:53:00 +08:00
|
|
|
@redisAction.isValidSession req.cookies["meetingid"], req.cookies["sessionid"], (err, isValid) ->
|
2013-09-16 07:10:19 +08:00
|
|
|
if isValid
|
|
|
|
next()
|
|
|
|
else
|
|
|
|
res.redirect "/"
|
2013-08-20 09:43:18 +08:00
|
|
|
|
2013-10-29 01:23:15 +08:00
|
|
|
# Checks whether the parameters passed by the user to login are correct
|
|
|
|
# @param username [string] the username passed by the user
|
|
|
|
# @param meetingID [string] the meetingID passed by the user
|
|
|
|
# @return [boolean] whether the parameters are correct or not
|
|
|
|
# @internal
|
|
|
|
_validateLoginParameters: (username, meetingID) ->
|
|
|
|
username? and meetingID? and
|
|
|
|
username.length <= config.maxUsernameLength and
|
|
|
|
meetingID.split(" ").length is 1
|