do password hashing asynchronously

This commit is contained in:
Johannes Loher 2017-11-23 21:40:48 +01:00
parent c918e8e574
commit 5e4e32c025
2 changed files with 15 additions and 11 deletions

View file

@ -30,14 +30,15 @@ private:
public:
Nullable!AuthInfo checkUser(string username, string password) @safe
{
auto result = users.findOne(["username" : username]);
/* checkHash should be called using vibe.core.concurrency.async to
avoid blocking, but https://github.com/vibe-d/vibe.d/issues/1521 is
blocking this */
import vibe.core.concurrency : async;
immutable result = users.findOne(["username" : username]);
if (result != Bson(null))
{
auto authInfo = result.deserializeBson!AuthInfo;
if (passwordHasher.checkHash(password, authInfo.passwordHash))
if ((()@trusted => async(() => passwordHasher.checkHash(password,
authInfo.passwordHash)).getResult)())
{
return authInfo.nullable;
}
@ -94,6 +95,8 @@ private:
public:
Nullable!AuthInfo checkUser(string username, string password) @trusted
{
import vibe.core.concurrency : async;
auto cn = pool.lockConnection();
scope (exit)
cn.close();
@ -107,7 +110,7 @@ public:
if (!result.empty)
{
auto authInfo = toAuthInfo(result.front);
if (passwordHasher.checkHash(password, authInfo.passwordHash))
if (async(() => passwordHasher.checkHash(password, authInfo.passwordHash)).getResult)
{
return authInfo.nullable;
}
@ -150,7 +153,7 @@ public:
private:
AuthInfo toAuthInfo(Row r)
AuthInfo toAuthInfo(in Row r)
{
import std.conv : to;

View file

@ -65,7 +65,6 @@ public:
render!("createevent.dt", _error, authInfo);
}
@auth(Role.user | Role.admin) @errorDisplay!getCreateevent void postCreateevent(Date begin,
Nullable!Date end, string description, string name, EventType type, bool shout)
{
@ -109,8 +108,10 @@ public:
@auth(Role.admin) @errorDisplay!getCreateuser void postCreateuser(string username,
string password, Privilege role)
{
import vibe.core.concurrency : async;
authenticator.addUser(AuthInfo("", username,
passwordHasher.generateHash(password), role));
async(() => passwordHasher.generateHash(password)).getResult, role));
redirect("/users");
}
@ -121,8 +122,8 @@ private:
string field;
}
SessionVar!(AuthInfo, "authInfo") authInfo = AuthInfo("",
string.init, string.init, Privilege.None);
SessionVar!(AuthInfo, "authInfo") authInfo = AuthInfo("", string.init,
string.init, Privilege.None);
@Autowire EventStore eventStore;
@Autowire Authenticator authenticator;