From 2408c8dcc7eb1139b6cddfaf8fa5298eec6f533f Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Tue, 1 May 2018 17:17:54 -0400 Subject: [PATCH] SPARK-1030 Make authentication real --- src/lib/api/public/Result.h | 1 + src/server/sparkd/http/ApiGateway.cpp | 39 ++++++++++++++++++--------- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/src/lib/api/public/Result.h b/src/lib/api/public/Result.h index 64ff8549b..de3625775 100644 --- a/src/lib/api/public/Result.h +++ b/src/lib/api/public/Result.h @@ -61,6 +61,7 @@ public: } bool success() const { return _code == ApiError::SUCCESS; } + operator bool() const { return success(); } ApiError code() const { return _code; } const char* error_message() const { return error_string(_code); } diff --git a/src/server/sparkd/http/ApiGateway.cpp b/src/server/sparkd/http/ApiGateway.cpp index 648bdb231..b6bbfca76 100644 --- a/src/server/sparkd/http/ApiGateway.cpp +++ b/src/server/sparkd/http/ApiGateway.cpp @@ -3,6 +3,7 @@ #include "lib/logging/logging.h" #include "lib/api/public/ApiUtils.h" #include "lib/api/core/ApiClientInterop.h" +#include "lib/api/services/UserService.client.pb.h" #include "lib/utils/bts_assert.h" #include #include @@ -17,6 +18,8 @@ namespace http { struct ApiGatewayImpl { std::mutex _m; const api::ServiceContext& _sc; + api::user::UserService _logon; + std::unordered_map _apiclient; std::vector _subscriptions; @@ -24,24 +27,34 @@ struct ApiGatewayImpl { ApiGatewayImpl(const api::ServiceContext& ctx) : _m(), _sc(ctx), + _logon(ctx.locate()), _apiclient(), _subscriptions() { } - void handle_login(const GatewayLogin& login, const Responder& r) { + void handle_login(const GatewayLogin& login, const Responder& respond) { LOG(Api,Info, "Logon request for {}", login.username()); - GatewayResponse result; - //todo: UserService authenticate_login_async - if (login.username() == "bts") { - result.mutable_logon()->set_is_valid(true); - result.mutable_logon()->set_token("SuperSecretToken"); - r(result); - } - else { - result.mutable_logon()->set_is_valid(false); - result.mutable_logon()->set_reason("Your mother is a hamster and your father smelt of elderberries."); - r(result); - } + + api::user::AuthenticateLoginRequest alr; + alr.set_login(login.username()); + alr.set_password(login.password()); + _logon.authenticate_login(alr, [respond](const api::Result& logon_response) { + GatewayResponse resp; + resp.mutable_logon()->set_is_valid(false); + if (logon_response) { + if (logon_response.result()->is_valid()) { + resp.mutable_logon()->set_is_valid(true); + resp.mutable_logon()->set_token(logon_response.result()->token().token()); + } + else { + resp.mutable_logon()->set_reason(logon_response.result()->reason()); + } + } + else { + resp.mutable_logon()->set_reason(logon_response.error_message()); + } + respond(resp); + }); } void error(const Responder& r, const std::string msg) {