mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
Cloud: Extract common oauth2 parts of Sumo and Osdu connector.
This commit is contained in:
parent
cc2d1de188
commit
eda26bcc25
@ -1711,7 +1711,12 @@ RiaOsduConnector* RiaApplication::makeOsduConnector()
|
||||
const QString authority = osduPreferences->authority();
|
||||
const QString scopes = osduPreferences->scopes();
|
||||
const QString clientId = osduPreferences->clientId();
|
||||
m_osduConnector = new RiaOsduConnector( RiuMainWindow::instance(), server, dataPartitionId, authority, scopes, clientId );
|
||||
const unsigned int port = 35327;
|
||||
|
||||
m_osduConnector = new RiaOsduConnector( RiuMainWindow::instance(), server, dataPartitionId, authority, scopes, clientId, port );
|
||||
m_osduConnector->setTokenDataFilePath( RiaSumoDefines::tokenPath() );
|
||||
m_osduConnector->importTokenFromFile();
|
||||
|
||||
return m_osduConnector;
|
||||
}
|
||||
|
||||
@ -1722,13 +1727,14 @@ RiaSumoConnector* RiaApplication::makeSumoConnector()
|
||||
{
|
||||
if ( !m_sumoConnector )
|
||||
{
|
||||
auto sumoPrefs = preferences()->sumoPreferences();
|
||||
const QString server = sumoPrefs->server();
|
||||
const QString authority = sumoPrefs->authority();
|
||||
const QString scopes = sumoPrefs->scopes();
|
||||
const QString clientId = sumoPrefs->clientId();
|
||||
auto sumoPrefs = preferences()->sumoPreferences();
|
||||
const QString server = sumoPrefs->server();
|
||||
const QString authority = sumoPrefs->authority();
|
||||
const QString scopes = sumoPrefs->scopes();
|
||||
const QString clientId = sumoPrefs->clientId();
|
||||
const unsigned int port = 53527;
|
||||
|
||||
m_sumoConnector = new RiaSumoConnector( RiuMainWindow::instance(), server, authority, scopes, clientId );
|
||||
m_sumoConnector = new RiaSumoConnector( RiuMainWindow::instance(), server, authority, scopes, clientId, port );
|
||||
|
||||
m_sumoConnector->setTokenDataFilePath( RiaSumoDefines::tokenPath() );
|
||||
m_sumoConnector->importTokenFromFile();
|
||||
|
@ -1,4 +1,5 @@
|
||||
set(SOURCE_GROUP_HEADER_FILES
|
||||
${CMAKE_CURRENT_LIST_DIR}/RiaCloudConnector.h
|
||||
${CMAKE_CURRENT_LIST_DIR}/RiaSumoConnector.h
|
||||
${CMAKE_CURRENT_LIST_DIR}/RiaSumoDefines.h
|
||||
${CMAKE_CURRENT_LIST_DIR}/RiaConnectorTools.h
|
||||
@ -7,6 +8,7 @@ set(SOURCE_GROUP_HEADER_FILES
|
||||
)
|
||||
|
||||
set(SOURCE_GROUP_SOURCE_FILES
|
||||
${CMAKE_CURRENT_LIST_DIR}/RiaCloudConnector.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RiaSumoConnector.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RiaSumoDefines.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RiaConnectorTools.cpp
|
||||
@ -14,9 +16,13 @@ set(SOURCE_GROUP_SOURCE_FILES
|
||||
${CMAKE_CURRENT_LIST_DIR}/RiaOsduOAuthHttpServerReplyHandler.cpp
|
||||
)
|
||||
|
||||
list(APPEND QT_MOC_HEADERS ${CMAKE_CURRENT_LIST_DIR}/RiaSumoConnector.h
|
||||
${CMAKE_CURRENT_LIST_DIR}/RiaOsduConnector.h
|
||||
${CMAKE_CURRENT_LIST_DIR}/RiaOsduOAuthHttpServerReplyHandler.h
|
||||
list(
|
||||
APPEND
|
||||
QT_MOC_HEADERS
|
||||
${CMAKE_CURRENT_LIST_DIR}/RiaCloudConnector.h
|
||||
${CMAKE_CURRENT_LIST_DIR}/RiaSumoConnector.h
|
||||
${CMAKE_CURRENT_LIST_DIR}/RiaOsduConnector.h
|
||||
${CMAKE_CURRENT_LIST_DIR}/RiaOsduOAuthHttpServerReplyHandler.h
|
||||
)
|
||||
|
||||
list(APPEND CODE_HEADER_FILES ${SOURCE_GROUP_HEADER_FILES})
|
||||
|
239
ApplicationLibCode/Application/Tools/Cloud/RiaCloudConnector.cpp
Normal file
239
ApplicationLibCode/Application/Tools/Cloud/RiaCloudConnector.cpp
Normal file
@ -0,0 +1,239 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Copyright (C) 2024 Equinor ASA
|
||||
//
|
||||
// ResInsight is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
// FITNESS FOR A PARTICULAR PURPOSE.
|
||||
//
|
||||
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
|
||||
// for more details.
|
||||
//
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "RiaCloudConnector.h"
|
||||
|
||||
#include "RiaConnectorTools.h"
|
||||
#include "RiaLogging.h"
|
||||
#include "RiaOsduDefines.h"
|
||||
#include "RiaOsduOAuthHttpServerReplyHandler.h"
|
||||
#include "RiaSumoDefines.h"
|
||||
|
||||
#include <QAbstractOAuth>
|
||||
#include <QDesktopServices>
|
||||
#include <QEventLoop>
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QNetworkReply>
|
||||
#include <QNetworkRequest>
|
||||
#include <QOAuth2AuthorizationCodeFlow>
|
||||
#include <QOAuthHttpServerReplyHandler>
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
#include <QTimer>
|
||||
#include <QUrl>
|
||||
#include <QUrlQuery>
|
||||
#include <QtCore>
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
RiaCloudConnector::RiaCloudConnector( QObject* parent,
|
||||
const QString& server,
|
||||
const QString& authority,
|
||||
const QString& scopes,
|
||||
const QString& clientId,
|
||||
unsigned int port )
|
||||
: QObject( parent )
|
||||
, m_server( server )
|
||||
, m_authority( authority )
|
||||
, m_scopes( scopes )
|
||||
, m_clientId( clientId )
|
||||
{
|
||||
m_authCodeFlow = new QOAuth2AuthorizationCodeFlow( this );
|
||||
m_networkAccessManager = new QNetworkAccessManager( this );
|
||||
m_authCodeFlow->setNetworkAccessManager( m_networkAccessManager );
|
||||
|
||||
RiaLogging::debug( "SSL BUILD VERSION: " + QSslSocket::sslLibraryBuildVersionString() );
|
||||
RiaLogging::debug( "SSL VERSION STRING: " + QSslSocket::sslLibraryVersionString() );
|
||||
|
||||
RiaLogging::debug( "Cloud config:" );
|
||||
RiaLogging::debug( " server: '" + server + "'" );
|
||||
RiaLogging::debug( " authority: '" + authority + "'" );
|
||||
RiaLogging::debug( " scopes: '" + scopes + "'" );
|
||||
RiaLogging::debug( " client id: '" + clientId + "'" );
|
||||
|
||||
connect( m_authCodeFlow,
|
||||
&QOAuth2AuthorizationCodeFlow::authorizeWithBrowser,
|
||||
[]( QUrl url )
|
||||
{
|
||||
RiaLogging::info( "Authorize with url: " + url.toString() );
|
||||
QUrlQuery query( url );
|
||||
url.setQuery( query );
|
||||
QDesktopServices::openUrl( url );
|
||||
} );
|
||||
|
||||
QString authUrl = constructAuthUrl( m_authority );
|
||||
m_authCodeFlow->setAuthorizationUrl( QUrl( authUrl ) );
|
||||
|
||||
QString tokenUrl = constructTokenUrl( m_authority );
|
||||
m_authCodeFlow->setAccessTokenUrl( QUrl( tokenUrl ) );
|
||||
|
||||
// App key
|
||||
m_authCodeFlow->setClientIdentifier( m_clientId );
|
||||
m_authCodeFlow->setScope( m_scopes );
|
||||
|
||||
auto replyHandler = new RiaOsduOAuthHttpServerReplyHandler( port, this );
|
||||
m_authCodeFlow->setReplyHandler( replyHandler );
|
||||
RiaLogging::debug( "Server callback: " + replyHandler->callback() );
|
||||
|
||||
connect( m_authCodeFlow, SIGNAL( granted() ), this, SLOT( accessGranted() ) );
|
||||
connect( m_authCodeFlow,
|
||||
SIGNAL( error( const QString&, const QString&, const QUrl& ) ),
|
||||
this,
|
||||
SLOT( errorReceived( const QString&, const QString&, const QUrl& ) ) );
|
||||
connect( m_authCodeFlow,
|
||||
SIGNAL( authorizationCallbackReceived( const QVariantMap& ) ),
|
||||
this,
|
||||
SLOT( authorizationCallbackReceived( const QVariantMap& ) ) );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
RiaCloudConnector::~RiaCloudConnector()
|
||||
{
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RiaCloudConnector::accessGranted()
|
||||
{
|
||||
m_token = m_authCodeFlow->token();
|
||||
|
||||
QString tokenDataJson = RiaConnectorTools::tokenDataAsJson( m_authCodeFlow );
|
||||
RiaConnectorTools::writeTokenData( m_tokenDataFilePath, tokenDataJson );
|
||||
|
||||
emit tokenReady( m_token );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RiaCloudConnector::errorReceived( const QString& error, const QString& errorDescription, const QUrl& uri )
|
||||
{
|
||||
RiaLogging::debug( "Cloud Error Received: " + error + ". Description: " + errorDescription );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RiaCloudConnector::authorizationCallbackReceived( const QVariantMap& data )
|
||||
{
|
||||
RiaLogging::debug( "Authorization callback received:" );
|
||||
for ( const auto& [key, value] : data.toStdMap() )
|
||||
{
|
||||
RiaLogging::debug( " Key: " + key + " Value: " + value.toString() );
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RiaCloudConnector::requestFailed( const QAbstractOAuth::Error error )
|
||||
{
|
||||
RiaLogging::error( "Request failed: " );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RiaCloudConnector::requestToken()
|
||||
{
|
||||
if ( m_token.isEmpty() )
|
||||
{
|
||||
RiaLogging::debug( "Requesting token." );
|
||||
m_authCodeFlow->grant();
|
||||
}
|
||||
else
|
||||
{
|
||||
RiaLogging::debug( "Has token: skipping token request." );
|
||||
emit accessGranted();
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
QString RiaCloudConnector::token() const
|
||||
{
|
||||
return m_token;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RiaCloudConnector::importTokenFromFile()
|
||||
{
|
||||
auto tokenDataJson = RiaConnectorTools::readTokenData( m_tokenDataFilePath );
|
||||
if ( !tokenDataJson.isEmpty() )
|
||||
{
|
||||
RiaConnectorTools::initializeTokenDataFromJson( m_authCodeFlow, tokenDataJson );
|
||||
m_token = m_authCodeFlow->token();
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RiaCloudConnector::setTokenDataFilePath( const QString& filePath )
|
||||
{
|
||||
m_tokenDataFilePath = filePath;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
QString RiaCloudConnector::constructAuthUrl( const QString& authority )
|
||||
{
|
||||
return authority + "/oauth2/v2.0/authorize";
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
QString RiaCloudConnector::constructTokenUrl( const QString& authority )
|
||||
{
|
||||
return authority + "/oauth2/v2.0/token";
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
QString RiaCloudConnector::requestTokenBlocking()
|
||||
{
|
||||
if ( !m_token.isEmpty() ) return m_token;
|
||||
|
||||
QTimer timer;
|
||||
timer.setSingleShot( true );
|
||||
QEventLoop loop;
|
||||
connect( this, SIGNAL( tokenReady( const QString& ) ), &loop, SLOT( quit() ) );
|
||||
connect( &timer, SIGNAL( timeout() ), &loop, SLOT( quit() ) );
|
||||
requestToken();
|
||||
timer.start( RiaSumoDefines::requestTimeoutMillis() );
|
||||
loop.exec();
|
||||
return m_token;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
QString RiaCloudConnector::server() const
|
||||
{
|
||||
return m_server;
|
||||
}
|
@ -0,0 +1,78 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Copyright (C) 2024 Equinor ASA
|
||||
//
|
||||
// ResInsight is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
// FITNESS FOR A PARTICULAR PURPOSE.
|
||||
//
|
||||
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
|
||||
// for more details.
|
||||
//
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QByteArray>
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QOAuth2AuthorizationCodeFlow>
|
||||
|
||||
// #include <map>
|
||||
|
||||
//==================================================================================================
|
||||
///
|
||||
//==================================================================================================
|
||||
class RiaCloudConnector : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
RiaCloudConnector( QObject* parent,
|
||||
const QString& server,
|
||||
const QString& authority,
|
||||
const QString& scopes,
|
||||
const QString& clientId,
|
||||
unsigned int port );
|
||||
~RiaCloudConnector() override;
|
||||
|
||||
QString token() const;
|
||||
|
||||
void importTokenFromFile();
|
||||
void setTokenDataFilePath( const QString& filePath );
|
||||
|
||||
QString server() const;
|
||||
|
||||
public slots:
|
||||
void requestToken();
|
||||
void accessGranted();
|
||||
void requestFailed( const QAbstractOAuth::Error error );
|
||||
|
||||
signals:
|
||||
void tokenReady( const QString& token );
|
||||
|
||||
private slots:
|
||||
void errorReceived( const QString& error, const QString& errorDescription, const QUrl& uri );
|
||||
void authorizationCallbackReceived( const QVariantMap& data );
|
||||
|
||||
protected:
|
||||
QString requestTokenBlocking();
|
||||
|
||||
static QString constructAuthUrl( const QString& authority );
|
||||
static QString constructTokenUrl( const QString& authority );
|
||||
|
||||
QOAuth2AuthorizationCodeFlow* m_authCodeFlow;
|
||||
QNetworkAccessManager* m_networkAccessManager;
|
||||
|
||||
const QString m_server;
|
||||
const QString m_authority;
|
||||
const QString m_scopes;
|
||||
const QString m_clientId;
|
||||
|
||||
QString m_token;
|
||||
|
||||
QString m_tokenDataFilePath;
|
||||
};
|
@ -31,118 +31,17 @@ RiaOsduConnector::RiaOsduConnector( QObject* parent,
|
||||
const QString& dataPartitionId,
|
||||
const QString& authority,
|
||||
const QString& scopes,
|
||||
const QString& clientId )
|
||||
: QObject( parent )
|
||||
, m_server( server )
|
||||
const QString& clientId,
|
||||
unsigned int port )
|
||||
: RiaCloudConnector( parent, server, authority, scopes, clientId, port )
|
||||
, m_dataPartitionId( dataPartitionId )
|
||||
, m_authority( authority )
|
||||
, m_scopes( scopes )
|
||||
, m_clientId( clientId )
|
||||
{
|
||||
m_networkAccessManager = new QNetworkAccessManager( this );
|
||||
|
||||
m_osdu = new QOAuth2AuthorizationCodeFlow( this );
|
||||
|
||||
RiaLogging::debug( "SSL BUILD VERSION: " + QSslSocket::sslLibraryBuildVersionString() );
|
||||
RiaLogging::debug( "SSL VERSION STRING: " + QSslSocket::sslLibraryVersionString() );
|
||||
|
||||
RiaLogging::debug( "OSDU config:" );
|
||||
RiaLogging::debug( " server: '" + server + "'" );
|
||||
RiaLogging::debug( " data partition id: '" + dataPartitionId + "'" );
|
||||
RiaLogging::debug( " authority: '" + authority + "'" );
|
||||
RiaLogging::debug( " scopes: '" + scopes + "'" );
|
||||
RiaLogging::debug( " client id: '" + clientId + "'" );
|
||||
|
||||
int port = 35327;
|
||||
|
||||
connect( m_osdu,
|
||||
&QOAuth2AuthorizationCodeFlow::authorizeWithBrowser,
|
||||
[]( QUrl url )
|
||||
{
|
||||
RiaLogging::debug( "Authorize with url: " + url.toString() );
|
||||
QUrlQuery query( url );
|
||||
url.setQuery( query );
|
||||
QDesktopServices::openUrl( url );
|
||||
} );
|
||||
|
||||
QString authUrl = constructAuthUrl( m_authority );
|
||||
m_osdu->setAuthorizationUrl( QUrl( authUrl ) );
|
||||
|
||||
QString tokenUrl = constructTokenUrl( m_authority );
|
||||
m_osdu->setAccessTokenUrl( QUrl( tokenUrl ) );
|
||||
|
||||
// App key
|
||||
m_osdu->setClientIdentifier( m_clientId );
|
||||
m_osdu->setScope( m_scopes );
|
||||
|
||||
auto replyHandler = new RiaOsduOAuthHttpServerReplyHandler( port, this );
|
||||
m_osdu->setReplyHandler( replyHandler );
|
||||
RiaLogging::debug( "Osdu server callback: " + replyHandler->callback() );
|
||||
|
||||
connect( m_osdu, SIGNAL( granted() ), this, SLOT( accessGranted() ) );
|
||||
connect( m_osdu,
|
||||
SIGNAL( error( const QString&, const QString&, const QUrl& ) ),
|
||||
this,
|
||||
SLOT( errorReceived( const QString&, const QString&, const QUrl& ) ) );
|
||||
|
||||
connect( m_osdu,
|
||||
SIGNAL( authorizationCallbackReceived( const QVariantMap& ) ),
|
||||
this,
|
||||
SLOT( authorizationCallbackReceived( const QVariantMap& ) ) );
|
||||
|
||||
connect( this,
|
||||
SIGNAL( parquetDownloadFinished( const QByteArray&, const QString&, const QString& ) ),
|
||||
this,
|
||||
SLOT( parquetDownloadComplete( const QByteArray&, const QString&, const QString& ) ) );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RiaOsduConnector::accessGranted()
|
||||
{
|
||||
RiaLogging::debug( "Access granted." );
|
||||
m_token = m_osdu->token();
|
||||
emit tokenReady( m_token );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RiaOsduConnector::errorReceived( const QString& error, const QString& errorDescription, const QUrl& uri )
|
||||
{
|
||||
RiaLogging::debug( "OSDU Error Received: " + error + ". Description: " + errorDescription );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RiaOsduConnector::authorizationCallbackReceived( const QVariantMap& data )
|
||||
{
|
||||
RiaLogging::debug( "Authorization callback received:" );
|
||||
for ( const auto& [key, value] : data.toStdMap() )
|
||||
{
|
||||
RiaLogging::debug( " Key: " + key + " Value: " + value.toString() );
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RiaOsduConnector::requestToken()
|
||||
{
|
||||
if ( m_token.isEmpty() )
|
||||
{
|
||||
RiaLogging::debug( "Requesting token." );
|
||||
m_osdu->grant();
|
||||
}
|
||||
else
|
||||
{
|
||||
RiaLogging::debug( "Has token: skipping token request." );
|
||||
emit accessGranted();
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
@ -390,22 +289,6 @@ QString RiaOsduConnector::constructFileDownloadUrl( const QString& server, const
|
||||
return server + "/api/file/v2/files/" + fileId + "/downloadURL";
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
QString RiaOsduConnector::constructAuthUrl( const QString& authority )
|
||||
{
|
||||
return authority + "/oauth2/v2.0/authorize";
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
QString RiaOsduConnector::constructTokenUrl( const QString& authority )
|
||||
{
|
||||
return authority + "/oauth2/v2.0/token";
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
@ -707,14 +590,6 @@ QNetworkReply*
|
||||
return reply;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
QString RiaOsduConnector::server() const
|
||||
{
|
||||
return m_server;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
@ -912,19 +787,3 @@ void RiaOsduConnector::parquetDownloadComplete( const QByteArray& contents, cons
|
||||
m_parquetData[id] = contents;
|
||||
m_parquetErrors[id] = errorMessage;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
QString RiaOsduConnector::requestTokenBlocking()
|
||||
{
|
||||
if ( m_token.isEmpty() )
|
||||
{
|
||||
QEventLoop loop;
|
||||
connect( this, SIGNAL( tokenReady( const QString& ) ), &loop, SLOT( quit() ) );
|
||||
requestToken();
|
||||
loop.exec();
|
||||
}
|
||||
|
||||
return m_token;
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "RiaCloudConnector.h"
|
||||
|
||||
#include <QtCore>
|
||||
|
||||
#include <QNetworkAccessManager>
|
||||
@ -64,7 +66,7 @@ struct OsduWellLog
|
||||
//==================================================================================================
|
||||
///
|
||||
//==================================================================================================
|
||||
class RiaOsduConnector : public QObject
|
||||
class RiaOsduConnector : public RiaCloudConnector
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
@ -73,7 +75,8 @@ public:
|
||||
const QString& dataParitionId,
|
||||
const QString& authority,
|
||||
const QString& scopes,
|
||||
const QString& clientId );
|
||||
const QString& clientId,
|
||||
unsigned int port );
|
||||
~RiaOsduConnector() override;
|
||||
|
||||
void requestFieldsByName( const QString& fieldName );
|
||||
@ -93,7 +96,6 @@ public:
|
||||
|
||||
void clearCachedData();
|
||||
|
||||
QString server() const;
|
||||
QString dataPartition() const;
|
||||
|
||||
std::vector<OsduField> fields() const;
|
||||
@ -103,14 +105,11 @@ public:
|
||||
std::vector<OsduWellLog> wellLogs( const QString& wellboreId ) const;
|
||||
|
||||
public slots:
|
||||
void requestToken();
|
||||
void parseFields( QNetworkReply* reply );
|
||||
void parseWells( QNetworkReply* reply );
|
||||
void parseWellbores( QNetworkReply* reply, const QString& wellId );
|
||||
void parseWellTrajectory( QNetworkReply* reply, const QString& wellboreId );
|
||||
void parseWellLogs( QNetworkReply* reply, const QString& wellboreId );
|
||||
|
||||
void accessGranted();
|
||||
void parquetDownloadComplete( const QByteArray&, const QString& url, const QString& id );
|
||||
|
||||
signals:
|
||||
@ -120,11 +119,8 @@ signals:
|
||||
void wellboresFinished( const QString& wellId );
|
||||
void wellboreTrajectoryFinished( const QString& wellboreId, int numTrajectories, const QString& errorMessage );
|
||||
void wellLogsFinished( const QString& wellboreId );
|
||||
void tokenReady( const QString& token );
|
||||
|
||||
private slots:
|
||||
void errorReceived( const QString& error, const QString& errorDescription, const QUrl& uri );
|
||||
void authorizationCallbackReceived( const QVariantMap& data );
|
||||
void requestParquetData( const QString& url, const QString& dataPartitionId, const QString& token, const QString& id );
|
||||
|
||||
private:
|
||||
@ -149,27 +145,15 @@ private:
|
||||
|
||||
static QString constructSearchUrl( const QString& server );
|
||||
static QString constructFileDownloadUrl( const QString& server, const QString& fileId );
|
||||
static QString constructAuthUrl( const QString& authority );
|
||||
static QString constructTokenUrl( const QString& authority );
|
||||
static QString constructWellLogDownloadUrl( const QString& server, const QString& wellLogId );
|
||||
static QString constructWellboreTrajectoriesDownloadUrl( const QString& server, const QString& wellboreTrajectoryId );
|
||||
|
||||
std::pair<QByteArray, QString> requestParquetDataByUrlBlocking( const QString& url, const QString& id );
|
||||
void requestParquetDataByUrl( const QString& url, const QString& id );
|
||||
|
||||
QString requestTokenBlocking();
|
||||
|
||||
QOAuth2AuthorizationCodeFlow* m_osdu;
|
||||
QNetworkAccessManager* m_networkAccessManager;
|
||||
|
||||
const QString m_server;
|
||||
const QString m_dataPartitionId;
|
||||
const QString m_authority;
|
||||
const QString m_scopes;
|
||||
const QString m_clientId;
|
||||
|
||||
mutable QMutex m_mutex;
|
||||
QString m_token;
|
||||
std::vector<OsduField> m_fields;
|
||||
std::vector<OsduWell> m_wells;
|
||||
std::map<QString, std::vector<OsduWellbore>> m_wellbores;
|
||||
|
@ -45,60 +45,14 @@
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
RiaSumoConnector::RiaSumoConnector( QObject* parent, const QString& server, const QString& authority, const QString& scopes, const QString& clientId )
|
||||
: QObject( parent )
|
||||
, m_server( server )
|
||||
, m_authority( authority )
|
||||
, m_scopes( scopes )
|
||||
, m_clientId( clientId )
|
||||
RiaSumoConnector::RiaSumoConnector( QObject* parent,
|
||||
const QString& server,
|
||||
const QString& authority,
|
||||
const QString& scopes,
|
||||
const QString& clientId,
|
||||
unsigned int port )
|
||||
: RiaCloudConnector( parent, server, authority, scopes, clientId, port )
|
||||
{
|
||||
m_authCodeFlow = new QOAuth2AuthorizationCodeFlow( this );
|
||||
m_networkAccessManager = new QNetworkAccessManager( this );
|
||||
m_authCodeFlow->setNetworkAccessManager( m_networkAccessManager );
|
||||
|
||||
RiaLogging::debug( "SSL BUILD VERSION: " + QSslSocket::sslLibraryBuildVersionString() );
|
||||
RiaLogging::debug( "SSL VERSION STRING: " + QSslSocket::sslLibraryVersionString() );
|
||||
|
||||
// NB: Make sure the port is not in use by another application
|
||||
const unsigned int port = 53527;
|
||||
|
||||
connect( m_authCodeFlow,
|
||||
&QOAuth2AuthorizationCodeFlow::authorizeWithBrowser,
|
||||
[]( QUrl url )
|
||||
{
|
||||
RiaLogging::info( "Authorize with url: " + url.toString() );
|
||||
QUrlQuery query( url );
|
||||
url.setQuery( query );
|
||||
QDesktopServices::openUrl( url );
|
||||
} );
|
||||
|
||||
QString authUrl = constructAuthUrl( m_authority );
|
||||
m_authCodeFlow->setAuthorizationUrl( QUrl( authUrl ) );
|
||||
|
||||
QString tokenUrl = constructTokenUrl( m_authority );
|
||||
m_authCodeFlow->setAccessTokenUrl( QUrl( tokenUrl ) );
|
||||
|
||||
// App key
|
||||
m_authCodeFlow->setClientIdentifier( m_clientId );
|
||||
m_authCodeFlow->setScope( m_scopes );
|
||||
|
||||
auto replyHandler = new RiaOsduOAuthHttpServerReplyHandler( port, this );
|
||||
m_authCodeFlow->setReplyHandler( replyHandler );
|
||||
|
||||
connect( m_authCodeFlow, SIGNAL( granted() ), this, SLOT( accessGranted() ) );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RiaSumoConnector::accessGranted()
|
||||
{
|
||||
m_token = m_authCodeFlow->token();
|
||||
|
||||
QString tokenDataJson = RiaConnectorTools::tokenDataAsJson( m_authCodeFlow );
|
||||
RiaConnectorTools::writeTokenData( m_tokenDataFilePath, tokenDataJson );
|
||||
|
||||
emit tokenReady( m_token );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
@ -122,15 +76,6 @@ void RiaSumoConnector::parquetDownloadComplete( const QString& blobId, const QBy
|
||||
m_redirectInfo.push_back( obj );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RiaSumoConnector::requestToken()
|
||||
{
|
||||
RiaLogging::debug( "Requesting token." );
|
||||
m_authCodeFlow->grant();
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
@ -138,35 +83,6 @@ RiaSumoConnector::~RiaSumoConnector()
|
||||
{
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
QString RiaSumoConnector::token() const
|
||||
{
|
||||
return m_token;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RiaSumoConnector::importTokenFromFile()
|
||||
{
|
||||
auto tokenDataJson = RiaConnectorTools::readTokenData( m_tokenDataFilePath );
|
||||
if ( !tokenDataJson.isEmpty() )
|
||||
{
|
||||
RiaConnectorTools::initializeTokenDataFromJson( m_authCodeFlow, tokenDataJson );
|
||||
m_token = m_authCodeFlow->token();
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RiaSumoConnector::setTokenDataFilePath( const QString& filePath )
|
||||
{
|
||||
m_tokenDataFilePath = filePath;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
@ -668,22 +584,6 @@ QString RiaSumoConnector::constructDownloadUrl( const QString& server, const QSt
|
||||
// https: // main-sumo-prod.radix.equinor.com/api/v1/objects('76d6d11f-2278-3fe2-f12f-77142ad163c6')/blob
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
QString RiaSumoConnector::constructAuthUrl( const QString& authority )
|
||||
{
|
||||
return authority + "/oauth2/v2.0/authorize";
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
QString RiaSumoConnector::constructTokenUrl( const QString& authority )
|
||||
{
|
||||
return authority + "/oauth2/v2.0/token";
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
@ -911,46 +811,6 @@ void RiaSumoConnector::parseBlobIds( QNetworkReply* reply, const SumoCaseId& cas
|
||||
emit blobIdFinished();
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RiaSumoConnector::saveFile( QNetworkReply* reply, const QString& fileId )
|
||||
{
|
||||
QByteArray result = reply->readAll();
|
||||
reply->deleteLater();
|
||||
|
||||
if ( reply->error() == QNetworkReply::NoError )
|
||||
{
|
||||
QEventLoop loop;
|
||||
|
||||
QJsonDocument doc = QJsonDocument::fromJson( result );
|
||||
QJsonObject jsonObj = doc.object();
|
||||
|
||||
QString signedUrl = jsonObj["SignedUrl"].toString();
|
||||
|
||||
RiaFileDownloader* downloader = new RiaFileDownloader;
|
||||
QUrl url( signedUrl );
|
||||
QString filePath = "/tmp/" + generateRandomString( 30 ) + ".txt";
|
||||
|
||||
QString formattedJsonString = doc.toJson( QJsonDocument::Indented );
|
||||
|
||||
RiaLogging::info( QString( "File download: %1 => %2" ).arg( signedUrl ).arg( filePath ) );
|
||||
connect( this, SIGNAL( fileDownloadFinished( const QString&, const QString& ) ), &loop, SLOT( quit() ) );
|
||||
connect( downloader,
|
||||
&RiaFileDownloader::done,
|
||||
[this, fileId, filePath]()
|
||||
{
|
||||
RiaLogging::info( QString( "Download complete %1 => %2" ).arg( fileId ).arg( filePath ) );
|
||||
emit( fileDownloadFinished( fileId, filePath ) );
|
||||
} );
|
||||
RiaLogging::info( "Starting download" );
|
||||
downloader->downloadFile( url, filePath );
|
||||
|
||||
downloader->deleteLater();
|
||||
loop.exec();
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
@ -960,24 +820,6 @@ void RiaSumoConnector::addStandardHeader( QNetworkRequest& networkRequest, const
|
||||
networkRequest.setRawHeader( "Authorization", "Bearer " + token.toUtf8() );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
QString RiaSumoConnector::requestTokenBlocking()
|
||||
{
|
||||
if ( !m_token.isEmpty() ) return m_token;
|
||||
|
||||
QTimer timer;
|
||||
timer.setSingleShot( true );
|
||||
QEventLoop loop;
|
||||
connect( this, SIGNAL( tokenReady( const QString& ) ), &loop, SLOT( quit() ) );
|
||||
connect( &timer, SIGNAL( timeout() ), &loop, SLOT( quit() ) );
|
||||
requestToken();
|
||||
timer.start( RiaSumoDefines::requestTimeoutMillis() );
|
||||
loop.exec();
|
||||
return m_token;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
@ -1019,31 +861,6 @@ void RiaSumoConnector::requestParquetData( const QString& url, const QString& to
|
||||
} );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
QString RiaSumoConnector::generateRandomString( int randomStringLength )
|
||||
{
|
||||
const QString possibleCharacters( "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" );
|
||||
QString randomString;
|
||||
for ( int i = 0; i < randomStringLength; ++i )
|
||||
{
|
||||
quint32 value = QRandomGenerator::global()->generate();
|
||||
int index = value % possibleCharacters.length();
|
||||
QChar nextChar = possibleCharacters.at( index );
|
||||
randomString.append( nextChar );
|
||||
}
|
||||
return randomString;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
QString RiaSumoConnector::server() const
|
||||
{
|
||||
return m_server;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
|
@ -18,6 +18,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "RiaCloudConnector.h"
|
||||
#include "RiaSumoDefines.h"
|
||||
|
||||
#include <QByteArray>
|
||||
@ -63,18 +64,18 @@ struct SumoEnsemble
|
||||
//==================================================================================================
|
||||
///
|
||||
//==================================================================================================
|
||||
class RiaSumoConnector : public QObject
|
||||
class RiaSumoConnector : public RiaCloudConnector
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
RiaSumoConnector( QObject* parent, const QString& server, const QString& authority, const QString& scopes, const QString& clientId );
|
||||
RiaSumoConnector( QObject* parent,
|
||||
const QString& server,
|
||||
const QString& authority,
|
||||
const QString& scopes,
|
||||
const QString& clientId,
|
||||
unsigned int port );
|
||||
~RiaSumoConnector() override;
|
||||
|
||||
QString token() const;
|
||||
|
||||
void importTokenFromFile();
|
||||
void setTokenDataFilePath( const QString& filePath );
|
||||
|
||||
void requestAssets();
|
||||
void requestAssetsBlocking();
|
||||
|
||||
@ -98,8 +99,6 @@ public:
|
||||
|
||||
QByteArray requestParquetDataBlocking( const SumoCaseId& caseId, const QString& ensembleName, const QString& vectorName );
|
||||
|
||||
QString server() const;
|
||||
|
||||
std::vector<SumoAsset> assets() const;
|
||||
std::vector<SumoCase> cases() const;
|
||||
std::vector<QString> ensembleNamesForCase( const SumoCaseId& caseId ) const;
|
||||
@ -109,8 +108,6 @@ public:
|
||||
std::vector<SumoRedirect> blobContents() const;
|
||||
|
||||
public slots:
|
||||
void requestToken();
|
||||
|
||||
void parseAssets( QNetworkReply* reply );
|
||||
void parseEnsembleNames( QNetworkReply* reply, const SumoCaseId& caseId );
|
||||
void parseCases( QNetworkReply* reply );
|
||||
@ -118,9 +115,6 @@ public slots:
|
||||
void parseRealizationNumbers( QNetworkReply* reply, const SumoCaseId& caseId, const QString& ensembleName );
|
||||
void parseBlobIds( QNetworkReply* reply, const SumoCaseId& caseId, const QString& ensembleName, const QString& vectorName );
|
||||
|
||||
void saveFile( QNetworkReply* reply, const QString& fileId );
|
||||
|
||||
void accessGranted();
|
||||
void requestFailed( const QAbstractOAuth::Error error );
|
||||
void parquetDownloadComplete( const QString& blobId, const QByteArray&, const QString& url );
|
||||
|
||||
@ -130,7 +124,6 @@ signals:
|
||||
void wellsFinished();
|
||||
void wellboresFinished( const QString& wellId );
|
||||
void wellboreTrajectoryFinished( const QString& wellboreId );
|
||||
void tokenReady( const QString& token );
|
||||
void parquetDownloadFinished( const QByteArray& contents, const QString& url );
|
||||
void ensembleNamesFinished();
|
||||
void vectorNamesFinished();
|
||||
@ -141,29 +134,15 @@ signals:
|
||||
private:
|
||||
void addStandardHeader( QNetworkRequest& networkRequest, const QString& token, const QString& contentType );
|
||||
|
||||
QString requestTokenBlocking();
|
||||
|
||||
QNetworkReply* makeRequest( const std::map<QString, QString>& parameters, const QString& server, const QString& token );
|
||||
QNetworkReply* makeDownloadRequest( const QString& url, const QString& token, const QString& contentType );
|
||||
void requestParquetData( const QString& url, const QString& token );
|
||||
|
||||
static QString generateRandomString( int length = 20 );
|
||||
// static QString generateRandomString( int length = 20 );
|
||||
static QString constructSearchUrl( const QString& server );
|
||||
static QString constructDownloadUrl( const QString& server, const QString& blobId );
|
||||
static QString constructAuthUrl( const QString& authority );
|
||||
static QString constructTokenUrl( const QString& authority );
|
||||
|
||||
private:
|
||||
QOAuth2AuthorizationCodeFlow* m_authCodeFlow;
|
||||
QNetworkAccessManager* m_networkAccessManager;
|
||||
|
||||
const QString m_server;
|
||||
const QString m_authority;
|
||||
const QString m_scopes;
|
||||
const QString m_clientId;
|
||||
|
||||
QString m_token;
|
||||
|
||||
std::vector<SumoAsset> m_assets;
|
||||
std::vector<SumoCase> m_cases;
|
||||
std::vector<QString> m_vectorNames;
|
||||
@ -177,6 +156,4 @@ private:
|
||||
std::vector<SumoRedirect> m_redirectInfo;
|
||||
|
||||
QByteArray m_parquetData;
|
||||
|
||||
QString m_tokenDataFilePath;
|
||||
};
|
||||
|
@ -34,5 +34,5 @@ QString RiaSumoDefines::tokenPath()
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
int RiaSumoDefines::requestTimeoutMillis()
|
||||
{
|
||||
return 500;
|
||||
return 120 * 1000;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user