Move access token to header

remotes/origin/HEAD
Konstantinos Sideris 7 years ago
parent 7f69c71814
commit 916c0d48af
  1. 4
      include/MatrixClient.h
  2. 88
      src/MatrixClient.cc

@ -181,6 +181,10 @@ signals:
private: private:
QNetworkReply *makeUploadRequest(QSharedPointer<QIODevice> iodev); QNetworkReply *makeUploadRequest(QSharedPointer<QIODevice> iodev);
QJsonObject getUploadReply(QNetworkReply *reply); QJsonObject getUploadReply(QNetworkReply *reply);
void setupAuth(QNetworkRequest &req)
{
req.setRawHeader("Authorization", QString("Bearer %1").arg(token_).toLocal8Bit());
}
// Client API prefix. // Client API prefix.
QString clientApiUrl_; QString clientApiUrl_;

@ -182,15 +182,12 @@ MatrixClient::login(const QString &username, const QString &password) noexcept
void void
MatrixClient::logout() noexcept MatrixClient::logout() noexcept
{ {
QUrlQuery query;
query.addQueryItem("access_token", token_);
QUrl endpoint(server_); QUrl endpoint(server_);
endpoint.setPath(clientApiUrl_ + "/logout"); endpoint.setPath(clientApiUrl_ + "/logout");
endpoint.setQuery(query);
QNetworkRequest request(endpoint); QNetworkRequest request(endpoint);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json"); request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
setupAuth(request);
QJsonObject body{}; QJsonObject body{};
auto reply = post(request, QJsonDocument(body).toJson(QJsonDocument::Compact)); auto reply = post(request, QJsonDocument(body).toJson(QJsonDocument::Compact));
@ -288,7 +285,6 @@ MatrixClient::sync() noexcept
query.addQueryItem("set_presence", "online"); query.addQueryItem("set_presence", "online");
query.addQueryItem("filter", filter_); query.addQueryItem("filter", filter_);
query.addQueryItem("timeout", "30000"); query.addQueryItem("timeout", "30000");
query.addQueryItem("access_token", token_);
if (next_batch_.isEmpty()) { if (next_batch_.isEmpty()) {
qDebug() << "Sync requires a valid next_batch token. Initial sync should " qDebug() << "Sync requires a valid next_batch token. Initial sync should "
@ -303,6 +299,7 @@ MatrixClient::sync() noexcept
endpoint.setQuery(query); endpoint.setQuery(query);
QNetworkRequest request(QString(endpoint.toEncoded())); QNetworkRequest request(QString(endpoint.toEncoded()));
setupAuth(request);
auto reply = get(request); auto reply = get(request);
connect(reply, &QNetworkReply::finished, this, [this, reply]() { connect(reply, &QNetworkReply::finished, this, [this, reply]() {
@ -346,13 +343,9 @@ MatrixClient::sendRoomMessage(mtx::events::MessageType ty,
uint64_t media_size, uint64_t media_size,
const QString &url) noexcept const QString &url) noexcept
{ {
QUrlQuery query;
query.addQueryItem("access_token", token_);
QUrl endpoint(server_); QUrl endpoint(server_);
endpoint.setPath(clientApiUrl_ + endpoint.setPath(clientApiUrl_ +
QString("/rooms/%1/send/m.room.message/%2").arg(roomid).arg(txnId)); QString("/rooms/%1/send/m.room.message/%2").arg(roomid).arg(txnId));
endpoint.setQuery(query);
QJsonObject body; QJsonObject body;
QJsonObject info = {{"size", static_cast<qint64>(media_size)}, {"mimetype", mime}}; QJsonObject info = {{"size", static_cast<qint64>(media_size)}, {"mimetype", mime}};
@ -383,6 +376,7 @@ MatrixClient::sendRoomMessage(mtx::events::MessageType ty,
QNetworkRequest request(QString(endpoint.toEncoded())); QNetworkRequest request(QString(endpoint.toEncoded()));
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json"); request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
setupAuth(request);
auto reply = put(request, QJsonDocument(body).toJson(QJsonDocument::Compact)); auto reply = put(request, QJsonDocument(body).toJson(QJsonDocument::Compact));
@ -429,13 +423,13 @@ MatrixClient::initialSync() noexcept
QUrlQuery query; QUrlQuery query;
query.addQueryItem("timeout", "0"); query.addQueryItem("timeout", "0");
query.addQueryItem("filter", filter_); query.addQueryItem("filter", filter_);
query.addQueryItem("access_token", token_);
QUrl endpoint(server_); QUrl endpoint(server_);
endpoint.setPath(clientApiUrl_ + "/sync"); endpoint.setPath(clientApiUrl_ + "/sync");
endpoint.setQuery(query); endpoint.setQuery(query);
QNetworkRequest request(QString(endpoint.toEncoded())); QNetworkRequest request(QString(endpoint.toEncoded()));
setupAuth(request);
auto reply = get(request); auto reply = get(request);
connect(reply, &QNetworkReply::finished, this, [this, reply]() { connect(reply, &QNetworkReply::finished, this, [this, reply]() {
@ -511,14 +505,11 @@ MatrixClient::getOwnProfile() noexcept
QSettings settings; QSettings settings;
auto userid = settings.value("auth/user_id", "").toString(); auto userid = settings.value("auth/user_id", "").toString();
QUrlQuery query;
query.addQueryItem("access_token", token_);
QUrl endpoint(server_); QUrl endpoint(server_);
endpoint.setPath(clientApiUrl_ + "/profile/" + userid); endpoint.setPath(clientApiUrl_ + "/profile/" + userid);
endpoint.setQuery(query);
QNetworkRequest request(QString(endpoint.toEncoded())); QNetworkRequest request(QString(endpoint.toEncoded()));
setupAuth(request);
QNetworkReply *reply = get(request); QNetworkReply *reply = get(request);
connect(reply, &QNetworkReply::finished, this, [this, reply]() { connect(reply, &QNetworkReply::finished, this, [this, reply]() {
@ -546,14 +537,11 @@ MatrixClient::getOwnProfile() noexcept
void void
MatrixClient::getOwnCommunities() noexcept MatrixClient::getOwnCommunities() noexcept
{ {
QUrlQuery query;
query.addQueryItem("access_token", token_);
QUrl endpoint(server_); QUrl endpoint(server_);
endpoint.setPath(clientApiUrl_ + "/joined_groups"); endpoint.setPath(clientApiUrl_ + "/joined_groups");
endpoint.setQuery(query);
QNetworkRequest request(QString(endpoint.toEncoded())); QNetworkRequest request(QString(endpoint.toEncoded()));
setupAuth(request);
QNetworkReply *reply = get(request); QNetworkReply *reply = get(request);
connect(reply, &QNetworkReply::finished, this, [this, reply]() { connect(reply, &QNetworkReply::finished, this, [this, reply]() {
@ -680,14 +668,11 @@ MatrixClient::fetchCommunityAvatar(const QString &communityId, const QUrl &avata
void void
MatrixClient::fetchCommunityProfile(const QString &communityId) MatrixClient::fetchCommunityProfile(const QString &communityId)
{ {
QUrlQuery query;
query.addQueryItem("access_token", token_);
QUrl endpoint(server_); QUrl endpoint(server_);
endpoint.setPath(clientApiUrl_ + "/groups/" + communityId + "/profile"); endpoint.setPath(clientApiUrl_ + "/groups/" + communityId + "/profile");
endpoint.setQuery(query);
QNetworkRequest request(QString(endpoint.toEncoded())); QNetworkRequest request(QString(endpoint.toEncoded()));
setupAuth(request);
QNetworkReply *reply = get(request); QNetworkReply *reply = get(request);
@ -711,14 +696,11 @@ MatrixClient::fetchCommunityProfile(const QString &communityId)
void void
MatrixClient::fetchCommunityRooms(const QString &communityId) MatrixClient::fetchCommunityRooms(const QString &communityId)
{ {
QUrlQuery query;
query.addQueryItem("access_token", token_);
QUrl endpoint(server_); QUrl endpoint(server_);
endpoint.setPath(clientApiUrl_ + "/groups/" + communityId + "/rooms"); endpoint.setPath(clientApiUrl_ + "/groups/" + communityId + "/rooms");
endpoint.setQuery(query);
QNetworkRequest request(QString(endpoint.toEncoded())); QNetworkRequest request(QString(endpoint.toEncoded()));
setupAuth(request);
QNetworkReply *reply = get(request); QNetworkReply *reply = get(request);
connect(reply, &QNetworkReply::finished, this, [this, reply, communityId]() { connect(reply, &QNetworkReply::finished, this, [this, reply, communityId]() {
@ -854,7 +836,6 @@ void
MatrixClient::messages(const QString &roomid, const QString &from_token, int limit) noexcept MatrixClient::messages(const QString &roomid, const QString &from_token, int limit) noexcept
{ {
QUrlQuery query; QUrlQuery query;
query.addQueryItem("access_token", token_);
query.addQueryItem("from", from_token); query.addQueryItem("from", from_token);
query.addQueryItem("dir", "b"); query.addQueryItem("dir", "b");
query.addQueryItem("limit", QString::number(limit)); query.addQueryItem("limit", QString::number(limit));
@ -864,6 +845,7 @@ MatrixClient::messages(const QString &roomid, const QString &from_token, int lim
endpoint.setQuery(query); endpoint.setQuery(query);
QNetworkRequest request(QString(endpoint.toEncoded())); QNetworkRequest request(QString(endpoint.toEncoded()));
setupAuth(request);
auto reply = get(request); auto reply = get(request);
connect(reply, &QNetworkReply::finished, this, [this, reply, roomid]() { connect(reply, &QNetworkReply::finished, this, [this, reply, roomid]() {
@ -997,15 +979,12 @@ MatrixClient::uploadFilter(const QString &filter) noexcept
QSettings settings; QSettings settings;
auto userid = settings.value("auth/user_id", "").toString(); auto userid = settings.value("auth/user_id", "").toString();
QUrlQuery query;
query.addQueryItem("access_token", token_);
QUrl endpoint(server_); QUrl endpoint(server_);
endpoint.setPath(clientApiUrl_ + QString("/user/%1/filter").arg(userid)); endpoint.setPath(clientApiUrl_ + QString("/user/%1/filter").arg(userid));
endpoint.setQuery(query);
QNetworkRequest request(endpoint); QNetworkRequest request(endpoint);
request.setHeader(QNetworkRequest::KnownHeaders::ContentTypeHeader, "application/json"); request.setHeader(QNetworkRequest::KnownHeaders::ContentTypeHeader, "application/json");
setupAuth(request);
auto reply = post(request, doc.toJson(QJsonDocument::Compact)); auto reply = post(request, doc.toJson(QJsonDocument::Compact));
@ -1036,15 +1015,12 @@ MatrixClient::uploadFilter(const QString &filter) noexcept
void void
MatrixClient::joinRoom(const QString &roomIdOrAlias) MatrixClient::joinRoom(const QString &roomIdOrAlias)
{ {
QUrlQuery query;
query.addQueryItem("access_token", token_);
QUrl endpoint(server_); QUrl endpoint(server_);
endpoint.setPath(clientApiUrl_ + QString("/join/%1").arg(roomIdOrAlias)); endpoint.setPath(clientApiUrl_ + QString("/join/%1").arg(roomIdOrAlias));
endpoint.setQuery(query);
QNetworkRequest request(endpoint); QNetworkRequest request(endpoint);
request.setHeader(QNetworkRequest::KnownHeaders::ContentTypeHeader, "application/json"); request.setHeader(QNetworkRequest::KnownHeaders::ContentTypeHeader, "application/json");
setupAuth(request);
auto reply = post(request, "{}"); auto reply = post(request, "{}");
connect(reply, &QNetworkReply::finished, this, [this, reply]() { connect(reply, &QNetworkReply::finished, this, [this, reply]() {
@ -1076,15 +1052,12 @@ MatrixClient::joinRoom(const QString &roomIdOrAlias)
void void
MatrixClient::leaveRoom(const QString &roomId) MatrixClient::leaveRoom(const QString &roomId)
{ {
QUrlQuery query;
query.addQueryItem("access_token", token_);
QUrl endpoint(server_); QUrl endpoint(server_);
endpoint.setPath(clientApiUrl_ + QString("/rooms/%1/leave").arg(roomId)); endpoint.setPath(clientApiUrl_ + QString("/rooms/%1/leave").arg(roomId));
endpoint.setQuery(query);
QNetworkRequest request(endpoint); QNetworkRequest request(endpoint);
request.setHeader(QNetworkRequest::KnownHeaders::ContentTypeHeader, "application/json"); request.setHeader(QNetworkRequest::KnownHeaders::ContentTypeHeader, "application/json");
setupAuth(request);
auto reply = post(request, "{}"); auto reply = post(request, "{}");
@ -1105,15 +1078,12 @@ MatrixClient::leaveRoom(const QString &roomId)
void void
MatrixClient::inviteUser(const QString &roomId, const QString &user) MatrixClient::inviteUser(const QString &roomId, const QString &user)
{ {
QUrlQuery query;
query.addQueryItem("access_token", token_);
QUrl endpoint(server_); QUrl endpoint(server_);
endpoint.setPath(clientApiUrl_ + QString("/rooms/%1/invite").arg(roomId)); endpoint.setPath(clientApiUrl_ + QString("/rooms/%1/invite").arg(roomId));
endpoint.setQuery(query);
QNetworkRequest request(endpoint); QNetworkRequest request(endpoint);
request.setHeader(QNetworkRequest::KnownHeaders::ContentTypeHeader, "application/json"); request.setHeader(QNetworkRequest::KnownHeaders::ContentTypeHeader, "application/json");
setupAuth(request);
QJsonObject body{{"user_id", user}}; QJsonObject body{{"user_id", user}};
auto reply = post(request, QJsonDocument(body).toJson(QJsonDocument::Compact)); auto reply = post(request, QJsonDocument(body).toJson(QJsonDocument::Compact));
@ -1136,15 +1106,12 @@ MatrixClient::inviteUser(const QString &roomId, const QString &user)
void void
MatrixClient::createRoom(const mtx::requests::CreateRoom &create_room_request) MatrixClient::createRoom(const mtx::requests::CreateRoom &create_room_request)
{ {
QUrlQuery query;
query.addQueryItem("access_token", token_);
QUrl endpoint(server_); QUrl endpoint(server_);
endpoint.setPath(clientApiUrl_ + QString("/createRoom")); endpoint.setPath(clientApiUrl_ + QString("/createRoom"));
endpoint.setQuery(query);
QNetworkRequest request(endpoint); QNetworkRequest request(endpoint);
request.setHeader(QNetworkRequest::KnownHeaders::ContentTypeHeader, "application/json"); request.setHeader(QNetworkRequest::KnownHeaders::ContentTypeHeader, "application/json");
setupAuth(request);
nlohmann::json body = create_room_request; nlohmann::json body = create_room_request;
auto reply = post(request, QString::fromStdString(body.dump()).toUtf8()); auto reply = post(request, QString::fromStdString(body.dump()).toUtf8());
@ -1181,14 +1148,9 @@ MatrixClient::sendTypingNotification(const QString &roomid, int timeoutInMillis)
QSettings settings; QSettings settings;
QString user_id = settings.value("auth/user_id").toString(); QString user_id = settings.value("auth/user_id").toString();
QUrlQuery query;
query.addQueryItem("access_token", token_);
QUrl endpoint(server_); QUrl endpoint(server_);
endpoint.setPath(clientApiUrl_ + QString("/rooms/%1/typing/%2").arg(roomid).arg(user_id)); endpoint.setPath(clientApiUrl_ + QString("/rooms/%1/typing/%2").arg(roomid).arg(user_id));
endpoint.setQuery(query);
QString msgType(""); QString msgType("");
QJsonObject body; QJsonObject body;
@ -1196,6 +1158,7 @@ MatrixClient::sendTypingNotification(const QString &roomid, int timeoutInMillis)
QNetworkRequest request(QString(endpoint.toEncoded())); QNetworkRequest request(QString(endpoint.toEncoded()));
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json"); request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
setupAuth(request);
put(request, QJsonDocument(body).toJson(QJsonDocument::Compact)); put(request, QJsonDocument(body).toJson(QJsonDocument::Compact));
} }
@ -1206,14 +1169,9 @@ MatrixClient::removeTypingNotification(const QString &roomid)
QSettings settings; QSettings settings;
QString user_id = settings.value("auth/user_id").toString(); QString user_id = settings.value("auth/user_id").toString();
QUrlQuery query;
query.addQueryItem("access_token", token_);
QUrl endpoint(server_); QUrl endpoint(server_);
endpoint.setPath(clientApiUrl_ + QString("/rooms/%1/typing/%2").arg(roomid).arg(user_id)); endpoint.setPath(clientApiUrl_ + QString("/rooms/%1/typing/%2").arg(roomid).arg(user_id));
endpoint.setQuery(query);
QString msgType(""); QString msgType("");
QJsonObject body; QJsonObject body;
@ -1221,6 +1179,7 @@ MatrixClient::removeTypingNotification(const QString &roomid)
QNetworkRequest request(QString(endpoint.toEncoded())); QNetworkRequest request(QString(endpoint.toEncoded()));
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json"); request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
setupAuth(request);
put(request, QJsonDocument(body).toJson(QJsonDocument::Compact)); put(request, QJsonDocument(body).toJson(QJsonDocument::Compact));
} }
@ -1228,15 +1187,12 @@ MatrixClient::removeTypingNotification(const QString &roomid)
void void
MatrixClient::readEvent(const QString &room_id, const QString &event_id) MatrixClient::readEvent(const QString &room_id, const QString &event_id)
{ {
QUrlQuery query;
query.addQueryItem("access_token", token_);
QUrl endpoint(server_); QUrl endpoint(server_);
endpoint.setPath(clientApiUrl_ + QString("/rooms/%1/read_markers").arg(room_id)); endpoint.setPath(clientApiUrl_ + QString("/rooms/%1/read_markers").arg(room_id));
endpoint.setQuery(query);
QNetworkRequest request(QString(endpoint.toEncoded())); QNetworkRequest request(QString(endpoint.toEncoded()));
request.setHeader(QNetworkRequest::KnownHeaders::ContentTypeHeader, "application/json"); request.setHeader(QNetworkRequest::KnownHeaders::ContentTypeHeader, "application/json");
setupAuth(request);
QJsonObject body({{"m.fully_read", event_id}, {"m.read", event_id}}); QJsonObject body({{"m.fully_read", event_id}, {"m.read", event_id}});
auto reply = post(request, QJsonDocument(body).toJson(QJsonDocument::Compact)); auto reply = post(request, QJsonDocument(body).toJson(QJsonDocument::Compact));
@ -1256,12 +1212,8 @@ MatrixClient::readEvent(const QString &room_id, const QString &event_id)
QNetworkReply * QNetworkReply *
MatrixClient::makeUploadRequest(QSharedPointer<QIODevice> iodev) MatrixClient::makeUploadRequest(QSharedPointer<QIODevice> iodev)
{ {
QUrlQuery query;
query.addQueryItem("access_token", token_);
QUrl endpoint(server_); QUrl endpoint(server_);
endpoint.setPath(mediaApiUrl_ + "/upload"); endpoint.setPath(mediaApiUrl_ + "/upload");
endpoint.setQuery(query);
if (!iodev->open(QIODevice::ReadOnly)) { if (!iodev->open(QIODevice::ReadOnly)) {
qWarning() << "Error while reading device:" << iodev->errorString(); qWarning() << "Error while reading device:" << iodev->errorString();
@ -1273,6 +1225,7 @@ MatrixClient::makeUploadRequest(QSharedPointer<QIODevice> iodev)
QNetworkRequest request(QString(endpoint.toEncoded())); QNetworkRequest request(QString(endpoint.toEncoded()));
request.setHeader(QNetworkRequest::ContentTypeHeader, mime.name()); request.setHeader(QNetworkRequest::ContentTypeHeader, mime.name());
setupAuth(request);
auto reply = post(request, iodev.data()); auto reply = post(request, iodev.data());
@ -1320,18 +1273,15 @@ MatrixClient::getUploadReply(QNetworkReply *reply)
void void
MatrixClient::redactEvent(const QString &room_id, const QString &event_id) MatrixClient::redactEvent(const QString &room_id, const QString &event_id)
{ {
QUrlQuery query;
query.addQueryItem("access_token", token_);
QUrl endpoint(server_); QUrl endpoint(server_);
endpoint.setPath(clientApiUrl_ + QString("/rooms/%1/redact/%2/%3") endpoint.setPath(clientApiUrl_ + QString("/rooms/%1/redact/%2/%3")
.arg(room_id) .arg(room_id)
.arg(event_id) .arg(event_id)
.arg(incrementTransactionId())); .arg(incrementTransactionId()));
endpoint.setQuery(query);
QNetworkRequest request(QString(endpoint.toEncoded())); QNetworkRequest request(QString(endpoint.toEncoded()));
request.setHeader(QNetworkRequest::KnownHeaders::ContentTypeHeader, "application/json"); request.setHeader(QNetworkRequest::KnownHeaders::ContentTypeHeader, "application/json");
setupAuth(request);
// TODO: no reason specified // TODO: no reason specified
QJsonObject body{}; QJsonObject body{};

Loading…
Cancel
Save