Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing bits for PKCE flow from QgsO2 subclass #15

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions src/o2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ void O2::link() {
setRefreshToken(QString());
setExpires(0);

if (grantFlow_ == GrantFlowAuthorizationCode || grantFlow_ == GrantFlowImplicit) {
if (grantFlow_ == GrantFlowAuthorizationCode || grantFlow_ == GrantFlowImplicit || grantFlow_ == GrantFlowPkce) {

#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
const thread_local QRegularExpression rx("([^a-zA-Z0-9]|[-])");
Expand Down Expand Up @@ -232,6 +232,17 @@ void O2::link() {
parameters.append(qMakePair(QString(O2_OAUTH2_STATE), uniqueState));
if ( !apiKey_.isEmpty() )
parameters.append(qMakePair(QString(O2_OAUTH2_API_KEY), apiKey_));

if ( grantFlow_ == GrantFlowPkce )
{
pkceCodeVerifier_ = ( QUuid::createUuid().toString( QUuid::WithoutBraces ) +
QUuid::createUuid().toString( QUuid::WithoutBraces ) ).toLatin1();
pkceCodeChallenge_ = QCryptographicHash::hash( pkceCodeVerifier_, QCryptographicHash::Sha256 ).toBase64(
QByteArray::Base64UrlEncoding | QByteArray::OmitTrailingEquals );
parameters.append( qMakePair( QString( O2_OAUTH2_PKCE_CODE_CHALLENGE_PARAM ), pkceCodeChallenge_ ) );
parameters.append( qMakePair( QString( O2_OAUTH2_PKCE_CODE_CHALLENGE_METHOD_PARAM ), QString( O2_OAUTH2_PKCE_CODE_CHALLENGE_METHOD_S256 ) ) );
}

const QVariantMap extraParams = extraRequestParams();
for (auto it = extraParams.constBegin(); it != extraParams.constEnd(); ++it) {
parameters.append(qMakePair(it.key(), it.value().toString()));
Expand Down Expand Up @@ -320,7 +331,7 @@ void O2::onVerificationReceived(const QMap<QString, QString> response) {
return;
}

if (grantFlow_ == GrantFlowAuthorizationCode) {
if (grantFlow_ == GrantFlowAuthorizationCode || grantFlow_ == GrantFlowPkce ) {
// Save access code
setCode(response.value(QString(O2_OAUTH2_GRANT_TYPE_CODE)));

Expand All @@ -334,9 +345,17 @@ void O2::onVerificationReceived(const QMap<QString, QString> response) {
QMap<QString, QString> parameters;
parameters.insert(O2_OAUTH2_GRANT_TYPE_CODE, code());
parameters.insert(O2_OAUTH2_CLIENT_ID, clientId_);
parameters.insert(O2_OAUTH2_CLIENT_SECRET, clientSecret_);
//No client secret with PKCE
if ( grantFlow_ != GrantFlowPkce )
{
parameters.insert(O2_OAUTH2_CLIENT_SECRET, clientSecret_);
}
parameters.insert(O2_OAUTH2_REDIRECT_URI, redirectUri_);
parameters.insert(O2_OAUTH2_GRANT_TYPE, O2_AUTHORIZATION_CODE);
if ( grantFlow() == GrantFlowPkce )
{
parameters.insert( O2_OAUTH2_PKCE_CODE_VERIFIER_PARAM, pkceCodeVerifier_ );
}
QByteArray data = buildRequestBody(parameters);

log( QStringLiteral("O2::onVerificationReceived: Exchange access code data:\n%1").arg(QString(data)) );
Expand Down
Loading