1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
;
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
);
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.
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].
93 else if (xSupplyAuthentication
->canSetAccount())
94 xSupplyAuthentication
->
95 setAccount(aRec
.UserList
[0].Passwords
[1].
99 if ( xSupplyAuthentication2
.is() && bCanUseSystemCredentials
)
100 xSupplyAuthentication2
->setUseSystemCredentials( sal_False
);
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,
154 xSupplyAuthentication
,
155 xSupplyAuthentication2
,
156 bCanUseSystemCredentials
,
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,
184 xSupplyAuthentication
,
185 xSupplyAuthentication2
,
186 bCanUseSystemCredentials
,
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,
209 xSupplyAuthentication
,
210 xSupplyAuthentication2
,
211 bCanUseSystemCredentials
,
218 catch (task::NoMasterException
const &)
219 {} // user did not enter master password
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
,
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() )
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
,
257 m_xPasswordContainer
->add( rURL
,
264 m_xPasswordContainer
->addUrl( rURL
, bPersist
);
267 catch ( task::NoMasterException
const & )
269 // user did not enter master password
275 //=========================================================================
276 //=========================================================================
277 //=========================================================================
279 PasswordContainerInteractionHandler::PasswordContainerInteractionHandler(
280 const uno::Reference
< uno::XComponentContext
>& xContext
)
281 : m_aPwContainerHelper( xContext
)
285 //=========================================================================
287 PasswordContainerInteractionHandler::~PasswordContainerInteractionHandler()
291 //=========================================================================
293 // XServiceInfo methods.
295 //=========================================================================
299 PasswordContainerInteractionHandler::getImplementationName()
300 throw ( uno::RuntimeException
)
302 return getImplementationName_Static();
305 //=========================================================================
308 PasswordContainerInteractionHandler::supportsService(
309 const OUString
& ServiceName
)
310 throw ( uno::RuntimeException
)
312 return cppu::supportsService(this, ServiceName
);
315 //=========================================================================
317 uno::Sequence
< OUString
> SAL_CALL
318 PasswordContainerInteractionHandler::getSupportedServiceNames()
319 throw ( uno::RuntimeException
)
321 return getSupportedServiceNames_Static();
324 //=========================================================================
327 PasswordContainerInteractionHandler::getImplementationName_Static()
329 return OUString( "com.sun.star.comp.uui.PasswordContainerInteractionHandler" );
332 //=========================================================================
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" );
343 //=========================================================================
345 // XInteractionHandler2 methods.
347 //=========================================================================
351 PasswordContainerInteractionHandler::handle(
352 const uno::Reference
< task::XInteractionRequest
>& rRequest
)
353 throw ( uno::RuntimeException
)
355 handleInteractionRequest( rRequest
);
360 PasswordContainerInteractionHandler::handleInteractionRequest(
361 const uno::Reference
< task::XInteractionRequest
>& rRequest
)
362 throw ( uno::RuntimeException
)
364 if ( !rRequest
.is() )
367 uno::Any
aAnyRequest( rRequest
->getRequest() );
369 ucb::AuthenticationRequest aAuthenticationRequest
;
370 if ( !( aAnyRequest
>>= aAuthenticationRequest
) )
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() )
393 if ( !xSupplyAuthentication
.is() )
396 // Try to obtain credentials from password container.
397 if ( m_aPwContainerHelper
.
398 handleAuthenticationRequest( aAuthenticationRequest
,
399 xSupplyAuthentication
,
401 // @@@ FIXME: this not able to
402 // handle master pw request!
403 // master pw request is never
404 // solvable without UI!
407 // successfully handled
408 xSupplyAuthentication
->select();
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 //=========================================================================
432 uno::Reference
< lang::XSingleServiceFactory
>
433 PasswordContainerInteractionHandler::createServiceFactory(
434 const uno::Reference
< lang::XMultiServiceFactory
>& rxServiceMgr
)
436 return uno::Reference
< lang::XSingleServiceFactory
>(
437 cppu::createOneInstanceFactory(
439 PasswordContainerInteractionHandler::getImplementationName_Static(),
440 PasswordContainerInteractionHandler_CreateInstance
,
441 PasswordContainerInteractionHandler::getSupportedServiceNames_Static() ) );
446 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */