xref: /trunk/main/uui/source/iahndl-authentication.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include "com/sun/star/task/DocumentPasswordRequest.hpp"
25 #include "com/sun/star/task/DocumentPasswordRequest2.hpp"
26 #include "com/sun/star/task/DocumentMSPasswordRequest.hpp"
27 #include "com/sun/star/task/DocumentMSPasswordRequest2.hpp"
28 #include "com/sun/star/task/MasterPasswordRequest.hpp"
29 #include "com/sun/star/task/XInteractionAbort.hpp"
30 #include "com/sun/star/task/XInteractionPassword.hpp"
31 #include "com/sun/star/task/XInteractionPassword2.hpp"
32 #include "com/sun/star/task/XInteractionRetry.hpp"
33 #include "com/sun/star/ucb/XInteractionSupplyAuthentication2.hpp"
34 #include "com/sun/star/ucb/URLAuthenticationRequest.hpp"
35 
36 #include "osl/diagnose.h"
37 #include "rtl/digest.h"
38 #include "vos/mutex.hxx"
39 #include "tools/errcode.hxx"
40 #include "vcl/msgbox.hxx"
41 #include "vcl/abstdlg.hxx"
42 #include "vcl/svapp.hxx"
43 
44 #include "ids.hrc"
45 #include "getcontinuations.hxx"
46 #include "passwordcontainer.hxx"
47 #include "loginerr.hxx"
48 #include "logindlg.hxx"
49 #include "masterpasscrtdlg.hxx"
50 #include "masterpassworddlg.hxx"
51 #include "passworddlg.hxx"
52 
53 #include "iahndl.hxx"
54 
55 using namespace com::sun::star;
56 
57 namespace {
58 
59 void
executeLoginDialog(Window * pParent,LoginErrorInfo & rInfo,rtl::OUString const & rRealm)60 executeLoginDialog(
61     Window * pParent,
62     LoginErrorInfo & rInfo,
63     rtl::OUString const & rRealm)
64 {
65     try
66     {
67         vos::OGuard aGuard(Application::GetSolarMutex());
68 
69         bool bAccount = (rInfo.GetFlags() & LOGINERROR_FLAG_MODIFY_ACCOUNT) != 0;
70         bool bSavePassword   = rInfo.GetCanRememberPassword();
71         bool bCanUseSysCreds = rInfo.GetCanUseSystemCredentials();
72 
73         sal_uInt16 nFlags = 0;
74         if (rInfo.GetPath().Len() == 0)
75             nFlags |= LF_NO_PATH;
76         if (rInfo.GetErrorText().Len() == 0)
77             nFlags |= LF_NO_ERRORTEXT;
78         if (!bAccount)
79             nFlags |= LF_NO_ACCOUNT;
80         if (!(rInfo.GetFlags() & LOGINERROR_FLAG_MODIFY_USER_NAME))
81             nFlags |= LF_USERNAME_READONLY;
82 
83         if (!bSavePassword)
84             nFlags |= LF_NO_SAVEPASSWORD;
85 
86         if (!bCanUseSysCreds)
87             nFlags |= LF_NO_USESYSCREDS;
88 
89         std::auto_ptr< ResMgr > xManager( ResMgr::CreateResMgr(CREATEVERSIONRESMGR_NAME(uui)));
90         UniString aRealm(rRealm);
91         std::auto_ptr< LoginDialog > xDialog(
92                 new LoginDialog( pParent, nFlags, rInfo.GetServer(), &aRealm, xManager.get()));
93         if (rInfo.GetErrorText().Len() != 0)
94             xDialog->SetErrorText(rInfo.GetErrorText());
95         xDialog->SetName(rInfo.GetUserName());
96         if (bAccount)
97             xDialog->ClearAccount();
98         else
99             xDialog->ClearPassword();
100         xDialog->SetPassword(rInfo.GetPassword());
101 
102         if (bSavePassword)
103         {
104             xDialog->SetSavePasswordText(
105                 ResId(rInfo.GetIsRememberPersistent()
106                           ? RID_SAVE_PASSWORD
107                           : RID_KEEP_PASSWORD,
108                       *xManager.get()));
109 
110             xDialog->SetSavePassword(rInfo.GetIsRememberPassword());
111         }
112 
113         if ( bCanUseSysCreds )
114             xDialog->SetUseSystemCredentials( rInfo.GetIsUseSystemCredentials() );
115 
116         rInfo.SetResult(xDialog->Execute() == RET_OK ? ERRCODE_BUTTON_OK :
117                                                        ERRCODE_BUTTON_CANCEL);
118         rInfo.SetUserName(xDialog->GetName());
119         rInfo.SetPassword(xDialog->GetPassword());
120         rInfo.SetAccount(xDialog->GetAccount());
121         rInfo.SetIsRememberPassword(xDialog->IsSavePassword());
122 
123         if ( bCanUseSysCreds )
124           rInfo.SetIsUseSystemCredentials( xDialog->IsUseSystemCredentials() );
125     }
126     catch (std::bad_alloc const &)
127     {
128         throw uno::RuntimeException(
129                   rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("out of memory")),
130                   uno::Reference< uno::XInterface >());
131     }
132 }
133 
getRememberModes(uno::Sequence<ucb::RememberAuthentication> const & rRememberModes,ucb::RememberAuthentication & rPreferredMode,ucb::RememberAuthentication & rAlternateMode)134 void getRememberModes(
135     uno::Sequence< ucb::RememberAuthentication > const & rRememberModes,
136     ucb::RememberAuthentication & rPreferredMode,
137     ucb::RememberAuthentication & rAlternateMode )
138 {
139     sal_Int32 nCount = rRememberModes.getLength();
140     OSL_ENSURE( (nCount > 0) && (nCount < 4),
141                 "ucb::RememberAuthentication sequence size mismatch!" );
142     if ( nCount == 1 )
143     {
144         rPreferredMode = rAlternateMode = rRememberModes[ 0 ];
145         return;
146     }
147     else
148     {
149         //bool bHasRememberModeNo = false;
150         bool bHasRememberModeSession = false;
151         bool bHasRememberModePersistent = false;
152 
153         for (sal_Int32 i = 0; i < nCount; ++i)
154         {
155             switch ( rRememberModes[i] )
156             {
157             case ucb::RememberAuthentication_NO:
158                 //bHasRememberModeNo = true;
159                 break;
160             case ucb::RememberAuthentication_SESSION:
161                 bHasRememberModeSession = true;
162                 break;
163             case ucb::RememberAuthentication_PERSISTENT:
164                 bHasRememberModePersistent = true;
165                 break;
166             default:
167                 OSL_TRACE( "Unsupported RememberAuthentication value" );
168                 break;
169             }
170         }
171 
172         if (bHasRememberModePersistent)
173         {
174             rPreferredMode = ucb::RememberAuthentication_PERSISTENT;
175             if (bHasRememberModeSession)
176                 rAlternateMode = ucb::RememberAuthentication_SESSION;
177             else
178                 rAlternateMode = ucb::RememberAuthentication_NO;
179         }
180         else
181         {
182             rPreferredMode = ucb::RememberAuthentication_SESSION;
183             rAlternateMode = ucb::RememberAuthentication_NO;
184         }
185     }
186 }
187 
188 void
handleAuthenticationRequest_(Window * pParent,uno::Reference<task::XInteractionHandler> const & xIH,uno::Reference<lang::XMultiServiceFactory> const & xServiceFactory,ucb::AuthenticationRequest const & rRequest,uno::Sequence<uno::Reference<task::XInteractionContinuation>> const & rContinuations,const rtl::OUString & rURL)189 handleAuthenticationRequest_(
190     Window * pParent,
191     uno::Reference< task::XInteractionHandler > const & xIH,
192     uno::Reference< lang::XMultiServiceFactory > const & xServiceFactory,
193     ucb::AuthenticationRequest const & rRequest,
194     uno::Sequence< uno::Reference< task::XInteractionContinuation > > const &
195         rContinuations,
196     const rtl::OUString & rURL)
197 {
198     uno::Reference< task::XInteractionRetry > xRetry;
199     uno::Reference< task::XInteractionAbort > xAbort;
200     uno::Reference< ucb::XInteractionSupplyAuthentication >
201         xSupplyAuthentication;
202     uno::Reference< ucb::XInteractionSupplyAuthentication2 >
203         xSupplyAuthentication2;
204     getContinuations(rContinuations, &xRetry, &xAbort, &xSupplyAuthentication);
205     if (xSupplyAuthentication.is())
206         xSupplyAuthentication2.set(xSupplyAuthentication, uno::UNO_QUERY);
207 
208     //////////////////////////
209     // First, try to obtain credentials from password container service.
210     uui::PasswordContainerHelper aPwContainerHelper(xServiceFactory);
211     if (aPwContainerHelper.handleAuthenticationRequest(rRequest,
212                                                        xSupplyAuthentication,
213                                                        rURL,
214                                                        xIH))
215     {
216         xSupplyAuthentication->select();
217         return;
218     }
219 
220     //////////////////////////
221     // Second, try to obtain credentials from user via password dialog.
222     ucb::RememberAuthentication eDefaultRememberMode
223         = ucb::RememberAuthentication_SESSION;
224     ucb::RememberAuthentication ePreferredRememberMode
225         = eDefaultRememberMode;
226     ucb::RememberAuthentication eAlternateRememberMode
227         = ucb::RememberAuthentication_NO;
228 
229     if (xSupplyAuthentication.is())
230     {
231         getRememberModes(
232             xSupplyAuthentication->getRememberPasswordModes(
233                 eDefaultRememberMode),
234             ePreferredRememberMode,
235             eAlternateRememberMode);
236     }
237 
238     sal_Bool bCanUseSystemCredentials;
239     sal_Bool bDefaultUseSystemCredentials;
240     if (xSupplyAuthentication2.is())
241     {
242         bCanUseSystemCredentials
243             = xSupplyAuthentication2->canUseSystemCredentials(
244                 bDefaultUseSystemCredentials);
245     }
246     else
247     {
248         bCanUseSystemCredentials = sal_False;
249         bDefaultUseSystemCredentials = sal_False;
250     }
251 
252     LoginErrorInfo aInfo;
253     aInfo.SetTitle(rRequest.ServerName);
254     aInfo.SetServer(rRequest.ServerName);
255     if (rRequest.HasAccount)
256         aInfo.SetAccount(rRequest.Account);
257     if (rRequest.HasUserName)
258         aInfo.SetUserName(rRequest.UserName);
259     if (rRequest.HasPassword)
260         aInfo.SetPassword(rRequest.Password);
261     aInfo.SetErrorText(rRequest.Diagnostic);
262 
263     aInfo.SetCanRememberPassword(
264         ePreferredRememberMode != eAlternateRememberMode);
265     aInfo.SetIsRememberPassword(
266         ePreferredRememberMode == eDefaultRememberMode);
267     aInfo.SetIsRememberPersistent(
268         ePreferredRememberMode == ucb::RememberAuthentication_PERSISTENT);
269 
270     aInfo.SetCanUseSystemCredentials(bCanUseSystemCredentials);
271     aInfo.SetIsUseSystemCredentials( bDefaultUseSystemCredentials );
272     aInfo.SetModifyAccount(rRequest.HasAccount
273                            && xSupplyAuthentication.is()
274                            && xSupplyAuthentication->canSetAccount());
275     aInfo.SetModifyUserName(rRequest.HasUserName
276                             && xSupplyAuthentication.is()
277                             && xSupplyAuthentication->canSetUserName());
278     executeLoginDialog(pParent,
279                        aInfo,
280                        rRequest.HasRealm ? rRequest.Realm : rtl::OUString());
281     switch (aInfo.GetResult())
282     {
283     case ERRCODE_BUTTON_OK:
284         if (xSupplyAuthentication.is())
285         {
286             if (xSupplyAuthentication->canSetUserName())
287                 xSupplyAuthentication->setUserName(aInfo.GetUserName());
288             if (xSupplyAuthentication->canSetPassword())
289                 xSupplyAuthentication->setPassword(aInfo.GetPassword());
290 
291             if (ePreferredRememberMode != eAlternateRememberMode)
292             {
293                 // user had te choice.
294                 if (aInfo.GetIsRememberPassword())
295                     xSupplyAuthentication->setRememberPassword(
296                         ePreferredRememberMode);
297                 else
298                     xSupplyAuthentication->setRememberPassword(
299                         eAlternateRememberMode);
300             }
301             else
302             {
303                 // user had no choice.
304                 xSupplyAuthentication->setRememberPassword(
305                     ePreferredRememberMode);
306             }
307 
308             if (rRequest.HasRealm)
309             {
310                 if (xSupplyAuthentication->canSetRealm())
311                     xSupplyAuthentication->setRealm(aInfo.GetAccount());
312             }
313             else if (xSupplyAuthentication->canSetAccount())
314                 xSupplyAuthentication->setAccount(aInfo.GetAccount());
315 
316             if ( xSupplyAuthentication2.is() && bCanUseSystemCredentials )
317                 xSupplyAuthentication2->setUseSystemCredentials(
318                     aInfo.GetIsUseSystemCredentials() );
319 
320             xSupplyAuthentication->select();
321         }
322 
323         //////////////////////////
324         // Third, store credentials in password container.
325 
326           if ( aInfo.GetIsUseSystemCredentials() )
327           {
328               if (aInfo.GetIsRememberPassword())
329               {
330                   if (!aPwContainerHelper.addRecord(
331                           rURL.getLength() ? rURL : rRequest.ServerName,
332                           rtl::OUString(), // empty u/p -> sys creds
333                           uno::Sequence< rtl::OUString >(),
334                           xIH,
335                           ePreferredRememberMode
336                               == ucb::RememberAuthentication_PERSISTENT))
337                   {
338                       xSupplyAuthentication->setRememberPassword(
339                           ucb::RememberAuthentication_NO);
340                   }
341               }
342               else if (eAlternateRememberMode
343                            == ucb::RememberAuthentication_SESSION)
344               {
345                   if (!aPwContainerHelper.addRecord(
346                           rURL.getLength() ? rURL : rRequest.ServerName,
347                           rtl::OUString(), // empty u/p -> sys creds
348                           uno::Sequence< rtl::OUString >(),
349                           xIH,
350                           false /* SESSION */))
351                   {
352                       xSupplyAuthentication->setRememberPassword(
353                           ucb::RememberAuthentication_NO);
354                   }
355               }
356           }
357           // Empty user name can not be valid:
358           else if (aInfo.GetUserName().Len() != 0)
359           {
360               uno::Sequence< rtl::OUString >
361                   aPassList(aInfo.GetAccount().Len() == 0 ? 1 : 2);
362               aPassList[0] = aInfo.GetPassword();
363               if (aInfo.GetAccount().Len() != 0)
364                   aPassList[1] = aInfo.GetAccount();
365 
366               if (aInfo.GetIsRememberPassword())
367               {
368                   if (!aPwContainerHelper.addRecord(
369                           rURL.getLength() ? rURL : rRequest.ServerName,
370                           aInfo.GetUserName(),
371                           aPassList,
372                           xIH,
373                           ePreferredRememberMode
374                               == ucb::RememberAuthentication_PERSISTENT))
375                   {
376                       xSupplyAuthentication->setRememberPassword(
377                           ucb::RememberAuthentication_NO);
378                   }
379               }
380               else if (eAlternateRememberMode
381                            == ucb::RememberAuthentication_SESSION)
382               {
383                   if (!aPwContainerHelper.addRecord(
384                           rURL.getLength() ? rURL : rRequest.ServerName,
385                           aInfo.GetUserName(),
386                           aPassList,
387                           xIH,
388                           false /* SESSION */))
389                   {
390                       xSupplyAuthentication->setRememberPassword(
391                           ucb::RememberAuthentication_NO);
392                   }
393               }
394           }
395           break;
396 
397     case ERRCODE_BUTTON_RETRY:
398         if (xRetry.is())
399             xRetry->select();
400         break;
401 
402     default:
403         if (xAbort.is())
404             xAbort->select();
405         break;
406     }
407 }
408 
409 void
executeMasterPasswordDialog(Window * pParent,LoginErrorInfo & rInfo,task::PasswordRequestMode nMode)410 executeMasterPasswordDialog(
411     Window * pParent,
412     LoginErrorInfo & rInfo,
413     task::PasswordRequestMode nMode)
414 {
415     rtl::OString aMaster;
416     try
417     {
418         vos::OGuard aGuard(Application::GetSolarMutex());
419 
420         std::auto_ptr< ResMgr > xManager(
421             ResMgr::CreateResMgr(CREATEVERSIONRESMGR_NAME(uui)));
422         if( nMode == task::PasswordRequestMode_PASSWORD_CREATE )
423         {
424             std::auto_ptr< MasterPasswordCreateDialog > xDialog(
425                 new MasterPasswordCreateDialog(pParent, xManager.get()));
426             rInfo.SetResult(xDialog->Execute()
427                 == RET_OK ? ERRCODE_BUTTON_OK : ERRCODE_BUTTON_CANCEL);
428             aMaster = rtl::OUStringToOString(
429                 xDialog->GetMasterPassword(), RTL_TEXTENCODING_UTF8);
430         }
431         else
432         {
433             std::auto_ptr< MasterPasswordDialog > xDialog(
434                 new MasterPasswordDialog(pParent, nMode, xManager.get()));
435             rInfo.SetResult(xDialog->Execute()
436                 == RET_OK ? ERRCODE_BUTTON_OK : ERRCODE_BUTTON_CANCEL);
437             aMaster = rtl::OUStringToOString(
438                 xDialog->GetMasterPassword(), RTL_TEXTENCODING_UTF8);
439         }
440     }
441     catch (std::bad_alloc const &)
442     {
443         throw uno::RuntimeException(
444                   rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("out of memory")),
445                   uno::Reference< uno::XInterface >());
446     }
447 
448     sal_uInt8 aKey[RTL_DIGEST_LENGTH_MD5];
449     rtl_digest_PBKDF2(aKey,
450                       RTL_DIGEST_LENGTH_MD5,
451                       reinterpret_cast< sal_uInt8 const * >(aMaster.getStr()),
452                       aMaster.getLength(),
453                       reinterpret_cast< sal_uInt8 const * >(
454                           "3B5509ABA6BC42D9A3A1F3DAD49E56A51"),
455                       32,
456                       1000);
457 
458     rtl::OUStringBuffer aBuffer;
459     for (int i = 0; i < RTL_DIGEST_LENGTH_MD5; ++i)
460     {
461         aBuffer.append(static_cast< sal_Unicode >('a' + (aKey[i] >> 4)));
462         aBuffer.append(static_cast< sal_Unicode >('a' + (aKey[i] & 15)));
463     }
464     rInfo.SetPassword(aBuffer.makeStringAndClear());
465 }
466 
467 void
handleMasterPasswordRequest_(Window * pParent,task::PasswordRequestMode nMode,uno::Sequence<uno::Reference<task::XInteractionContinuation>> const & rContinuations)468 handleMasterPasswordRequest_(
469     Window * pParent,
470     task::PasswordRequestMode nMode,
471     uno::Sequence< uno::Reference< task::XInteractionContinuation > > const &
472         rContinuations)
473 {
474     uno::Reference< task::XInteractionRetry > xRetry;
475     uno::Reference< task::XInteractionAbort > xAbort;
476     uno::Reference< ucb::XInteractionSupplyAuthentication >
477         xSupplyAuthentication;
478     getContinuations(rContinuations, &xRetry, &xAbort, &xSupplyAuthentication);
479     LoginErrorInfo aInfo;
480 
481     // in case of master password a hash code is returned
482     executeMasterPasswordDialog(pParent, aInfo, nMode);
483 
484     switch (aInfo.GetResult())
485     {
486     case ERRCODE_BUTTON_OK:
487         if (xSupplyAuthentication.is())
488         {
489             if (xSupplyAuthentication->canSetPassword())
490                 xSupplyAuthentication->setPassword(aInfo.GetPassword());
491             xSupplyAuthentication->select();
492         }
493         break;
494 
495     case ERRCODE_BUTTON_RETRY:
496         if (xRetry.is())
497             xRetry->select();
498         break;
499 
500     default:
501         if (xAbort.is())
502             xAbort->select();
503         break;
504     }
505 }
506 
507 void
executePasswordDialog(Window * pParent,LoginErrorInfo & rInfo,task::PasswordRequestMode nMode,::rtl::OUString aDocName,bool bMSCryptoMode,bool bIsPasswordToModify,bool bIsSimplePasswordRequest)508 executePasswordDialog(
509     Window * pParent,
510     LoginErrorInfo & rInfo,
511     task::PasswordRequestMode nMode,
512     ::rtl::OUString aDocName,
513     bool bMSCryptoMode,
514     bool bIsPasswordToModify,
515     bool bIsSimplePasswordRequest )
516 {
517     try
518     {
519         vos::OGuard aGuard(Application::GetSolarMutex());
520 
521         std::auto_ptr< ResMgr > xManager(
522             ResMgr::CreateResMgr(CREATEVERSIONRESMGR_NAME(uui)));
523         if( nMode == task::PasswordRequestMode_PASSWORD_CREATE )
524         {
525             if (bIsSimplePasswordRequest)
526             {
527                 std::auto_ptr< PasswordDialog > pDialog(
528                     new PasswordDialog( pParent, nMode, xManager.get(), aDocName,
529                     bIsPasswordToModify, bIsSimplePasswordRequest ) );
530                 pDialog->SetMinLen(0);
531 
532                 rInfo.SetResult( pDialog->Execute() == RET_OK ? ERRCODE_BUTTON_OK : ERRCODE_BUTTON_CANCEL );
533                 rInfo.SetPassword( pDialog->GetPassword() );
534             }
535             else
536             {
537                 const sal_uInt16 nMaxPasswdLen = bMSCryptoMode ? 15 : 0;   // 0 -> allow any length
538 
539                 VclAbstractDialogFactory * pFact = VclAbstractDialogFactory::Create();
540                 AbstractPasswordToOpenModifyDialog *pTmp = pFact->CreatePasswordToOpenModifyDialog( pParent, 0, nMaxPasswdLen, bIsPasswordToModify );
541                 std::auto_ptr< AbstractPasswordToOpenModifyDialog > pDialog( pTmp );
542 
543                 rInfo.SetResult( pDialog->Execute() == RET_OK ? ERRCODE_BUTTON_OK : ERRCODE_BUTTON_CANCEL );
544                 rInfo.SetPassword( pDialog->GetPasswordToOpen() );
545                 rInfo.SetPasswordToModify( pDialog->GetPasswordToModify() );
546                 rInfo.SetRecommendToOpenReadonly( pDialog->IsRecommendToOpenReadonly() );
547             }
548         }
549         else // enter password or reenter password
550         {
551             std::auto_ptr< PasswordDialog > pDialog(
552                 new PasswordDialog( pParent, nMode, xManager.get(), aDocName,
553                 bIsPasswordToModify, bIsSimplePasswordRequest ) );
554             pDialog->SetMinLen(0);
555 
556             rInfo.SetResult( pDialog->Execute() == RET_OK ? ERRCODE_BUTTON_OK : ERRCODE_BUTTON_CANCEL );
557             rInfo.SetPassword( bIsPasswordToModify ? String() : pDialog->GetPassword() );
558             rInfo.SetPasswordToModify( bIsPasswordToModify ? pDialog->GetPassword() : String() );
559         }
560     }
561     catch (std::bad_alloc const &)
562     {
563         throw uno::RuntimeException(
564             rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("out of memory")),
565             uno::Reference< uno::XInterface>());
566     }
567 }
568 
569 void
handlePasswordRequest_(Window * pParent,task::PasswordRequestMode nMode,uno::Sequence<uno::Reference<task::XInteractionContinuation>> const & rContinuations,::rtl::OUString aDocumentName,bool bMSCryptoMode,bool bIsPasswordToModify,bool bIsSimplePasswordRequest=false)570 handlePasswordRequest_(
571     Window * pParent,
572     task::PasswordRequestMode nMode,
573     uno::Sequence< uno::Reference< task::XInteractionContinuation > > const &
574         rContinuations,
575     ::rtl::OUString aDocumentName,
576     bool bMSCryptoMode,
577     bool bIsPasswordToModify,
578     bool bIsSimplePasswordRequest = false )
579 {
580     uno::Reference< task::XInteractionRetry > xRetry;
581     uno::Reference< task::XInteractionAbort > xAbort;
582     uno::Reference< task::XInteractionPassword > xPassword;
583     uno::Reference< task::XInteractionPassword2 > xPassword2;
584     getContinuations(rContinuations, &xRetry, &xAbort, &xPassword2, &xPassword);
585 
586     if ( xPassword2.is() && !xPassword.is() )
587         xPassword.set( xPassword2, uno::UNO_QUERY_THROW );
588 
589     LoginErrorInfo aInfo;
590 
591     executePasswordDialog( pParent, aInfo, nMode,
592             aDocumentName, bMSCryptoMode, bIsPasswordToModify, bIsSimplePasswordRequest );
593 
594     switch (aInfo.GetResult())
595     {
596     case ERRCODE_BUTTON_OK:
597         OSL_ENSURE( !bIsPasswordToModify || xPassword2.is(), "PasswordToModify is requested, but there is no Interaction!" );
598         if (xPassword.is())
599         {
600             if (xPassword2.is())
601             {
602                 xPassword2->setPasswordToModify( aInfo.GetPasswordToModify() );
603                 xPassword2->setRecommendReadOnly( aInfo.IsRecommendToOpenReadonly() );
604             }
605 
606             xPassword->setPassword(aInfo.GetPassword());
607             xPassword->select();
608         }
609         break;
610 
611     case ERRCODE_BUTTON_RETRY:
612         if (xRetry.is())
613             xRetry->select();
614         break;
615 
616     default:
617         if (xAbort.is())
618             xAbort->select();
619         break;
620     }
621 }
622 
623 } // namespace
624 
625 bool
handleAuthenticationRequest(uno::Reference<task::XInteractionRequest> const & rRequest)626 UUIInteractionHelper::handleAuthenticationRequest(
627     uno::Reference< task::XInteractionRequest > const & rRequest)
628 {
629     uno::Any aAnyRequest(rRequest->getRequest());
630 
631     ucb::URLAuthenticationRequest aURLAuthenticationRequest;
632     if (aAnyRequest >>= aURLAuthenticationRequest)
633     {
634         handleAuthenticationRequest_(getParentProperty(),
635                                      getInteractionHandler(),
636                                      m_xServiceFactory,
637                                      aURLAuthenticationRequest,
638                                      rRequest->getContinuations(),
639                                      aURLAuthenticationRequest.URL);
640         return true;
641     }
642 
643     ucb::AuthenticationRequest aAuthenticationRequest;
644     if (aAnyRequest >>= aAuthenticationRequest)
645     {
646         handleAuthenticationRequest_(getParentProperty(),
647                                      getInteractionHandler(),
648                                      m_xServiceFactory,
649                                      aAuthenticationRequest,
650                                      rRequest->getContinuations(),
651                                      rtl::OUString());
652         return true;
653     }
654     return false;
655 }
656 
657 bool
handleMasterPasswordRequest(uno::Reference<task::XInteractionRequest> const & rRequest)658 UUIInteractionHelper::handleMasterPasswordRequest(
659     uno::Reference< task::XInteractionRequest > const & rRequest)
660 {
661     uno::Any aAnyRequest(rRequest->getRequest());
662 
663     task::MasterPasswordRequest aMasterPasswordRequest;
664     if (aAnyRequest >>= aMasterPasswordRequest)
665     {
666         handleMasterPasswordRequest_(getParentProperty(),
667                                      aMasterPasswordRequest.Mode,
668                                      rRequest->getContinuations());
669         return true;
670     }
671     return false;
672 }
673 
674 bool
handlePasswordRequest(uno::Reference<task::XInteractionRequest> const & rRequest)675 UUIInteractionHelper::handlePasswordRequest(
676     uno::Reference< task::XInteractionRequest > const & rRequest)
677 {
678     // parameters to be filled for the call to handlePasswordRequest_
679     Window * pParent = getParentProperty();
680     task::PasswordRequestMode nMode = task::PasswordRequestMode_PASSWORD_ENTER;
681     uno::Sequence< uno::Reference< task::XInteractionContinuation > > const & rContinuations = rRequest->getContinuations();
682     ::rtl::OUString aDocumentName;
683     bool bMSCryptoMode          = false;
684     bool bIsPasswordToModify    = false;
685 
686     bool bDoHandleRequest = false;
687 
688     uno::Any aAnyRequest(rRequest->getRequest());
689 
690     task::DocumentPasswordRequest2 aDocumentPasswordRequest2;
691     if (!bDoHandleRequest && (aAnyRequest >>= aDocumentPasswordRequest2))
692     {
693         nMode               = aDocumentPasswordRequest2.Mode;
694         aDocumentName       = aDocumentPasswordRequest2.Name;
695         OSL_ENSURE( bMSCryptoMode == false, "bMSCryptoMode should be false" );
696         bIsPasswordToModify = aDocumentPasswordRequest2.IsRequestPasswordToModify;
697 
698         bDoHandleRequest = true;
699     }
700 
701     task::DocumentPasswordRequest aDocumentPasswordRequest;
702     if (!bDoHandleRequest && (aAnyRequest >>= aDocumentPasswordRequest))
703     {
704         nMode               = aDocumentPasswordRequest.Mode;
705         aDocumentName       = aDocumentPasswordRequest.Name;
706         OSL_ENSURE( bMSCryptoMode == false, "bMSCryptoMode should be false" );
707         OSL_ENSURE( bIsPasswordToModify == false, "bIsPasswordToModify should be false" );
708 
709         bDoHandleRequest = true;
710     }
711 
712     task::DocumentMSPasswordRequest2 aDocumentMSPasswordRequest2;
713     if (!bDoHandleRequest && (aAnyRequest >>= aDocumentMSPasswordRequest2))
714     {
715         nMode               = aDocumentMSPasswordRequest2.Mode;
716         aDocumentName       = aDocumentMSPasswordRequest2.Name;
717         bMSCryptoMode       = true;
718         bIsPasswordToModify = aDocumentMSPasswordRequest2.IsRequestPasswordToModify;
719 
720         bDoHandleRequest = true;
721     }
722 
723     task::DocumentMSPasswordRequest aDocumentMSPasswordRequest;
724     if (!bDoHandleRequest && (aAnyRequest >>= aDocumentMSPasswordRequest))
725     {
726         nMode               = aDocumentMSPasswordRequest.Mode;
727         aDocumentName       = aDocumentMSPasswordRequest.Name;
728         bMSCryptoMode       = true;
729         OSL_ENSURE( bIsPasswordToModify == false, "bIsPasswordToModify should be false" );
730 
731         bDoHandleRequest = true;
732     }
733 
734     if (bDoHandleRequest)
735     {
736         handlePasswordRequest_( pParent, nMode, rContinuations,
737                 aDocumentName, bMSCryptoMode, bIsPasswordToModify );
738         return true;
739     }
740 
741     task::PasswordRequest aPasswordRequest;
742     if( aAnyRequest >>= aPasswordRequest )
743     {
744         handlePasswordRequest_(getParentProperty(),
745                                aPasswordRequest.Mode,
746                                rRequest->getContinuations(),
747                                rtl::OUString(),
748                                false /* bool bMSCryptoMode */,
749                                false /* bool bIsPasswordToModify */,
750                                true  /* bool bIsSimplePasswordRequest */ );
751         return true;
752     }
753 
754     return false;
755 }
756