use mongodb for authentication

This commit is contained in:
Johannes Loher 2017-08-07 03:31:46 +02:00
parent 1b5e64859c
commit da53da6101

View file

@ -9,11 +9,18 @@ import std.exception : enforce;
import std.typecons : Nullable; import std.typecons : Nullable;
import vibe.core.path : Path; import vibe.core.path : Path;
import vibe.data.bson : Bson;
import vibe.db.mongo.database : MongoDatabase;
import vibe.db.mongo.mongo : connectMongoDB;
import vibe.http.common : HTTPStatusException; import vibe.http.common : HTTPStatusException;
import vibe.http.server : HTTPServerRequest, HTTPServerResponse; import vibe.http.server : HTTPServerRequest, HTTPServerResponse;
import vibe.http.status : HTTPStatus; import vibe.http.status : HTTPStatus;
import vibe.web.auth; import vibe.web.auth;
import vibe.web.web : errorDisplay, noRoute, redirect, render, SessionVar, terminateSession; import vibe.web.web : errorDisplay, noRoute, redirect, render, SessionVar,
terminateSession;
struct AuthInfo struct AuthInfo
{ {
@ -46,7 +53,7 @@ public:
@noAuth @errorDisplay!getLogin void postLogin(string username, string password) @noAuth @errorDisplay!getLogin void postLogin(string username, string password)
{ {
enforce(username == "foo" && password == "bar", "Benutzername oder Passwort ungültig"); enforce(checkUser(username, password), "Benutzername oder Passwort ungültig");
immutable AuthInfo authInfo = {username}; immutable AuthInfo authInfo = {username};
auth = authInfo; auth = authInfo;
redirect("/"); redirect("/");
@ -86,8 +93,23 @@ public:
string field; string field;
} }
this()
{
database = connectMongoDB("localhost").getDatabase("CalendarWebapp");
}
private: private:
immutable fileName = Path("events.json"); immutable fileName = Path("events.json");
SessionVar!(AuthInfo, "auth") auth; SessionVar!(AuthInfo, "auth") auth;
MongoDatabase database;
bool checkUser(string username, string password)
{
auto users = database["users"];
auto result = users.findOne(["_id" : username, "password" : password]);
return result != Bson(null);
}
} }