mirror of
https://github.com/allankoechke/pocketbase-cpp.git
synced 2026-12-01 01:18:25 +00:00
Authentication endpoints updated [Verify user, request email verification, password reset, etc]
This commit is contained in:
@@ -14,15 +14,17 @@ find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core Network)
|
||||
|
||||
add_executable(pocketbase-cpp
|
||||
main.cpp
|
||||
sdk/globals.h
|
||||
sdk/client.h sdk/client.cpp
|
||||
sdk/services/recordservice.h sdk/services/recordservice.cpp
|
||||
sdk/services/basecrudservice.h sdk/services/basecrudservice.cpp
|
||||
sdk/services/crudservice.h sdk/services/crudservice.cpp
|
||||
sdk/services/baseservice.h
|
||||
sdk/services/baseauthstore.h sdk/services/baseauthstore.cpp
|
||||
sdk/dtos/basemodel.h sdk/dtos/basemodel.cpp
|
||||
sdk/dtos/listresult.h sdk/dtos/listresult.cpp
|
||||
sdk/dtos/recordmodel.h
|
||||
|
||||
# sdk/baseauthstore.h sdk/baseauthstore.cpp
|
||||
# sdk/adminservice.h sdk/adminservice.cpp
|
||||
# sdk/collectionservice.h sdk/collectionservice.cpp
|
||||
# sdk/localauthstore.h sdk/localauthstore.cpp
|
||||
@@ -30,8 +32,6 @@ add_executable(pocketbase-cpp
|
||||
# sdk/utils/clientresponseerror.h
|
||||
# sdk/models/AdminModel.h
|
||||
# sdk/models/collectionmodel.hpp
|
||||
# sdk/models/recordmodel.hpp
|
||||
# sdk/utils/utils.h
|
||||
)
|
||||
target_link_libraries(pocketbase-cpp Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Network)
|
||||
|
||||
|
||||
23
main.cpp
23
main.cpp
@@ -1,14 +1,31 @@
|
||||
#include <QCoreApplication>
|
||||
#include "sdk/client.h"
|
||||
// #include "sdk/RecordService.h"
|
||||
#include "sdk/services/recordservice.h"
|
||||
#include "sdk/services/baseauthstore.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QCoreApplication a(argc, argv);
|
||||
|
||||
PocketBase client{"http://127.0.0.1:5740/"};
|
||||
qDebug() << client.lang();
|
||||
// client.collection("users")->authWithPassword("user@email.com", "123456789");
|
||||
|
||||
// Auth with password
|
||||
client.collection("users")->authWithPassword("test@user.com", "123456789");
|
||||
// qDebug() << client.authStore()->isValid() << user.isValid();
|
||||
// qDebug() << client.authStore()->token();
|
||||
// qDebug() << client.authStore()->model()->getId();
|
||||
// qDebug() << client.authStore()->model()->repr();
|
||||
|
||||
// Auth Refresh
|
||||
//auto refreshUser = client.collection("users")->authRefresh();
|
||||
//qDebug() << refreshUser.isValid();
|
||||
|
||||
// Request Verification
|
||||
// client.collection("users")->requestVerification("allankoech.ke@gmail.com");
|
||||
|
||||
qDebug() << client.collection("users")->confirmVerification("12345");
|
||||
|
||||
qDebug() << client.collection("users")->requestPasswordReset("allankoech.ke@gmail.com");
|
||||
|
||||
return a.exec();
|
||||
}
|
||||
|
||||
103
sdk/client.cpp
103
sdk/client.cpp
@@ -1,34 +1,29 @@
|
||||
#include "client.h"
|
||||
#include "services/recordservice.h"
|
||||
#include "services/baseauthstore.h"
|
||||
|
||||
PocketBase::PocketBase(const QString& baseUrl, const QString& lang, int timeout, QObject* parent)
|
||||
: QObject(parent),
|
||||
m_baseurl(baseUrl),
|
||||
m_lang(lang),
|
||||
m_timeout(timeout),
|
||||
m_networkManager(new QNetworkAccessManager(this)),
|
||||
m_lang(lang),
|
||||
a(12),
|
||||
b(13)
|
||||
{
|
||||
setBaseUrl(baseUrl);
|
||||
qDebug() << baseUrl;
|
||||
m_authStore(new BaseAuthStore("", nullptr, this)) {}
|
||||
|
||||
RecordService* PocketBase::collection(const QString& idOrName) {
|
||||
if (!m_recordServices.contains(idOrName)) {
|
||||
m_recordServices[idOrName] = new RecordService(this, idOrName);
|
||||
}
|
||||
|
||||
return m_recordServices[idOrName];
|
||||
}
|
||||
|
||||
// std::shared_ptr<RecordService> PocketBase::collection(const QString& idOrName) {
|
||||
// qDebug() << "Collection init [" << idOrName << "]";
|
||||
// if (!recordServices.contains(idOrName)) {
|
||||
// qDebug() << "[RecordService] No record, creating new one ...";
|
||||
// recordServices[idOrName] = std::make_shared<RecordService>(this, idOrName);
|
||||
// qDebug() << "[RecordService] New instance created ...";
|
||||
// }
|
||||
|
||||
// return recordServices[idOrName];
|
||||
// }
|
||||
|
||||
QString PocketBase::filter(const QString &expr, const QMap<QString, QVariant> &query) {
|
||||
if (query.isEmpty()) {
|
||||
return expr;
|
||||
}
|
||||
|
||||
for( const auto key : query.keys()) {
|
||||
for( const auto &key : query.keys()) {
|
||||
auto value = query.value( key );
|
||||
|
||||
// if ( value.isNull() || value is num || value is bool) {
|
||||
@@ -47,35 +42,29 @@ QString PocketBase::filter(const QString &expr, const QMap<QString, QVariant> &q
|
||||
}
|
||||
|
||||
QUrl PocketBase::buildUrl(const QString& path) {
|
||||
qDebug() << path;
|
||||
qDebug() << "--";
|
||||
// qDebug() << m_baseurl;
|
||||
qDebug() << "--";
|
||||
QUrl url(m_baseurl);
|
||||
|
||||
QUrl url("http://127.0.0.1:5740/"); // m_baseurl);
|
||||
if (!url.path().endsWith("/")) {
|
||||
url.setPath(url.path() + "/");
|
||||
}
|
||||
|
||||
url.setPath(url.path() + path);
|
||||
if(path.startsWith("/"))
|
||||
url.setPath(url.path() + path.mid(1));
|
||||
else
|
||||
url.setPath(url.path() + path);
|
||||
|
||||
return url;
|
||||
}
|
||||
|
||||
QNetworkReply* PocketBase::send(const QString& path, const QJsonObject params) {
|
||||
qDebug() << path;
|
||||
qDebug() << baseUrl();
|
||||
qDebug() << "Accessing the baseURL";
|
||||
setLang("something");
|
||||
qDebug() << "Lang set";
|
||||
a = 5;
|
||||
qDebug() << "Getting a";
|
||||
qDebug() << a;
|
||||
qDebug() << b;
|
||||
qDebug() << m_timeout;
|
||||
qDebug() << m_lang;
|
||||
qDebug() << QString(m_baseurl);
|
||||
BaseAuthStore *PocketBase::authStore() const { return m_authStore; }
|
||||
|
||||
QJsonObject PocketBase::send(const QString& path, const QJsonObject params) {
|
||||
QUrl url = buildUrl(path);
|
||||
qDebug() << "[PocketBase] Making a (" << params.value("method").toString() << ") request to '" << url.toString() << "'";
|
||||
|
||||
|
||||
QNetworkRequest request(url);
|
||||
request.setRawHeader("Content-Type", "application/json");
|
||||
|
||||
// If there are query parameters, pass them into the URL
|
||||
if( params.contains("query") && !params.value("query").isNull() ) {
|
||||
@@ -83,13 +72,12 @@ QNetworkReply* PocketBase::send(const QString& path, const QJsonObject params) {
|
||||
/// TODO
|
||||
}
|
||||
|
||||
|
||||
QNetworkRequest request(url);
|
||||
|
||||
// Add headers and other request configuration
|
||||
// if (m_authStore->token().isEmpty() && !request.hasRawHeader("Authorization")) {
|
||||
// request.setRawHeader("Authorization", m_authStore->token().toUtf8());
|
||||
// }
|
||||
if( params.contains("headers") ) {
|
||||
auto headers = params.value("headers").toObject();
|
||||
if( headers.contains("auth") && headers.value("auth").toBool() ) {
|
||||
request.setRawHeader("Authorization", "Bearer " + m_authStore->token().toUtf8());
|
||||
}
|
||||
}
|
||||
|
||||
// QJsonDocument jsonDoc(reqConfig);
|
||||
// QByteArray jsonData = jsonDoc.toJson();
|
||||
@@ -99,13 +87,30 @@ QNetworkReply* PocketBase::send(const QString& path, const QJsonObject params) {
|
||||
|
||||
if(params.value("method").toString() == "GET") {
|
||||
reply = m_networkManager->get(request);
|
||||
} else if(params.value("method").toString() == "GET") {
|
||||
} else if(params.value("method").toString() == "POST") {
|
||||
reply = m_networkManager->post(request, doc.toJson());
|
||||
} else {
|
||||
qDebug() << "Unhandled: " << params.value("method").toString();
|
||||
return QJsonObject();
|
||||
}
|
||||
|
||||
return reply;
|
||||
QEventLoop wait_loop;
|
||||
connect(reply, &QNetworkReply::finished, &wait_loop, &QEventLoop::quit);
|
||||
wait_loop.exec();
|
||||
|
||||
int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||
QJsonDocument resJsonDoc = QJsonDocument::fromJson(reply->readAll());
|
||||
// qDebug() << "res: " << resJsonDoc;
|
||||
|
||||
QJsonObject responseObject;
|
||||
responseObject.insert("statusCode", statusCode);
|
||||
responseObject.insert("data", resJsonDoc.object());
|
||||
|
||||
if( reply->error() != QNetworkReply::NoError ) {
|
||||
responseObject.insert("error", reply->errorString());
|
||||
}
|
||||
|
||||
return responseObject;
|
||||
}
|
||||
|
||||
// QUrl PocketBase::getFileUrl(const RecordService& record, const QString& filename, const QMap<QString, QString>& queryParams) {
|
||||
@@ -125,11 +130,11 @@ QString PocketBase::getFileToken() {
|
||||
QJsonObject params;
|
||||
params.insert("method", "POST");
|
||||
|
||||
QNetworkReply* reply = send("/api/files/token", params);
|
||||
auto reply = send("/api/files/token", params);
|
||||
|
||||
auto data = reply->readAll().data();
|
||||
// auto data = reply->readAll().data();
|
||||
|
||||
qDebug() << "Reply [File Token] " << data;
|
||||
//qDebug() << "Reply [File Token] " << data;
|
||||
|
||||
// Handle reply and extract token
|
||||
|
||||
|
||||
16
sdk/client.h
16
sdk/client.h
@@ -15,6 +15,9 @@
|
||||
#include <QJsonDocument>
|
||||
#include <QDebug>
|
||||
|
||||
class RecordService;
|
||||
class BaseAuthStore;
|
||||
|
||||
class PocketBase : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
@@ -24,14 +27,16 @@ public:
|
||||
Q_PROPERTY(QString baseUrl READ baseUrl WRITE setBaseUrl NOTIFY baseUrlChanged FINAL)
|
||||
Q_PROPERTY(QString lang READ lang WRITE setLang NOTIFY langChanged FINAL)
|
||||
|
||||
// RecordService* collection(const QString& idOrName);
|
||||
RecordService* collection(const QString& idOrName);
|
||||
// BaseAuthStore* authStore() { return m_authStore; }
|
||||
QNetworkReply* send(const QString& path, const QJsonObject params);
|
||||
QJsonObject send(const QString& path, const QJsonObject params);
|
||||
|
||||
QString filter(const QString& expr, const QMap<QString, QVariant>& query);
|
||||
QString getFileToken();
|
||||
QUrl buildUrl(const QString& path);
|
||||
|
||||
BaseAuthStore* authStore() const;
|
||||
|
||||
QString baseUrl() const;
|
||||
void setBaseUrl(const QString &newBaseUrl);
|
||||
|
||||
@@ -40,16 +45,17 @@ public:
|
||||
|
||||
signals:
|
||||
void baseUrlChanged();
|
||||
|
||||
void langChanged();
|
||||
|
||||
void requestFinished(QNetworkReply* reply);
|
||||
|
||||
private:
|
||||
int a = 100, b = 12331332;
|
||||
QString m_baseurl, m_lang;
|
||||
int m_timeout;
|
||||
|
||||
QNetworkAccessManager* m_networkManager;
|
||||
BaseAuthStore* m_authStore;
|
||||
|
||||
QMap<QString, RecordService*> m_recordServices;
|
||||
};
|
||||
|
||||
typedef PocketBase Client;
|
||||
|
||||
@@ -6,7 +6,7 @@ BaseModel::BaseModel(const QJsonObject& data, QObject* parent)
|
||||
}
|
||||
|
||||
QString BaseModel::toString() const {
|
||||
return QString("<%1: %2>").arg(this->metaObject()->className()).arg(id);
|
||||
return QString("<%1: %2>").arg(this->metaObject()->className()).arg(m_id);
|
||||
}
|
||||
|
||||
QString BaseModel::repr() const {
|
||||
@@ -14,25 +14,26 @@ QString BaseModel::repr() const {
|
||||
}
|
||||
|
||||
void BaseModel::load(const QJsonObject& data) {
|
||||
id = data["id"].toString("");
|
||||
created = toDateTime(data["created"].toString(""));
|
||||
updated = toDateTime(data["updated"].toString(""));
|
||||
m_id = data["id"].toString("");
|
||||
m_created = toDateTime(data["created"].toString(""));
|
||||
m_updated = toDateTime(data["updated"].toString(""));
|
||||
m_data = data;
|
||||
}
|
||||
|
||||
bool BaseModel::isNew() const {
|
||||
return id.isEmpty();
|
||||
return m_id.isEmpty();
|
||||
}
|
||||
|
||||
QString BaseModel::getId() const {
|
||||
return id;
|
||||
return m_id;
|
||||
}
|
||||
|
||||
QDateTime BaseModel::getCreated() const {
|
||||
return created;
|
||||
return m_created;
|
||||
}
|
||||
|
||||
QDateTime BaseModel::getUpdated() const {
|
||||
return updated;
|
||||
return m_updated;
|
||||
}
|
||||
|
||||
QDateTime BaseModel::toDateTime(const QString& datetimeStr) const {
|
||||
|
||||
@@ -13,9 +13,10 @@ public:
|
||||
explicit BaseModel(const QJsonObject& data = QJsonObject(), QObject* parent = nullptr);
|
||||
|
||||
BaseModel(const BaseModel &data) {
|
||||
id = data.getId();
|
||||
created = data.getCreated();
|
||||
updated = data.getUpdated();
|
||||
m_id = data.getId();
|
||||
m_created = data.getCreated();
|
||||
m_updated = data.getUpdated();
|
||||
m_data = data.data();
|
||||
}
|
||||
|
||||
|
||||
@@ -23,15 +24,17 @@ public:
|
||||
virtual QString repr() const;
|
||||
virtual void load(const QJsonObject& data);
|
||||
virtual bool isNew() const;
|
||||
virtual QJsonObject data() const { return m_data; }
|
||||
|
||||
virtual QString getId() const;
|
||||
virtual QDateTime getCreated() const;
|
||||
virtual QDateTime getUpdated() const;
|
||||
|
||||
protected:
|
||||
QString id;
|
||||
QDateTime created;
|
||||
QDateTime updated;
|
||||
QString m_id;
|
||||
QDateTime m_created;
|
||||
QDateTime m_updated;
|
||||
QJsonObject m_data;
|
||||
|
||||
QDateTime toDateTime(const QString& datetimeStr) const;
|
||||
};
|
||||
|
||||
@@ -19,6 +19,8 @@ public:
|
||||
load(data);
|
||||
}
|
||||
|
||||
bool isEmpty() const { return expand.isEmpty(); }
|
||||
|
||||
void load(const QJsonObject &data) override {
|
||||
BaseModel::load(data);
|
||||
expand.clear();
|
||||
@@ -36,17 +38,17 @@ public:
|
||||
QList<RecordModel> recordModels;
|
||||
if (data.type() == QVariant::List) {
|
||||
for (const auto &item : data.toList()) {
|
||||
// recordModels.append(RecordModel(item.toJsonObject()));
|
||||
//recordModels.append(RecordModel(item.toJsonObject()));
|
||||
}
|
||||
} else if (data.type() == QVariant::Map) {
|
||||
// recordModels.append(RecordModel(data.toJsonObject()));
|
||||
//recordModels.append(RecordModel(data.toJsonObject()));
|
||||
}
|
||||
return recordModels;
|
||||
}
|
||||
|
||||
void loadExpanded() {
|
||||
for (auto it = expand.begin(); it != expand.end(); ++it) {
|
||||
auto l = parseExpanded(it.value());
|
||||
// auto l = parseExpanded(it.value());
|
||||
// expand[it.key()] = QVariantList(l.begin(), l.end());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
namespace Utils {
|
||||
inline QString camelToSnake(const QString &str) {
|
||||
QString result = str;
|
||||
result.replace(QRegularExpression("([a-z])([A-Z])"), "\\1_\\2").toLower();
|
||||
auto _ = result.replace(QRegularExpression("([a-z])([A-Z])"), "\\1_\\2").toLower();
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -20,6 +20,17 @@ inline QJsonObject urlQueryToJson(const QUrlQuery &query) {
|
||||
}
|
||||
return jsonObj;
|
||||
}
|
||||
|
||||
inline QByteArray base64UrlDecode(const QString& base64Url) {
|
||||
QString base64 = base64Url;
|
||||
base64.replace('-', '+').replace('_', '/');
|
||||
|
||||
while (base64.length() % 4 != 0) {
|
||||
base64.append('=');
|
||||
}
|
||||
|
||||
return QByteArray::fromBase64(base64.toUtf8());
|
||||
}
|
||||
}
|
||||
|
||||
#endif // GLOBALUTILS_H
|
||||
|
||||
109
sdk/recordservice.h
Normal file
109
sdk/recordservice.h
Normal file
@@ -0,0 +1,109 @@
|
||||
#ifndef RECORDSERVICE_H
|
||||
#define RECORDSERVICE_H
|
||||
|
||||
#include <QString>
|
||||
#include <QUrlQuery>
|
||||
#include <QVariant>
|
||||
#include <QList>
|
||||
#include <QPair>
|
||||
#include <QObject>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QEventLoop>
|
||||
|
||||
#include "crudservice.h"
|
||||
#include "../dtos/recordmodel.h"
|
||||
#include "../client.h"
|
||||
|
||||
// Forward declaration
|
||||
class PocketBase;
|
||||
|
||||
class RecordAuthResponse {
|
||||
public:
|
||||
QString token;
|
||||
RecordModel record;
|
||||
|
||||
RecordAuthResponse(const QString& token, const RecordModel& record);
|
||||
|
||||
bool isValid() const;
|
||||
|
||||
private:
|
||||
bool validateToken(const QString& token) const;
|
||||
};
|
||||
|
||||
class AuthProviderInfo {
|
||||
public:
|
||||
QString name;
|
||||
QString state;
|
||||
QString codeVerifier;
|
||||
QString codeChallenge;
|
||||
QString codeChallengeMethod;
|
||||
QString authUrl;
|
||||
|
||||
AuthProviderInfo(const QString& name, const QString& state, const QString& codeVerifier,
|
||||
const QString& codeChallenge, const QString& codeChallengeMethod,
|
||||
const QString& authUrl);
|
||||
};
|
||||
|
||||
class AuthMethodsList {
|
||||
public:
|
||||
bool usernamePassword;
|
||||
bool emailPassword;
|
||||
QList<AuthProviderInfo> authProviders;
|
||||
|
||||
// TEMPORARY
|
||||
AuthMethodsList(){}
|
||||
|
||||
AuthMethodsList(bool usernamePassword, bool emailPassword, const QList<AuthProviderInfo>& authProviders);
|
||||
};
|
||||
|
||||
class RecordService : public CrudService {
|
||||
public:
|
||||
RecordService(PocketBase* client, const QString& collectionIdOrName);
|
||||
|
||||
BaseModel* decode(const QJsonObject& data) override;
|
||||
|
||||
QString baseCollectionPath() const;
|
||||
|
||||
QString baseCrudPath() const override;
|
||||
|
||||
QString getFileUrl(const RecordModel& record, const QString& filename, const QUrlQuery& queryParams = QUrlQuery()) const;
|
||||
|
||||
void subscribe(std::function<void(const QVariant&)> callback);
|
||||
|
||||
void subscribeOne(const QString& recordId, std::function<void(const QVariant&)> callback);
|
||||
|
||||
void unsubscribe(const QStringList& recordIds);
|
||||
|
||||
RecordModel update(const QString& id, const QJsonObject& bodyParams = QJsonObject(), const QUrlQuery& queryParams = QUrlQuery());
|
||||
|
||||
bool deleteRecord(const QString& id, const QUrlQuery& queryParams = QUrlQuery());
|
||||
|
||||
RecordAuthResponse authResponse(const QJsonObject& responseData);
|
||||
|
||||
AuthMethodsList listAuthMethods(const QUrlQuery& queryParams = QUrlQuery());
|
||||
|
||||
QNetworkReply* authWithPassword(const QString& usernameOrEmail, const QString& password, const QJsonObject& bodyParams = QJsonObject(), const QUrlQuery& queryParams = QUrlQuery());
|
||||
|
||||
QNetworkReply* authWithOAuth2(const QString& provider, const QString& code, const QString& codeVerifier, const QString& redirectUrl, const QJsonObject& createData = QJsonObject(), const QJsonObject& bodyParams = QJsonObject(), const QUrlQuery& queryParams = QUrlQuery());
|
||||
|
||||
QNetworkReply* authRefresh(const QJsonObject& bodyParams = QJsonObject(), const QUrlQuery& queryParams = QUrlQuery());
|
||||
|
||||
bool requestEmailChange(const QString& newEmail, const QJsonObject& bodyParams = QJsonObject(), const QUrlQuery& queryParams = QUrlQuery());
|
||||
|
||||
bool confirmEmailChange(const QString& token, const QString& password, const QJsonObject& bodyParams = QJsonObject(), const QUrlQuery& queryParams = QUrlQuery());
|
||||
|
||||
bool requestPasswordReset(const QString& email, const QJsonObject& bodyParams = QJsonObject(), const QUrlQuery& queryParams = QUrlQuery());
|
||||
|
||||
bool requestVerification(const QString& email, const QJsonObject& bodyParams = QJsonObject(), const QUrlQuery& queryParams = QUrlQuery());
|
||||
|
||||
bool confirmPasswordReset(const QString& passwordResetToken, const QString& password, const QString& passwordConfirm, const QJsonObject& bodyParams = QJsonObject(), const QUrlQuery& queryParams = QUrlQuery());
|
||||
|
||||
bool confirmVerification(const QString& token, const QJsonObject& bodyParams = QJsonObject(), const QUrlQuery& queryParams = QUrlQuery());
|
||||
|
||||
private:
|
||||
QString m_collectionIdOrName;
|
||||
// PocketBase *client;
|
||||
};
|
||||
|
||||
#endif // RECORDSERVICE_H
|
||||
@@ -1,24 +0,0 @@
|
||||
#include "baseauthstore.h"
|
||||
#include "RecordService.h"
|
||||
#include "AdminService.h"
|
||||
|
||||
BaseAuthStore::BaseAuthStore(const QString& baseToken, std::shared_ptr<RecordService> baseModel, QObject* parent)
|
||||
: QObject(parent), m_baseToken(baseToken), m_baseModel(baseModel) {}
|
||||
|
||||
QString BaseAuthStore::token() const {
|
||||
return m_baseToken;
|
||||
}
|
||||
|
||||
std::shared_ptr<RecordService> BaseAuthStore::model() const {
|
||||
return m_baseModel;
|
||||
}
|
||||
|
||||
void BaseAuthStore::save(const QString& token, std::shared_ptr<RecordService> model) {
|
||||
m_baseToken = token.isEmpty() ? "" : token;
|
||||
m_baseModel = model ? model : nullptr;
|
||||
}
|
||||
|
||||
void BaseAuthStore::clear() {
|
||||
m_baseToken.clear();
|
||||
m_baseModel.reset();
|
||||
}
|
||||
43
sdk/services/baseauthstore.cpp
Normal file
43
sdk/services/baseauthstore.cpp
Normal file
@@ -0,0 +1,43 @@
|
||||
#include "baseauthstore.h"
|
||||
#include "../dtos/recordmodel.h"
|
||||
|
||||
BaseAuthStore::BaseAuthStore(const QString& baseToken, RecordModel* baseModel, QObject* parent)
|
||||
: QObject(parent), m_baseToken(baseToken), m_baseModel(baseModel) {}
|
||||
|
||||
QString BaseAuthStore::token() const {
|
||||
return m_baseToken;
|
||||
}
|
||||
|
||||
RecordModel* BaseAuthStore::model() const {
|
||||
return m_baseModel;
|
||||
}
|
||||
|
||||
bool BaseAuthStore::isValid() const
|
||||
{
|
||||
auto parts = m_baseToken.split(".");
|
||||
|
||||
if (parts.size() != 3) {
|
||||
return false;
|
||||
}
|
||||
|
||||
QByteArray payload = Utils::base64UrlDecode(parts[1]);
|
||||
QJsonParseError parseError;
|
||||
QJsonDocument jsonDoc = QJsonDocument::fromJson(payload, &parseError);
|
||||
|
||||
if (parseError.error != QJsonParseError::NoError) {
|
||||
qWarning() << "Failed to parse JSON payload:" << parseError.errorString();
|
||||
return false;
|
||||
}
|
||||
|
||||
return jsonDoc["exp"].toDouble() > QDateTime::currentSecsSinceEpoch();
|
||||
}
|
||||
|
||||
void BaseAuthStore::save(const QString& token, RecordModel* model) {
|
||||
m_baseToken = token.isEmpty() ? "" : token;
|
||||
m_baseModel = model ? model : nullptr;
|
||||
}
|
||||
|
||||
void BaseAuthStore::clear() {
|
||||
m_baseToken.clear();
|
||||
m_baseModel=nullptr;
|
||||
}
|
||||
@@ -3,21 +3,21 @@
|
||||
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
#include <memory>
|
||||
#include <QJsonDocument>
|
||||
|
||||
class RecordService;
|
||||
class AdminService;
|
||||
class RecordModel;
|
||||
|
||||
class BaseAuthStore : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit BaseAuthStore(const QString& token = "", std::shared_ptr<RecordService> baseModel = nullptr, QObject* parent = nullptr);
|
||||
explicit BaseAuthStore(const QString& token = "", RecordModel* baseModel = nullptr, QObject* parent = nullptr);
|
||||
|
||||
QString token() const;
|
||||
std::shared_ptr<RecordService> model() const;
|
||||
RecordModel* model() const;
|
||||
bool isValid() const;
|
||||
|
||||
void save(const QString& token = "", std::shared_ptr<RecordService> model = nullptr);
|
||||
void save(const QString& token = "", RecordModel* model = nullptr);
|
||||
void clear();
|
||||
|
||||
// TODO
|
||||
@@ -28,7 +28,7 @@ signals:
|
||||
|
||||
private:
|
||||
QString m_baseToken;
|
||||
std::shared_ptr<RecordService> m_baseModel;
|
||||
RecordModel* m_baseModel;
|
||||
};
|
||||
|
||||
#endif // BASEAUTHSTORE_H
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "basecrudservice.h"
|
||||
#include "../client.h"
|
||||
|
||||
BaseCrudService::BaseCrudService(QObject* parent)
|
||||
: BaseService(parent) {}
|
||||
@@ -19,7 +20,11 @@ ListResult BaseCrudService::_getList(
|
||||
params["page"] = page;
|
||||
params["perPage"] = perPage;
|
||||
|
||||
QJsonObject response_data; // = client->send(basePath, { {"method", "GET"}, {"params", params} }).toObject();
|
||||
QJsonObject payload;
|
||||
payload.insert("method", "GET");
|
||||
payload.insert("params", params);
|
||||
|
||||
QJsonObject response_data = client->send(basePath, payload);
|
||||
QList<BaseModel*> items;
|
||||
if (response_data.contains("items")) {
|
||||
QJsonArray itemsArray = response_data["items"].toArray();
|
||||
@@ -40,8 +45,13 @@ BaseModel* BaseCrudService::_getOne(
|
||||
const QString& basePath,
|
||||
const QString& id,
|
||||
const QJsonObject& queryParams) {
|
||||
// return decode(client->send(QString("%1/%2").arg(basePath, QUrl::toPercentEncoding(id)), { {"method", "GET"}, {"params", queryParams} }).toObject());
|
||||
return new BaseModel();
|
||||
|
||||
QJsonObject payload;
|
||||
payload.insert("method", "GET");
|
||||
payload.insert("params", queryParams);
|
||||
|
||||
auto one = client->send(QString("%1/%2").arg(basePath, QUrl::toPercentEncoding(id)), payload);
|
||||
return new BaseModel(one);
|
||||
}
|
||||
|
||||
BaseModel* BaseCrudService::_getFirstListItem(
|
||||
|
||||
@@ -11,7 +11,6 @@
|
||||
#include <QNetworkReply>
|
||||
|
||||
#include "baseservice.h"
|
||||
// #include "../client.h"
|
||||
#include "../dtos/basemodel.h"
|
||||
#include "../dtos/listresult.h"
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#include <QObject>
|
||||
#include <QJsonObject>
|
||||
|
||||
#include "../client.h"
|
||||
class PocketBase;
|
||||
|
||||
class BaseService : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "crudservice.h"
|
||||
#include "../client.h"
|
||||
|
||||
CrudService::CrudService(PocketBase* client, QObject* parent)
|
||||
: BaseCrudService(parent) { Q_UNUSED(client) }
|
||||
|
||||
@@ -6,7 +6,9 @@
|
||||
#include <QList>
|
||||
|
||||
#include "basecrudservice.h"
|
||||
#include "../client.h"
|
||||
|
||||
// Forward declaration
|
||||
class PocketBase;
|
||||
|
||||
|
||||
class CrudService : public BaseCrudService {
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#include "recordservice.h"
|
||||
#include "baseauthstore.h"
|
||||
#include "../client.h"
|
||||
|
||||
RecordAuthResponse::RecordAuthResponse(const QString &token, const RecordModel &record)
|
||||
RecordAuthResponse::RecordAuthResponse(const QString &token, RecordModel* record)
|
||||
: token(token), record(record) {}
|
||||
|
||||
bool RecordAuthResponse::isValid() const {
|
||||
@@ -8,9 +10,22 @@ bool RecordAuthResponse::isValid() const {
|
||||
}
|
||||
|
||||
bool RecordAuthResponse::validateToken(const QString &token) const {
|
||||
// TODO
|
||||
// Implement token validation logic here
|
||||
return true;
|
||||
auto parts = token.split(".");
|
||||
|
||||
if (parts.size() != 3) {
|
||||
return false;
|
||||
}
|
||||
|
||||
QByteArray payload = Utils::base64UrlDecode(parts[1]);
|
||||
QJsonParseError parseError;
|
||||
QJsonDocument jsonDoc = QJsonDocument::fromJson(payload, &parseError);
|
||||
|
||||
if (parseError.error != QJsonParseError::NoError) {
|
||||
qWarning() << "Failed to parse JSON payload:" << parseError.errorString();
|
||||
return false;
|
||||
}
|
||||
|
||||
return jsonDoc["exp"].toDouble() > QDateTime::currentSecsSinceEpoch();
|
||||
}
|
||||
|
||||
AuthProviderInfo::AuthProviderInfo(const QString &name, const QString &state, const QString &codeVerifier, const QString &codeChallenge, const QString &codeChallengeMethod, const QString &authUrl)
|
||||
@@ -25,10 +40,10 @@ RecordService::RecordService(PocketBase* client, const QString &collectionIdOrNa
|
||||
m_collectionIdOrName(collectionIdOrName),
|
||||
client(client) {}
|
||||
|
||||
// std::shared_ptr<BaseModel> RecordService::decode(const QJsonObject &data) {
|
||||
// // TODO
|
||||
// return std::make_shared<Record>(data);
|
||||
// }
|
||||
BaseModel* RecordService::decode(const QJsonObject &data) {
|
||||
// TODO
|
||||
return new RecordModel(data);
|
||||
}
|
||||
|
||||
QString RecordService::baseCollectionPath() const {
|
||||
return "/api/collections/" + QUrl::toPercentEncoding(m_collectionIdOrName);
|
||||
@@ -77,9 +92,9 @@ RecordModel RecordService::update(
|
||||
const QUrlQuery &queryParams) {
|
||||
auto item = CrudService::update(id, bodyParams, Utils::urlQueryToJson(queryParams));
|
||||
try {
|
||||
// if (client->authStore()->model().collectionId != nullptr && item.id() == client->authStore()->model().id()) {
|
||||
// client->authStore()->save(client->authStore()->token(), item);
|
||||
// }
|
||||
if (client->authStore()->model()->collectionId != nullptr && item->getId() == client->authStore()->model()->getId()) {
|
||||
// client->authStore()->save(client->authStore()->token(), item);
|
||||
}
|
||||
} catch (...) {
|
||||
}
|
||||
|
||||
@@ -101,14 +116,25 @@ bool RecordService::deleteRecord(
|
||||
}
|
||||
|
||||
RecordAuthResponse RecordService::authResponse(const QJsonObject &responseData) {
|
||||
qDebug() << "Auth response ...";
|
||||
// auto record = std::dynamic_pointer_cast<Record>(decode(responseData["record"].toObject()));
|
||||
// QString token = responseData["token"].toString();
|
||||
// if (!token.isEmpty()) { // && !record.isEmpty()) {
|
||||
// client->authStore()->save(token, record);
|
||||
// }
|
||||
// return RecordAuthResponse(token, record);
|
||||
return RecordAuthResponse{"", RecordModel{QJsonObject{}}};
|
||||
// qDebug() << "[RecordService] Auth response";
|
||||
|
||||
// Ensure response is well formed before creating the RecordAuthResponse
|
||||
if( responseData.contains("data") &&
|
||||
responseData["data"].toObject().contains("record") &&
|
||||
responseData["data"].toObject().contains("token")) {
|
||||
auto data = responseData["data"].toObject();
|
||||
auto record = new RecordModel(data["record"].toObject());
|
||||
QString token = data["token"].toString();
|
||||
|
||||
if (!token.isEmpty() && !record->isEmpty()) {
|
||||
client->authStore()->save(token, record);
|
||||
// qDebug() << "[RecordService] Token Saved!";
|
||||
}
|
||||
|
||||
return RecordAuthResponse(token, record);
|
||||
}
|
||||
|
||||
return RecordAuthResponse{"", nullptr};
|
||||
}
|
||||
|
||||
AuthMethodsList RecordService::listAuthMethods(const QUrlQuery &queryParams) {
|
||||
@@ -128,8 +154,8 @@ AuthMethodsList RecordService::listAuthMethods(const QUrlQuery &queryParams) {
|
||||
return AuthMethodsList(); // usernamePassword, emailPassword, authProviders);
|
||||
}
|
||||
|
||||
QNetworkReply* RecordService::authWithPassword(const QString &usernameOrEmail, const QString &password, const QJsonObject &bodyParams, const QUrlQuery &queryParams) {
|
||||
qDebug() << "Auth with Password begin";
|
||||
RecordAuthResponse RecordService::authWithPassword(const QString &usernameOrEmail, const QString &password, const QJsonObject &bodyParams, const QUrlQuery &queryParams) {
|
||||
// qDebug() << "[RecordService] Authenticate with password";
|
||||
|
||||
QJsonObject body, params;
|
||||
body.insert("identity", usernameOrEmail);
|
||||
@@ -138,45 +164,33 @@ QNetworkReply* RecordService::authWithPassword(const QString &usernameOrEmail, c
|
||||
params.insert("method", "POST");
|
||||
params.insert("body", body);
|
||||
|
||||
qDebug() << params;
|
||||
// qDebug() << "[RecordService] Request parameters: " << params;
|
||||
|
||||
QEventLoop wait_loop;
|
||||
QNetworkReply* reply = client->send(baseCollectionPath() + "/auth-with-password", params);
|
||||
QJsonObject httpResponse = client->send(baseCollectionPath() + "/auth-with-password", params);
|
||||
|
||||
connect(reply, &QNetworkReply::finished, &wait_loop, [&](){ qDebug() << "Finished!";});
|
||||
connect(reply, &QNetworkReply::finished, &wait_loop, &QEventLoop::quit);
|
||||
wait_loop.exec();
|
||||
|
||||
qDebug() << "Loop finished!";
|
||||
auto data = reply->readAll();
|
||||
qDebug() << "res: " << data;
|
||||
|
||||
if( reply->error() != QNetworkReply::NoError ) {
|
||||
qDebug() << "Password authentication error!" << reply->errorString();
|
||||
}
|
||||
|
||||
|
||||
// QJsonObject responseData =
|
||||
// return authResponse(QJsonObject());
|
||||
return nullptr;
|
||||
return authResponse(httpResponse);
|
||||
}
|
||||
|
||||
QNetworkReply* RecordService::authWithOAuth2(const QString &provider, const QString &code, const QString &codeVerifier, const QString &redirectUrl, const QJsonObject &createData, const QJsonObject &bodyParams, const QUrlQuery &queryParams) {
|
||||
QJsonObject params = bodyParams;
|
||||
params.insert("provider", provider);
|
||||
params.insert("code", code);
|
||||
params.insert("codeVerifier", codeVerifier);
|
||||
params.insert("redirectUrl", redirectUrl);
|
||||
params.insert("createData", createData);
|
||||
RecordAuthResponse RecordService::authWithOAuth2(const QString &provider, const QString &code, const QString &codeVerifier, const QString &redirectUrl, const QJsonObject &createData, const QJsonObject &bodyParams, const QUrlQuery &queryParams) {
|
||||
// QJsonObject params = bodyParams;
|
||||
// params.insert("provider", provider);
|
||||
// params.insert("code", code);
|
||||
// params.insert("codeVerifier", codeVerifier);
|
||||
// params.insert("redirectUrl", redirectUrl);
|
||||
// params.insert("createData", createData);
|
||||
// QJsonObject responseData = client->send(baseCollectionPath() + "/auth-with-oauth2", "POST", queryParams, params);
|
||||
// return authResponse(QJsonObject());
|
||||
return nullptr;
|
||||
return authResponse(QJsonObject());
|
||||
}
|
||||
|
||||
QNetworkReply* RecordService::authRefresh(const QJsonObject &bodyParams, const QUrlQuery &queryParams) {
|
||||
// QJsonObject responseData = client->send(baseCollectionPath() + "/auth-refresh", "POST", queryParams, bodyParams);
|
||||
// return authResponse(QJsonObject());
|
||||
return nullptr;
|
||||
RecordAuthResponse RecordService::authRefresh(const QJsonObject &bodyParams, const QUrlQuery &queryParams) {
|
||||
QJsonObject payload, headers;
|
||||
payload.insert("method", "POST");
|
||||
payload.insert("body", bodyParams);
|
||||
headers.insert("auth", true);
|
||||
payload.insert("headers", headers);
|
||||
|
||||
QJsonObject responseData = client->send(baseCollectionPath() + "/auth-refresh", payload);
|
||||
return authResponse(responseData);
|
||||
}
|
||||
|
||||
bool RecordService::requestEmailChange(const QString &newEmail, const QJsonObject &bodyParams, const QUrlQuery &queryParams) {
|
||||
@@ -195,17 +209,27 @@ bool RecordService::confirmEmailChange(const QString &token, const QString &pass
|
||||
}
|
||||
|
||||
bool RecordService::requestPasswordReset(const QString &email, const QJsonObject &bodyParams, const QUrlQuery &queryParams) {
|
||||
QJsonObject params = bodyParams;
|
||||
params.insert("email", email);
|
||||
// client->send(baseCollectionPath() + "/request-password-reset", "POST", queryParams, params);
|
||||
return true;
|
||||
QJsonObject payload, body;
|
||||
body["email"] = email;
|
||||
payload.insert("method", "POST");
|
||||
payload.insert("body", body);
|
||||
|
||||
auto jsonResponse = client->send(baseCollectionPath() + "/request-password-reset", payload);
|
||||
// qDebug() << "Password Reset: " << jsonResponse;
|
||||
return jsonResponse.value("statusCode").toInt() == 204;
|
||||
}
|
||||
|
||||
bool RecordService::requestVerification(const QString &email, const QJsonObject &bodyParams, const QUrlQuery &queryParams) {
|
||||
QJsonObject params = bodyParams;
|
||||
params.insert("email", email);
|
||||
// client->send(baseCollectionPath() + "/request-verification", "POST", queryParams, params);
|
||||
return true;
|
||||
bool RecordService::requestVerification(const QString &email) {
|
||||
QJsonObject payload, headers, body;
|
||||
body["email"] = email;
|
||||
payload.insert("method", "POST");
|
||||
payload.insert("body", body);
|
||||
headers.insert("auth", true);
|
||||
payload.insert("headers", headers);
|
||||
|
||||
auto jsonResponse = client->send(baseCollectionPath() + "/request-verification", payload);
|
||||
// qDebug() << "Verification Response: " << jsonResponse;
|
||||
return jsonResponse.value("statusCode").toInt() == 204;
|
||||
}
|
||||
|
||||
bool RecordService::confirmPasswordReset(const QString &passwordResetToken, const QString &password, const QString &passwordConfirm, const QJsonObject &bodyParams, const QUrlQuery &queryParams) {
|
||||
@@ -217,9 +241,14 @@ bool RecordService::confirmPasswordReset(const QString &passwordResetToken, cons
|
||||
return true;
|
||||
}
|
||||
|
||||
bool RecordService::confirmVerification(const QString &token, const QJsonObject &bodyParams, const QUrlQuery &queryParams) {
|
||||
QJsonObject params = bodyParams;
|
||||
params.insert("token", token);
|
||||
// client->send(baseCollectionPath() + "/confirm-verification", "POST", queryParams, params);
|
||||
return true;
|
||||
bool RecordService::confirmVerification(const QString &token) {
|
||||
QJsonObject payload, headers, body;
|
||||
body["token"] = token;
|
||||
payload.insert("method", "POST");
|
||||
payload.insert("body", body);
|
||||
headers.insert("auth", true);
|
||||
payload.insert("headers", headers);
|
||||
auto jsonResponse = client->send(baseCollectionPath() + "/confirm-verification", payload);
|
||||
qDebug() << "Confirm Verification Response: " << jsonResponse;
|
||||
return jsonResponse.value("statusCode").toInt() == 204;
|
||||
}
|
||||
|
||||
@@ -12,15 +12,17 @@
|
||||
#include <QEventLoop>
|
||||
|
||||
#include "crudservice.h"
|
||||
#include "../client.h"
|
||||
#include "../dtos/recordmodel.h"
|
||||
|
||||
// Forward declaration
|
||||
class PocketBase;
|
||||
|
||||
class RecordAuthResponse {
|
||||
public:
|
||||
QString token;
|
||||
RecordModel record;
|
||||
RecordModel* record;
|
||||
|
||||
RecordAuthResponse(const QString& token, const RecordModel& record);
|
||||
RecordAuthResponse(const QString& token, RecordModel* record);
|
||||
|
||||
bool isValid() const;
|
||||
|
||||
@@ -58,7 +60,7 @@ class RecordService : public CrudService {
|
||||
public:
|
||||
RecordService(PocketBase* client, const QString& collectionIdOrName);
|
||||
|
||||
// BaseModel* decode(const QJsonObject& data) override;
|
||||
BaseModel* decode(const QJsonObject& data) override;
|
||||
|
||||
QString baseCollectionPath() const;
|
||||
|
||||
@@ -80,11 +82,11 @@ public:
|
||||
|
||||
AuthMethodsList listAuthMethods(const QUrlQuery& queryParams = QUrlQuery());
|
||||
|
||||
QNetworkReply* authWithPassword(const QString& usernameOrEmail, const QString& password, const QJsonObject& bodyParams = QJsonObject(), const QUrlQuery& queryParams = QUrlQuery());
|
||||
RecordAuthResponse authWithPassword(const QString& usernameOrEmail, const QString& password, const QJsonObject& bodyParams = QJsonObject(), const QUrlQuery& queryParams = QUrlQuery());
|
||||
|
||||
QNetworkReply* authWithOAuth2(const QString& provider, const QString& code, const QString& codeVerifier, const QString& redirectUrl, const QJsonObject& createData = QJsonObject(), const QJsonObject& bodyParams = QJsonObject(), const QUrlQuery& queryParams = QUrlQuery());
|
||||
RecordAuthResponse authWithOAuth2(const QString& provider, const QString& code, const QString& codeVerifier, const QString& redirectUrl, const QJsonObject& createData = QJsonObject(), const QJsonObject& bodyParams = QJsonObject(), const QUrlQuery& queryParams = QUrlQuery());
|
||||
|
||||
QNetworkReply* authRefresh(const QJsonObject& bodyParams = QJsonObject(), const QUrlQuery& queryParams = QUrlQuery());
|
||||
RecordAuthResponse authRefresh(const QJsonObject& bodyParams = QJsonObject(), const QUrlQuery& queryParams = QUrlQuery());
|
||||
|
||||
bool requestEmailChange(const QString& newEmail, const QJsonObject& bodyParams = QJsonObject(), const QUrlQuery& queryParams = QUrlQuery());
|
||||
|
||||
@@ -92,11 +94,11 @@ public:
|
||||
|
||||
bool requestPasswordReset(const QString& email, const QJsonObject& bodyParams = QJsonObject(), const QUrlQuery& queryParams = QUrlQuery());
|
||||
|
||||
bool requestVerification(const QString& email, const QJsonObject& bodyParams = QJsonObject(), const QUrlQuery& queryParams = QUrlQuery());
|
||||
bool requestVerification(const QString& email);
|
||||
|
||||
bool confirmPasswordReset(const QString& passwordResetToken, const QString& password, const QString& passwordConfirm, const QJsonObject& bodyParams = QJsonObject(), const QUrlQuery& queryParams = QUrlQuery());
|
||||
|
||||
bool confirmVerification(const QString& token, const QJsonObject& bodyParams = QJsonObject(), const QUrlQuery& queryParams = QUrlQuery());
|
||||
bool confirmVerification(const QString& token);
|
||||
|
||||
private:
|
||||
QString m_collectionIdOrName;
|
||||
|
||||
Reference in New Issue
Block a user