Skip to content
Permalink
Browse files

[oauth] Fix GUI register button enabled

  • Loading branch information
elpaso committed Jul 19, 2018
1 parent d56fc88 commit ad7a574e6efcf7dafbaafb71af169b6ad3d56b98
@@ -148,10 +148,12 @@ void QgsAuthOAuth2Edit::setupConnections()
connect( leDefinedDirPath, &QLineEdit::textChanged, this, &QgsAuthOAuth2Edit::definedCustomDirChanged );

connect( btnSoftStatementDir, &QToolButton::clicked, this, &QgsAuthOAuth2Edit::getSoftStatementDir );
connect( leSoftwareStatementJwtPath, &QLineEdit::textChanged,this, &QgsAuthOAuth2Edit::softwareStatementJwtPathChanged );
connect( leSoftwareStatementConfigUrl, &QLineEdit::textChanged, [ = ] ( const QString &txt ) {
btnRegister->setEnabled( QUrl( txt ).isValid() && ! leSoftwareStatementJwtPath->text().isEmpty() );
});
connect( leSoftwareStatementJwtPath, &QLineEdit::textChanged, this, &QgsAuthOAuth2Edit::softwareStatementJwtPathChanged );
connect( leSoftwareStatementConfigUrl, &QLineEdit::textChanged, [ = ]( const QString & txt )
{
btnRegister->setEnabled( ! leSoftwareStatementJwtPath->text().isEmpty()
&& ( QUrl( txt ).isValid() || ! mRegistrationEndpoint.isEmpty() ) );
} );
connect( btnRegister, &QPushButton::clicked, this, &QgsAuthOAuth2Edit::getSoftwareStatementConfig );

// FIXME: in the testbed13 code this signal does not exists (but a connection was attempted)
@@ -586,7 +588,7 @@ void QgsAuthOAuth2Edit::getDefinedCustomDir()
void QgsAuthOAuth2Edit::getSoftStatementDir()
{
QString softStatementFile = QFileDialog::getOpenFileName( this, tr( "Select software statement file" ),
QDir::homePath(), tr( "JSON Web Token (*.jwt)") );
QDir::homePath(), tr( "JSON Web Token (*.jwt)" ) );
this->raise();
this->activateWindow();

@@ -956,36 +958,37 @@ void QgsAuthOAuth2Edit::clearQueryPairs()
}
}

