17
17
#include " qgsauthidentcertmethod.h"
18
18
#include " qgsauthidentcertedit.h"
19
19
20
+ #include < QDir>
21
+ #include < QFile>
22
+ #include < QUuid>
20
23
#ifndef QT_NO_OPENSSL
21
24
#include < QtCrypto>
22
25
#include < QSslConfiguration>
27
30
#include " qgsauthmanager.h"
28
31
#include " qgslogger.h"
29
32
30
-
31
33
static const QString AUTH_METHOD_KEY = " Identity-Cert" ;
32
34
static const QString AUTH_METHOD_DESCRIPTION = " Identity certificate authentication" ;
33
35
@@ -38,12 +40,13 @@ QgsAuthIdentCertMethod::QgsAuthIdentCertMethod()
38
40
: QgsAuthMethod()
39
41
{
40
42
setVersion ( 2 );
41
- setExpansions ( QgsAuthMethod::NetworkRequest );
43
+ setExpansions ( QgsAuthMethod::NetworkRequest | QgsAuthMethod::DataSourceURI );
42
44
setDataProviders ( QStringList ()
43
45
<< " ows"
44
46
<< " wfs" // convert to lowercase
45
47
<< " wcs"
46
- << " wms" );
48
+ << " wms"
49
+ << " postgres" );
47
50
}
48
51
49
52
QgsAuthIdentCertMethod::~QgsAuthIdentCertMethod ()
@@ -101,6 +104,128 @@ bool QgsAuthIdentCertMethod::updateNetworkRequest( QNetworkRequest &request, con
101
104
return true ;
102
105
}
103
106
107
+ bool QgsAuthIdentCertMethod::updateDataSourceUriItems ( QStringList &connectionItems, const QString &authcfg,
108
+ const QString &dataprovider )
109
+ {
110
+ Q_UNUSED ( dataprovider )
111
+
112
+ QgsDebugMsg ( QString ( " Update URI items for authcfg: %1" ).arg ( authcfg ) );
113
+
114
+ QString pkiTempFilePrefix = " tmppki_" ;
115
+
116
+ QgsAuthMethodConfig amConfig;
117
+ if ( !QgsAuthManager::instance ()->loadAuthenticationConfig ( authcfg, amConfig, true ) )
118
+ {
119
+ QgsDebugMsg ( QString ( " Update URI items: FAILED to retrieve config for authcfg: %1" ).arg ( authcfg ) );
120
+ return false ;
121
+ }
122
+
123
+ if ( !amConfig.isValid () )
124
+ {
125
+ QgsDebugMsg ( QString ( " Update URI items: FAILED retrieved invalid Auth method for authcfg: %1" ).arg ( authcfg ) );
126
+ return false ;
127
+ }
128
+
129
+ // get client cent and key
130
+ QSslCertificate clientCert = QgsAuthManager::instance ()->getCertIdentityBundle ( amConfig.config ( " certid" ) ).first ;
131
+ QSslKey clientKey = QgsAuthManager::instance ()->getCertIdentityBundle ( amConfig.config ( " certid" ) ).second ;
132
+
133
+ // get common name of the client certificate
134
+ QString commonName = QgsAuthCertUtils::resolvedCertName ( clientCert, false );
135
+
136
+ // get CA
137
+ QByteArray caCert = QgsAuthManager::instance ()->getTrustedCaCertsPemText ();
138
+
139
+ // save client cert to temp file
140
+ QFile certFile ( QDir::tempPath () + QDir::separator () + pkiTempFilePrefix + QUuid::createUuid () + " .pem" );
141
+ if ( certFile.open ( QIODevice::WriteOnly ) )
142
+ {
143
+ certFile.write ( clientCert.toPem () );
144
+ }
145
+ else
146
+ {
147
+ QgsDebugMsg ( QString ( " Update URI items: FAILED to save client cert temporary file" ) );
148
+ return false ;
149
+ }
150
+
151
+ certFile.setPermissions ( QFile::ReadUser | QFile::WriteUser );
152
+
153
+ // save key cert to temp file setting it's permission only read to the current user
154
+ QFile keyFile ( QDir::tempPath () + QDir::separator () + pkiTempFilePrefix + QUuid::createUuid () + " .pem" );
155
+ if ( keyFile.open ( QIODevice::WriteOnly ) )
156
+ {
157
+ keyFile.write ( clientKey.toPem () );
158
+ }
159
+ else
160
+ {
161
+ QgsDebugMsg ( QString ( " Update URI items: FAILED to save client key temporary file" ) );
162
+ return false ;
163
+ }
164
+
165
+ keyFile.setPermissions ( QFile::ReadUser );
166
+
167
+ // save CA to tempo file
168
+ QFile caFile ( QDir::tempPath () + QDir::separator () + pkiTempFilePrefix + QUuid::createUuid () + " .pem" );
169
+ if ( caFile.open ( QIODevice::WriteOnly ) )
170
+ {
171
+ caFile.write ( caCert );
172
+ }
173
+ else
174
+ {
175
+ QgsDebugMsg ( QString ( " Update URI items: FAILED to save CAs to temporary file" ) );
176
+ return false ;
177
+ }
178
+
179
+ caFile.setPermissions ( QFile::ReadUser | QFile::WriteUser );
180
+
181
+ // add uri parameters
182
+ QString userparam = " user='" + commonName + " '" ;
183
+ int userindx = connectionItems.indexOf ( QRegExp ( " ^user='.*" ) );
184
+ if ( userindx != -1 )
185
+ {
186
+ connectionItems.replace ( userindx, userparam );
187
+ }
188
+ else
189
+ {
190
+ connectionItems.append ( userparam );
191
+ }
192
+
193
+ QString certparam = " sslcert='" + certFile.fileName () + " '" ;
194
+ int sslcertindx = connectionItems.indexOf ( QRegExp ( " ^sslcert='.*" ) );
195
+ if ( sslcertindx != -1 )
196
+ {
197
+ connectionItems.replace ( sslcertindx, certparam );
198
+ }
199
+ else
200
+ {
201
+ connectionItems.append ( certparam );
202
+ }
203
+
204
+ QString keyparam = " sslkey='" + keyFile.fileName () + " '" ;
205
+ int sslkeyindx = connectionItems.indexOf ( QRegExp ( " ^sslkey='.*" ) );
206
+ if ( sslkeyindx != -1 )
207
+ {
208
+ connectionItems.replace ( sslkeyindx, keyparam );
209
+ }
210
+ else
211
+ {
212
+ connectionItems.append ( keyparam );
213
+ }
214
+
215
+ QString caparam = " sslrootcert='" + caFile.fileName () + " '" ;
216
+ int sslcaindx = connectionItems.indexOf ( QRegExp ( " ^sslrootcert='.*" ) );
217
+ if ( sslcaindx != -1 )
218
+ {
219
+ connectionItems.replace ( sslcaindx, caparam );
220
+ }
221
+ else
222
+ {
223
+ connectionItems.append ( caparam );
224
+ }
225
+
226
+ return true ;
227
+ }
228
+
104
229
void QgsAuthIdentCertMethod::clearCachedConfig ( const QString &authcfg )
105
230
{
106
231
removePkiConfigBundle ( authcfg );
0 commit comments