Skip to content

Commit 560e841

Browse files
elpasonyalldawson
authored andcommitted
[authmanager] Fix OAuth2 implicit grant flow
1 parent 8eb1930 commit 560e841

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

src/auth/oauth2/qgso2.cpp

+36-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ void QgsO2::link()
170170
setRefreshToken( QString() );
171171
setExpires( 0 );
172172

173-
if ( grantFlow_ == GrantFlowAuthorizationCode )
173+
if ( grantFlow_ == GrantFlowAuthorizationCode || grantFlow_ == GrantFlowImplicit )
174174
{
175175
if ( mIsLocalHost )
176176
{
@@ -238,6 +238,15 @@ void QgsO2::link()
238238
}
239239
}
240240

241+
242+
void QgsO2::setState( const QString & )
243+
{
244+
qsrand( QTime::currentTime().msec() );
245+
state_ = QString::number( qrand() );
246+
Q_EMIT stateChanged();
247+
}
248+
249+
241250
void QgsO2::onVerificationReceived( QMap<QString, QString> response )
242251
{
243252
QgsDebugMsgLevel( QStringLiteral( "QgsO2::onVerificationReceived: Emitting closeBrowser()" ), 4 );
@@ -295,6 +304,32 @@ void QgsO2::onVerificationReceived( QMap<QString, QString> response )
295304
connect( tokenReply, &QNetworkReply::finished, this, &QgsO2::onTokenReplyFinished, Qt::QueuedConnection );
296305
connect( tokenReply, qgis::overload<QNetworkReply::NetworkError>::of( &QNetworkReply::error ), this, &QgsO2::onTokenReplyError, Qt::QueuedConnection );
297306
}
307+
else if ( grantFlow_ == GrantFlowImplicit )
308+
{
309+
// Check for mandatory tokens
310+
if ( response.contains( O2_OAUTH2_ACCESS_TOKEN ) )
311+
{
312+
qDebug() << "O2::onVerificationReceived: Access token returned for implicit flow";
313+
setToken( response.value( O2_OAUTH2_ACCESS_TOKEN ) );
314+
if ( response.contains( O2_OAUTH2_EXPIRES_IN ) )
315+
{
316+
bool ok = false;
317+
int expiresIn = response.value( O2_OAUTH2_EXPIRES_IN ).toInt( &ok );
318+
if ( ok )
319+
{
320+
qDebug() << "O2::onVerificationReceived: Token expires in" << expiresIn << "seconds";
321+
setExpires( QDateTime::currentMSecsSinceEpoch() / 1000 + expiresIn );
322+
}
323+
}
324+
setLinked( true );
325+
Q_EMIT linkingSucceeded();
326+
}
327+
else
328+
{
329+
qWarning() << "O2::onVerificationReceived: Access token missing from response for implicit flow";
330+
Q_EMIT linkingFailed();
331+
}
332+
}
298333
else
299334
{
300335
setToken( response.value( O2_OAUTH2_ACCESS_TOKEN ) );

src/auth/oauth2/qgso2.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ class QgsO2: public O2
5555
//! Retrieve oauth2 state
5656
QString state() const { return state_; }
5757

58-
//! Store oauth2 state to \a value
59-
void setState( const QString &value ) { state_ = value; }
58+
//! Store oauth2 state to a random value when called
59+
void setState( const QString &value );
6060

6161
public slots:
6262

0 commit comments

Comments
 (0)