171 lines
4.0 KiB
Plaintext
171 lines
4.0 KiB
Plaintext
newRouter = import("router.noja").new;
|
|
serve = import("server.noja").serve;
|
|
cookie = import("cookie.noja");
|
|
Request = import("request.noja").Request;
|
|
respond = import("response.noja").new;
|
|
urlencoded = import("urlencoded.noja");
|
|
|
|
user_table = {};
|
|
session_table = {};
|
|
|
|
fun getCookie(req: Request, name: String) {
|
|
src = req.headers["Cookie"];
|
|
if src == none:
|
|
return none;
|
|
cookies, error = cookie.parseCookieHeader(src);
|
|
if error != none:
|
|
return none, error;
|
|
return cookies[name];
|
|
}
|
|
|
|
fun getSessionID(req: Request) {
|
|
session_id, error = getCookie(req, "SESSID");
|
|
if error != none:
|
|
return none, error;
|
|
if session_id == none:
|
|
return none;
|
|
return string.toInt(session_id);
|
|
}
|
|
|
|
fun getUsername(req: Request) {
|
|
session_id, error = getSessionID(req);
|
|
if error != none:
|
|
return none, none, error;
|
|
username = session_table[session_id];
|
|
return username, session_id;
|
|
}
|
|
|
|
router = newRouter();
|
|
|
|
router->plug("/", {
|
|
fun GET()
|
|
return respond(307, none, {"Location": "/login"});
|
|
});
|
|
|
|
router->plug("/home", {
|
|
|
|
fun GET(req: Request) {
|
|
|
|
username, _, error = getUsername(req);
|
|
if error != none:
|
|
return respond(400, error);
|
|
if username == none:
|
|
return respond(401, "You're not logged in");
|
|
|
|
content = string.cat(
|
|
"<html>",
|
|
"<head>",
|
|
"<title>Homepage</title>",
|
|
"</head>",
|
|
"<body>",
|
|
"<a>Hello, ", username, "!</a>",
|
|
"</body>",
|
|
"</html>"
|
|
);
|
|
return respond(200, content);
|
|
}
|
|
});
|
|
|
|
router->plug("/login", {
|
|
GET: 'pages/login.html',
|
|
fun POST(req: Request) {
|
|
|
|
username, _, error = getUsername(req);
|
|
if error != none:
|
|
return respond(400, error);
|
|
if username != none:
|
|
return respond(400, "You're already logged in");
|
|
|
|
fun areValid(params) {
|
|
expect = {username: String, password: String};
|
|
return istypeof(expect, params)
|
|
and count(params.username) > 0
|
|
and count(params.password) > 0;
|
|
}
|
|
|
|
params = urlencoded.parse(req.body);
|
|
if not areValid(params):
|
|
return respond(400, "Invalid parameters");
|
|
username = params.username;
|
|
password = params.password;
|
|
|
|
info = user_table[username];
|
|
|
|
if info == none:
|
|
return respond(200, "No such user");
|
|
if info != password:
|
|
return respond(200, "Wrong password");
|
|
|
|
# Log-in succeded
|
|
|
|
# Create a new session
|
|
session_id = random.generate(0, 1000);
|
|
session_table[session_id] = username;
|
|
|
|
message = string.cat("Welcome, ", username, "!");
|
|
headers = {
|
|
"Location" : "/home",
|
|
"Set-Cookie": string.cat("SESSID=", toString(session_id))
|
|
};
|
|
return respond(303, message, headers);
|
|
}
|
|
});
|
|
|
|
router->plug("/signup", {
|
|
GET: 'pages/signup.html',
|
|
|
|
fun POST(req: Request) {
|
|
|
|
username, _, error = getUsername(req);
|
|
if error != none:
|
|
return respond(400, error);
|
|
if username != none:
|
|
return respond(400, "You're already logged in");
|
|
|
|
fun areValid(params) {
|
|
expect = {username: String, password: String};
|
|
return istypeof(expect, params)
|
|
and count(params.username) > 0
|
|
and count(params.password) > 0;
|
|
}
|
|
|
|
params = urlencoded.parse(req.body);
|
|
if not areValid(params):
|
|
return respond(400, "Invalid parameters");
|
|
username = params.username;
|
|
password = params.password;
|
|
|
|
if user_table[password] != none:
|
|
return respond(200, "User already exists");
|
|
|
|
# Signed up!
|
|
user_table[username] = password;
|
|
session_id = random.generate(0, 1000);
|
|
session_table[session_id] = username;
|
|
|
|
# Respond
|
|
message = string.cat("Welcome, ", username, "!");
|
|
headers = {
|
|
"Location" : "/home",
|
|
"Set-Cookie": string.cat("SESSID=", toString(session_id))
|
|
};
|
|
return respond(303, message, headers);
|
|
}
|
|
});
|
|
|
|
router->plug("/logout", {
|
|
fun GET(req: Request) {
|
|
|
|
username, session_id, error = getUsername(req);
|
|
if error != none:
|
|
return respond(400, error);
|
|
if username == none:
|
|
return respond(401, "You're not logged in");
|
|
|
|
session_table[session_id] = none;
|
|
|
|
return respond(303, "Goodbye!", {"Location": "/login"});
|
|
}
|
|
});
|
|
|
|
error(serve("127.0.0.1", 8080, router)); |