LibreOffice Module uui (master) 1
passwordcontainer.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
21#include <osl/diagnose.h>
22
23#include <com/sun/star/lang/XMultiServiceFactory.hpp>
24#include <com/sun/star/task/NoMasterException.hpp>
25#include <com/sun/star/task/PasswordContainer.hpp>
26#include <com/sun/star/task/XInteractionHandler2.hpp>
27#include <com/sun/star/ucb/AuthenticationRequest.hpp>
28#include <com/sun/star/ucb/URLAuthenticationRequest.hpp>
29#include <com/sun/star/ucb/XInteractionSupplyAuthentication.hpp>
30#include <com/sun/star/ucb/XInteractionSupplyAuthentication2.hpp>
31#include <officecfg/Office/Common.hxx>
32
33#include "passwordcontainer.hxx"
34
35using namespace com::sun::star;
36
37namespace {
38
39
40bool fillContinuation(
41 bool bUseSystemCredentials,
42 const ucb::AuthenticationRequest & rRequest,
43 const task::UrlRecord & aRec,
44 const uno::Reference< ucb::XInteractionSupplyAuthentication > &
45 xSupplyAuthentication,
46 const uno::Reference< ucb::XInteractionSupplyAuthentication2 > &
47 xSupplyAuthentication2,
48 bool bCanUseSystemCredentials,
49 bool bCheckForEqualPasswords )
50{
51 if ( bUseSystemCredentials )
52 {
53 // "use system creds" record found.
54 // Wants client that we use it?
55 if ( xSupplyAuthentication2.is() && bCanUseSystemCredentials )
56 {
57 xSupplyAuthentication2->setUseSystemCredentials( true );
58 return true;
59 }
60 return false;
61 }
62 else if (aRec.UserList.hasElements())
63 {
64 if (!aRec.UserList[0].Passwords.hasElements())
65 {
66 // Password sequence can be empty, for instance if master
67 // password was not given (e.g. master pw dialog canceled)
68 // pw container does not throw NoMasterException in this case.
69 // bug???
70 return false;
71 }
72
73 // "user/pass" record found.
74 if (!bCheckForEqualPasswords || !rRequest.HasPassword
75 || rRequest.Password != aRec.UserList[0].Passwords[0]) // failed login attempt?
76 {
77 if (xSupplyAuthentication->canSetUserName())
78 xSupplyAuthentication->
79 setUserName(aRec.UserList[0].UserName);
80
81 if (xSupplyAuthentication->canSetPassword())
82 xSupplyAuthentication->
83 setPassword(aRec.UserList[0].Passwords[0]);
84 if (aRec.UserList[0].Passwords.getLength() > 1)
85 {
86 if (rRequest.HasRealm)
87 {
88 if (xSupplyAuthentication->canSetRealm())
89 xSupplyAuthentication->
90 setRealm(aRec.UserList[0].Passwords[1]);
91 }
92 else if (xSupplyAuthentication->canSetAccount())
93 xSupplyAuthentication->
94 setAccount(aRec.UserList[0].Passwords[1]);
95 }
96
97 if ( xSupplyAuthentication2.is() && bCanUseSystemCredentials )
98 xSupplyAuthentication2->setUseSystemCredentials( false );
99
100 return true;
101 }
102 }
103 return false;
104}
105
106} // namespace
107
108namespace uui {
109
110
112 uno::Reference< uno::XComponentContext > const & xContext ):
113 m_xPasswordContainer(task::PasswordContainer::create(xContext))
114{}
115
116
118 ucb::AuthenticationRequest const & rRequest,
119 uno::Reference< ucb::XInteractionSupplyAuthentication > const &
120 xSupplyAuthentication,
121 OUString const & rURL,
122 uno::Reference< task::XInteractionHandler2 > const & xIH )
123{
124 // Is continuation even a XInteractionSupplyAuthentication2, which
125 // is derived from XInteractionSupplyAuthentication?
126 uno::Reference< ucb::XInteractionSupplyAuthentication2 >
127 xSupplyAuthentication2(xSupplyAuthentication, uno::UNO_QUERY);
128
129 bool bCanUseSystemCredentials = false;
130 if (xSupplyAuthentication2.is())
131 {
132 sal_Bool bDefaultUseSystemCredentials;
133 bCanUseSystemCredentials
134 = xSupplyAuthentication2->canUseSystemCredentials(
135 bDefaultUseSystemCredentials );
136 }
137
138 if ( bCanUseSystemCredentials )
139 {
140 // Does the configuration mandate that we try system credentials first?
141 bool bUseSystemCredentials = ::officecfg::Office::Common::Passwords::TrySystemCredentialsFirst::get();
142 if (!bUseSystemCredentials)
143 {
144 // Runtime / Persistent info avail for current auth request?
145 OUString aResult = m_xPasswordContainer->findUrl(
146 rURL.isEmpty() ? rRequest.ServerName : rURL);
147 bUseSystemCredentials = !aResult.isEmpty();
148 }
149 if ( bUseSystemCredentials )
150 {
151 if ( fillContinuation( true,
152 rRequest,
153 task::UrlRecord(),
154 xSupplyAuthentication,
155 xSupplyAuthentication2,
156 bCanUseSystemCredentials,
157 false ) )
158 {
159 return true;
160 }
161 }
162 }
163
164 // m_xPasswordContainer works with userName passwdSequences pairs:
165 if (rRequest.HasUserName && rRequest.HasPassword)
166 {
167 try
168 {
169 if (rRequest.UserName.isEmpty())
170 {
171 task::UrlRecord aRec;
172 if ( !rURL.isEmpty() )
173 aRec = m_xPasswordContainer->find(rURL, xIH);
174
175 if ( !aRec.UserList.hasElements() )
176 {
177 // compat: try server name.
178 aRec = m_xPasswordContainer->find(rRequest.ServerName, xIH);
179 }
180
181 if ( fillContinuation( false,
182 rRequest,
183 aRec,
184 xSupplyAuthentication,
185 xSupplyAuthentication2,
186 bCanUseSystemCredentials,
187 false ) )
188 {
189 return true;
190 }
191 }
192 else
193 {
194 task::UrlRecord aRec;
195 if ( !rURL.isEmpty() )
196 aRec = m_xPasswordContainer->findForName(
197 rURL, rRequest.UserName, xIH);
198
199 if ( !aRec.UserList.hasElements() )
200 {
201 // compat: try server name.
202 aRec = m_xPasswordContainer->findForName(
203 rRequest.ServerName, rRequest.UserName, xIH);
204 }
205
206 if ( fillContinuation( false,
207 rRequest,
208 aRec,
209 xSupplyAuthentication,
210 xSupplyAuthentication2,
211 bCanUseSystemCredentials,
212 true ) )
213 {
214 return true;
215 }
216 }
217 }
218 catch (task::NoMasterException const &)
219 {} // user did not enter master password
220 }
221 return false;
222}
223
224
226 OUString const & rURL,
227 OUString const & rUsername,
228 uno::Sequence< OUString > const & rPasswords,
229 uno::Reference< task::XInteractionHandler2 > const & xIH,
230 bool bPersist )
231{
232 try
233 {
234 if ( !rUsername.isEmpty() )
235 {
236 OSL_ENSURE( m_xPasswordContainer.is(),
237 "Got no XPasswordContainer!" );
238 if ( !m_xPasswordContainer.is() )
239 return false;
240
241 if ( bPersist )
242 {
243 // If persistent storing of passwords is not yet
244 // allowed, enable it.
245 if ( !m_xPasswordContainer->isPersistentStoringAllowed() )
246 m_xPasswordContainer->allowPersistentStoring( true );
247
248 m_xPasswordContainer->addPersistent( rURL,
249 rUsername,
250 rPasswords,
251 xIH );
252 }
253 else
254 m_xPasswordContainer->add( rURL,
255 rUsername,
256 rPasswords,
257 xIH );
258 }
259 else
260 {
261 m_xPasswordContainer->addUrl( rURL, bPersist );
262 }
263 }
264 catch ( task::NoMasterException const & )
265 {
266 // user did not enter master password
267 return false;
268 }
269 return true;
270}
271
272
274 const uno::Reference< uno::XComponentContext >& xContext )
275: m_aPwContainerHelper( xContext )
276{
277}
278
279
280// virtual
282{
283}
284
285
286// XServiceInfo methods.
287
288
289// virtual
290OUString SAL_CALL
292{
293 return "com.sun.star.comp.uui.PasswordContainerInteractionHandler";
294}
295
296
297// virtual
298sal_Bool SAL_CALL
300 const OUString& ServiceName )
301{
303}
304
305
306// virtual
307uno::Sequence< OUString > SAL_CALL
309{
310 return { "com.sun.star.task.PasswordContainerInteractionHandler" };
311}
312
313
314// XInteractionHandler2 methods.
315
316
317// virtual
318void SAL_CALL
320 const uno::Reference< task::XInteractionRequest >& rRequest )
321{
322 handleInteractionRequest( rRequest );
323}
324
325// virtual
326sal_Bool SAL_CALL
328 const uno::Reference< task::XInteractionRequest >& rRequest )
329{
330 if ( !rRequest.is() )
331 return false;
332
333 uno::Any aAnyRequest( rRequest->getRequest() );
334
335 ucb::AuthenticationRequest aAuthenticationRequest;
336 if ( !( aAnyRequest >>= aAuthenticationRequest ) )
337 return false;
338
339 OUString aURL;
340 ucb::URLAuthenticationRequest aURLAuthenticationRequest;
341 if ( aAnyRequest >>= aURLAuthenticationRequest )
342 aURL = aURLAuthenticationRequest.URL;
343
344 const uno::Sequence< uno::Reference< task::XInteractionContinuation > >
345 rContinuations = rRequest->getContinuations();
346
347 uno::Reference< ucb::XInteractionSupplyAuthentication >
348 xSupplyAuthentication;
349
350 for ( const auto& rContinuation : rContinuations )
351 {
352 xSupplyAuthentication.set( rContinuation, uno::UNO_QUERY );
353 if( xSupplyAuthentication.is() )
354 break;
355 }
356
357 if ( !xSupplyAuthentication.is() )
358 return false;
359
360 // Try to obtain credentials from password container.
362 handleAuthenticationRequest( aAuthenticationRequest,
363 xSupplyAuthentication,
364 aURL,
365 // @@@ FIXME: this not able to
366 // handle master pw request!
367 // master pw request is never
368 // solvable without UI!
369 this ) )
370 {
371 // successfully handled
372 xSupplyAuthentication->select();
373 return true;
374 }
375 return false;
376}
377
378
379} // namespace uui
380
381extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
383 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
384{
385 return cppu::acquire(new uui::PasswordContainerInteractionHandler(context));
386}
387
388/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
PasswordContainerHelper(css::uno::Reference< css::uno::XComponentContext > const &xContext)
bool handleAuthenticationRequest(css::ucb::AuthenticationRequest const &rRequest, css::uno::Reference< css::ucb::XInteractionSupplyAuthentication > const &xSupplyAuthentication, OUString const &rURL, css::uno::Reference< css::task::XInteractionHandler2 > const &xIH)
This member function tries to handle an authentication interaction request by looking up credentials ...
css::uno::Reference< css::task::XPasswordContainer2 > m_xPasswordContainer
bool addRecord(OUString const &rURL, OUString const &rUsername, css::uno::Sequence< OUString > const &rPasswords, css::uno::Reference< css::task::XInteractionHandler2 > const &xIH, bool bPersist)
This member function adds credentials for the given URL to the password container.
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
virtual OUString SAL_CALL getImplementationName() override
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
PasswordContainerInteractionHandler(const css::uno::Reference< css::uno::XComponentContext > &xContext)
virtual void SAL_CALL handle(const css::uno::Reference< css::task::XInteractionRequest > &Request) override
virtual sal_Bool SAL_CALL handleInteractionRequest(const css::uno::Reference< css::task::XInteractionRequest > &Request) override
URL aURL
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
css::uno::Reference< css::deployment::XPackageRegistry > create(css::uno::Reference< css::deployment::XPackageRegistry > const &xRootRegistry, OUString const &context, OUString const &cachePath, css::uno::Reference< css::uno::XComponentContext > const &xComponentContext)
Definition: fltdlg.cxx:29
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_uui_PasswordContainerInteractionHandler_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
unsigned char sal_Bool