SPARK-1905 Attach token to api request header (c++)
This commit is contained in:
1 parent
f4ee9434cc
commit
e1568c1e67
8 files changed
+62
-21
No files matched your search
@@ -3,7 +3,7 @@
|
||||
namespace bts {
|
||||
namespace api {
|
||||
|
||||
ClientChannel::ClientChannel() : _id(1)
|
||||
ClientChannel::ClientChannel(const ServiceContext& ctx) : _id(1), _context(ctx)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#include "Result.h"
|
||||
#include "ApiException.h"
|
||||
#include "RawUpdate.h"
|
||||
//#include "Update.h"
|
||||
#include "ServiceContext.h"
|
||||
#include <rxcpp/rx.hpp>
|
||||
#include <functional>
|
||||
#include <google/protobuf/io/zero_copy_stream_impl_lite.h>
|
||||
@@ -12,12 +12,12 @@
|
||||
namespace bts {
|
||||
namespace api {
|
||||
|
||||
|
||||
class ClientChannel
|
||||
{
|
||||
int _id;
|
||||
ServiceContext _context;
|
||||
public:
|
||||
ClientChannel();
|
||||
ClientChannel(const ServiceContext&);
|
||||
virtual ~ClientChannel();
|
||||
|
||||
template <typename Request, typename Response>
|
||||
@@ -25,6 +25,7 @@ public:
|
||||
RequestHeader header;
|
||||
header.set_method(method_id);
|
||||
header.set_request_id(++_id);
|
||||
header.mutable_token()->set_token(_context.token().token());
|
||||
int header_size = header.ByteSize()+1;
|
||||
assert(header_size < 128);
|
||||
|
||||
|
||||
@@ -4,7 +4,8 @@
|
||||
namespace bts {
|
||||
namespace api {
|
||||
|
||||
QuickbusClientChannel::QuickbusClientChannel(Quickbus& bus, const char* type_name, const char* service_qualifier) :
|
||||
QuickbusClientChannel::QuickbusClientChannel(Quickbus& bus, const ServiceContext& context, const char* type_name, const char* service_qualifier) :
|
||||
ClientChannel(context),
|
||||
_pclient(api_client_create(&bus, type_name, service_qualifier))
|
||||
{
|
||||
}
|
||||
|
||||
@@ -6,12 +6,13 @@
|
||||
namespace bts {
|
||||
namespace api {
|
||||
|
||||
class ServiceContext;
|
||||
class QuickbusClientChannel : public ClientChannel
|
||||
{
|
||||
ApiClient* _pclient;
|
||||
|
||||
public:
|
||||
QuickbusClientChannel(Quickbus&, const char* type_name, const char* service_qualifier);
|
||||
QuickbusClientChannel(Quickbus&, const ServiceContext&, const char* type_name, const char* service_qualifier);
|
||||
~QuickbusClientChannel();
|
||||
|
||||
private:
|
||||
|
||||
@@ -3,34 +3,65 @@
|
||||
#include "QuickbusClientChannel.h"
|
||||
#include "ApiServer.h"
|
||||
#include "lib/logging/logging.h"
|
||||
#include "lib/api/services/AuthService.client.pb.h"
|
||||
|
||||
namespace bts {
|
||||
namespace api {
|
||||
|
||||
struct ServiceContextImpl {
|
||||
ServiceContextImpl(Quickbus* maybe_bus):
|
||||
_qbus(maybe_bus ? *maybe_bus : *api_create_quickbus(nullptr, 0)),
|
||||
_owns_qb(maybe_bus == nullptr),
|
||||
_token(),
|
||||
_user()
|
||||
{}
|
||||
~ServiceContextImpl() {
|
||||
if (_owns_qb) {
|
||||
api_destroy_quickbus(&_qbus);
|
||||
}
|
||||
}
|
||||
Quickbus& _qbus;
|
||||
bool _owns_qb;
|
||||
AuthToken _token;
|
||||
std::string _user;
|
||||
};
|
||||
|
||||
ServiceContext::ServiceContext(Quickbus* maybe_bus) :
|
||||
_qbus(maybe_bus ? *maybe_bus : *api_create_quickbus(nullptr, 0)),
|
||||
_owns_qb(maybe_bus == nullptr)
|
||||
_self(std::make_shared<ServiceContextImpl>(maybe_bus))
|
||||
{
|
||||
}
|
||||
|
||||
ServiceContext::~ServiceContext() {
|
||||
if (_owns_qb) {
|
||||
api_destroy_quickbus(&_qbus);
|
||||
}
|
||||
}
|
||||
ServiceContext::~ServiceContext() {}
|
||||
|
||||
std::unique_ptr<ClientChannel> ServiceContext::create_channel(const char * type_name, const char * service_qualifier) const {
|
||||
LOG(Api,Info,"Locate Service: {}{}",std::string(type_name) ,std::string(service_qualifier));
|
||||
return std::unique_ptr<ClientChannel>(new QuickbusClientChannel(_qbus, type_name, service_qualifier));
|
||||
return std::unique_ptr<ClientChannel>(new QuickbusClientChannel(_self->_qbus, *this, type_name, service_qualifier));
|
||||
}
|
||||
|
||||
ServerChannel ServiceContext::publish(ApiServer& service, const std::string& service_type, const std::string& qualifier) const {
|
||||
return ServerChannel(_qbus, service_type, qualifier, service);
|
||||
return ServerChannel(_self->_qbus, service_type, qualifier, service);
|
||||
}
|
||||
|
||||
bool ServiceContext::login(const std::string& username, const std::string& password) {
|
||||
auto auth = locate<user::AuthService>();
|
||||
user::AuthenticateLoginRequest request;
|
||||
request.set_login(username);
|
||||
request.set_password(password);
|
||||
auto result = auth.authenticate_login(request);
|
||||
if (result.success()) {
|
||||
_self->_token = result.value()->token(); //.CopyFrom(result.value()->token());
|
||||
_self->_user = username;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void ServiceContext::post(const Action& action, uint64_t delay) {
|
||||
api_post(_qbus, action, delay);
|
||||
api_post(_self->_qbus, action, 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; }
|
||||
|
||||
}}//namespace
|
||||
#endif
|
||||
@@ -5,7 +5,6 @@
|
||||
// client
|
||||
#include "../core/ApiClientInterop.h"
|
||||
#include "../core/ApiServerInterop.h"
|
||||
#include "ClientChannel.h"
|
||||
// server
|
||||
#include "ServerChannel.h"
|
||||
|
||||
@@ -16,13 +15,16 @@
|
||||
namespace bts {
|
||||
namespace api {
|
||||
|
||||
class ClientChannel;
|
||||
struct ServiceContextImpl;
|
||||
|
||||
class BTS_EXPORT ServiceContext
|
||||
{
|
||||
Quickbus& _qbus;
|
||||
bool _owns_qb;
|
||||
std::shared_ptr<ServiceContextImpl> _self;
|
||||
|
||||
public:
|
||||
ServiceContext(Quickbus* maybe_bus=nullptr);
|
||||
// ServiceContext(ServiceContext& other) = default;
|
||||
~ServiceContext();
|
||||
|
||||
template <class T>
|
||||
@@ -30,11 +32,14 @@ public:
|
||||
return T(create_channel(T::name(), qualifier));
|
||||
}
|
||||
|
||||
Quickbus& bus() const;
|
||||
const AuthToken token() const;
|
||||
const std::string& user() const;
|
||||
bool login(const std::string& username, const std::string& password);
|
||||
|
||||
typedef std::function<void()> Action;
|
||||
void post(const Action&, uint64_t delayms=0);
|
||||
|
||||
Quickbus& bus() const { return _qbus; }
|
||||
|
||||
private:
|
||||
friend class ApiServer;
|
||||
std::unique_ptr<ClientChannel> create_channel(const char*, const char*) const;
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "lib/api/public/ApiUtils.h"
|
||||
#include "lib/api/core/ApiServerInterop.h"
|
||||
#include <unordered_map>
|
||||
#include <mutex>
|
||||
{{ns-open package}}
|
||||
{{#each service}}
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
#include "lib/api/services/OrderService.pb.h"
|
||||
#include "lib/orderman/orderman_utils/orderman_common.h"
|
||||
#include "lib/orderman/orders/Order.h"
|
||||
#include <future>
|
||||
|
||||
namespace bts {
|
||||
namespace orderman {
|
||||
|
||||
Reference in new issue
Block a user