void QgsAuthOAuth2Edit::parseSoftwareStatement(const QString& path)
void QgsAuthOAuth2Edit::parseSoftwareStatement( const QString &path )
{
QFile file(path);
QFile file( path );
QByteArray softwareStatementBase64;
if(file.open(QIODevice::ReadOnly | QIODevice::Text))
if ( file.open( QIODevice::ReadOnly | QIODevice::Text ) )
{
softwareStatementBase64=file.readAll();
softwareStatementBase64 = file.readAll();
}
if(softwareStatementBase64.isEmpty())
if ( softwareStatementBase64.isEmpty() )
{
QgsDebugMsg( QStringLiteral( "Error software statement is empty: %1" ).arg( QString( path ) ) );
file.close();
return;
}
mRegistrationEndpoint = QString();
file.close();
mSoftwareStatement.insert("software_statement",softwareStatementBase64);
QByteArray payload=softwareStatementBase64.split('.')[1];
QByteArray decoded=QByteArray::fromBase64(payload/*, QByteArray::Base64UrlEncoding*/);
mSoftwareStatement.insert( "software_statement", softwareStatementBase64 );
QByteArray payload = softwareStatementBase64.split( '.' )[1];
QByteArray decoded = QByteArray::fromBase64( payload/*, QByteArray::Base64UrlEncoding*/ );
QByteArray errStr;
bool res = false;
QMap<QString, QVariant> jsonData = QJsonWrapper::parseJson(decoded, &res, &errStr).toMap();
QMap<QString, QVariant> jsonData = QJsonWrapper::parseJson( decoded, &res, &errStr ).toMap();
if ( !res )
{
QgsDebugMsg( QStringLiteral( "Error parsing JSON: %1" ).arg( QString( errStr ) ));
QgsDebugMsg( QStringLiteral( "Error parsing JSON: %1" ).arg( QString( errStr ) ) );
return;
}
if(jsonData.contains("grant_types") && jsonData.contains( QLatin1Literal( "redirect_uris" ) ) )
if ( jsonData.contains( "grant_types" ) && jsonData.contains( QLatin1Literal( "redirect_uris" ) ) )
{
QString grantType = jsonData[QLatin1Literal ( "grant_types" ) ].toStringList()[0];
if(grantType == QLatin1Literal( "authorization_code" ) )
QString grantType = jsonData[QLatin1Literal( "grant_types" ) ].toStringList()[0];
if ( grantType == QLatin1Literal( "authorization_code" ) )
{
updateGrantFlow( static_cast<int>( QgsAuthOAuth2Config::AuthCode ) );
}
@@ -995,16 +998,16 @@ void QgsAuthOAuth2Edit::parseSoftwareStatement(const QString& path)
}
//Set redirect_uri
QString redirectUri = jsonData[QLatin1Literal( "redirect_uris" ) ].toStringList()[0];
leRedirectUrl->setText(redirectUri);
leRedirectUrl->setText( redirectUri );
}
else
{
QgsDebugMsgLevel( QStringLiteral( "Error software statement is invalid: %1" ).arg( QString( path ) ), 4 );
return;
}
if(jsonData.contains(QLatin1Literal( "registration_endpoint")) )
if ( jsonData.contains( QLatin1Literal( "registration_endpoint" ) ) )
{
mRegistrationEndpoint = jsonData[QLatin1Literal("registration_endpoint")].toString();
mRegistrationEndpoint = jsonData[QLatin1Literal( "registration_endpoint" )].toString();
leSoftwareStatementConfigUrl->setText( mRegistrationEndpoint );
}
QgsDebugMsgLevel( QStringLiteral( "JSON: %1" ).arg( QString::fromLocal8Bit( decoded.data() ) ), 4 );
@@ -1013,30 +1016,30 @@ void QgsAuthOAuth2Edit::parseSoftwareStatement(const QString& path)
void QgsAuthOAuth2Edit::configReplyFinished()
{
qDebug() << "QgsAuthOAuth2Edit::onConfigReplyFinished";
QNetworkReply *configReply = qobject_cast<QNetworkReply *>(sender());
if (configReply->error() == QNetworkReply::NoError)
QNetworkReply *configReply = qobject_cast<QNetworkReply *>( sender() );
if ( configReply->error() == QNetworkReply::NoError )
{
QByteArray replyData = configReply->readAll();
QByteArray errStr;
bool res = false;
QVariantMap config = QJsonWrapper::parseJson(replyData, &res, &errStr).toMap();
QVariantMap config = QJsonWrapper::parseJson( replyData, &res, &errStr ).toMap();

if ( !res )
{
QgsDebugMsg( QStringLiteral( "Error parsing JSON: %1" ).arg( QString( errStr ) ) );
return;
}
// I haven't found any docs about the content of this confg JSON file
// I assume that registration_endpoint is all that it contains
// But we also might have other optional information here
if(config.contains(QLatin1Literal( "registration_endpoint")) )
// I assume that registration_endpoint is all that it MUST contain.
// But we also MAY have other optional information here
if ( config.contains( QLatin1Literal( "registration_endpoint" ) ) )
{
if ( config.contains(QLatin1Literal("authorization_endpoint" ) ) )
leRequestUrl->setText(config.value(QLatin1Literal("authorization_endpoint" ) ).toString());
if ( config.contains(QLatin1Literal("token_endpoint" ) ) )
leTokenUrl->setText(config.value(QLatin1Literal("token_endpoint" ) ).toString());
if ( config.contains( QLatin1Literal( "authorization_endpoint" ) ) )
leRequestUrl->setText( config.value( QLatin1Literal( "authorization_endpoint" ) ).toString() );
if ( config.contains( QLatin1Literal( "token_endpoint" ) ) )
leTokenUrl->setText( config.value( QLatin1Literal( "token_endpoint" ) ).toString() );

registerSoftStatement(config.value(QLatin1Literal("registration_endpoint")).toString());
registerSoftStatement( config.value( QLatin1Literal( "registration_endpoint" ) ).toString() );
}
else
{
@@ -1053,86 +1056,86 @@ void QgsAuthOAuth2Edit::registerReplyFinished()
//JSV todo
//better error handling
qDebug() << "QgsAuthOAuth2Edit::onRegisterReplyFinished";
QNetworkReply *registerReply = qobject_cast<QNetworkReply *>(sender());
if (registerReply->error() == QNetworkReply::NoError)
QNetworkReply *registerReply = qobject_cast<QNetworkReply *>( sender() );
if ( registerReply->error() == QNetworkReply::NoError )
{
QByteArray replyData = registerReply->readAll();
QByteArray errStr;
bool res = false;
QVariantMap clientInfo = QJsonWrapper::parseJson(replyData, &res, &errStr).toMap();
QVariantMap clientInfo = QJsonWrapper::parseJson( replyData, &res, &errStr ).toMap();

// According to RFC 7591 sec. 3.2.1. Client Information Response the only
// required field is client_id
leClientId->setText(clientInfo.value(QLatin1Literal("client_id" ) ).toString());
if ( clientInfo.contains(QLatin1Literal("client_secret" )) )
leClientSecret->setText(clientInfo.value(QLatin1Literal("client_secret" ) ).toString());
if ( clientInfo.contains(QLatin1Literal("authorization_endpoint" ) ) )
leRequestUrl->setText(clientInfo.value(QLatin1Literal("authorization_endpoint" ) ).toString());
if ( clientInfo.contains(QLatin1Literal("token_endpoint" ) ) )
leTokenUrl->setText(clientInfo.value(QLatin1Literal("token_endpoint" ) ).toString());
if ( clientInfo.contains(QLatin1Literal("scopes" ) ) )
leScope->setText(clientInfo.value(QLatin1Literal("scopes" ) ).toString());

tabConfigs->setCurrentIndex(0);
leClientId->setText( clientInfo.value( QLatin1Literal( "client_id" ) ).toString() );
if ( clientInfo.contains( QLatin1Literal( "client_secret" ) ) )
leClientSecret->setText( clientInfo.value( QLatin1Literal( "client_secret" ) ).toString() );
if ( clientInfo.contains( QLatin1Literal( "authorization_endpoint" ) ) )
leRequestUrl->setText( clientInfo.value( QLatin1Literal( "authorization_endpoint" ) ).toString() );
if ( clientInfo.contains( QLatin1Literal( "token_endpoint" ) ) )
leTokenUrl->setText( clientInfo.value( QLatin1Literal( "token_endpoint" ) ).toString() );
if ( clientInfo.contains( QLatin1Literal( "scopes" ) ) )
leScope->setText( clientInfo.value( QLatin1Literal( "scopes" ) ).toString() );

tabConfigs->setCurrentIndex( 0 );
}
else
{
QString errorMsg = QStringLiteral( "Client registration failed with error: %1" ).arg( registerReply->errorString() );
QgsMessageLog::logMessage( errorMsg, QLatin1Literal( "OAuth2" ) , Qgis::Critical);
QgsMessageLog::logMessage( errorMsg, QLatin1Literal( "OAuth2" ), Qgis::Critical );
}
mDownloading = false;
registerReply->deleteLater();
}

void QgsAuthOAuth2Edit::networkError(QNetworkReply::NetworkError error)
void QgsAuthOAuth2Edit::networkError( QNetworkReply::NetworkError error )
{
QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
QNetworkReply *reply = qobject_cast<QNetworkReply *>( sender() );
qWarning() << "QgsAuthOAuth2Edit::onNetworkError: " << error << ": " << reply->errorString();
QString errorMsg = QStringLiteral( "Network error: %1" ).arg( reply->errorString() );
QgsMessageLog::logMessage( errorMsg, QLatin1Literal( "OAuth2" ), Qgis::Critical );
qDebug() << "QgsAuthOAuth2Edit::onNetworkError: " << reply->readAll();
}


void QgsAuthOAuth2Edit::registerSoftStatement(const QString& registrationUrl)
void QgsAuthOAuth2Edit::registerSoftStatement( const QString &registrationUrl )
{
QUrl regUrl(registrationUrl);
if( !regUrl.isValid() )
QUrl regUrl( registrationUrl );
if ( !regUrl.isValid() )
{
qWarning()<<"Registration url is not valid";
qWarning() << "Registration url is not valid";
return;
}
QByteArray errStr;
bool res = false;
QByteArray json = QJsonWrapper::toJson(QVariant(mSoftwareStatement),&res,&errStr);
QNetworkRequest registerRequest(regUrl);
registerRequest.setHeader(QNetworkRequest::ContentTypeHeader, QLatin1Literal( "application/json") );
QNetworkReply * registerReply;
QByteArray json = QJsonWrapper::toJson( QVariant( mSoftwareStatement ), &res, &errStr );
QNetworkRequest registerRequest( regUrl );
registerRequest.setHeader( QNetworkRequest::ContentTypeHeader, QLatin1Literal( "application/json" ) );
QNetworkReply *registerReply;
// For testability: use GET if protocol is file://
if ( regUrl.scheme() == QLatin1Literal( "file" ) )
registerReply = QgsNetworkAccessManager::instance()->get(registerRequest);
else
registerReply = QgsNetworkAccessManager::instance()->post(registerRequest, json);
registerReply = QgsNetworkAccessManager::instance()->get( registerRequest );
else
registerReply = QgsNetworkAccessManager::instance()->post( registerRequest, json );
mDownloading = true;
connect(registerReply, &QNetworkReply::finished, this, &QgsAuthOAuth2Edit::registerReplyFinished, Qt::QueuedConnection);
connect(registerReply, qgis::overload<QNetworkReply::NetworkError>::of( &QNetworkReply::error ), this, &QgsAuthOAuth2Edit::networkError, Qt::QueuedConnection);
connect( registerReply, &QNetworkReply::finished, this, &QgsAuthOAuth2Edit::registerReplyFinished, Qt::QueuedConnection );
connect( registerReply, qgis::overload<QNetworkReply::NetworkError>::of( &QNetworkReply::error ), this, &QgsAuthOAuth2Edit::networkError, Qt::QueuedConnection );
}

void QgsAuthOAuth2Edit::getSoftwareStatementConfig()
{
if(!mRegistrationEndpoint.isEmpty())
if ( !mRegistrationEndpoint.isEmpty() )
{
registerSoftStatement(mRegistrationEndpoint);
registerSoftStatement( mRegistrationEndpoint );
}
else
{
QString config = leSoftwareStatementConfigUrl->text();
QUrl configUrl(config);
QNetworkRequest configRequest(configUrl);
QNetworkReply * configReply = QgsNetworkAccessManager::instance()->get(configRequest);
QUrl configUrl( config );
QNetworkRequest configRequest( configUrl );
QNetworkReply *configReply = QgsNetworkAccessManager::instance()->get( configRequest );
mDownloading = true;
connect(configReply, &QNetworkReply::finished, this, &QgsAuthOAuth2Edit::configReplyFinished, Qt::QueuedConnection);
connect(configReply, qgis::overload<QNetworkReply::NetworkError>::of( &QNetworkReply::error ), this, &QgsAuthOAuth2Edit::networkError, Qt::QueuedConnection);
connect( configReply, &QNetworkReply::finished, this, &QgsAuthOAuth2Edit::configReplyFinished, Qt::QueuedConnection );
connect( configReply, qgis::overload<QNetworkReply::NetworkError>::of( &QNetworkReply::error ), this, &QgsAuthOAuth2Edit::networkError, Qt::QueuedConnection );
}
}

@@ -1141,7 +1144,7 @@ QString QgsAuthOAuth2Edit::registrationEndpoint() const
return mRegistrationEndpoint;
}

void QgsAuthOAuth2Edit::setRegistrationEndpoint(const QString& registrationEndpoint)
void QgsAuthOAuth2Edit::setRegistrationEndpoint( const QString &registrationEndpoint )
{
mRegistrationEndpoint = registrationEndpoint;
}
@@ -124,18 +124,18 @@ class QgsAuthOAuth2Edit : public QgsAuthMethodEdit, private Ui::QgsAuthOAuth2Edi

void registerReplyFinished();

void networkError(QNetworkReply::NetworkError error);
void networkError( QNetworkReply::NetworkError error );

//! For testability
QString registrationEndpoint() const;

//! For testability
void setRegistrationEndpoint(const QString& registrationEndpoint);
void setRegistrationEndpoint( const QString &registrationEndpoint );

private:

void initGui();
void parseSoftwareStatement(const QString& path);
void parseSoftwareStatement( const QString &path );

QWidget *parentWidget() const;
QLineEdit *parentNameField() const;
@@ -169,7 +169,7 @@ class QgsAuthOAuth2Edit : public QgsAuthMethodEdit, private Ui::QgsAuthOAuth2Edi
QToolButton *btnTokenClear = nullptr;
QString mRegistrationEndpoint;
QMap<QString, QVariant> mSoftwareStatement;
void registerSoftStatement(const QString& registrationUrl);
void registerSoftStatement( const QString &registrationUrl );
bool mDownloading = false;
friend class TestQgsAuthOAuth2Method;
};
@@ -462,11 +462,11 @@ void QgsAuthOAuth2Method::onRefreshFinished( QNetworkReply::NetworkError err )

