mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
Cloud: Handle refresh of access token.
This commit is contained in:
@@ -25,6 +25,7 @@
|
|||||||
#include "RiaSumoDefines.h"
|
#include "RiaSumoDefines.h"
|
||||||
|
|
||||||
#include <QAbstractOAuth>
|
#include <QAbstractOAuth>
|
||||||
|
#include <QDateTime>
|
||||||
#include <QDesktopServices>
|
#include <QDesktopServices>
|
||||||
#include <QEventLoop>
|
#include <QEventLoop>
|
||||||
#include <QNetworkAccessManager>
|
#include <QNetworkAccessManager>
|
||||||
@@ -100,6 +101,30 @@ RiaCloudConnector::RiaCloudConnector( QObject* parent,
|
|||||||
SIGNAL( authorizationCallbackReceived( const QVariantMap& ) ),
|
SIGNAL( authorizationCallbackReceived( const QVariantMap& ) ),
|
||||||
this,
|
this,
|
||||||
SLOT( authorizationCallbackReceived( const QVariantMap& ) ) );
|
SLOT( authorizationCallbackReceived( const QVariantMap& ) ) );
|
||||||
|
|
||||||
|
connect( m_authCodeFlow,
|
||||||
|
&QOAuth2AuthorizationCodeFlow::tokenChanged,
|
||||||
|
this,
|
||||||
|
[&]( const QString& token )
|
||||||
|
{
|
||||||
|
RiaLogging::debug( "Access token changed." );
|
||||||
|
exportTokenToFile();
|
||||||
|
} );
|
||||||
|
|
||||||
|
connect( m_authCodeFlow,
|
||||||
|
&QOAuth2AuthorizationCodeFlow::refreshTokenChanged,
|
||||||
|
this,
|
||||||
|
[&]( const QString& refreshToken )
|
||||||
|
{
|
||||||
|
RiaLogging::debug( "Refresh token changed." );
|
||||||
|
exportTokenToFile();
|
||||||
|
} );
|
||||||
|
|
||||||
|
connect( m_authCodeFlow,
|
||||||
|
&QOAuth2AuthorizationCodeFlow::expirationAtChanged,
|
||||||
|
this,
|
||||||
|
[&]( const QDateTime& expiration )
|
||||||
|
{ RiaLogging::debug( QString( "Access token expiration changed: %1" ).arg( expiration.toString() ) ); } );
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
@@ -114,12 +139,8 @@ RiaCloudConnector::~RiaCloudConnector()
|
|||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
void RiaCloudConnector::accessGranted()
|
void RiaCloudConnector::accessGranted()
|
||||||
{
|
{
|
||||||
m_token = m_authCodeFlow->token();
|
QString currentToken = m_authCodeFlow->token();
|
||||||
|
emit tokenReady( currentToken );
|
||||||
QString tokenDataJson = RiaConnectorTools::tokenDataAsJson( m_authCodeFlow );
|
|
||||||
RiaConnectorTools::writeTokenData( m_tokenDataFilePath, tokenDataJson );
|
|
||||||
|
|
||||||
emit tokenReady( m_token );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
@@ -155,11 +176,20 @@ void RiaCloudConnector::requestFailed( const QAbstractOAuth::Error error )
|
|||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
void RiaCloudConnector::requestToken()
|
void RiaCloudConnector::requestToken()
|
||||||
{
|
{
|
||||||
if ( m_token.isEmpty() )
|
if ( token().isEmpty() )
|
||||||
{
|
{
|
||||||
RiaLogging::debug( "Requesting token." );
|
RiaLogging::debug( "No valid access token found." );
|
||||||
|
if ( !m_authCodeFlow->refreshToken().isEmpty() )
|
||||||
|
{
|
||||||
|
RiaLogging::info( "Refreshing access token with refresh token." );
|
||||||
|
m_authCodeFlow->refreshAccessToken();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
RiaLogging::info( "Requesting token." );
|
||||||
m_authCodeFlow->grant();
|
m_authCodeFlow->grant();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
RiaLogging::debug( "Has token: skipping token request." );
|
RiaLogging::debug( "Has token: skipping token request." );
|
||||||
@@ -172,7 +202,25 @@ void RiaCloudConnector::requestToken()
|
|||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
QString RiaCloudConnector::token() const
|
QString RiaCloudConnector::token() const
|
||||||
{
|
{
|
||||||
return m_token;
|
QString currentToken = m_authCodeFlow->token();
|
||||||
|
QDateTime expiration = m_authCodeFlow->expirationAt();
|
||||||
|
if ( !currentToken.isEmpty() && expiration.isValid() && expiration > QDateTime::currentDateTime() )
|
||||||
|
{
|
||||||
|
return currentToken;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return QString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
///
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
void RiaCloudConnector::exportTokenToFile()
|
||||||
|
{
|
||||||
|
QString tokenDataJson = RiaConnectorTools::tokenDataAsJson( m_authCodeFlow );
|
||||||
|
RiaConnectorTools::writeTokenData( m_tokenDataFilePath, tokenDataJson );
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
@@ -184,7 +232,6 @@ void RiaCloudConnector::importTokenFromFile()
|
|||||||
if ( !tokenDataJson.isEmpty() )
|
if ( !tokenDataJson.isEmpty() )
|
||||||
{
|
{
|
||||||
RiaConnectorTools::initializeTokenDataFromJson( m_authCodeFlow, tokenDataJson );
|
RiaConnectorTools::initializeTokenDataFromJson( m_authCodeFlow, tokenDataJson );
|
||||||
m_token = m_authCodeFlow->token();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -217,7 +264,8 @@ QString RiaCloudConnector::constructTokenUrl( const QString& authority )
|
|||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
QString RiaCloudConnector::requestTokenBlocking()
|
QString RiaCloudConnector::requestTokenBlocking()
|
||||||
{
|
{
|
||||||
if ( !m_token.isEmpty() ) return m_token;
|
QString currentToken = token();
|
||||||
|
if ( !currentToken.isEmpty() ) return currentToken;
|
||||||
|
|
||||||
QTimer timer;
|
QTimer timer;
|
||||||
timer.setSingleShot( true );
|
timer.setSingleShot( true );
|
||||||
@@ -227,7 +275,7 @@ QString RiaCloudConnector::requestTokenBlocking()
|
|||||||
requestToken();
|
requestToken();
|
||||||
timer.start( RiaSumoDefines::requestTimeoutMillis() );
|
timer.start( RiaSumoDefines::requestTimeoutMillis() );
|
||||||
loop.exec();
|
loop.exec();
|
||||||
return m_token;
|
return m_authCodeFlow->token();
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
|||||||
@@ -42,13 +42,14 @@ public:
|
|||||||
QString token() const;
|
QString token() const;
|
||||||
|
|
||||||
void importTokenFromFile();
|
void importTokenFromFile();
|
||||||
|
void exportTokenToFile();
|
||||||
|
|
||||||
void setTokenDataFilePath( const QString& filePath );
|
void setTokenDataFilePath( const QString& filePath );
|
||||||
|
|
||||||
QString server() const;
|
QString server() const;
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void requestToken();
|
void requestToken();
|
||||||
void accessGranted();
|
|
||||||
void requestFailed( const QAbstractOAuth::Error error );
|
void requestFailed( const QAbstractOAuth::Error error );
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
@@ -57,6 +58,7 @@ signals:
|
|||||||
private slots:
|
private slots:
|
||||||
void errorReceived( const QString& error, const QString& errorDescription, const QUrl& uri );
|
void errorReceived( const QString& error, const QString& errorDescription, const QUrl& uri );
|
||||||
void authorizationCallbackReceived( const QVariantMap& data );
|
void authorizationCallbackReceived( const QVariantMap& data );
|
||||||
|
void accessGranted();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
QString requestTokenBlocking();
|
QString requestTokenBlocking();
|
||||||
@@ -72,7 +74,5 @@ protected:
|
|||||||
const QString m_scopes;
|
const QString m_scopes;
|
||||||
const QString m_clientId;
|
const QString m_clientId;
|
||||||
|
|
||||||
QString m_token;
|
|
||||||
|
|
||||||
QString m_tokenDataFilePath;
|
QString m_tokenDataFilePath;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -77,7 +77,7 @@ void RiaOsduConnector::requestFieldsByName( const QString& token, const QString&
|
|||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
void RiaOsduConnector::requestFieldsByName( const QString& fieldName )
|
void RiaOsduConnector::requestFieldsByName( const QString& fieldName )
|
||||||
{
|
{
|
||||||
requestFieldsByName( m_token, fieldName );
|
requestFieldsByName( token(), fieldName );
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
@@ -113,7 +113,7 @@ void RiaOsduConnector::requestFieldsByName( const QString& server, const QString
|
|||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
void RiaOsduConnector::requestWellsByFieldId( const QString& fieldId )
|
void RiaOsduConnector::requestWellsByFieldId( const QString& fieldId )
|
||||||
{
|
{
|
||||||
requestWellsByFieldId( m_server, m_dataPartitionId, m_token, fieldId );
|
requestWellsByFieldId( m_server, m_dataPartitionId, token(), fieldId );
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
@@ -149,7 +149,7 @@ void RiaOsduConnector::requestWellsByFieldId( const QString& server, const QStri
|
|||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
void RiaOsduConnector::requestWellboresByWellId( const QString& wellId )
|
void RiaOsduConnector::requestWellboresByWellId( const QString& wellId )
|
||||||
{
|
{
|
||||||
requestWellboresByWellId( m_server, m_dataPartitionId, m_token, wellId );
|
requestWellboresByWellId( m_server, m_dataPartitionId, token(), wellId );
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
@@ -200,7 +200,7 @@ std::vector<OsduWellLog> RiaOsduConnector::requestWellLogsByWellboreIdBlocking(
|
|||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
void RiaOsduConnector::requestWellLogsByWellboreId( const QString& wellboreId )
|
void RiaOsduConnector::requestWellLogsByWellboreId( const QString& wellboreId )
|
||||||
{
|
{
|
||||||
requestWellLogsByWellboreId( m_server, m_dataPartitionId, m_token, wellboreId );
|
requestWellLogsByWellboreId( m_server, m_dataPartitionId, token(), wellboreId );
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
@@ -239,7 +239,7 @@ void RiaOsduConnector::requestWellLogsByWellboreId( const QString& server,
|
|||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
void RiaOsduConnector::requestWellboreTrajectoryByWellboreId( const QString& wellboreId )
|
void RiaOsduConnector::requestWellboreTrajectoryByWellboreId( const QString& wellboreId )
|
||||||
{
|
{
|
||||||
requestWellboreTrajectoryByWellboreId( m_server, m_dataPartitionId, m_token, wellboreId );
|
requestWellboreTrajectoryByWellboreId( m_server, m_dataPartitionId, token(), wellboreId );
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
@@ -709,8 +709,7 @@ void RiaOsduConnector::requestWellLogParquetDataById( const QString& wellLogId )
|
|||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
void RiaOsduConnector::requestParquetDataByUrl( const QString& url, const QString& id )
|
void RiaOsduConnector::requestParquetDataByUrl( const QString& url, const QString& id )
|
||||||
{
|
{
|
||||||
QString token = m_token;
|
requestParquetData( url, m_dataPartitionId, token(), id );
|
||||||
requestParquetData( url, m_dataPartitionId, token, id );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
|||||||
@@ -95,7 +95,7 @@ void RiaSumoConnector::requestCasesForField( const QString& fieldName )
|
|||||||
QNetworkRequest m_networkRequest;
|
QNetworkRequest m_networkRequest;
|
||||||
m_networkRequest.setUrl( QUrl( constructSearchUrl( m_server ) ) );
|
m_networkRequest.setUrl( QUrl( constructSearchUrl( m_server ) ) );
|
||||||
|
|
||||||
addStandardHeader( m_networkRequest, m_token, RiaDefines::contentTypeJson() );
|
addStandardHeader( m_networkRequest, token(), RiaDefines::contentTypeJson() );
|
||||||
|
|
||||||
QString payloadTemplate = R"(
|
QString payloadTemplate = R"(
|
||||||
{
|
{
|
||||||
@@ -156,7 +156,7 @@ void RiaSumoConnector::requestAssets()
|
|||||||
QNetworkRequest m_networkRequest;
|
QNetworkRequest m_networkRequest;
|
||||||
m_networkRequest.setUrl( QUrl( m_server + "/api/v1/userpermissions" ) );
|
m_networkRequest.setUrl( QUrl( m_server + "/api/v1/userpermissions" ) );
|
||||||
|
|
||||||
addStandardHeader( m_networkRequest, m_token, RiaDefines::contentTypeJson() );
|
addStandardHeader( m_networkRequest, token(), RiaDefines::contentTypeJson() );
|
||||||
|
|
||||||
auto reply = m_networkAccessManager->get( m_networkRequest );
|
auto reply = m_networkAccessManager->get( m_networkRequest );
|
||||||
|
|
||||||
@@ -218,7 +218,7 @@ void RiaSumoConnector::requestEnsembleByCasesId( const SumoCaseId& caseId )
|
|||||||
QNetworkRequest m_networkRequest;
|
QNetworkRequest m_networkRequest;
|
||||||
m_networkRequest.setUrl( QUrl( m_server + "/api/v1/search" ) );
|
m_networkRequest.setUrl( QUrl( m_server + "/api/v1/search" ) );
|
||||||
|
|
||||||
addStandardHeader( m_networkRequest, m_token, RiaDefines::contentTypeJson() );
|
addStandardHeader( m_networkRequest, token(), RiaDefines::contentTypeJson() );
|
||||||
|
|
||||||
auto payload = payloadTemplate.arg( caseId.get() );
|
auto payload = payloadTemplate.arg( caseId.get() );
|
||||||
auto reply = m_networkAccessManager->post( m_networkRequest, payload.toUtf8() );
|
auto reply = m_networkAccessManager->post( m_networkRequest, payload.toUtf8() );
|
||||||
@@ -291,7 +291,7 @@ void RiaSumoConnector::requestVectorNamesForEnsemble( const SumoCaseId& caseId,
|
|||||||
QNetworkRequest m_networkRequest;
|
QNetworkRequest m_networkRequest;
|
||||||
m_networkRequest.setUrl( QUrl( m_server + "/api/v1/search" ) );
|
m_networkRequest.setUrl( QUrl( m_server + "/api/v1/search" ) );
|
||||||
|
|
||||||
addStandardHeader( m_networkRequest, m_token, RiaDefines::contentTypeJson() );
|
addStandardHeader( m_networkRequest, token(), RiaDefines::contentTypeJson() );
|
||||||
|
|
||||||
auto payload = payloadTemplate.arg( caseId.get() ).arg( ensembleName );
|
auto payload = payloadTemplate.arg( caseId.get() ).arg( ensembleName );
|
||||||
auto reply = m_networkAccessManager->post( m_networkRequest, payload.toUtf8() );
|
auto reply = m_networkAccessManager->post( m_networkRequest, payload.toUtf8() );
|
||||||
@@ -360,7 +360,7 @@ void RiaSumoConnector::requestRealizationIdsForEnsemble( const SumoCaseId& caseI
|
|||||||
QNetworkRequest m_networkRequest;
|
QNetworkRequest m_networkRequest;
|
||||||
m_networkRequest.setUrl( QUrl( m_server + "/api/v1/search" ) );
|
m_networkRequest.setUrl( QUrl( m_server + "/api/v1/search" ) );
|
||||||
|
|
||||||
addStandardHeader( m_networkRequest, m_token, RiaDefines::contentTypeJson() );
|
addStandardHeader( m_networkRequest, token(), RiaDefines::contentTypeJson() );
|
||||||
|
|
||||||
auto payload = payloadTemplate.arg( caseId.get() ).arg( ensembleName );
|
auto payload = payloadTemplate.arg( caseId.get() ).arg( ensembleName );
|
||||||
auto reply = m_networkAccessManager->post( m_networkRequest, payload.toUtf8() );
|
auto reply = m_networkAccessManager->post( m_networkRequest, payload.toUtf8() );
|
||||||
@@ -423,7 +423,7 @@ void RiaSumoConnector::requestBlobIdForEnsemble( const SumoCaseId& caseId, const
|
|||||||
QNetworkRequest m_networkRequest;
|
QNetworkRequest m_networkRequest;
|
||||||
m_networkRequest.setUrl( QUrl( m_server + "/api/v1/search" ) );
|
m_networkRequest.setUrl( QUrl( m_server + "/api/v1/search" ) );
|
||||||
|
|
||||||
addStandardHeader( m_networkRequest, m_token, RiaDefines::contentTypeJson() );
|
addStandardHeader( m_networkRequest, token(), RiaDefines::contentTypeJson() );
|
||||||
|
|
||||||
auto payload = payloadTemplate.arg( caseId.get() ).arg( ensembleName ).arg( vectorName );
|
auto payload = payloadTemplate.arg( caseId.get() ).arg( ensembleName ).arg( vectorName );
|
||||||
auto reply = m_networkAccessManager->post( m_networkRequest, payload.toUtf8() );
|
auto reply = m_networkAccessManager->post( m_networkRequest, payload.toUtf8() );
|
||||||
@@ -469,7 +469,7 @@ void RiaSumoConnector::requestBlobDownload( const QString& blobId )
|
|||||||
// did not work. Use ManualRedirectPolicy instead, and inspect the reply for the redirection target.
|
// did not work. Use ManualRedirectPolicy instead, and inspect the reply for the redirection target.
|
||||||
networkRequest.setAttribute( QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::ManualRedirectPolicy );
|
networkRequest.setAttribute( QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::ManualRedirectPolicy );
|
||||||
|
|
||||||
addStandardHeader( networkRequest, m_token, RiaDefines::contentTypeJson() );
|
addStandardHeader( networkRequest, token(), RiaDefines::contentTypeJson() );
|
||||||
|
|
||||||
auto reply = m_networkAccessManager->get( networkRequest );
|
auto reply = m_networkAccessManager->get( networkRequest );
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user