Skip to content

Commit

Permalink
credential store and start connection handling
Browse files Browse the repository at this point in the history
  • Loading branch information
VitorVieiraZ committed Feb 4, 2025
1 parent fed12ad commit 3fb4246
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 102 deletions.
24 changes: 12 additions & 12 deletions app/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -567,18 +567,18 @@ int main( int argc, char *argv[] )
syncManager.syncProject( project, SyncOptions::Authorized, SyncOptions::Retry );
} );

QObject::connect( &activeProject, &ActiveProject::projectReloaded, &lambdaContext, [merginApi = ma.get(), &activeProject]()
{
merginApi->reloadProjectRole( activeProject.projectFullName() );
} );

QObject::connect( ma.get(), &MerginApi::authChanged, &lambdaContext, [merginApi = ma.get(), &activeProject]()
{
if ( activeProject.isProjectLoaded() )
{
merginApi->reloadProjectRole( activeProject.projectFullName() );
}
} );
// QObject::connect( &activeProject, &ActiveProject::projectReloaded, &lambdaContext, [merginApi = ma.get(), &activeProject]()
// {
// merginApi->reloadProjectRole( activeProject.projectFullName() );
// } );

// QObject::connect( ma.get(), &MerginApi::authChanged, &lambdaContext, [merginApi = ma.get(), &activeProject]()
// {
// if ( activeProject.isProjectLoaded() )
// {
// merginApi->reloadProjectRole( activeProject.projectFullName() );
// }
// } );

QObject::connect( ma.get(), &MerginApi::projectRoleUpdated, &activeProject, [&activeProject]( const QString & projectFullName, const QString & role )
{
Expand Down
1 change: 1 addition & 0 deletions core/credentialstore.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class CredentialStore : public QObject
signals:
//! Emitted when a key is read, with both key and its retrieved value.
void keyRead( const QString &key, const QString &value );

//void credentialsLoaded();
};

Expand Down
1 change: 1 addition & 0 deletions core/merginapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ MerginApi::MerginApi( LocalProjectsManager &localProjects, QObject *parent )

QObject::connect( mUserAuth, &MerginUserAuth::credentialsLoaded, this, [this]()
{
qDebug() << "REACHED HERE! ";
QObject::connect( this, &MerginApi::pingMerginFinished, this, &MerginApi::getUserInfo, Qt::SingleShotConnection );
QObject::connect( this, &MerginApi::userInfoReplyFinished, this, &MerginApi::getWorkspaceInfo, Qt::SingleShotConnection );
} );
Expand Down
90 changes: 0 additions & 90 deletions core/merginuserauth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,150 +108,60 @@ void MerginUserAuth::saveAuthData()
#endif
}

// void MerginUserAuth::loadAuthData()
// {
// #if defined(Q_OS_ANDROID) || defined(Q_OS_IOS)
// // mobile => QtKeychain
// connect( mCredentialStore, &CredentialStore::keyRead, this, [this]( const QString & key, const QVariant & value ) //read credentials in chain to emit authChanged only
// {
// if ( key == "username" )
// {
// mUsername = value.toString();
// mCredentialStore->readKey( "password" );
// }
// else if ( key == "password" )
// {
// mPassword = value.toString();
// mCredentialStore->readKey( "userId" );
// }
// else if ( key == "userId" )
// {
// mUserId = value.toInt();
// mCredentialStore->readKey( "token" );
// }
// else if ( key == "token" )
// {
// mAuthToken = value.toByteArray();
// mCredentialStore->readKey( "expire" );
// }
// else if ( key == "expire" )
// {
// mTokenExpiration = value.toDateTime();

// if ( mTokenExpiration < QDateTime::currentDateTimeUtc() )
// {
// clearTokenData();
// }

// emit authChanged();
// }
// } );

// mCredentialStore->readKey( "username" );
// #else
// // desktop => QSettings
// QSettings settings;
// settings.beginGroup( QStringLiteral( "Input/" ) );
// mUsername = settings.value( QStringLiteral( "username" ) ).toString();
// mPassword = settings.value( QStringLiteral( "password" ) ).toString();
// mUserId = settings.value( QStringLiteral( "userId" ) ).toInt();
// mTokenExpiration = settings.value( QStringLiteral( "expire" ) ).toDateTime();
// mAuthToken = settings.value( QStringLiteral( "token" ) ).toByteArray();
// settings.endGroup();