void QgsAuthOAuth2Method::onAuthCode()
{
bool ok = false;
QString code = QInputDialog::getText( QApplication::activeWindow(), QStringLiteral( "Enter the authorization code" ) , QStringLiteral("Authoriation code" ), QLineEdit::Normal, QStringLiteral( "Required" ), &ok, Qt::Dialog, Qt::InputMethodHint::ImhNone);
if( ok && !code.isEmpty())
bool ok = false;
QString code = QInputDialog::getText( QApplication::activeWindow(), QStringLiteral( "Enter the authorization code" ), QStringLiteral( "Authoriation code" ), QLineEdit::Normal, QStringLiteral( "Required" ), &ok, Qt::Dialog, Qt::InputMethodHint::ImhNone );
if ( ok && !code.isEmpty() )
{
emit setAuthCode(code);
emit setAuthCode( code );
}
}

@@ -144,7 +144,7 @@ void QgsO2::clearProperties()
// TODO: clear object properties
}

void QgsO2::onSetAuthCode(const QString& code)
void QgsO2::onSetAuthCode( const QString &code )
{
setCode( code );
onVerificationReceived( QMap<QString, QString>() );
@@ -64,7 +64,7 @@ class QgsO2: public O2
void clearProperties();

//! Triggered when auth code was set
void onSetAuthCode(const QString &code);
void onSetAuthCode( const QString &code );

//! Authenticate.
void link() override;

0 comments on commit ad7a574

Please sign in to comment.
You can’t perform that action at this time.