SPARK-1669 1/2 - send login token with market data query
This commit is contained in:
1 parent
490ad06cf5
commit
c09485affc
10 files changed
+68
-25
No files matched your search
@@ -13,11 +13,13 @@ using System.Linq;
|
||||
using SecurityKey = System.UInt32;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Threading;
|
||||
using bts.api.user;
|
||||
|
||||
namespace bts {
|
||||
[Export(typeof(IInitializable))]
|
||||
class MDSource : IObserver<List<SecurityAndHandle>>, IInitializable, IDisposable {
|
||||
readonly Lazy<IBus> _bus;
|
||||
readonly Lazy<AuthToken> _token;
|
||||
readonly Lazy<SyntheticSecurityService> _ss;
|
||||
readonly MDConsumer _consumer = new MDConsumer();
|
||||
readonly Co2RelayControl _relay;
|
||||
@@ -27,14 +29,20 @@ namespace bts {
|
||||
List<IDisposable> _dispose = new List<IDisposable>();
|
||||
|
||||
[ImportingConstructor]
|
||||
public MDSource(Lazy<IBus> bus, Lazy<SyntheticSecurityService> ss) {
|
||||
public MDSource(
|
||||
Lazy<IBus> bus,
|
||||
Lazy<AuthToken> token,
|
||||
Lazy<SyntheticSecurityService> ss) {
|
||||
_bus = bus;
|
||||
_token = token;
|
||||
_ss = ss;
|
||||
_relay = new Co2RelayControl(_consumer);
|
||||
_relay.preload_securities((co2.MDRequest.Types.Flags)0xff).ignore();
|
||||
_dispose.Add(_consumer.on_security.Subscribe(this));
|
||||
}
|
||||
|
||||
public void initialize() {
|
||||
_relay.set_token(_token.Value);
|
||||
_dispose.Add(
|
||||
_ss.Value.subscribei_metadata()
|
||||
.Select(upd => upd.all.ToDictionary(i => i.SecurityKey, i => i.Strategy))
|
||||
|
||||
@@ -25,24 +25,17 @@ namespace mbts.ui.shell
|
||||
internal class LoginContainer
|
||||
{
|
||||
[ImportingConstructor]
|
||||
public LoginContainer(UserService user, [Import("settings")] PersistenceService persist, [Import("autologin", AllowDefault=true)]string auto_login) {
|
||||
if (string.IsNullOrEmpty(auto_login)) {
|
||||
var model = LoginModel.instance;
|
||||
model.provide_services(user, persist);
|
||||
// await_login pumps the dispatcher but doesn't allow control to return to MEF until the username and workspace are available
|
||||
model.await_login();
|
||||
var u = model.get_user();
|
||||
UserName = u.name;
|
||||
UserId = u.id;
|
||||
Workspace = model.get_workspace();
|
||||
model.Dispose();
|
||||
}
|
||||
else {
|
||||
UserName = auto_login;
|
||||
Environment.SetEnvironmentVariable("BTS_USER", UserName);
|
||||
Workspace = string.Empty;
|
||||
LoginModel.instance.Dispose();
|
||||
}
|
||||
public LoginContainer(UserService user, [Import("settings")] PersistenceService persist) {
|
||||
var model = LoginModel.instance;
|
||||
model.provide_services(user, persist);
|
||||
// await_login pumps the dispatcher but doesn't allow control to return to MEF until the username and workspace are available
|
||||
model.await_login();
|
||||
var u = model.get_user();
|
||||
UserName = u.name;
|
||||
UserId = u.id;
|
||||
Token = u.token;
|
||||
Workspace = model.get_workspace();
|
||||
model.Dispose();
|
||||
}
|
||||
|
||||
[Export("username")]
|
||||
@@ -50,14 +43,22 @@ namespace mbts.ui.shell
|
||||
|
||||
[Export("userid")]
|
||||
public uint UserId { get; private set; }
|
||||
[Export]
|
||||
public AuthToken Token { get; private set; }
|
||||
|
||||
[Export("workspace")]
|
||||
public string Workspace { get; private set; }
|
||||
}
|
||||
|
||||
public struct User {
|
||||
public string name;
|
||||
public UInt32 id;
|
||||
public User(string name, uint id, AuthToken token) {
|
||||
this.name = name;
|
||||
this.id = id;
|
||||
this.token = token;
|
||||
}
|
||||
public readonly string name;
|
||||
public readonly UInt32 id;
|
||||
public readonly AuthToken token;
|
||||
}
|
||||
|
||||
public class LoginModel : IDisposable
|
||||
@@ -117,7 +118,7 @@ namespace mbts.ui.shell
|
||||
_enum_workspace = new PreloginWorkspaceEnumerator(persist);
|
||||
var response = await _logon.get_users_async(new GetUsersRequest());
|
||||
if (response.success) {
|
||||
Users = response.value.Users.Select(x => new User { name = x.Login, id = x.Id }).OrderBy(u => u.name).ToArray();
|
||||
Users = response.value.Users.Select(x => new User(x.Login,x.Id, null)).OrderBy(u => u.name).ToArray();
|
||||
}
|
||||
else {
|
||||
ErrorMessage = "Logon: " + response.error_message;
|
||||
@@ -137,7 +138,7 @@ namespace mbts.ui.shell
|
||||
if (response.success) {
|
||||
if (response.value.IsValid) {
|
||||
_workspace = workspace;
|
||||
set_authenticated_user(new User { name =username, id = response.value.UserId});
|
||||
set_authenticated_user(new User(username, response.value.UserId, response.value.Token));
|
||||
return (true, "");
|
||||
}
|
||||
else {
|
||||
|
||||
@@ -82,6 +82,8 @@ namespace mbts.co2
|
||||
internal unsafe static extern IntPtr stop_relay(IntPtr relay);
|
||||
[DllImport("libspark")]
|
||||
internal unsafe static extern IntPtr add_relay_symbol(IntPtr relay, UserSymbol symbol, bts.co2.MDRequest.Types.Flags flags);
|
||||
[DllImport("libspark")]
|
||||
internal unsafe static extern IntPtr set_relay_token(IntPtr relay, string user_name);
|
||||
|
||||
[DllImport("libspark")]
|
||||
internal unsafe static extern void post_on_scan_security_thread(Action callback);
|
||||
|
||||
@@ -6,6 +6,7 @@ using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using bts.api;
|
||||
using bts;
|
||||
using bts.api.user;
|
||||
|
||||
namespace mbts.co2
|
||||
{
|
||||
@@ -22,6 +23,10 @@ namespace mbts.co2
|
||||
Log.assert(_relay != IntPtr.Zero);
|
||||
}
|
||||
|
||||
public void set_token(AuthToken token) {
|
||||
Co2NativeMethods.set_relay_token(_relay, token.Token);
|
||||
}
|
||||
|
||||
public async Task preload_securities(MDRequest.Types.Flags flags=MDRequest.Types.Flags.Security) {
|
||||
var securities = UserSymbol.get_all();
|
||||
foreach (var future in securities.Where(s => s.type == SecurityType.Future)) {
|
||||
|
||||
@@ -29,6 +29,11 @@ message AuthToken {
|
||||
string token = 1;
|
||||
}
|
||||
|
||||
message TokenPayload {
|
||||
uint32 date = 1;
|
||||
uint32 id = 2;
|
||||
}
|
||||
|
||||
message GetUsersRequest {}
|
||||
message GetUsersResponse { repeated User users = 1; string error = 2; }
|
||||
message AddOrUpdateUserRequest {AuthToken token = 1; User user = 2;}
|
||||
|
||||
@@ -172,6 +172,11 @@ CAPI void add_relay_symbol(md::RelayControl* prelay, BlitUserSymbol& symbol, uin
|
||||
}
|
||||
}
|
||||
|
||||
CAPI void set_relay_token(md::RelayControl* prelay, const char* token) {
|
||||
md::set_relay_token(prelay, std::string(token));
|
||||
}
|
||||
|
||||
|
||||
typedef void(*usersymbol_cb)(BlitUserSymbol& symbol);
|
||||
CAPI void md_consumer_get_system_md_symbols(usersymbol_cb cb) {
|
||||
std::set<UserSymbol> system_symbols;
|
||||
|
||||
@@ -61,6 +61,7 @@ message MDRequest {
|
||||
MISC = 0x40;
|
||||
}
|
||||
string host = 2;
|
||||
string token = 3;
|
||||
}
|
||||
|
||||
message ChannelUpdate {
|
||||
|
||||
@@ -210,9 +210,14 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
void set_token(const std::string& token) {
|
||||
_request.set_token(token);
|
||||
}
|
||||
|
||||
std::string compact_log_format() const {
|
||||
ostringstream log;
|
||||
log << "{";
|
||||
log << std::endl << "token: " << _request.token();
|
||||
for (const auto& ch : _request.channels()) {
|
||||
log << std::endl;
|
||||
log << ch.symbol() << " ";
|
||||
@@ -415,6 +420,12 @@ void add_relay_symbol(RelayControl* relay, const SecurityChannel& chan) {
|
||||
relay->client.add_symbol(chan);
|
||||
});
|
||||
}
|
||||
void set_relay_token(RelayControl* relay, const std::string& token) {
|
||||
bts_assert(relay);
|
||||
relay->context.post([=]() {
|
||||
relay->client.set_token(token);
|
||||
});
|
||||
}
|
||||
|
||||
utils::Dispatcher& scan_securities_thread() {
|
||||
return utils::Dispatcher::background();
|
||||
|
||||
@@ -21,6 +21,7 @@ namespace md {
|
||||
|
||||
struct RelayControl;
|
||||
void add_relay_symbol(RelayControl* relay, const co2::SecurityChannel& chan);
|
||||
void set_relay_token(RelayControl* relay, const std::string& token);
|
||||
|
||||
struct IRelayWriter {
|
||||
virtual ~IRelayWriter() {};
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "lib/api/services/UserService.pb.h"
|
||||
#include "lib/api/services/AdminService.client.pb.h"
|
||||
#include "lib/utils/PBUtils.h"
|
||||
#include "lib/utils/base64.hpp"
|
||||
|
||||
#include <boost/tokenizer.hpp>
|
||||
#include <fmt/format.h>
|
||||
@@ -24,8 +25,11 @@ struct UserServiceServer::PImpl
|
||||
PImpl(bts::api::ServiceContext& context) :
|
||||
admin(context.locate<api::admin::AdminService>()) { }
|
||||
|
||||
std::string generate_auth_token(const api::admin::BTSUser&) const {
|
||||
return "BT5p@rk";
|
||||
std::string generate_auth_token(const api::admin::BTSUser& user) const {
|
||||
api::user::TokenPayload payload;
|
||||
payload.set_date(datetime::ll_now());
|
||||
payload.set_id(user.id());
|
||||
return websocketpp::base64_encode(payload.SerializeAsString());
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user