// emit authChanged();
// #endif
// }

void MerginUserAuth::loadAuthData()
{
qDebug() << "TESTLOADAUTHDATA: Starting loadAuthData()";

#if defined(Q_OS_ANDROID) || defined(Q_OS_IOS)
// mobile => QtKeychain via CredentialStore asynchronous chain
if ( mCredentialStore )
{
qDebug() << "TESTLOADAUTHDATA: Using CredentialStore for authentication";

connect( mCredentialStore, &CredentialStore::keyRead, this, [this]( const QString & key, const QString & value )
{
qDebug() << "TESTLOADAUTHDATA: keyRead signal received - key:" << key << ", value:" << value;

if ( key == "username" )
{
mUsername = value;
qDebug() << "TESTLOADAUTHDATA: Username set to" << mUsername;
mCredentialStore->readKey( QStringLiteral( "password" ) );
}
else if ( key == "password" )
{
mPassword = value;
qDebug() << "TESTLOADAUTHDATA: Password set";
mCredentialStore->readKey( QStringLiteral( "userId" ) );
}
else if ( key == "userId" )
{
mUserId = value.toInt();
qDebug() << "TESTLOADAUTHDATA: UserId set to" << mUserId;
mCredentialStore->readKey( QStringLiteral( "token" ) );
}
else if ( key == "token" )
{
mAuthToken = QByteArray::fromBase64( value.toUtf8() );
qDebug() << "TESTLOADAUTHDATA: Token set (Base64 decoded)";
mCredentialStore->readKey( QStringLiteral( "expire" ) );
}
else if ( key == "expire" )
{
mTokenExpiration = QDateTime::fromString( value, Qt::ISODate );
qDebug() << "TESTLOADAUTHDATA: Token expiration set to" << mTokenExpiration.toString(Qt::ISODate);

if ( mTokenExpiration < QDateTime::currentDateTimeUtc() )
{
qDebug() << "TESTLOADAUTHDATA: Token is expired.";
CoreUtils::log( "Auth", "Token is expired." );
}

emit authChanged();
qDebug() << "TESTLOADAUTHDATA: authChanged() emitted";

emit credentialsLoaded();
qDebug() << "TESTLOADAUTHDATA: credentialsLoaded() emitted";
}
} );

qDebug() << "TESTLOADAUTHDATA: Reading username from CredentialStore";
mCredentialStore->readKey( QStringLiteral( "username" ) );
}
else
{
qDebug() << "TESTLOADAUTHDATA: mCredentialStore is null!";
}
#else
// desktop => QSettings
qDebug() << "TESTLOADAUTHDATA: Using QSettings for authentication";
QSettings settings;
settings.beginGroup( QStringLiteral( "Input/" ) );

mUsername = settings.value( QStringLiteral( "username" ) ).toString();
qDebug() << "TESTLOADAUTHDATA: Username set to" << mUsername;

mPassword = settings.value( QStringLiteral( "password" ) ).toString();
qDebug() << "TESTLOADAUTHDATA: Password set";

mUserId = settings.value( QStringLiteral( "userId" ) ).toInt();
qDebug() << "TESTLOADAUTHDATA: UserId set to" << mUserId;

mTokenExpiration = settings.value( QStringLiteral( "expire" ) ).toDateTime();
qDebug() << "TESTLOADAUTHDATA: Token expiration set to" << mTokenExpiration.toString(Qt::ISODate);

mAuthToken = settings.value( QStringLiteral( "token" ) ).toByteArray();
qDebug() << "TESTLOADAUTHDATA: Token set (raw bytes)";

settings.endGroup();
#endif

qDebug() << "TESTLOADAUTHDATA: Finished loadAuthData()";
}

QString MerginUserAuth::username() const
Expand Down

0 comments on commit 3fb4246

Please sign in to comment.