bump product version to 5.0.4.1
[LibreOffice.git] / uui / source / passwordcontainer.cxx
blob08a8b686b3072a45169e8becdd26e1fb0c96e258
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>
23 #include <osl/diagnose.h>
25 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
26 #include <com/sun/star/task/NoMasterException.hpp>
27 #include <com/sun/star/task/PasswordContainer.hpp>
28 #include <com/sun/star/task/XInteractionHandler2.hpp>
29 #include <com/sun/star/ucb/AuthenticationRequest.hpp>
30 #include <com/sun/star/ucb/URLAuthenticationRequest.hpp>
31 #include <com/sun/star/ucb/XInteractionSupplyAuthentication.hpp>
32 #include <com/sun/star/ucb/XInteractionSupplyAuthentication2.hpp>
34 #include "passwordcontainer.hxx"
36 using namespace com::sun::star;
38 namespace {
41 bool fillContinuation(
42 bool bUseSystemCredentials,
43 const ucb::AuthenticationRequest & rRequest,
44 const task::UrlRecord & aRec,
45 const uno::Reference< ucb::XInteractionSupplyAuthentication > &
46 xSupplyAuthentication,
47 const uno::Reference< ucb::XInteractionSupplyAuthentication2 > &
48 xSupplyAuthentication2,
49 bool bCanUseSystemCredentials,
50 bool bCheckForEqualPasswords )
52 if ( bUseSystemCredentials )
54 // "use system creds" record found.
55 // Wants client that we use it?
56 if ( xSupplyAuthentication2.is() && bCanUseSystemCredentials )
58 xSupplyAuthentication2->setUseSystemCredentials( sal_True );
59 return true;
61 return false;
63 else if (aRec.UserList.getLength() != 0)
65 if (aRec.UserList[0].Passwords.getLength() == 0)
67 // Password sequence can be empty, for instance if master
68 // password was not given (e.g. master pw dialog canceled)
69 // pw container does not throw NoMasterException in this case.
70 // bug???
71 return false;
74 // "user/pass" record found.
75 if (!bCheckForEqualPasswords || !rRequest.HasPassword
76 || rRequest.Password != aRec.UserList[0].Passwords[0]) // failed login attempt?
78 if (xSupplyAuthentication->canSetUserName())
79 xSupplyAuthentication->
80 setUserName(aRec.UserList[0].UserName.getStr());
82 if (xSupplyAuthentication->canSetPassword())
83 xSupplyAuthentication->
84 setPassword(aRec.UserList[0].Passwords[0].getStr());
85 if (aRec.UserList[0].Passwords.getLength() > 1)
87 if (rRequest.HasRealm)
89 if (xSupplyAuthentication->canSetRealm())
90 xSupplyAuthentication->
91 setRealm(aRec.UserList[0].Passwords[1].
92 getStr());
94 else if (xSupplyAuthentication->canSetAccount())
95 xSupplyAuthentication->
96 setAccount(aRec.UserList[0].Passwords[1].
97 getStr());
100 if ( xSupplyAuthentication2.is() && bCanUseSystemCredentials )
101 xSupplyAuthentication2->setUseSystemCredentials( sal_False );
103 return true;
106 return false;
109 } // namespace
111 namespace uui {
114 PasswordContainerHelper::PasswordContainerHelper(
115 uno::Reference< uno::XComponentContext > const & xContext ):
116 m_xPasswordContainer(task::PasswordContainer::create(xContext))
120 bool PasswordContainerHelper::handleAuthenticationRequest(
121 ucb::AuthenticationRequest const & rRequest,
122 uno::Reference< ucb::XInteractionSupplyAuthentication > const &
123 xSupplyAuthentication,
124 OUString const & rURL,
125 uno::Reference< task::XInteractionHandler2 > const & xIH )
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 bool bCanUseSystemCredentials = 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;
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 )
232 uno::Reference< task::XInteractionHandler > xIH1(xIH, uno::UNO_QUERY);
236 if ( !rUsername.isEmpty() )
238 OSL_ENSURE( m_xPasswordContainer.is(),
239 "Got no XPasswordContainer!" );
240 if ( !m_xPasswordContainer.is() )
241 return false;
243 if ( bPersist )
245 // If persistent storing of passwords is not yet
246 // allowed, enable it.
247 if ( !m_xPasswordContainer->isPersistentStoringAllowed() )
248 m_xPasswordContainer->allowPersistentStoring( sal_True );
250 m_xPasswordContainer->addPersistent( rURL,
251 rUsername,
252 rPasswords,
253 xIH1 );
255 else
256 m_xPasswordContainer->add( rURL,
257 rUsername,
258 rPasswords,
259 xIH1 );
261 else
263 m_xPasswordContainer->addUrl( rURL, bPersist );
266 catch ( task::NoMasterException const & )
268 // user did not enter master password
269 return false;
271 return true;
278 PasswordContainerInteractionHandler::PasswordContainerInteractionHandler(
279 const uno::Reference< uno::XComponentContext >& xContext )
280 : m_aPwContainerHelper( xContext )
285 // virtual
286 PasswordContainerInteractionHandler::~PasswordContainerInteractionHandler()
292 // XServiceInfo methods.
296 // virtual
297 OUString SAL_CALL
298 PasswordContainerInteractionHandler::getImplementationName()
299 throw ( uno::RuntimeException, std::exception )
301 return getImplementationName_Static();
305 // virtual
306 sal_Bool SAL_CALL
307 PasswordContainerInteractionHandler::supportsService(
308 const OUString& ServiceName )
309 throw ( uno::RuntimeException, std::exception )
311 return cppu::supportsService(this, ServiceName);
315 // virtual
316 uno::Sequence< OUString > SAL_CALL
317 PasswordContainerInteractionHandler::getSupportedServiceNames()
318 throw ( uno::RuntimeException, std::exception )
320 return getSupportedServiceNames_Static();
324 // static
325 OUString
326 PasswordContainerInteractionHandler::getImplementationName_Static()
328 return OUString( "com.sun.star.comp.uui.PasswordContainerInteractionHandler" );
332 // static
333 uno::Sequence< OUString >
334 PasswordContainerInteractionHandler::getSupportedServiceNames_Static()
336 uno::Sequence< OUString > aSNS( 1 );
337 aSNS.getArray()[ 0 ] =
338 "com.sun.star.task.PasswordContainerInteractionHandler";
339 return aSNS;
344 // XInteractionHandler2 methods.
348 // virtual
349 void SAL_CALL
350 PasswordContainerInteractionHandler::handle(
351 const uno::Reference< task::XInteractionRequest >& rRequest )
352 throw ( uno::RuntimeException, std::exception )
354 handleInteractionRequest( rRequest );
357 // virtual
358 sal_Bool SAL_CALL
359 PasswordContainerInteractionHandler::handleInteractionRequest(
360 const uno::Reference< task::XInteractionRequest >& rRequest )
361 throw ( uno::RuntimeException, std::exception )
363 if ( !rRequest.is() )
364 return false;
366 uno::Any aAnyRequest( rRequest->getRequest() );
368 ucb::AuthenticationRequest aAuthenticationRequest;
369 if ( !( aAnyRequest >>= aAuthenticationRequest ) )
370 return false;
372 OUString aURL;
373 ucb::URLAuthenticationRequest aURLAuthenticationRequest;
374 if ( aAnyRequest >>= aURLAuthenticationRequest )
375 aURL = aURLAuthenticationRequest.URL;
377 uno::Sequence< uno::Reference< task::XInteractionContinuation > >
378 rContinuations = rRequest->getContinuations();
380 uno::Reference< ucb::XInteractionSupplyAuthentication >
381 xSupplyAuthentication;
383 for ( sal_Int32 i = 0; i < rContinuations.getLength(); ++i )
385 xSupplyAuthentication
386 = uno::Reference< ucb::XInteractionSupplyAuthentication >(
387 rContinuations[i], uno::UNO_QUERY );
388 if( xSupplyAuthentication.is() )
389 break;
392 if ( !xSupplyAuthentication.is() )
393 return false;
395 // Try to obtain credentials from password container.
396 if ( m_aPwContainerHelper.
397 handleAuthenticationRequest( aAuthenticationRequest,
398 xSupplyAuthentication,
399 aURL,
400 // @@@ FIXME: this not able to
401 // handle master pw request!
402 // master pw request is never
403 // solvable without UI!
404 this ) )
406 // successfully handled
407 xSupplyAuthentication->select();
408 return true;
410 return false;
415 // Service factory implementation.
419 static uno::Reference< uno::XInterface > SAL_CALL
420 PasswordContainerInteractionHandler_CreateInstance(
421 const uno::Reference< lang::XMultiServiceFactory> & rSMgr )
422 throw( uno::Exception )
424 lang::XServiceInfo * pX = static_cast< lang::XServiceInfo * >(
425 new PasswordContainerInteractionHandler( comphelper::getComponentContext(rSMgr) ) );
426 return uno::Reference< uno::XInterface >::query( pX );
430 // static
431 uno::Reference< lang::XSingleServiceFactory >
432 PasswordContainerInteractionHandler::createServiceFactory(
433 const uno::Reference< lang::XMultiServiceFactory >& rxServiceMgr )
435 return uno::Reference< lang::XSingleServiceFactory >(
436 cppu::createOneInstanceFactory(
437 rxServiceMgr,
438 PasswordContainerInteractionHandler::getImplementationName_Static(),
439 PasswordContainerInteractionHandler_CreateInstance,
440 PasswordContainerInteractionHandler::getSupportedServiceNames_Static() ) );
443 } // namespace uui
445 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */