/
qgsauthoauth2config.cpp
832 lines (743 loc) · 24.3 KB
/
qgsauthoauth2config.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
/***************************************************************************
begin : July 30, 2016
copyright : (C) 2016 by Monsanto Company, USA
author : Larry Shaffer, Boundless Spatial
email : lshaffer at boundlessgeo dot com
***************************************************************************
* *
* This program 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 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <functional>
#include "qgsauthoauth2config.h"
#include <QDir>
#include "Json.h"
#include "qgsapplication.h"
#include "qgslogger.h"
QgsAuthOAuth2Config::QgsAuthOAuth2Config( QObject *parent )
: QObject( parent )
, mQueryPairs( QVariantMap() )
{
// internal signal bounces
connect( this, &QgsAuthOAuth2Config::idChanged, this, &QgsAuthOAuth2Config::configChanged );
connect( this, &QgsAuthOAuth2Config::versionChanged, this, &QgsAuthOAuth2Config::configChanged );
connect( this, &QgsAuthOAuth2Config::configTypeChanged, this, &QgsAuthOAuth2Config::configChanged );
connect( this, &QgsAuthOAuth2Config::grantFlowChanged, this, &QgsAuthOAuth2Config::configChanged );
connect( this, &QgsAuthOAuth2Config::nameChanged, this, &QgsAuthOAuth2Config::configChanged );
connect( this, &QgsAuthOAuth2Config::descriptionChanged, this, &QgsAuthOAuth2Config::configChanged );
connect( this, &QgsAuthOAuth2Config::requestUrlChanged, this, &QgsAuthOAuth2Config::configChanged );
connect( this, &QgsAuthOAuth2Config::tokenUrlChanged, this, &QgsAuthOAuth2Config::configChanged );
connect( this, &QgsAuthOAuth2Config::refreshTokenUrlChanged, this, &QgsAuthOAuth2Config::configChanged );
connect( this, &QgsAuthOAuth2Config::redirectUrlChanged, this, &QgsAuthOAuth2Config::configChanged );
connect( this, &QgsAuthOAuth2Config::redirectPortChanged, this, &QgsAuthOAuth2Config::configChanged );
connect( this, &QgsAuthOAuth2Config::clientIdChanged, this, &QgsAuthOAuth2Config::configChanged );
connect( this, &QgsAuthOAuth2Config::clientSecretChanged, this, &QgsAuthOAuth2Config::configChanged );
connect( this, &QgsAuthOAuth2Config::usernameChanged, this, &QgsAuthOAuth2Config::configChanged );
connect( this, &QgsAuthOAuth2Config::passwordChanged, this, &QgsAuthOAuth2Config::configChanged );
connect( this, &QgsAuthOAuth2Config::scopeChanged, this, &QgsAuthOAuth2Config::configChanged );
connect( this, &QgsAuthOAuth2Config::apiKeyChanged, this, &QgsAuthOAuth2Config::configChanged );
connect( this, &QgsAuthOAuth2Config::persistTokenChanged, this, &QgsAuthOAuth2Config::configChanged );
connect( this, &QgsAuthOAuth2Config::accessMethodChanged, this, &QgsAuthOAuth2Config::configChanged );
connect( this, &QgsAuthOAuth2Config::requestTimeoutChanged, this, &QgsAuthOAuth2Config::configChanged );
connect( this, &QgsAuthOAuth2Config::queryPairsChanged, this, &QgsAuthOAuth2Config::configChanged );
connect( this, &QgsAuthOAuth2Config::customHeaderChanged, this, &QgsAuthOAuth2Config::configChanged );
// always recheck validity on any change
// this, in turn, may emit validityChanged( bool )
connect( this, &QgsAuthOAuth2Config::configChanged, this, &QgsAuthOAuth2Config::validateConfig );
validateConfig();
}
void QgsAuthOAuth2Config::setId( const QString &value )
{
QString preval( mId );
mId = value;
if ( preval != value )
emit idChanged( mId );
}
void QgsAuthOAuth2Config::setVersion( int value )
{
int preval( mVersion );
mVersion = value;
if ( preval != value )
emit versionChanged( mVersion );
}
void QgsAuthOAuth2Config::setConfigType( QgsAuthOAuth2Config::ConfigType value )
{
ConfigType preval( mConfigType );
mConfigType = value;
if ( preval != value )
emit configTypeChanged( mConfigType );
}
void QgsAuthOAuth2Config::setGrantFlow( QgsAuthOAuth2Config::GrantFlow value )
{
GrantFlow preval( mGrantFlow );
mGrantFlow = value;
if ( preval != value )
emit grantFlowChanged( mGrantFlow );
}
void QgsAuthOAuth2Config::setName( const QString &value )
{
QString preval( mName );
mName = value;
if ( preval != value )
emit nameChanged( mName );
}
void QgsAuthOAuth2Config::setDescription( const QString &value )
{
QString preval( mDescription );
mDescription = value;
if ( preval != value )
emit descriptionChanged( mDescription );
}
void QgsAuthOAuth2Config::setRequestUrl( const QString &value )
{
QString preval( mRequestUrl );
mRequestUrl = value;
if ( preval != value )
emit requestUrlChanged( mRequestUrl );
}
void QgsAuthOAuth2Config::setTokenUrl( const QString &value )
{
QString preval( mTokenUrl );
mTokenUrl = value;
if ( preval != value )
emit tokenUrlChanged( mTokenUrl );
}
void QgsAuthOAuth2Config::setRefreshTokenUrl( const QString &value )
{
QString preval( mRefreshTokenUrl );
mRefreshTokenUrl = value;
if ( preval != value )
emit refreshTokenUrlChanged( mRefreshTokenUrl );
}
void QgsAuthOAuth2Config::setRedirectUrl( const QString &value )
{
QString preval( mRedirectURL );
mRedirectURL = value;
if ( preval != value )
emit redirectUrlChanged( mRedirectURL );
}
void QgsAuthOAuth2Config::setRedirectPort( int value )
{
int preval( mRedirectPort );
mRedirectPort = value;
if ( preval != value )
emit redirectPortChanged( mRedirectPort );
}
void QgsAuthOAuth2Config::setClientId( const QString &value )
{
QString preval( mClientId );
mClientId = value;
if ( preval != value )
emit clientIdChanged( mClientId );
}
void QgsAuthOAuth2Config::setClientSecret( const QString &value )
{
QString preval( mClientSecret );
mClientSecret = value;
if ( preval != value )
emit clientSecretChanged( mClientSecret );
}
void QgsAuthOAuth2Config::setUsername( const QString &value )
{
QString preval( mUsername );
mUsername = value;
if ( preval != value )
emit usernameChanged( mUsername );
}
void QgsAuthOAuth2Config::setPassword( const QString &value )
{
QString preval( mPassword );
mPassword = value;
if ( preval != value )
emit passwordChanged( mPassword );
}
void QgsAuthOAuth2Config::setScope( const QString &value )
{
QString preval( mScope );
mScope = value;
if ( preval != value )
emit scopeChanged( mScope );
}
void QgsAuthOAuth2Config::setApiKey( const QString &value )
{
QString preval( mApiKey );
mApiKey = value;
if ( preval != value )
emit apiKeyChanged( mApiKey );
}
void QgsAuthOAuth2Config::setPersistToken( bool persist )
{
bool preval( mPersistToken );
mPersistToken = persist;
if ( preval != persist )
emit persistTokenChanged( mPersistToken );
}
void QgsAuthOAuth2Config::setAccessMethod( QgsAuthOAuth2Config::AccessMethod value )
{
AccessMethod preval( mAccessMethod );
mAccessMethod = value;
if ( preval != value )
emit accessMethodChanged( mAccessMethod );
}
void QgsAuthOAuth2Config::setCustomHeader( const QString &header )
{
QString preval( mCustomHeader );
mCustomHeader = header;
if ( preval != header )
emit customHeaderChanged( mCustomHeader );
}
void QgsAuthOAuth2Config::setRequestTimeout( int value )
{
int preval( mRequestTimeout );
mRequestTimeout = value;
if ( preval != value )
emit requestTimeoutChanged( mRequestTimeout );
}
void QgsAuthOAuth2Config::setQueryPairs( const QVariantMap &pairs )
{
QVariantMap preval( mQueryPairs );
mQueryPairs = pairs;
if ( preval != pairs )
emit queryPairsChanged( mQueryPairs );
}
void QgsAuthOAuth2Config::setToDefaults()
{
setId( QString() );
setVersion( 1 );
setConfigType( QgsAuthOAuth2Config::Custom );
setGrantFlow( QgsAuthOAuth2Config::AuthCode );
setName( QString() );
setDescription( QString() );
setRequestUrl( QString() );
setTokenUrl( QString() );
setRefreshTokenUrl( QString() );
setRedirectUrl( QString() );
setRedirectPort( 7070 );
setClientId( QString() );
setClientSecret( QString() );
setUsername( QString() );
setPassword( QString() );
setScope( QString() );
setApiKey( QString() );
setPersistToken( false );
setAccessMethod( QgsAuthOAuth2Config::Header );
setCustomHeader( QString() );
setRequestTimeout( 30 ); // in seconds
setQueryPairs( QVariantMap() );
}
bool QgsAuthOAuth2Config::operator==( const QgsAuthOAuth2Config &other ) const
{
return ( other.version() == this->version()
&& other.configType() == this->configType()
&& other.grantFlow() == this->grantFlow()
&& other.name() == this->name()
&& other.description() == this->description()
&& other.requestUrl() == this->requestUrl()
&& other.tokenUrl() == this->tokenUrl()
&& other.refreshTokenUrl() == this->refreshTokenUrl()
&& other.redirectUrl() == this->redirectUrl()
&& other.redirectPort() == this->redirectPort()
&& other.clientId() == this->clientId()
&& other.clientSecret() == this->clientSecret()
&& other.username() == this->username()
&& other.password() == this->password()
&& other.scope() == this->scope()
&& other.apiKey() == this->apiKey()
&& other.persistToken() == this->persistToken()
&& other.accessMethod() == this->accessMethod()
&& other.customHeader() == this->customHeader()
&& other.requestTimeout() == this->requestTimeout()
&& other.queryPairs() == this->queryPairs() );
}
bool QgsAuthOAuth2Config::operator!=( const QgsAuthOAuth2Config &other ) const
{
return !( *this == other );
}
bool QgsAuthOAuth2Config::isValid() const
{
return mValid;
}
// slot
void QgsAuthOAuth2Config::validateConfig()
{
validateConfigId( false );
}
// public
void QgsAuthOAuth2Config::validateConfigId( bool needsId )
{
bool oldvalid = mValid;
if ( mGrantFlow == AuthCode || mGrantFlow == Implicit )
{
mValid = ( !requestUrl().isEmpty()
&& !tokenUrl().isEmpty()
&& !clientId().isEmpty()
&& ( mGrantFlow == AuthCode ? !clientSecret().isEmpty() : true )
&& redirectPort() > 0
&& ( needsId ? !id().isEmpty() : true ) );
}
else if ( mGrantFlow == ResourceOwner )
{
mValid = ( !tokenUrl().isEmpty()
&& !username().isEmpty()
&& !password().isEmpty()
&& ( needsId ? !id().isEmpty() : true ) );
}
if ( mValid != oldvalid )
emit validityChanged( mValid );
}
bool QgsAuthOAuth2Config::loadConfigTxt(
const QByteArray &configtxt, QgsAuthOAuth2Config::ConfigFormat format )
{
QByteArray errStr;
bool res = false;
switch ( format )
{
case JSON:
{
QVariant variant = QJsonWrapper::parseJson( configtxt, &res, &errStr );
if ( !res )
{
QgsDebugMsg( QStringLiteral( "Error parsing JSON: %1" ).arg( QString( errStr ) ) );
return res;
}
const QVariantMap variantMap = variant.toMap();
// safety check -- qvariant2qobject asserts if an non-matching property is found in the json
for ( QVariantMap::const_iterator iter = variantMap.constBegin(); iter != variantMap.constEnd(); ++iter )
{
QVariant property = this->property( iter.key().toLatin1() );
if ( !property.isValid() ) // e.g. not a auth config json file
return false;
}
QJsonWrapper::qvariant2qobject( variantMap, this );
break;
}
default:
QgsDebugMsg( QStringLiteral( "Unsupported output format" ) );
}
return true;
}
QByteArray QgsAuthOAuth2Config::saveConfigTxt(
QgsAuthOAuth2Config::ConfigFormat format, bool pretty, bool *ok ) const
{
QByteArray out;
QByteArray errStr;
bool res = false;
if ( !isValid() )
{
QgsDebugMsg( QStringLiteral( "FAILED, config is not valid" ) );
if ( ok )
*ok = res;
return out;
}
switch ( format )
{
case JSON:
{
QVariantMap variant = QJsonWrapper::qobject2qvariant( this );
out = QJsonWrapper::toJson( variant, &res, &errStr, pretty );
if ( !res )
{
QgsDebugMsg( QStringLiteral( "Error serializing JSON: %1" ).arg( QString( errStr ) ) );
}
break;
}
default:
QgsDebugMsg( QStringLiteral( "Unsupported output format" ) );
}
if ( ok )
*ok = res;
return out;
}
QVariantMap QgsAuthOAuth2Config::mappedProperties() const
{
QVariantMap vmap;
vmap.insert( QStringLiteral( "apiKey" ), this->apiKey() );
vmap.insert( QStringLiteral( "clientId" ), this->clientId() );
vmap.insert( QStringLiteral( "clientSecret" ), this->clientSecret() );
vmap.insert( QStringLiteral( "configType" ), static_cast<int>( this->configType() ) );
vmap.insert( QStringLiteral( "description" ), this->description() );
vmap.insert( QStringLiteral( "grantFlow" ), static_cast<int>( this->grantFlow() ) );
vmap.insert( QStringLiteral( "id" ), this->id() );
vmap.insert( QStringLiteral( "name" ), this->name() );
vmap.insert( QStringLiteral( "password" ), this->password() );
vmap.insert( QStringLiteral( "persistToken" ), this->persistToken() );
vmap.insert( QStringLiteral( "queryPairs" ), this->queryPairs() );
vmap.insert( QStringLiteral( "redirectPort" ), this->redirectPort() );
vmap.insert( QStringLiteral( "redirectUrl" ), this->redirectUrl() );
vmap.insert( QStringLiteral( "refreshTokenUrl" ), this->refreshTokenUrl() );
vmap.insert( QStringLiteral( "accessMethod" ), static_cast<int>( this->accessMethod() ) );
vmap.insert( QStringLiteral( "customHeader" ), this->customHeader() );
vmap.insert( QStringLiteral( "requestTimeout" ), this->requestTimeout() );
vmap.insert( QStringLiteral( "requestUrl" ), this->requestUrl() );
vmap.insert( QStringLiteral( "scope" ), this->scope() );
vmap.insert( QStringLiteral( "tokenUrl" ), this->tokenUrl() );
vmap.insert( QStringLiteral( "username" ), this->username() );
vmap.insert( QStringLiteral( "version" ), this->version() );
return vmap;
}
// static
QByteArray QgsAuthOAuth2Config::serializeFromVariant(
const QVariantMap &variant,
QgsAuthOAuth2Config::ConfigFormat format,
bool pretty,
bool *ok )
{
QByteArray out;
QByteArray errStr;
bool res = false;
switch ( format )
{
case JSON:
out = QJsonWrapper::toJson( variant, &res, &errStr, pretty );
if ( !res )
{
QgsDebugMsg( QStringLiteral( "Error serializing JSON: %1" ).arg( QString( errStr ) ) );
}
break;
default:
QgsDebugMsg( QStringLiteral( "Unsupported output format" ) );
}
if ( ok )
*ok = res;
return out;
}
// static
QVariantMap QgsAuthOAuth2Config::variantFromSerialized(
const QByteArray &serial,
QgsAuthOAuth2Config::ConfigFormat format,
bool *ok )
{
QVariantMap vmap;
QByteArray errStr;
bool res = false;
switch ( format )
{
case JSON:
{
QVariant var = QJsonWrapper::parseJson( serial, &res, &errStr );
if ( !res )
{
QgsDebugMsg( QStringLiteral( "Error parsing JSON to variant: %1" ).arg( QString( errStr ) ) );
if ( ok )
*ok = res;
return vmap;
}
if ( var.isNull() )
{
QgsDebugMsg( QStringLiteral( "Error parsing JSON to variant: %1" ).arg( "invalid or null" ) );
if ( ok )
*ok = res;
return vmap;
}
vmap = var.toMap();
if ( vmap.isEmpty() )
{
QgsDebugMsg( QStringLiteral( "Error parsing JSON to variantmap: %1" ).arg( "map empty" ) );
if ( ok )
*ok = res;
return vmap;
}
break;
}
default:
QgsDebugMsg( QStringLiteral( "Unsupported output format" ) );
}
if ( ok )
*ok = res;
return vmap;
}
//static
bool QgsAuthOAuth2Config::writeOAuth2Config(
const QString &filepath,
QgsAuthOAuth2Config *config,
QgsAuthOAuth2Config::ConfigFormat format,
bool pretty )
{
bool res = false;
QByteArray configtxt = config->saveConfigTxt( format, pretty, &res );
if ( !res )
{
QgsDebugMsg( QStringLiteral( "FAILED to save config to text" ) );
return false;
}
QFile config_file( filepath );
QString file_path( config_file.fileName() );
if ( config_file.open( QIODevice::ReadWrite | QIODevice::Truncate | QIODevice::Text ) )
{
qint64 bytesWritten = config_file.write( configtxt );
config_file.close();
if ( bytesWritten == -1 )
{
QgsDebugMsg( QStringLiteral( "FAILED to write config file: %1" ).arg( file_path ) );
return false;
}
}
else
{
QgsDebugMsg( QStringLiteral( "FAILED to open for writing config file: %1" ).arg( file_path ) );
return false;
}
if ( !config_file.setPermissions( QFile::ReadOwner | QFile::WriteOwner ) )
{
QgsDebugMsg( QStringLiteral( "FAILED to set permissions config file: %1" ).arg( file_path ) );
return false;
}
return true;
}
// static
QList<QgsAuthOAuth2Config *> QgsAuthOAuth2Config::loadOAuth2Configs(
const QString &configdirectory,
QObject *parent,
QgsAuthOAuth2Config::ConfigFormat format,
bool *ok )
{
QList<QgsAuthOAuth2Config *> configs = QList<QgsAuthOAuth2Config *>();
bool res = false;
QStringList namefilters;
switch ( format )
{
case JSON:
namefilters << QStringLiteral( "*.json" );
break;
default:
QgsDebugMsg( QStringLiteral( "Unsupported output format" ) );
if ( ok )
*ok = res;
return configs;
}
QDir configdir( configdirectory );
configdir.setNameFilters( namefilters );
const QStringList configfiles = configdir.entryList( namefilters );
if ( configfiles.size() > 0 )
{
QgsDebugMsg( QStringLiteral( "Config files found in: %1...\n%2" )
.arg( configdir.path(), configfiles.join( QLatin1String( ", " ) ) ) );
}
else
{
QgsDebugMsg( QStringLiteral( "No config files found in: %1" ).arg( configdir.path() ) );
if ( ok ) *ok = res;
return configs;
}
// Add entries
for ( const auto &configfile : configfiles )
{
QByteArray configtxt;
QFile cfile( configdir.path() + '/' + configfile );
if ( cfile.exists() )
{
bool ret = cfile.open( QIODevice::ReadOnly | QIODevice::Text );
if ( ret )
{
configtxt = cfile.readAll();
}
else
{
QgsDebugMsg( QStringLiteral( "FAILED to open config for reading: %1" ).arg( configfile ) );
}
cfile.close();
}
if ( configtxt.isEmpty() )
{
QgsDebugMsg( QStringLiteral( "EMPTY read of config: %1" ).arg( configfile ) );
continue;
}
QgsAuthOAuth2Config *config = new QgsAuthOAuth2Config( parent );
if ( !config->loadConfigTxt( configtxt, format ) )
{
QgsDebugMsg( QStringLiteral( "FAILED to load config: %1" ).arg( configfile ) );
config->deleteLater();
continue;
}
configs << config;
}
if ( ok ) *ok = true;
return configs;
}
// static
QgsStringMap QgsAuthOAuth2Config::mapOAuth2Configs(
const QString &configdirectory,
QObject *parent,
QgsAuthOAuth2Config::ConfigFormat format,
bool *ok )
{
QgsStringMap configs = QgsStringMap();
bool res = false;
QStringList namefilters;
switch ( format )
{
case JSON:
namefilters << QStringLiteral( "*.json" );
break;
default:
QgsDebugMsg( QStringLiteral( "Unsupported output format" ) );
if ( ok )
*ok = res;
return configs;
}
QDir configdir( configdirectory );
configdir.setNameFilters( namefilters );
const QStringList configfiles = configdir.entryList( namefilters );
if ( configfiles.size() > 0 )
{
QgsDebugMsg( QStringLiteral( "Config files found in: %1...\n%2" )
.arg( configdir.path(), configfiles.join( QLatin1String( ", " ) ) ) );
}
else
{
QgsDebugMsg( QStringLiteral( "No config files found in: %1" ).arg( configdir.path() ) );
if ( ok )
*ok = res;
return configs;
}
// Add entries
for ( const auto &configfile : configfiles )
{
QByteArray configtxt;
QFile cfile( configdir.path() + '/' + configfile );
if ( cfile.exists() )
{
bool ret = cfile.open( QIODevice::ReadOnly | QIODevice::Text );
if ( ret )
{
configtxt = cfile.readAll();
}
else
{
QgsDebugMsg( QStringLiteral( "FAILED to open config for reading: %1" ).arg( configfile ) );
}
cfile.close();
}
if ( configtxt.isEmpty() )
{
QgsDebugMsg( QStringLiteral( "EMPTY read of config: %1" ).arg( configfile ) );
continue;
}
// validate the config before caching it
std::unique_ptr<QgsAuthOAuth2Config, std::function<void( QgsAuthOAuth2Config * )> > config( new QgsAuthOAuth2Config( parent ), []( QgsAuthOAuth2Config * cfg ) { cfg->deleteLater( );} );
if ( !config->loadConfigTxt( configtxt, format ) )
{
QgsDebugMsg( QStringLiteral( "FAILED to load config: %1" ).arg( configfile ) );
continue;
}
if ( config->id().isEmpty() )
{
QgsDebugMsg( QStringLiteral( "NO ID SET for config: %1" ).arg( configfile ) );
continue;
}
configs.insert( config->id(), configtxt );
}
if ( ok )
*ok = true;
return configs;
}
QStringList QgsAuthOAuth2Config::configLocations( const QString &extradir )
{
QStringList dirs;
// in order of override preference, i.e. user over pkg dir
dirs << QgsAuthOAuth2Config::oauth2ConfigsPkgDataDir()
<< QgsAuthOAuth2Config::oauth2ConfigsUserSettingsDir();
if ( !extradir.isEmpty() )
{
// configs of similar IDs in this dir will override existing in standard dirs
dirs << extradir;
}
return dirs;
}
QgsStringMap QgsAuthOAuth2Config::mappedOAuth2ConfigsCache( QObject *parent, const QString &extradir )
{
QgsStringMap configs;
bool ok = false;
// Load from default locations
const QStringList configdirs = configLocations( extradir );
for ( const auto &configdir : configdirs )
{
QFileInfo configdirinfo( configdir );
if ( !configdirinfo.exists() || !configdirinfo.isDir() )
{
continue;
}
QgsStringMap newconfigs = QgsAuthOAuth2Config::mapOAuth2Configs(
configdirinfo.canonicalFilePath(), parent, QgsAuthOAuth2Config::JSON, &ok );
if ( ok )
{
QgsStringMap::const_iterator i = newconfigs.constBegin();
while ( i != newconfigs.constEnd() )
{
configs.insert( i.key(), i.value() );
++i;
}
}
}
return configs;
}
// static
QString QgsAuthOAuth2Config::oauth2ConfigsPkgDataDir()
{
return QgsApplication::pkgDataPath() + QStringLiteral( "/oauth2_configs" );
}
// static
QString QgsAuthOAuth2Config::oauth2ConfigsUserSettingsDir()
{
return QgsApplication::qgisSettingsDirPath() + QStringLiteral( "oauth2_configs" );
}
// static
QString QgsAuthOAuth2Config::configTypeString( QgsAuthOAuth2Config::ConfigType configtype )
{
switch ( configtype )
{
case QgsAuthOAuth2Config::Custom:
return tr( "Custom" );
case QgsAuthOAuth2Config::Predefined:
default:
return tr( "Predefined" );
}
}
// static
QString QgsAuthOAuth2Config::grantFlowString( QgsAuthOAuth2Config::GrantFlow flow )
{
switch ( flow )
{
case QgsAuthOAuth2Config::AuthCode:
return tr( "Authorization Code" );
case QgsAuthOAuth2Config::Implicit:
return tr( "Implicit" );
case QgsAuthOAuth2Config::ResourceOwner:
default:
return tr( "Resource Owner" );
}
}
// static
QString QgsAuthOAuth2Config::accessMethodString( QgsAuthOAuth2Config::AccessMethod method )
{
switch ( method )
{
case QgsAuthOAuth2Config::Header:
return tr( "Header" );
case QgsAuthOAuth2Config::Form:
return tr( "Form (POST only)" );
case QgsAuthOAuth2Config::Query:
default:
return tr( "URL Query" );
}
}
// static
QString QgsAuthOAuth2Config::tokenCacheDirectory( bool temporary )
{
QDir setdir( QgsApplication::qgisSettingsDirPath() );
return QStringLiteral( "%1/oauth2-cache" ).arg( temporary ? QDir::tempPath() : setdir.canonicalPath() );
}
// static
QString QgsAuthOAuth2Config::tokenCacheFile( const QString &suffix )
{
return QStringLiteral( "authcfg-%1.ini" ).arg( !suffix.isEmpty() ? suffix : QStringLiteral( "cache" ) );
}
// static
QString QgsAuthOAuth2Config::tokenCachePath( const QString &suffix, bool temporary )
{
return QStringLiteral( "%1/%2" ).arg( QgsAuthOAuth2Config::tokenCacheDirectory( temporary ),
QgsAuthOAuth2Config::tokenCacheFile( suffix ) );
}