Skip to content

Commit

Permalink
Resync embedded o2 library to match QGIS fork
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Nov 26, 2024
1 parent 7726067 commit c8b47e6
Show file tree
Hide file tree
Showing 49 changed files with 537 additions and 364 deletions.
3 changes: 1 addition & 2 deletions external/o2/README_QGIS.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@

O2 Library from https://github.com/pipacs/o2/archive/99902cc37e083a8311c1f8eee918e93c93cbc937.tar.gz
Embedded copy of the O2 Library from the QGIS fork at https://github.com/qgis/o2/commit/4aae6439ec2fc5f6445e69f208b7710521b09ccc
2 changes: 1 addition & 1 deletion external/o2/src/o0abstractstore.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class O0_EXPORT O0AbstractStore: public QObject {
Q_OBJECT

public:
explicit O0AbstractStore(QObject *parent = 0): QObject(parent) {
explicit O0AbstractStore(QObject *parent = nullptr): QObject(parent) {
}

/// Retrieve a string value by key.
Expand Down
48 changes: 38 additions & 10 deletions external/o2/src/o0baseauth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@

static const quint16 DefaultLocalPort = 1965;

O0BaseAuth::O0BaseAuth(QObject *parent, O0AbstractStore *store): QObject(parent), store_(0), useExternalWebInterceptor_(false), replyServer_(NULL), pollServer_(NULL) {
localPort_ = DefaultLocalPort;
std::function<void( const QString&, O0BaseAuth::LogLevel level ) > O0BaseAuth::sLoggingFunction;

O0BaseAuth::O0BaseAuth(QObject *parent, O0AbstractStore *store): QObject(parent), localPort_( DefaultLocalPort ) {
setStore(store);
}

Expand All @@ -32,12 +33,12 @@ void O0BaseAuth::setStore(O0AbstractStore *store) {
bool O0BaseAuth::linked() {
QString key = QString(O2_KEY_LINKED).arg(clientId_);
bool result = !store_->value(key).isEmpty();
//qDebug() << "O0BaseAuth::linked:" << (result? "Yes": "No");
log( QStringLiteral( "O0BaseAuth::linked: %1 " ).arg( result? "Yes": "No" ) );
return result;
}

void O0BaseAuth::setLinked(bool v) {
//qDebug() << "O0BaseAuth::setLinked:" << (v? "true": "false");
log( QStringLiteral( "O0BaseAuth::setLinked: %1 " ).arg( v? "true": "false" ) );
bool oldValue = linked();
QString key = QString(O2_KEY_LINKED).arg(clientId_);
store_->setValue(key, v? "1": "");
Expand Down Expand Up @@ -110,15 +111,14 @@ int O0BaseAuth::localPort() {
}

void O0BaseAuth::setLocalPort(int value) {
//qDebug() << "O0BaseAuth::setLocalPort:" << value;
localPort_ = value;
log( QStringLiteral( "O0BaseAuth::setLocalPort:%1" ).arg( value ) );
localPort_ = static_cast<quint16>(value);
Q_EMIT localPortChanged();
}

QVariantMap O0BaseAuth::extraTokens() {
QString key = QString(O2_KEY_EXTRA_TOKENS).arg(clientId_);
QString value = store_->value(key);
QByteArray bytes = QByteArray::fromBase64(value.toLatin1());
const QString key = QString(O2_KEY_EXTRA_TOKENS).arg(clientId_);
QByteArray bytes = QByteArray::fromBase64(store_->value(key).toLatin1());
QDataStream stream(&bytes, QIODevice::ReadOnly);
stream >> extraTokens_;
return extraTokens_;
Expand Down Expand Up @@ -160,10 +160,38 @@ O2PollServer *O0BaseAuth::pollServer() const
return pollServer_;
}

void O0BaseAuth::setLoggingFunction( std::function<void (const QString&, LogLevel)> function)
{
sLoggingFunction = std::move( function );
}

void O0BaseAuth::log(const QString& message, LogLevel level)
{
if ( sLoggingFunction )
{
sLoggingFunction( message, level );
}
else
{
switch ( level )
{
case LogLevel::Debug:
qDebug() << message;
break;
case LogLevel::Warning:
qWarning() << message;
break;
case LogLevel::Critical:
qCritical() << message;
break;
}
}
}

QByteArray O0BaseAuth::createQueryParameters(const QList<O0RequestParameter> &parameters) {
QByteArray ret;
bool first = true;
foreach (O0RequestParameter h, parameters) {
for (const O0RequestParameter& h : parameters) {
if (first) {
first = false;
} else {
Expand Down
30 changes: 24 additions & 6 deletions external/o2/src/o0baseauth.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ class O0_EXPORT O0BaseAuth : public QObject
{
Q_OBJECT
public:
explicit O0BaseAuth(QObject *parent = 0, O0AbstractStore *store = 0);
enum class LogLevel
{
Debug,
Warning,
Critical
};
explicit O0BaseAuth(QObject *parent = nullptr, O0AbstractStore *store = nullptr);

public:
/// Are we authenticated?
Expand Down Expand Up @@ -54,7 +60,7 @@ class O0_EXPORT O0BaseAuth : public QObject
/// Should we use a reply server (default) or an external web interceptor?
Q_PROPERTY(bool useExternalWebInterceptor READ useExternalWebInterceptor WRITE setUseExternalWebInterceptor)
bool useExternalWebInterceptor();
void setUseExternalWebInterceptor(bool inUseExternalWebInterceptor);
void setUseExternalWebInterceptor(bool useExternalWebInterceptor);

/// Page content on local host after successful oauth.
/// Provide it in case you do not want to close the browser, but display something
Expand All @@ -75,6 +81,14 @@ class O0_EXPORT O0BaseAuth : public QObject
/// Construct query string from list of headers
static QByteArray createQueryParameters(const QList<O0RequestParameter> &parameters);

/// Sets a custom logging function to use instead of the default qDebug()/qWarning() mechanism
static void setLoggingFunction( std::function<void( const QString&, LogLevel level ) > function );

/// Logs a message
///
/// This will default to using qDebug/qWarning, unless a custom logger function has been registered
static void log( const QString& message, LogLevel level = LogLevel::Debug );

public Q_SLOTS:
/// Authenticate.
Q_INVOKABLE virtual void link() = 0;
Expand Down Expand Up @@ -141,16 +155,20 @@ public Q_SLOTS:
QUrl authorizeUrl_;
QUrl accessTokenUrl_;
quint16 localPort_;
O0AbstractStore *store_;
O0AbstractStore *store_{nullptr};
QVariantMap extraTokens_;
QByteArray pkceCodeVerifier_;
QString pkceCodeChallenge_;
bool useExternalWebInterceptor_;
bool useExternalWebInterceptor_{false};
QByteArray replyContent_;
static std::function<void( const QString&, LogLevel level ) > sLoggingFunction;

private:
O2ReplyServer *replyServer_;
O2PollServer *pollServer_;
O2ReplyServer *replyServer_{nullptr};
O2PollServer *pollServer_{nullptr};

friend class TestBaseAuth;
friend class TestO2;
};

#endif // O0BASEAUTH
9 changes: 7 additions & 2 deletions external/o2/src/o0export.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
#pragma once

#ifndef O0_EXPORT
// For exporting symbols from Windows' DLLs
#if defined (_WIN32) && defined(O2_SHARED_LIB)
#ifdef O2_DLL_EXPORT
#define O0_EXPORT __declspec(dllexport)
//#define O0_EXPORT __declspec(dllexport)
#define O0_EXPORT Q_DECL_EXPORT
#else
#define O0_EXPORT __declspec(dllimport)
//#define O0_EXPORT __declspec(dllimport)
#define O0_EXPORT Q_DECL_IMPORT
#endif
#else
#define O0_EXPORT
#endif

#endif // O0_EXPORT
10 changes: 5 additions & 5 deletions external/o2/src/o0jsonresponse.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "o0jsonresponse.h"
#include "o0baseauth.h"

#include <QByteArray>
#include <QDebug>
#if QT_VERSION >= 0x050000
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
#include <QJsonDocument>
#include <QJsonObject>
#else
Expand All @@ -11,16 +11,16 @@
#endif

QVariantMap parseJsonResponse(const QByteArray &data) {
#if QT_VERSION >= 0x050000
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
QJsonParseError err;
QJsonDocument doc = QJsonDocument::fromJson(data, &err);
if (err.error != QJsonParseError::NoError) {
qWarning() << "parseTokenResponse: Failed to parse token response due to err:" << err.errorString();
O0BaseAuth::log( QStringLiteral("parseTokenResponse: Failed to parse token response due to err: %1").arg( err.errorString() ), O0BaseAuth::LogLevel::Warning );
return QVariantMap();
}

if (!doc.isObject()) {
qWarning() << "parseTokenResponse: Token response is not an object";
O0BaseAuth::log( QStringLiteral("parseTokenResponse: Token response is not an object"), O0BaseAuth::LogLevel::Warning );
return QVariantMap();
}

Expand Down
9 changes: 5 additions & 4 deletions external/o2/src/o0keychainstore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Created by michaelpollind on 3/13/17.
//
#include "o0keychainstore.h"
#include "o0baseauth.h"

#include <QDebug>
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
#include <qt6keychain/keychain.h>
#else
Expand Down Expand Up @@ -70,14 +70,15 @@ void o0keyChainStore::initJob(QKeychain::Job &job) const {

int o0keyChainStore::executeJob(QKeychain::Job &job, const char *actionName) const {
QEventLoop loop;
job.connect( &job, SIGNAL(finished(QKeychain::Job*)), &loop, SLOT(quit()) );
job.connect(&job, &Job::finished, &loop, &QEventLoop::quit);
job.start();
loop.exec();

const QKeychain::Error errorCode = job.error();
if (errorCode != QKeychain::NoError) {
qWarning() << "keychain store could not" << actionName << name_ << ":"
<< job.errorString() << "(" << errorCode << ").";
O0BaseAuth::log( QStringLiteral("keychain store could not %1 %2: %3 (%4)").arg(
actionName, name_, job.errorString() )
.arg( errorCode ), O0BaseAuth::LogLevel::Warning );
}
return errorCode;
}
6 changes: 3 additions & 3 deletions external/o2/src/o0keychainstore.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ class Job;
class O0_EXPORT o0keyChainStore : public O0AbstractStore{
Q_OBJECT
public:
explicit o0keyChainStore(const QString& app,const QString& name,QObject *parent = 0);
explicit o0keyChainStore(const QString& app,const QString& name,QObject *parent = nullptr);

/// Retrieve a string value by key.
QString value(const QString &key, const QString &defaultValue = QString());
QString value(const QString &key, const QString &defaultValue = QString()) override;

/// Set a string value for a key.
void setValue(const QString &key, const QString &value);
void setValue(const QString &key, const QString &value) override;

// The functions below return QKeychain::Error casted to int. They don't
// return the enumerator directly because it can not be forward-declared reliably,
Expand Down
7 changes: 3 additions & 4 deletions external/o2/src/o0settingsstore.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <QCryptographicHash>
#include <QByteArray>
#include <QDebug>

#include "o0settingsstore.h"

Expand Down Expand Up @@ -45,10 +44,10 @@ void O0SettingsStore::setValue(const QString &key, const QString &value) {

const QSettings::Status status = settings_->status();
if (status != QSettings::NoError) {
qCritical() << "O0SettingsStore QSettings error:" << status;
O0BaseAuth::log( QStringLiteral( "O0SettingsStore QSettings error: %1" ).arg( status ), O0BaseAuth::LogLevel::Critical );
if (status == QSettings::AccessError) {
qCritical() << "Did you forget to set organization name and application name "
"in QSettings or QCoreApplication?";
O0BaseAuth::log( QStringLiteral( "Did you forget to set organization name and application name "
"in QSettings or QCoreApplication?" ), O0BaseAuth::LogLevel::Critical );
}
}
}
8 changes: 4 additions & 4 deletions external/o2/src/o0settingsstore.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ class O0_EXPORT O0SettingsStore: public O0AbstractStore {

public:
/// Constructor
explicit O0SettingsStore(const QString &encryptionKey, QObject *parent = 0);
explicit O0SettingsStore(const QString &encryptionKey, QObject *parent = nullptr);

/// Construct with an explicit QSettings instance
explicit O0SettingsStore(QSettings *settings, const QString &encryptionKey, QObject *parent = 0);
explicit O0SettingsStore(QSettings *settings, const QString &encryptionKey, QObject *parent = nullptr);

/// Group key prefix
Q_PROPERTY(QString groupKey READ groupKey WRITE setGroupKey NOTIFY groupKeyChanged)
QString groupKey() const;
void setGroupKey(const QString &groupKey);

/// Get a string value for a key
QString value(const QString &key, const QString &defaultValue = QString());
QString value(const QString &key, const QString &defaultValue = QString()) override;

/// Set a string value for a key
void setValue(const QString &key, const QString &value);
void setValue(const QString &key, const QString &value) override;

Q_SIGNALS:
// Property change signals
Expand Down
8 changes: 4 additions & 4 deletions external/o2/src/o0simplecrypt.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class O0_EXPORT O0SimpleCrypt
a cyphertext the result. The result is a base64 encoded version of the binary array that is the
actual result of the encryption, so it can be stored easily in a text format.
*/
QString encryptToString(QByteArray plaintext) ;
QString encryptToString(const QByteArray& plaintext) ;
/**
Encrypts the @arg plaintext string with the key the class was initialized with, and returns
a binary cyphertext in a QByteArray the result.
Expand All @@ -171,7 +171,7 @@ class O0_EXPORT O0SimpleCrypt
This method returns a byte array, that is useable for storing a binary format. If you need
a string you can store in a text file, use encryptToString() instead.
*/
QByteArray encryptToByteArray(QByteArray plaintext) ;
QByteArray encryptToByteArray(const QByteArray& plaintext) ;

/**
Decrypts a cyphertext string encrypted with this class with the set key back to the
Expand All @@ -196,15 +196,15 @@ class O0_EXPORT O0SimpleCrypt
If an error occured, such as non-matching keys between encryption and decryption,
an empty string or a string containing nonsense may be returned.
*/
QString decryptToString(QByteArray cypher) ;
QString decryptToString(const QByteArray& cypher) ;
/**
Decrypts a cyphertext binary encrypted with this class with the set key back to the
plain text version.
If an error occured, such as non-matching keys between encryption and decryption,
an empty string or a string containing nonsense may be returned.
*/
QByteArray decryptToByteArray(QByteArray cypher) ;
QByteArray decryptToByteArray(const QByteArray& cypher) ;

//enum to describe options that have been used for the encryption. Currently only one, but
//that only leaves room for future extensions like adding a cryptographic hash...
Expand Down
Loading

0 comments on commit c8b47e6

Please sign in to comment.