From e4307a4d3fc43914c8d28157c0e71633843fa045 Mon Sep 17 00:00:00 2001 From: Kai Sommerfeld Date: Thu, 6 May 2010 14:23:16 +0200 Subject: #i110213# - setup master password container on demand. --- ucb/source/ucp/webdav/DAVAuthListener.hxx | 3 - ucb/source/ucp/webdav/DAVAuthListenerImpl.hxx | 1 - ucb/source/ucp/webdav/DAVResourceAccess.cxx | 4 +- ucb/source/ucp/webdav/NeonSession.cxx | 21 --- uui/source/iahndl-authentication.cxx | 245 ++++++++++++++++++-------- uui/source/loginerr.hxx | 61 ++++--- uui/source/passwordcontainer.cxx | 13 +- 7 files changed, 226 insertions(+), 122 deletions(-) diff --git a/ucb/source/ucp/webdav/DAVAuthListener.hxx b/ucb/source/ucp/webdav/DAVAuthListener.hxx index c092f9fd311f..71bed1c0a14d 100644 --- a/ucb/source/ucp/webdav/DAVAuthListener.hxx +++ b/ucb/source/ucp/webdav/DAVAuthListener.hxx @@ -30,9 +30,7 @@ #include #include -#ifndef _COM_SUN_STAR_UCB_XREFERENCE_HPP_ #include -#endif #include namespace webdav_ucp @@ -46,7 +44,6 @@ class DAVAuthListener : public salhelper::SimpleReferenceObject const ::rtl::OUString & inHostName, ::rtl::OUString & inoutUserName, ::rtl::OUString & outPassWord, - sal_Bool bAllowPersistentStoring, sal_Bool bCanUseSystemCredentials ) = 0; }; diff --git a/ucb/source/ucp/webdav/DAVAuthListenerImpl.hxx b/ucb/source/ucp/webdav/DAVAuthListenerImpl.hxx index 3d03cc63952b..c0c9968613af 100644 --- a/ucb/source/ucp/webdav/DAVAuthListenerImpl.hxx +++ b/ucb/source/ucp/webdav/DAVAuthListenerImpl.hxx @@ -61,7 +61,6 @@ namespace webdav_ucp const ::rtl::OUString & inHostName, ::rtl::OUString & inoutUserName, ::rtl::OUString & outPassWord, - sal_Bool bAllowPersistentStoring, sal_Bool bCanUseSystemCredentials ); private: diff --git a/ucb/source/ucp/webdav/DAVResourceAccess.cxx b/ucb/source/ucp/webdav/DAVResourceAccess.cxx index 4ffc8dd88cf5..872d45405735 100644 --- a/ucb/source/ucp/webdav/DAVResourceAccess.cxx +++ b/ucb/source/ucp/webdav/DAVResourceAccess.cxx @@ -57,7 +57,6 @@ int DAVAuthListener_Impl::authenticate( const ::rtl::OUString & inHostName, ::rtl::OUString & inoutUserName, ::rtl::OUString & outPassWord, - sal_Bool bAllowPersistentStoring, sal_Bool bCanUseSystemCredentials ) { if ( m_xEnv.is() ) @@ -79,7 +78,8 @@ int DAVAuthListener_Impl::authenticate( = new ucbhelper::SimpleAuthenticationRequest( m_aURL, inHostName, inRealm, inoutUserName, outPassWord, ::rtl::OUString(), - bAllowPersistentStoring, bCanUseSystemCredentials ); + true /*bAllowPersistentStoring*/, + bCanUseSystemCredentials ); xIH->handle( xRequest.get() ); rtl::Reference< ucbhelper::InteractionContinuation > xSelection diff --git a/ucb/source/ucp/webdav/NeonSession.cxx b/ucb/source/ucp/webdav/NeonSession.cxx index 8fc1730ba018..254fae546940 100644 --- a/ucb/source/ucp/webdav/NeonSession.cxx +++ b/ucb/source/ucp/webdav/NeonSession.cxx @@ -60,7 +60,6 @@ #include #include #include -#include #include #include @@ -330,31 +329,11 @@ extern "C" int NeonSession_NeonAuth( void * inUserData, ( ne_strcasecmp( inAuthProtocol, "Negotiate" ) == 0 ) ); #endif - // #i97003# (tkr): Ask XMasterPasswordHandling if we should store the - // credentials persistently and give this information to the auth listener - uno::Reference< task::XMasterPasswordHandling > xMasterPasswordHandling; - try - { - xMasterPasswordHandling = - uno::Reference< task::XMasterPasswordHandling >( - theSession->getMSF()->createInstance( - rtl::OUString::createFromAscii( - "com.sun.star.task.PasswordContainer" ) ), - uno::UNO_QUERY ); - } - catch ( uno::Exception const & ) - { - } - int theRetVal = pListener->authenticate( rtl::OUString::createFromAscii( inRealm ), theSession->getHostName(), theUserName, thePassWord, - xMasterPasswordHandling.is() - ? xMasterPasswordHandling-> - isPersistentStoringAllowed() - : sal_False, bCanUseSystemCreds); rtl::OString aUser( diff --git a/uui/source/iahndl-authentication.cxx b/uui/source/iahndl-authentication.cxx index f31397f1ef92..4183b852735e 100644 --- a/uui/source/iahndl-authentication.cxx +++ b/uui/source/iahndl-authentication.cxx @@ -67,10 +67,9 @@ executeLoginDialog( { vos::OGuard aGuard(Application::GetSolarMutex()); - bool bAccount = (rInfo.GetFlags() & LOGINERROR_FLAG_MODIFY_ACCOUNT) - != 0; - bool bSavePassword = rInfo.GetIsPersistentPassword() - || rInfo.GetIsSavePassword(); + bool bAccount + = (rInfo.GetFlags() & LOGINERROR_FLAG_MODIFY_ACCOUNT) != 0; + bool bSavePassword = rInfo.GetCanRememberPassword(); bool bCanUseSysCreds = rInfo.GetCanUseSystemCredentials(); sal_uInt16 nFlags = 0; @@ -109,12 +108,13 @@ executeLoginDialog( if (bSavePassword) { - xDialog-> - SetSavePasswordText(ResId(rInfo.GetIsPersistentPassword() ? - RID_SAVE_PASSWORD : - RID_KEEP_PASSWORD, - *xManager.get())); - xDialog->SetSavePassword(rInfo.GetIsSavePassword()); + xDialog->SetSavePasswordText( + ResId(rInfo.GetIsRememberPersistent() + ? RID_SAVE_PASSWORD + : RID_KEEP_PASSWORD, + *xManager.get())); + + xDialog->SetSavePassword(rInfo.GetIsRememberPassword()); } if ( bCanUseSysCreds ) @@ -126,7 +126,7 @@ executeLoginDialog( rInfo.SetUserName(xDialog->GetName()); rInfo.SetPassword(xDialog->GetPassword()); rInfo.SetAccount(xDialog->GetAccount()); - rInfo.SetSavePassword(xDialog->IsSavePassword()); + rInfo.SetIsRememberPassword(xDialog->IsSavePassword()); if ( bCanUseSysCreds ) rInfo.SetIsUseSystemCredentials( xDialog->IsUseSystemCredentials() ); @@ -139,6 +139,60 @@ executeLoginDialog( } } +void getRememberModes( + uno::Sequence< ucb::RememberAuthentication > const & rRememberModes, + ucb::RememberAuthentication & rPreferredMode, + ucb::RememberAuthentication & rAlternateMode ) +{ + sal_Int32 nCount = rRememberModes.getLength(); + OSL_ENSURE( (nCount > 0) && (nCount < 4), + "ucb::RememberAuthentication sequence size mismatch!" ); + if ( nCount == 1 ) + { + rPreferredMode = rAlternateMode = rRememberModes[ 0 ]; + return; + } + else + { + //bool bHasRememberModeNo = false; + bool bHasRememberModeSession = false; + bool bHasRememberModePersistent = false; + + for (sal_Int32 i = 0; i < nCount; ++i) + { + switch ( rRememberModes[i] ) + { + case ucb::RememberAuthentication_NO: + //bHasRememberModeNo = true; + break; + case ucb::RememberAuthentication_SESSION: + bHasRememberModeSession = true; + break; + case ucb::RememberAuthentication_PERSISTENT: + bHasRememberModePersistent = true; + break; + default: + OSL_TRACE( "Unsupported RememberAuthentication value" ); + break; + } + } + + if (bHasRememberModePersistent) + { + rPreferredMode = ucb::RememberAuthentication_PERSISTENT; + if (bHasRememberModeSession) + rAlternateMode = ucb::RememberAuthentication_SESSION; + else + rAlternateMode = ucb::RememberAuthentication_NO; + } + else + { + rPreferredMode = ucb::RememberAuthentication_SESSION; + rAlternateMode = ucb::RememberAuthentication_NO; + } + } +} + void handleAuthenticationRequest_( Window * pParent, @@ -161,7 +215,7 @@ handleAuthenticationRequest_( xSupplyAuthentication2.set(xSupplyAuthentication, uno::UNO_QUERY); ////////////////////////// - // First, try to obatin credentials from password container service. + // First, try to obtain credentials from password container service. uui::PasswordContainerHelper aPwContainerHelper(xServiceFactory); if (aPwContainerHelper.handleAuthenticationRequest(rRequest, xSupplyAuthentication, @@ -174,26 +228,20 @@ handleAuthenticationRequest_( ////////////////////////// // Second, try to obtain credentials from user via password dialog. - bool bRemember; - bool bRememberPersistent; + ucb::RememberAuthentication eDefaultRememberMode + = ucb::RememberAuthentication_SESSION; + ucb::RememberAuthentication ePreferredRememberMode + = eDefaultRememberMode; + ucb::RememberAuthentication eAlternateRememberMode + = ucb::RememberAuthentication_NO; + if (xSupplyAuthentication.is()) { - ucb::RememberAuthentication eDefault; - uno::Sequence< ucb::RememberAuthentication > - aModes(xSupplyAuthentication->getRememberPasswordModes(eDefault)); - bRemember = eDefault != ucb::RememberAuthentication_NO; - bRememberPersistent = false; - for (sal_Int32 i = 0; i < aModes.getLength(); ++i) - if (aModes[i] == ucb::RememberAuthentication_PERSISTENT) - { - bRememberPersistent = true; - break; - } - } - else - { - bRemember = false; - bRememberPersistent = false; + getRememberModes( + xSupplyAuthentication->getRememberPasswordModes( + eDefaultRememberMode), + ePreferredRememberMode, + eAlternateRememberMode); } sal_Bool bCanUseSystemCredentials; @@ -220,8 +268,14 @@ handleAuthenticationRequest_( if (rRequest.HasPassword) aInfo.SetPassword(rRequest.Password); aInfo.SetErrorText(rRequest.Diagnostic); - aInfo.SetPersistentPassword(bRememberPersistent); - aInfo.SetSavePassword(bRemember); + + aInfo.SetCanRememberPassword( + ePreferredRememberMode != eAlternateRememberMode); + aInfo.SetIsRememberPassword( + eDefaultRememberMode != ucb::RememberAuthentication_NO); + aInfo.SetIsRememberPersistent( + ePreferredRememberMode == ucb::RememberAuthentication_PERSISTENT); + aInfo.SetCanUseSystemCredentials(bCanUseSystemCredentials); aInfo.SetIsUseSystemCredentials( bDefaultUseSystemCredentials ); aInfo.SetModifyAccount(rRequest.HasAccount @@ -242,13 +296,24 @@ handleAuthenticationRequest_( xSupplyAuthentication->setUserName(aInfo.GetUserName()); if (xSupplyAuthentication->canSetPassword()) xSupplyAuthentication->setPassword(aInfo.GetPassword()); - xSupplyAuthentication-> - setRememberPassword( - aInfo.GetIsSavePassword() ? - bRememberPersistent ? - ucb::RememberAuthentication_PERSISTENT : - ucb::RememberAuthentication_SESSION : - ucb::RememberAuthentication_NO); + + if (ePreferredRememberMode != eAlternateRememberMode) + { + // user had te choice. + if (aInfo.GetIsRememberPassword()) + xSupplyAuthentication->setRememberPassword( + ePreferredRememberMode); + else + xSupplyAuthentication->setRememberPassword( + eAlternateRememberMode); + } + else + { + // user had no choice. + xSupplyAuthentication->setRememberPassword( + ePreferredRememberMode); + } + if (rRequest.HasRealm) { if (xSupplyAuthentication->canSetRealm()) @@ -267,38 +332,76 @@ handleAuthenticationRequest_( ////////////////////////// // Third, store credentials in password container. - if ( aInfo.GetIsUseSystemCredentials() ) - { - if (aInfo.GetIsSavePassword()) - { - aPwContainerHelper.addRecord( - rURL.getLength() ? rURL : rRequest.ServerName, - rtl::OUString(), // empty u/p -> sys creds - uno::Sequence< rtl::OUString >(), - xIH, - bRememberPersistent); - } - } - // Empty user name can not be valid: - else if (aInfo.GetUserName().Len() != 0) - { - if (aInfo.GetIsSavePassword()) - { - uno::Sequence< rtl::OUString > - aPassList(aInfo.GetAccount().Len() == 0 ? 1 : 2); - aPassList[0] = aInfo.GetPassword(); - if (aInfo.GetAccount().Len() != 0) - aPassList[1] = aInfo.GetAccount(); - - aPwContainerHelper.addRecord( - rURL.getLength() ? rURL : rRequest.ServerName, - aInfo.GetUserName(), - aPassList, - xIH, - bRememberPersistent); - } - } - break; + if ( aInfo.GetIsUseSystemCredentials() ) + { + if (aInfo.GetIsRememberPassword()) + { + if (!aPwContainerHelper.addRecord( + rURL.getLength() ? rURL : rRequest.ServerName, + rtl::OUString(), // empty u/p -> sys creds + uno::Sequence< rtl::OUString >(), + xIH, + ePreferredRememberMode + == ucb::RememberAuthentication_PERSISTENT)) + { + xSupplyAuthentication->setRememberPassword( + ucb::RememberAuthentication_NO); + } + } + else if (eAlternateRememberMode + == ucb::RememberAuthentication_SESSION) + { + if (!aPwContainerHelper.addRecord( + rURL.getLength() ? rURL : rRequest.ServerName, + rtl::OUString(), // empty u/p -> sys creds + uno::Sequence< rtl::OUString >(), + xIH, + false /* SESSION */)) + { + xSupplyAuthentication->setRememberPassword( + ucb::RememberAuthentication_NO); + } + } + } + // Empty user name can not be valid: + else if (aInfo.GetUserName().Len() != 0) + { + uno::Sequence< rtl::OUString > + aPassList(aInfo.GetAccount().Len() == 0 ? 1 : 2); + aPassList[0] = aInfo.GetPassword(); + if (aInfo.GetAccount().Len() != 0) + aPassList[1] = aInfo.GetAccount(); + + if (aInfo.GetIsRememberPassword()) + { + if (!aPwContainerHelper.addRecord( + rURL.getLength() ? rURL : rRequest.ServerName, + aInfo.GetUserName(), + aPassList, + xIH, + ePreferredRememberMode + == ucb::RememberAuthentication_PERSISTENT)) + { + xSupplyAuthentication->setRememberPassword( + ucb::RememberAuthentication_NO); + } + } + else if (eAlternateRememberMode + == ucb::RememberAuthentication_SESSION) + { + if (!aPwContainerHelper.addRecord( + rURL.getLength() ? rURL : rRequest.ServerName, + aInfo.GetUserName(), + aPassList, + xIH, + false /* SESSION */)) + { + xSupplyAuthentication->setRememberPassword( + ucb::RememberAuthentication_NO); + } + } + } + break; case ERRCODE_BUTTON_RETRY: if (xRetry.is()) diff --git a/uui/source/loginerr.hxx b/uui/source/loginerr.hxx index c8cbba96564b..39e5b81caf85 100644 --- a/uui/source/loginerr.hxx +++ b/uui/source/loginerr.hxx @@ -32,12 +32,13 @@ //========================================================================= -#define LOGINERROR_FLAG_SET_SAVE_PASSWORD 1 -#define LOGINERROR_FLAG_MODIFY_ACCOUNT 2 -#define LOGINERROR_FLAG_MODIFY_USER_NAME 4 -#define LOGINERROR_FLAG_PERSISTENT_PASSWORD 8 -#define LOGINERROR_FLAG_CAN_USE_SYSCREDS 16 -#define LOGINERROR_FLAG_IS_USE_SYSCREDS 32 +#define LOGINERROR_FLAG_MODIFY_ACCOUNT 1 +#define LOGINERROR_FLAG_MODIFY_USER_NAME 2 +#define LOGINERROR_FLAG_CAN_REMEMBER_PASSWORD 4 +#define LOGINERROR_FLAG_IS_REMEMBER_PASSWORD 8 +#define LOGINERROR_FLAG_CAN_USE_SYSCREDS 16 +#define LOGINERROR_FLAG_IS_USE_SYSCREDS 32 +#define LOGINERROR_FLAG_REMEMBER_PERSISTENT 64 class LoginErrorInfo { @@ -56,27 +57,30 @@ public: LoginErrorInfo() : _nFlags( LOGINERROR_FLAG_MODIFY_USER_NAME ), _nRet( ERRCODE_BUTTON_CANCEL ) - { - } + {} const String& GetTitle() const { return _aTitle; } - const String& GetServer() const { return _aServer; } + const String& GetServer() const { return _aServer; } const String& GetAccount() const { return _aAccount; } const String& GetUserName() const { return _aUserName; } const String& GetPassword() const { return _aPassword; } const String& GetPath() const { return _aPath; } const String& GetErrorText() const { return _aErrorText; } - BOOL GetIsPersistentPassword() const - { return ( _nFlags & LOGINERROR_FLAG_PERSISTENT_PASSWORD ); } - BOOL GetIsSavePassword() const - { return ( _nFlags & LOGINERROR_FLAG_SET_SAVE_PASSWORD ); } + + BOOL GetCanRememberPassword() const + { return ( _nFlags & LOGINERROR_FLAG_CAN_REMEMBER_PASSWORD ); } + BOOL GetIsRememberPersistent() const + { return ( _nFlags & LOGINERROR_FLAG_REMEMBER_PERSISTENT ); } + BOOL GetIsRememberPassword() const + { return ( _nFlags & LOGINERROR_FLAG_IS_REMEMBER_PASSWORD ); } + BOOL GetCanUseSystemCredentials() const { return ( _nFlags & LOGINERROR_FLAG_CAN_USE_SYSCREDS ); } BOOL GetIsUseSystemCredentials() const { return ( _nFlags & LOGINERROR_FLAG_IS_USE_SYSCREDS ) == LOGINERROR_FLAG_IS_USE_SYSCREDS; } - BYTE GetFlags() const { return _nFlags; } - USHORT GetResult() const { return _nRet; } + BYTE GetFlags() const { return _nFlags; } + USHORT GetResult() const { return _nRet; } void SetTitle( const String& aTitle ) { _aTitle = aTitle; } @@ -94,8 +98,11 @@ public: { _aErrorText = aErrorText; } void SetFlags( BYTE nFlags ) { _nFlags = nFlags; } - inline void SetSavePassword( BOOL bSet ); - inline void SetPersistentPassword( BOOL bSet ); + + inline void SetCanRememberPassword( BOOL bSet ); + inline void SetIsRememberPassword( BOOL bSet ); + inline void SetIsRememberPersistent( BOOL bSet ); + inline void SetCanUseSystemCredentials( BOOL bSet ); inline void SetIsUseSystemCredentials( BOOL bSet ); inline void SetModifyAccount( BOOL bSet ); @@ -104,20 +111,28 @@ public: { _nRet = nRet; } }; -inline void LoginErrorInfo::SetSavePassword( BOOL bSet ) +inline void LoginErrorInfo::SetCanRememberPassword( BOOL bSet ) +{ + if ( bSet ) + _nFlags |= LOGINERROR_FLAG_CAN_REMEMBER_PASSWORD; + else + _nFlags &= ~LOGINERROR_FLAG_CAN_REMEMBER_PASSWORD; +} + +inline void LoginErrorInfo::SetIsRememberPassword( BOOL bSet ) { if ( bSet ) - _nFlags |= LOGINERROR_FLAG_SET_SAVE_PASSWORD; + _nFlags |= LOGINERROR_FLAG_IS_REMEMBER_PASSWORD; else - _nFlags &= ~LOGINERROR_FLAG_SET_SAVE_PASSWORD; + _nFlags &= ~LOGINERROR_FLAG_IS_REMEMBER_PASSWORD; } -inline void LoginErrorInfo::SetPersistentPassword( BOOL bSet ) +inline void LoginErrorInfo::SetIsRememberPersistent( BOOL bSet ) { if ( bSet ) - _nFlags |= LOGINERROR_FLAG_PERSISTENT_PASSWORD; + _nFlags |= LOGINERROR_FLAG_REMEMBER_PERSISTENT; else - _nFlags &= ~LOGINERROR_FLAG_PERSISTENT_PASSWORD; + _nFlags &= ~LOGINERROR_FLAG_REMEMBER_PERSISTENT; } inline void LoginErrorInfo::SetCanUseSystemCredentials( BOOL bSet ) diff --git a/uui/source/passwordcontainer.cxx b/uui/source/passwordcontainer.cxx index 26d22b320d8a..0a056289c29d 100644 --- a/uui/source/passwordcontainer.cxx +++ b/uui/source/passwordcontainer.cxx @@ -30,6 +30,7 @@ #include "com/sun/star/lang/XMultiServiceFactory.hpp" #include "com/sun/star/task/NoMasterException.hpp" #include "com/sun/star/task/XInteractionHandler.hpp" +#include "com/sun/star/task/XMasterPasswordHandling.hpp" #include "com/sun/star/task/XPasswordContainer.hpp" #include "com/sun/star/task/XUrlContainer.hpp" #include "com/sun/star/ucb/AuthenticationRequest.hpp" @@ -271,10 +272,20 @@ bool PasswordContainerHelper::addRecord( return false; if ( bPersist ) + { + uno::Reference< task::XMasterPasswordHandling > xMPH( + m_xPasswordContainer, uno::UNO_QUERY_THROW ); + + // If persistent storing of passwords is not yet + // allowed, enable it. + if ( !xMPH->isPersistentStoringAllowed() ) + xMPH->allowPersistentStoring( sal_True ); + m_xPasswordContainer->addPersistent( rURL, rUsername, rPasswords, xIH ); + } else m_xPasswordContainer->add( rURL, rUsername, @@ -429,7 +440,7 @@ PasswordContainerInteractionHandler::handle( // @@@ FIXME: this not able to // handle master pw request! // master pw request is never - // solvabe without UI! + // solvable without UI! this ) ) { // successfully handled -- cgit