Hi there,
I played around with Sailfish Secrets to securely store a secret token in my app. I used parts of the code from the example in the repository.
This worked the first time, but when I try to update the token because it changes in a defined period I am not able to update it. I get an UNIQUE constraint error:
Unable to execute insert secret query: UNIQUE constraint failed: Secrets.CollectionName, Secrets.SecretName Unable to fetch row
It seems that you can insert it only one time and then it is fixed?!? Is this the right way at all to store my token?
Here is my code:
Wallet.h
#ifndef WALLET_H
#define WALLET_H
#include <QObject>
#include <Sailfish/Secrets/secretmanager.h>
class Wallet : public QObject
{
Q_OBJECT
public:
explicit Wallet(QObject *parent = nullptr);
public slots:
void storeToken(const QString &token);
private:
void createCollection();
Sailfish::Secrets::SecretManager m_secretManager;
Sailfish::Secrets::Secret::Identifier m_token;
};
Wallet.cpp
#include "wallet.h"
#include <Sailfish/Secrets/createcollectionrequest.h>
#include <Sailfish/Secrets/storesecretrequest.h>
#include <Sailfish/Secrets/storedsecretrequest.h>
Wallet::Wallet(QObject *parent) :
QObject(parent)
{
m_token = Sailfish::Secrets::Secret::Identifier(
QStringLiteral("token"),
QStringLiteral(WALLET_COLLECTION_NAME),
Sailfish::Secrets::SecretManager::DefaultEncryptedStoragePluginName);
createCollection();
}
void Wallet::storeToken(const QString &token)
{
// store data in wallet
Sailfish::Secrets::Secret secret(m_token);
secret.setData(token.toLatin1());
Sailfish::Secrets::StoreSecretRequest storeCode;
storeCode.setManager(&m_secretManager);
storeCode.setSecretStorageType(Sailfish::Secrets::StoreSecretRequest::CollectionSecret);
storeCode.setUserInteractionMode(Sailfish::Secrets::SecretManager::SystemInteraction);
storeCode.setAccessControlMode(Sailfish::Secrets::SecretManager::OwnerOnlyMode);
storeCode.setSecret(secret);
storeCode.startRequest();
storeCode.waitForFinished();
}
void Wallet::createCollection()
{
Sailfish::Secrets::CreateCollectionRequest createCollection;
createCollection.setManager(&m_secretManager);
createCollection.setCollectionLockType(Sailfish::Secrets::CreateCollectionRequest::DeviceLock);
createCollection.setDeviceLockUnlockSemantic(Sailfish::Secrets::SecretManager::DeviceLockKeepUnlocked);
createCollection.setAccessControlMode(Sailfish::Secrets::SecretManager::OwnerOnlyMode);
createCollection.setUserInteractionMode(Sailfish::Secrets::SecretManager::SystemInteraction);
createCollection.setCollectionName(WALLET_COLLECTION_NAME);
createCollection.setStoragePluginName(Sailfish::Secrets::SecretManager::DefaultEncryptedStoragePluginName);
createCollection.setEncryptionPluginName(Sailfish::Secrets::SecretManager::DefaultEncryptedStoragePluginName);
createCollection.startRequest();
createCollection.waitForFinished();
}