ServiceContext.token needs more atomic

This commit is contained in:
Eric Harding committed 2018-12-07 14:38:22 -05:00
1 parent ba4b6aa1ff
commit 04dadc46d5
4 files changed
+34 -21

No files matched your search

+16 -13
View File
@@ -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<LoginResult> login(string username, string password) {
var auth_service = locate<AuthService>();
@@ -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; }
}
}
+2 -1
View File
@@ -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);
+14 -5
View File
@@ -4,6 +4,7 @@
#include "ApiServer.h"
#include "lib/logging/logging.h"
#include "lib/api/services/AuthService.client.pb.h"
#include <memory>
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<AuthToken>();
st->CopyFrom(t);
_token = st;
}
Quickbus& _qbus;
bool _owns_qb;
AuthToken _token;
std::shared_ptr<AuthToken> _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<AuthToken> ServiceContext::token() const {
return _self->_token;
}
}}//namespace
#endif
+2 -2
View File
@@ -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<AuthToken> token() const;
const LoginResult login(const std::string& username, const std::string& password);
void login(const AuthToken& token);