Added deviceid query and login

This commit is contained in:
timoreo 2023-12-09 14:39:57 +01:00
parent 3e1a13590a
commit 833de44d81
Signed by: timoreo
GPG key ID: 121A72C3512BA288
2 changed files with 33 additions and 8 deletions

View file

@ -43,7 +43,8 @@ private:
std::string hsUrl; std::string hsUrl;
std::string token; std::string token;
Store* store; Store* store;
std::string userIdCache = ""; std::string userIdCache;
std::string deviceIdCache;
int requestId = 0; int requestId = 0;
bool stopSyncing = false; bool stopSyncing = false;
bool isSyncing = false; bool isSyncing = false;
@ -62,10 +63,11 @@ private:
json_t* doRequestCurl(const char* method, std::string url, json_t* body, u32 timeout, CURLcode* retRes); json_t* doRequestCurl(const char* method, std::string url, json_t* body, u32 timeout, CURLcode* retRes);
public: public:
Client(std::string homeserverUrl, std::string matrixToken = "", Store* clientStore = NULL); Client(std::string homeserverUrl, std::string matrixToken = "", Store* clientStore = NULL);
std::string getToken(); std::string getToken() const;
bool login(std::string username, std::string password); bool login(std::string username, std::string password, std::string device_id="");
void logout(); void logout();
std::string getUserId(); std::string getUserId();
std::string getDeviceId();
std::string resolveRoom(std::string alias); std::string resolveRoom(std::string alias);
std::vector<std::string> getJoinedRooms(); std::vector<std::string> getJoinedRooms();
RoomInfo getRoomInfo(std::string roomId); RoomInfo getRoomInfo(std::string roomId);

View file

@ -57,11 +57,11 @@ Client::Client(std::string homeserverUrl, std::string matrixToken, Store* client
#endif #endif
} }
std::string Client::getToken() { std::string Client::getToken() const {
return token; return token;
} }
bool Client::login(std::string username, std::string password) { bool Client::login(std::string username, std::string password, std::string device_id) {
json_t* request = json_object(); json_t* request = json_object();
json_object_set_new(request, "type", json_string("m.login.password")); json_object_set_new(request, "type", json_string("m.login.password"));
json_t* identifier = json_object(); json_t* identifier = json_object();
@ -70,10 +70,12 @@ bool Client::login(std::string username, std::string password) {
json_object_set_new(request, "identifier", identifier); json_object_set_new(request, "identifier", identifier);
json_object_set_new(request, "password", json_string(password.c_str())); json_object_set_new(request, "password", json_string(password.c_str()));
json_object_set_new(request, "initial_device_display_name", json_string("Nintendo 3DS")); json_object_set_new(request, "initial_device_display_name", json_string("Nintendo 3DS"));
if(!device_id.empty()) {
json_object_set_new(request, "device_id", json_string(device_id.c_str()));
}
json_t* ret = doRequest("POST", "/_matrix/client/r0/login", request); json_t* ret = doRequest("POST", "/_matrix/client/r0/login", request);
json_decref(request); json_decref(request);
printf_top("Result : %s\n" ,json_dumps(ret, JSON_ENSURE_ASCII | JSON_ESCAPE_SLASH));
const char* tokenCStr = json_object_get_string_value(ret, "access_token"); const char* tokenCStr = json_object_get_string_value(ret, "access_token");
if (!tokenCStr) { if (!tokenCStr) {
if (ret) json_decref(ret); if (ret) json_decref(ret);
@ -92,7 +94,7 @@ void Client::logout() {
} }
std::string Client::getUserId() { std::string Client::getUserId() {
if (userIdCache != "") { if (!userIdCache.empty()) {
return userIdCache; return userIdCache;
} }
json_t* ret = doRequest("GET", "/_matrix/client/r0/account/whoami"); json_t* ret = doRequest("GET", "/_matrix/client/r0/account/whoami");
@ -101,11 +103,32 @@ std::string Client::getUserId() {
if (ret) json_decref(ret); if (ret) json_decref(ret);
return ""; return "";
} }
const char* deviceIdCStr = json_object_get_string_value(ret, "device_id");
std::string deviceIdStr = deviceIdCStr;
std::string userIdStr = userIdCStr; std::string userIdStr = userIdCStr;
json_decref(ret); json_decref(ret);
userIdCache = std::string(userIdStr); userIdCache = std::string(userIdStr);
deviceIdCache = std::string(deviceIdStr);
return userIdCache; return userIdCache;
} }
std::string Client::getDeviceId() {
if(!deviceIdCache.empty()){
return deviceIdCache;
}
json_t* ret = doRequest("GET", "/_matrix/client/r0/account/whoami");
const char* userIdCStr = json_object_get_string_value(ret, "user_id");
if (!userIdCStr) {
if (ret) json_decref(ret);
return "";
}
const char* deviceIdCStr = json_object_get_string_value(ret, "device_id");
std::string deviceIdStr = deviceIdCStr;
std::string userIdStr = userIdCStr;
json_decref(ret);
userIdCache = std::string(userIdStr);
deviceIdCache = std::string(deviceIdStr);
return deviceIdCache;
}
std::string Client::resolveRoom(std::string alias) { std::string Client::resolveRoom(std::string alias) {
if (alias[0] == '!') { if (alias[0] == '!') {