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