Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AuthException, throw it when it should. #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions serv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ static int64_t getRandom63()
return dist(generator);
}

class AuthException : public std::runtime_error
{
public:
AuthException(const char *m) : std::runtime_error(m) {}
};


struct Users
{
Users(LockedSqw& lsqw) : d_lsqw(lsqw)
Expand Down Expand Up @@ -171,7 +178,7 @@ struct AuthReqs
auto cookies = getCookies(req.get_header_value("Cookie"));
auto siter = cookies.find("session");
if(siter == cookies.end()) {
throw std::runtime_error("No session cookie");
throw AuthException("No session cookie");
}
return siter->second;
}
Expand Down Expand Up @@ -235,6 +242,8 @@ void checkImageOwnership(LockedSqw& lsqw, Users& u, std::string& user, std::stri
}
}



int main(int argc, char**argv)
{
argparse::ArgumentParser args("serv");
Expand Down Expand Up @@ -285,17 +294,20 @@ int main(int argc, char**argv)

svr.set_exception_handler([](const auto& req, auto& res, std::exception_ptr ep) {
string reason;
res.status = 500;
try {
std::rethrow_exception(ep);
} catch (AuthException &e) {
res.status = 403;
reason = fmt::format("Authentication Error: {}", e.what());
} catch (std::exception &e) {
reason = fmt::format("An error occurred: {}", e.what());
} catch (...) {
reason = "An unknown error occurred";
}
cout<<req.path<<": 500 created for "<<reason<<endl;
cout<<req.path<<": "<<res.status<<" created for "<<reason<<endl;
string html = fmt::format("<html><body><h1>Error</h1>{}</body></html>", reason);
res.set_content(html, "text/html");
res.status = 500;
});

svr.set_mount_point("/", args.get<string>("html-dir"));
Expand Down Expand Up @@ -559,7 +571,7 @@ int main(int argc, char**argv)

svr.Get("/my-images", [&lsqw, a](const httplib::Request &req, httplib::Response &res) {
if(!a.check(req)) {
throw std::runtime_error("Not logged-in");
throw AuthException("Not logged-in");
}
lsqw.queryJ(res, "select images.id, postid, images.tstamp,content_type,length(image) as size, public, posts.publicUntilTstamp from images,posts where postId = posts.id and user=?", {a.getUser(req)});
});
Expand Down