From 5e4e32c025967018e2c5a01071cbe5a03c281b96 Mon Sep 17 00:00:00 2001 From: Johannes Loher Date: Thu, 23 Nov 2017 21:40:48 +0100 Subject: [PATCH] do password hashing asynchronously --- source/calendarwebapp/authenticator.d | 17 ++++++++++------- source/calendarwebapp/calendarwebapp.d | 9 +++++---- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/source/calendarwebapp/authenticator.d b/source/calendarwebapp/authenticator.d index 0322398..9b41013 100644 --- a/source/calendarwebapp/authenticator.d +++ b/source/calendarwebapp/authenticator.d @@ -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; diff --git a/source/calendarwebapp/calendarwebapp.d b/source/calendarwebapp/calendarwebapp.d index fdc958f..359da88 100644 --- a/source/calendarwebapp/calendarwebapp.d +++ b/source/calendarwebapp/calendarwebapp.d @@ -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;