Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / uui / source / passwordcontainer.cxx
bloba0bca1197259cc7a06c3bacfe2dfdfdf5391a58d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "comphelper/processfactory.hxx"
21 #include "cppuhelper/factory.hxx"
22 #include <cppuhelper/supportsservice.hxx>
24 #include "com/sun/star/lang/XMultiServiceFactory.hpp"
25 #include "com/sun/star/task/NoMasterException.hpp"
26 #include "com/sun/star/task/PasswordContainer.hpp"
27 #include "com/sun/star/task/XInteractionHandler2.hpp"
28 #include "com/sun/star/ucb/AuthenticationRequest.hpp"
29 #include "com/sun/star/ucb/URLAuthenticationRequest.hpp"
30 #include "com/sun/star/ucb/XInteractionSupplyAuthentication.hpp"
31 #include "com/sun/star/ucb/XInteractionSupplyAuthentication2.hpp"
33 #include "passwordcontainer.hxx"
35 using namespace com::sun::star;
37 namespace {
39 //=========================================================================
40 bool 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 )
51 if ( bUseSystemCredentials )
53 // "use system creds" record found.
54 // Wants client that we use it?
55 if ( xSupplyAuthentication2.is() && bCanUseSystemCredentials )
57 xSupplyAuthentication2->setUseSystemCredentials( sal_True );
58 return true;
60 return false;
62 else if (aRec.UserList.getLength() != 0)
64 if (aRec.UserList[0].Passwords.getLength() == 0)
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;
73 // "user/pass" record found.
74 if (!bCheckForEqualPasswords || !rRequest.HasPassword
75 || rRequest.Password != aRec.UserList[0].Passwords[0]) // failed login attempt?
77 if (xSupplyAuthentication->canSetUserName())
78 xSupplyAuthentication->
79 setUserName(aRec.UserList[0].UserName.getStr());
81 if (xSupplyAuthentication->canSetPassword())
82 xSupplyAuthentication->
83 setPassword(aRec.UserList[0].Passwords[0].getStr());
84 if (aRec.UserList[0].Passwords.getLength() > 1)
86 if (rRequest.HasRealm)
88 if (xSupplyAuthentication->canSetRealm())
89 xSupplyAuthentication->
90 setRealm(aRec.UserList[0].Passwords[1].
91 getStr());
93 else if (xSupplyAuthentication->canSetAccount())
94 xSupplyAuthentication->
95 setAccount(aRec.UserList[0].Passwords[1].
96 getStr());
99 if ( xSupplyAuthentication2.is() && bCanUseSystemCredentials )
100 xSupplyAuthentication2->setUseSystemCredentials( sal_False );
102 return true;
105 return false;
108 } // namespace
110 namespace uui {
112 //=========================================================================
113 PasswordContainerHelper::PasswordContainerHelper(
114 uno::Reference< uno::XComponentContext > const & xContext ):
115 m_xPasswordContainer(task::PasswordContainer::create(xContext))
118 //=========================================================================
119 bool PasswordContainerHelper::handleAuthenticationRequest(
120 ucb::AuthenticationRequest const & rRequest,
121 uno::Reference< ucb::XInteractionSupplyAuthentication > const &
122 xSupplyAuthentication,
123 OUString const & rURL,
124 uno::Reference< task::XInteractionHandler2 > const & xIH )
125 SAL_THROW((uno::RuntimeException))
127 uno::Reference< task::XInteractionHandler > xIH1(xIH, uno::UNO_QUERY);
129 // Is continuation even a XInteractionSupplyAuthentication2, which
130 // is derived from XInteractionSupplyAuthentication?
131 uno::Reference< ucb::XInteractionSupplyAuthentication2 >
132 xSupplyAuthentication2(xSupplyAuthentication, uno::UNO_QUERY);
134 sal_Bool bCanUseSystemCredentials = sal_False;
135 if (xSupplyAuthentication2.is())
137 sal_Bool bDefaultUseSystemCredentials;
138 bCanUseSystemCredentials
139 = xSupplyAuthentication2->canUseSystemCredentials(
140 bDefaultUseSystemCredentials );
143 if ( bCanUseSystemCredentials )
145 // Runtime / Persistent info avail for current auth request?
147 OUString aResult = m_xPasswordContainer->findUrl(
148 rURL.isEmpty() ? rRequest.ServerName : rURL );
149 if ( !aResult.isEmpty() )
151 if ( fillContinuation( true,
152 rRequest,
153 task::UrlRecord(),
154 xSupplyAuthentication,
155 xSupplyAuthentication2,
156 bCanUseSystemCredentials,
157 false ) )
159 return true;
164 // m_xPasswordContainer works with userName passwdSequences pairs:
165 if (rRequest.HasUserName && rRequest.HasPassword)
169 if (rRequest.UserName.isEmpty())
171 task::UrlRecord aRec;
172 if ( !rURL.isEmpty() )
173 aRec = m_xPasswordContainer->find(rURL, xIH1);
175 if ( aRec.UserList.getLength() == 0 )
177 // compat: try server name.
178 aRec = m_xPasswordContainer->find(rRequest.ServerName, xIH1);
181 if ( fillContinuation( false,
182 rRequest,
183 aRec,
184 xSupplyAuthentication,
185 xSupplyAuthentication2,
186 bCanUseSystemCredentials,
187 false ) )
189 return true;
192 else
194 task::UrlRecord aRec;
195 if ( !rURL.isEmpty() )
196 aRec = m_xPasswordContainer->findForName(
197 rURL, rRequest.UserName, xIH1);
199 if ( aRec.UserList.getLength() == 0 )
201 // compat: try server name.
202 aRec = m_xPasswordContainer->findForName(
203 rRequest.ServerName, rRequest.UserName, xIH1);
206 if ( fillContinuation( false,
207 rRequest,
208 aRec,
209 xSupplyAuthentication,
210 xSupplyAuthentication2,
211 bCanUseSystemCredentials,
212 true ) )
214 return true;
218 catch (task::NoMasterException const &)
219 {} // user did not enter master password
221 return false;
224 //=========================================================================
225 bool PasswordContainerHelper::addRecord(
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 SAL_THROW((uno::RuntimeException))
233 uno::Reference< task::XInteractionHandler > xIH1(xIH, uno::UNO_QUERY);
237 if ( !rUsername.isEmpty() )
239 OSL_ENSURE( m_xPasswordContainer.is(),
240 "Got no XPasswordContainer!" );
241 if ( !m_xPasswordContainer.is() )
242 return false;
244 if ( bPersist )
246 // If persistent storing of passwords is not yet
247 // allowed, enable it.
248 if ( !m_xPasswordContainer->isPersistentStoringAllowed() )
249 m_xPasswordContainer->allowPersistentStoring( sal_True );
251 m_xPasswordContainer->addPersistent( rURL,
252 rUsername,
253 rPasswords,
254 xIH1 );
256 else
257 m_xPasswordContainer->add( rURL,
258 rUsername,
259 rPasswords,
260 xIH1 );
262 else
264 m_xPasswordContainer->addUrl( rURL, bPersist );
267 catch ( task::NoMasterException const & )
269 // user did not enter master password
270 return false;
272 return true;
275 //=========================================================================
276 //=========================================================================
277 //=========================================================================
279 PasswordContainerInteractionHandler::PasswordContainerInteractionHandler(
280 const uno::Reference< uno::XComponentContext >& xContext )
281 : m_aPwContainerHelper( xContext )
285 //=========================================================================
286 // virtual
287 PasswordContainerInteractionHandler::~PasswordContainerInteractionHandler()
291 //=========================================================================
293 // XServiceInfo methods.
295 //=========================================================================
297 // virtual
298 OUString SAL_CALL
299 PasswordContainerInteractionHandler::getImplementationName()
300 throw ( uno::RuntimeException )
302 return getImplementationName_Static();
305 //=========================================================================
306 // virtual
307 sal_Bool SAL_CALL
308 PasswordContainerInteractionHandler::supportsService(
309 const OUString& ServiceName )
310 throw ( uno::RuntimeException )
312 return cppu::supportsService(this, ServiceName);
315 //=========================================================================
316 // virtual
317 uno::Sequence< OUString > SAL_CALL
318 PasswordContainerInteractionHandler::getSupportedServiceNames()
319 throw ( uno::RuntimeException )
321 return getSupportedServiceNames_Static();
324 //=========================================================================
325 // static
326 OUString
327 PasswordContainerInteractionHandler::getImplementationName_Static()
329 return OUString( "com.sun.star.comp.uui.PasswordContainerInteractionHandler" );
332 //=========================================================================
333 // static
334 uno::Sequence< OUString >
335 PasswordContainerInteractionHandler::getSupportedServiceNames_Static()
337 uno::Sequence< OUString > aSNS( 1 );
338 aSNS.getArray()[ 0 ] = OUString(
339 "com.sun.star.task.PasswordContainerInteractionHandler" );
340 return aSNS;
343 //=========================================================================
345 // XInteractionHandler2 methods.
347 //=========================================================================
349 // virtual
350 void SAL_CALL
351 PasswordContainerInteractionHandler::handle(
352 const uno::Reference< task::XInteractionRequest >& rRequest )
353 throw ( uno::RuntimeException )
355 handleInteractionRequest( rRequest );
358 // virtual
359 sal_Bool SAL_CALL
360 PasswordContainerInteractionHandler::handleInteractionRequest(
361 const uno::Reference< task::XInteractionRequest >& rRequest )
362 throw ( uno::RuntimeException )
364 if ( !rRequest.is() )
365 return false;
367 uno::Any aAnyRequest( rRequest->getRequest() );
369 ucb::AuthenticationRequest aAuthenticationRequest;
370 if ( !( aAnyRequest >>= aAuthenticationRequest ) )
371 return false;
373 OUString aURL;
374 ucb::URLAuthenticationRequest aURLAuthenticationRequest;
375 if ( aAnyRequest >>= aURLAuthenticationRequest )
376 aURL = aURLAuthenticationRequest.URL;
378 uno::Sequence< uno::Reference< task::XInteractionContinuation > >
379 rContinuations = rRequest->getContinuations();
381 uno::Reference< ucb::XInteractionSupplyAuthentication >
382 xSupplyAuthentication;
384 for ( sal_Int32 i = 0; i < rContinuations.getLength(); ++i )
386 xSupplyAuthentication
387 = uno::Reference< ucb::XInteractionSupplyAuthentication >(
388 rContinuations[i], uno::UNO_QUERY );
389 if( xSupplyAuthentication.is() )
390 break;
393 if ( !xSupplyAuthentication.is() )
394 return false;
396 // Try to obtain credentials from password container.
397 if ( m_aPwContainerHelper.
398 handleAuthenticationRequest( aAuthenticationRequest,
399 xSupplyAuthentication,
400 aURL,
401 // @@@ FIXME: this not able to
402 // handle master pw request!
403 // master pw request is never
404 // solvable without UI!
405 this ) )
407 // successfully handled
408 xSupplyAuthentication->select();
409 return true;
411 return false;
414 //=========================================================================
416 // Service factory implementation.
418 //=========================================================================
420 static uno::Reference< uno::XInterface > SAL_CALL
421 PasswordContainerInteractionHandler_CreateInstance(
422 const uno::Reference< lang::XMultiServiceFactory> & rSMgr )
423 throw( uno::Exception )
425 lang::XServiceInfo * pX = static_cast< lang::XServiceInfo * >(
426 new PasswordContainerInteractionHandler( comphelper::getComponentContext(rSMgr) ) );
427 return uno::Reference< uno::XInterface >::query( pX );
430 //=========================================================================
431 // static
432 uno::Reference< lang::XSingleServiceFactory >
433 PasswordContainerInteractionHandler::createServiceFactory(
434 const uno::Reference< lang::XMultiServiceFactory >& rxServiceMgr )
436 return uno::Reference< lang::XSingleServiceFactory >(
437 cppu::createOneInstanceFactory(
438 rxServiceMgr,
439 PasswordContainerInteractionHandler::getImplementationName_Static(),
440 PasswordContainerInteractionHandler_CreateInstance,
441 PasswordContainerInteractionHandler::getSupportedServiceNames_Static() ) );
444 } // namespace uui
446 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */