Axivion: delete Api token if server is removed

Fixes: QTCREATORBUG-31669
Change-Id: I646f604c5cf17d77fd7c8880164c27e7a6e61f10
Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Mehdi Salem
2024-10-09 18:15:12 +02:00
committed by hjk
parent ca5583af2e
commit 9f37e0c44b
3 changed files with 50 additions and 12 deletions

View File

@@ -45,7 +45,6 @@
#include <memory> #include <memory>
constexpr char s_axivionTextMarkId[] = "AxivionTextMark"; constexpr char s_axivionTextMarkId[] = "AxivionTextMark";
constexpr char s_axivionKeychainService[] = "keychain.axivion.qtcreator";
using namespace Core; using namespace Core;
using namespace ProjectExplorer; using namespace ProjectExplorer;
@@ -114,17 +113,6 @@ static QString apiTokenDescription()
return "Automatically created by " + ua + " on " + user + "@" + QSysInfo::machineHostName(); return "Automatically created by " + ua + " on " + user + "@" + QSysInfo::machineHostName();
} }
static QString escapeKey(const QString &string)
{
QString escaped = string;
return escaped.replace('\\', "\\\\").replace('@', "\\@");
}
static QString credentialKey(const AxivionServer &server)
{
return escapeKey(server.username) + '@' + escapeKey(server.dashboard);
}
template <typename DtoType> template <typename DtoType>
struct GetDtoStorage struct GetDtoStorage
{ {

View File

@@ -4,9 +4,12 @@
#include "axivionsettings.h" #include "axivionsettings.h"
#include "axiviontr.h" #include "axiviontr.h"
#include "coreplugin/messagemanager.h"
#include <coreplugin/dialogs/ioptionspage.h> #include <coreplugin/dialogs/ioptionspage.h>
#include <coreplugin/credentialquery.h>
#include <coreplugin/icore.h> #include <coreplugin/icore.h>
#include <solutions/tasking/tasktree.h>
#include <utils/algorithm.h> #include <utils/algorithm.h>
#include <utils/id.h> #include <utils/id.h>
@@ -28,6 +31,7 @@
using namespace Core; using namespace Core;
using namespace Utils; using namespace Utils;
using namespace Tasking;
namespace Axivion::Internal { namespace Axivion::Internal {
@@ -252,9 +256,37 @@ bool AxivionSettings::updateDashboardServers(const QList<AxivionServer> &other,
if (selected == oldDefault && m_allServers == other) if (selected == oldDefault && m_allServers == other)
return false; return false;
// collect dashserver items that have been removed,
// so we can delete the api tokens from the credentials store
const QStringList previousKeys = Utils::transform(m_allServers, &credentialKey);
const QStringList updatedKeys = Utils::transform(other, &credentialKey);
const QStringList keysToRemove = Utils::filtered(previousKeys, [updatedKeys](const QString &key) {
return !updatedKeys.contains(key);
});
m_defaultServerId.setValue(selected.toString(), BeQuiet); m_defaultServerId.setValue(selected.toString(), BeQuiet);
m_allServers = other; m_allServers = other;
emit changed(); // should we be more detailed? (id) emit changed(); // should we be more detailed? (id)
const LoopList iterator(keysToRemove);
const auto onDeleteKeySetup = [iterator](CredentialQuery &query) {
MessageManager::writeSilently(Tr::tr("Axivion: Deleting Api token for %1 as respective "
"dashboard server was removed.").arg(*iterator));
query.setOperation(CredentialOperation::Delete);
query.setService(s_axivionKeychainService);
query.setKey(*iterator);
};
const Group recipe {
For (iterator) >> Do {
CredentialQueryTask(onDeleteKeySetup)
}
};
m_taskTreeRunner.start(recipe);
return true; return true;
} }
@@ -263,6 +295,17 @@ const QList<PathMapping> AxivionSettings::validPathMappings() const
return pathMappingSettings().validPathMappings(); return pathMappingSettings().validPathMappings();
} }
static QString escapeKey(const QString &string)
{
QString escaped = string;
return escaped.replace('\\', "\\\\").replace('@', "\\@");
}
QString credentialKey(const AxivionServer &server)
{
return escapeKey(server.username) + '@' + escapeKey(server.dashboard);
}
// AxivionSettingsPage // AxivionSettingsPage
// may allow some invalid, but does some minimal check for legality // may allow some invalid, but does some minimal check for legality

View File

@@ -8,12 +8,16 @@
#include <QtGlobal> #include <QtGlobal>
#include <solutions/tasking/tasktreerunner.h>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
class QJsonObject; class QJsonObject;
QT_END_NAMESPACE QT_END_NAMESPACE
namespace Axivion::Internal { namespace Axivion::Internal {
constexpr char s_axivionKeychainService[] = "keychain.axivion.qtcreator";
class AxivionServer class AxivionServer
{ {
public: public:
@@ -64,8 +68,11 @@ public:
private: private:
Utils::StringAspect m_defaultServerId{this}; Utils::StringAspect m_defaultServerId{this};
QList<AxivionServer> m_allServers; QList<AxivionServer> m_allServers;
Tasking::TaskTreeRunner m_taskTreeRunner;
}; };
AxivionSettings &settings(); AxivionSettings &settings();
QString credentialKey(const AxivionServer &server);
} // Axivion::Internal } // Axivion::Internal