From 04dadc46d5f30486632508cc2bb1fa763fc6ff12 Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Fri, 7 Dec 2018 14:33:34 -0500 Subject: [PATCH] ServiceContext.token needs more atomic --- src/client/spark_api/api/ServiceContext.cs | 29 ++++++++++++---------- src/lib/api/public/ClientChannel.h | 3 ++- src/lib/api/public/ServiceContext.cpp | 19 ++++++++++---- src/lib/api/public/ServiceContext.h | 4 +-- 4 files changed, 34 insertions(+), 21 deletions(-) diff --git a/src/client/spark_api/api/ServiceContext.cs b/src/client/spark_api/api/ServiceContext.cs index b27ea9412..89d53f482 100644 --- a/src/client/spark_api/api/ServiceContext.cs +++ b/src/client/spark_api/api/ServiceContext.cs @@ -35,6 +35,7 @@ namespace bts.api.core { public AuthTokenImmutable? token => _token ?? (AuthTokenImmutable?)null; public string user { get; private set; } public uint user_id { get; private set; } + public bool is_authenticated => token.HasValue; public async Task login(string username, string password) { var auth_service = locate(); @@ -43,21 +44,23 @@ namespace bts.api.core { Password = password ?? "" }); if (result.success) { - if (result.value.IsValid) { - _token = result.value.Token; - user = username; - user_id = result.value.UserId; + if (result.value.IsValid) { + _token = result.value.Token; + user = username; + user_id = result.value.UserId; return new LoginResult(); } - else { - return new LoginResult(result.value.Reason); + else { + return new LoginResult(result.value.Reason); } } - else { - return new LoginResult(result.error_message); + else { + return new LoginResult(result.error_message); } } public void login(AuthTokenImmutable token) { + this.user = ""; + this.user_id = 0; this._token = token.CloneToMutable(); } @@ -83,10 +86,10 @@ namespace bts.api.core { #endregion } - public class LoginResult { - public LoginResult() { success = true; } - public LoginResult(string reason) { success = false; this.reason = reason; } - public bool success { get; } - public string reason { get; } + public class LoginResult { + public LoginResult() { success = true; } + public LoginResult(string reason) { success = false; this.reason = reason; } + public bool success { get; } + public string reason { get; } } } diff --git a/src/lib/api/public/ClientChannel.h b/src/lib/api/public/ClientChannel.h index 46266a1e9..3918f3f05 100644 --- a/src/lib/api/public/ClientChannel.h +++ b/src/lib/api/public/ClientChannel.h @@ -25,7 +25,8 @@ public: RequestHeader header; header.set_method(method_id); header.set_request_id(++_id); - header.mutable_token()->set_token(_context.token().token()); + auto ptoken = _context.token(); + if (ptoken) header.mutable_token()->CopyFrom(*ptoken); int header_size = header.ByteSize()+1; assert(header_size < 128); diff --git a/src/lib/api/public/ServiceContext.cpp b/src/lib/api/public/ServiceContext.cpp index bcb22faac..39de8b4fa 100644 --- a/src/lib/api/public/ServiceContext.cpp +++ b/src/lib/api/public/ServiceContext.cpp @@ -4,6 +4,7 @@ #include "ApiServer.h" #include "lib/logging/logging.h" #include "lib/api/services/AuthService.client.pb.h" +#include namespace bts { namespace api { @@ -30,9 +31,14 @@ struct ServiceContextImpl { api_destroy_quickbus(&_qbus); } } + void set_token(const AuthToken& t) { + auto st = std::make_shared(); + st->CopyFrom(t); + _token = st; + } Quickbus& _qbus; bool _owns_qb; - AuthToken _token; + std::shared_ptr _token; std::string _user; }; @@ -60,7 +66,7 @@ const LoginResult ServiceContext::login(const std::string& username, const std:: auto result = auth.authenticate_login(request); if (result.success()) { if (result.value()->is_valid()) { - _self->_token = result.value()->token(); + _self->set_token(result.value()->token()); _self->_user = username; return {}; } @@ -73,7 +79,8 @@ const LoginResult ServiceContext::login(const std::string& username, const std:: } } void ServiceContext::login(const AuthToken& token) { - _self->_token = token; + _self->set_token(token); + _self->_user.clear(); } void ServiceContext::post(const Action& action, uint64_t delay) { @@ -81,8 +88,10 @@ void ServiceContext::post(const Action& action, uint64_t delay) { } Quickbus& ServiceContext::bus() const { return _self->_qbus; } -const AuthToken& ServiceContext::token() const { return _self->_token; } -const std::string& ServiceContext::user() const { return _self->_user; } +bool ServiceContext::is_authenticated() const { return _self->_token != nullptr; } +const std::shared_ptr ServiceContext::token() const { + return _self->_token; +} }}//namespace #endif diff --git a/src/lib/api/public/ServiceContext.h b/src/lib/api/public/ServiceContext.h index 11a685a70..181e673f3 100644 --- a/src/lib/api/public/ServiceContext.h +++ b/src/lib/api/public/ServiceContext.h @@ -40,8 +40,8 @@ public: } Quickbus& bus() const; - const AuthToken& token() const; - const std::string& user() const; + bool is_authenticated() const; + const std::shared_ptr token() const; const LoginResult login(const std::string& username, const std::string& password); void login(const AuthToken& token);