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 .
19 #ifndef INCLUDED_SVL_SOURCE_PASSWORDCONTAINER_PASSWORDCONTAINER_HXX
20 #define INCLUDED_SVL_SOURCE_PASSWORDCONTAINER_PASSWORDCONTAINER_HXX
24 #include <com/sun/star/task/XPasswordContainer2.hpp>
25 #include <com/sun/star/task/PasswordRequestMode.hpp>
26 #include <com/sun/star/lang/XServiceInfo.hpp>
27 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
28 #include <com/sun/star/lang/XEventListener.hpp>
29 #include <com/sun/star/lang/XComponent.hpp>
30 #include <cppuhelper/implbase.hxx>
31 #include <cppuhelper/typeprovider.hxx>
32 #include <cppuhelper/queryinterface.hxx>
33 #include <cppuhelper/factory.hxx>
35 #include <tools/stream.hxx>
36 #include <unotools/configitem.hxx>
37 #include <ucbhelper/interactionrequest.hxx>
39 #include <rtl/ref.hxx>
40 #include <osl/mutex.hxx>
42 #include "syscreds.hxx"
44 #define MEMORY_RECORD 0
45 #define PERSISTENT_RECORD 1
52 // there are two lists of passwords, memory passwords and persistent passwords
54 ::std::vector
< OUString
> m_aMemPass
;
56 // persistent passwords are encrypted in one string
60 void InitArrays( bool bHasMemoryList
, const ::std::vector
< OUString
>& aMemoryList
,
61 bool bHasPersistentList
, const OUString
& aPersistentList
)
63 m_bHasMemPass
= bHasMemoryList
;
65 m_aMemPass
= aMemoryList
;
67 m_bHasPersPass
= bHasPersistentList
;
68 if ( bHasPersistentList
)
69 m_aPersPass
= aPersistentList
;
74 NamePassRecord( const OUString
& aName
)
76 , m_bHasMemPass( false )
77 , m_bHasPersPass( false )
81 NamePassRecord( const OUString
& aName
, const OUString
& aPersistentList
)
83 , m_bHasMemPass( false )
84 , m_bHasPersPass( true )
85 , m_aPersPass( aPersistentList
)
89 NamePassRecord( const NamePassRecord
& aRecord
)
90 : m_aName( aRecord
.m_aName
)
91 , m_bHasMemPass( false )
92 , m_bHasPersPass( false )
94 InitArrays( aRecord
.m_bHasMemPass
, aRecord
.m_aMemPass
, aRecord
.m_bHasPersPass
, aRecord
.m_aPersPass
);
97 NamePassRecord
& operator=( const NamePassRecord
& aRecord
)
101 m_aName
= aRecord
.m_aName
;
105 InitArrays( aRecord
.m_bHasMemPass
, aRecord
.m_aMemPass
, aRecord
.m_bHasPersPass
, aRecord
.m_aPersPass
);
110 const OUString
& GetUserName() const
115 bool HasPasswords( sal_Int8 nStatus
) const
117 if ( nStatus
== MEMORY_RECORD
)
118 return m_bHasMemPass
;
119 if ( nStatus
== PERSISTENT_RECORD
)
120 return m_bHasPersPass
;
125 ::std::vector
< OUString
> GetMemPasswords() const
130 return ::std::vector
< OUString
>();
133 OUString
GetPersPasswords() const
135 if ( m_bHasPersPass
)
141 void SetMemPasswords( const ::std::vector
< OUString
>& aMemList
)
143 m_aMemPass
= aMemList
;
144 m_bHasMemPass
= true;
147 void SetPersPasswords( const OUString
& aPersList
)
149 m_aPersPass
= aPersList
;
150 m_bHasPersPass
= true;
153 void RemovePasswords( sal_Int8 nStatus
)
155 if ( nStatus
== MEMORY_RECORD
)
157 m_bHasMemPass
= false;
160 else if ( nStatus
== PERSISTENT_RECORD
)
162 m_bHasPersPass
= false;
170 typedef ::std::pair
< const OUString
, ::std::vector
< NamePassRecord
> > PairUrlRecord
;
171 typedef ::std::map
< OUString
, ::std::vector
< NamePassRecord
> > PassMap
;
174 class PasswordContainer
;
177 : public ::utl::ConfigItem
180 PasswordContainer
* mainCont
;
184 virtual void ImplCommit() override
;
187 StorageItem( PasswordContainer
* point
, const OUString
& path
) :
188 ConfigItem( path
, ConfigItemMode::NONE
),
192 css::uno::Sequence
< OUString
> aNode
{ path
+ "/Store" };
193 EnableNotification( aNode
);
197 void update( const OUString
& url
, const NamePassRecord
& rec
);
198 void remove( const OUString
& url
, const OUString
& rec
);
201 bool getEncodedMP( OUString
& aResult
);
202 void setEncodedMP( const OUString
& aResult
, bool bAcceptEnmpty
= false );
203 void setUseStorage( bool bUse
);
206 virtual void Notify( const css::uno::Sequence
< OUString
>& aPropertyNames
) override
;
210 class PasswordContainer
: public ::cppu::WeakImplHelper
<
211 css::task::XPasswordContainer2
,
212 css::lang::XServiceInfo
,
213 css::lang::XEventListener
>
216 PassMap m_aContainer
;
217 std::unique_ptr
<StorageItem
> m_pStorageFile
;
219 OUString m_aMasterPasswd
; // master password is set when the string is not empty
220 css::uno::Reference
< css::lang::XComponent
> mComponent
;
221 SysCredentialsConfig mUrlContainer
;
223 /// @throws css::uno::RuntimeException
224 css::uno::Sequence
< css::task::UserRecord
> CopyToUserRecordSequence(
225 const ::std::vector
< NamePassRecord
>& original
,
226 const css::uno::Reference
< css::task::XInteractionHandler
>& Handler
);
228 css::task::UserRecord
CopyToUserRecord(
229 const NamePassRecord
& aRecord
,
230 bool& io_bTryToDecode
,
231 const css::uno::Reference
< css::task::XInteractionHandler
>& aHandler
);
233 /// @throws css::uno::RuntimeException
234 css::uno::Sequence
< css::task::UserRecord
> FindUsr(
235 const ::std::vector
< NamePassRecord
>& userlist
,
236 const OUString
& name
,
237 const css::uno::Reference
< css::task::XInteractionHandler
>& Handler
);
238 /// @throws css::uno::RuntimeException
239 bool createUrlRecord(
240 const PassMap::iterator
& rIter
,
242 const OUString
& aName
,
243 const css::uno::Reference
< css::task::XInteractionHandler
>& aHandler
,
244 css::task::UrlRecord
& rRec
);
246 /// @throws css::uno::RuntimeException
247 css::task::UrlRecord
find(
248 const OUString
& aURL
,
249 const OUString
& aName
,
250 bool bName
, // only needed to support empty user names
251 const css::uno::Reference
< css::task::XInteractionHandler
>& aHandler
);
253 static OUString
GetDefaultMasterPassword();
255 static OUString
RequestPasswordFromUser(
256 css::task::PasswordRequestMode aRMode
,
257 const css::uno::Reference
< css::task::XInteractionHandler
>& xHandler
);
259 /// @throws css::uno::RuntimeException
260 OUString
const & GetMasterPassword( const css::uno::Reference
< css::task::XInteractionHandler
>& Handler
);
262 /// @throws css::uno::RuntimeException
263 void UpdateVector( const OUString
& url
, ::std::vector
< NamePassRecord
>& toUpdate
, NamePassRecord
const & rec
, bool writeFile
);
265 /// @throws css::uno::RuntimeException
266 void PrivateAdd( const OUString
& aUrl
,
267 const OUString
& aUserName
,
268 const css::uno::Sequence
< OUString
>& aPasswords
,
270 const css::uno::Reference
< css::task::XInteractionHandler
>& Handler
);
272 /// @throws css::uno::RuntimeException
273 static ::std::vector
< OUString
> DecodePasswords( const OUString
& aLine
, const OUString
& aMasterPassword
, css::task::PasswordRequestMode mode
);
275 /// @throws css::uno::RuntimeException
276 static OUString
EncodePasswords(const std::vector
< OUString
>& lines
, const OUString
& aMasterPassword
);
279 PasswordContainer( const css::uno::Reference
< css::lang::XMultiServiceFactory
>& );
280 virtual ~PasswordContainer() override
;
282 virtual void SAL_CALL
add( const OUString
& aUrl
,
283 const OUString
& aUserName
,
284 const css::uno::Sequence
< OUString
>& aPasswords
,
285 const css::uno::Reference
< css::task::XInteractionHandler
>& Handler
) override
;
287 virtual void SAL_CALL
addPersistent( const OUString
& aUrl
,
288 const OUString
& aUserName
,
289 const css::uno::Sequence
< OUString
>& aPasswords
,
290 const css::uno::Reference
< css::task::XInteractionHandler
>& Handler
) override
;
292 virtual css::task::UrlRecord SAL_CALL
293 find( const OUString
& aUrl
,
294 const css::uno::Reference
< css::task::XInteractionHandler
>& Handler
) override
;
296 virtual css::task::UrlRecord SAL_CALL
297 findForName( const OUString
& aUrl
,
298 const OUString
& aUserName
,
299 const css::uno::Reference
< css::task::XInteractionHandler
>& Handler
) override
;
301 virtual void SAL_CALL
remove( const OUString
& aUrl
,
302 const OUString
& aUserName
) override
;
304 virtual void SAL_CALL
removePersistent( const OUString
& aUrl
,
305 const OUString
& aUserName
) override
;
307 virtual void SAL_CALL
removeAllPersistent() override
;
309 virtual css::uno::Sequence
< css::task::UrlRecord
> SAL_CALL
310 getAllPersistent( const css::uno::Reference
< css::task::XInteractionHandler
>& Handler
) override
;
314 /// @throws css::uno::RuntimeException
315 static OUString
impl_getStaticImplementationName( );
316 /// @throws css::uno::RuntimeException
317 static css::uno::Sequence
< OUString
>
318 impl_getStaticSupportedServiceNames( );
319 /// @throws css::uno::RuntimeException
320 static css::uno::Reference
< css::lang::XSingleServiceFactory
>
321 impl_createFactory( const css::uno::Reference
< css::lang::XMultiServiceFactory
>& ServiceManager
);
322 /// @throws css::uno::RuntimeException
323 static css::uno::Reference
< css::uno::XInterface
> SAL_CALL
324 impl_createInstance( const css::uno::Reference
< css::lang::XMultiServiceFactory
>& xServiceManager
);
327 virtual OUString SAL_CALL
getImplementationName( ) override
;
328 virtual sal_Bool SAL_CALL
supportsService( const OUString
& ServiceName
) override
;
330 virtual css::uno::Sequence
< OUString
> SAL_CALL
331 getSupportedServiceNames( ) override
;
334 virtual void SAL_CALL
disposing( const css::lang::EventObject
& Source
) override
;
336 // XMasterPasswordHandling
337 virtual sal_Bool SAL_CALL
authorizateWithMasterPassword( const css::uno::Reference
< css::task::XInteractionHandler
>& xHandler
) override
;
338 virtual sal_Bool SAL_CALL
changeMasterPassword( const css::uno::Reference
< css::task::XInteractionHandler
>& xHandler
) override
;
339 virtual void SAL_CALL
removeMasterPassword() override
;
340 virtual sal_Bool SAL_CALL
hasMasterPassword( ) override
;
341 virtual sal_Bool SAL_CALL
allowPersistentStoring( sal_Bool bAllow
) override
;
342 virtual sal_Bool SAL_CALL
isPersistentStoringAllowed( ) override
;
344 // XMasterPasswordHandling2
345 virtual sal_Bool SAL_CALL
useDefaultMasterPassword( const css::uno::Reference
< css::task::XInteractionHandler
>& xHandler
) override
;
346 virtual sal_Bool SAL_CALL
isDefaultMasterPasswordUsed( ) override
;
349 virtual void SAL_CALL
addUrl( const OUString
& Url
, sal_Bool MakePersistent
) override
;
350 virtual OUString SAL_CALL
findUrl( const OUString
& Url
) override
;
351 virtual void SAL_CALL
removeUrl( const OUString
& Url
) override
;
352 virtual css::uno::Sequence
< OUString
> SAL_CALL
getUrls( sal_Bool OnlyPersistent
) override
;
358 class MasterPasswordRequest_Impl
: public ucbhelper::InteractionRequest
360 ::rtl::Reference
< ucbhelper::InteractionSupplyAuthentication
> m_xAuthSupplier
;
363 MasterPasswordRequest_Impl( css::task::PasswordRequestMode Mode
);
365 const ::rtl::Reference
< ucbhelper::InteractionSupplyAuthentication
> &
366 getAuthenticationSupplier() const { return m_xAuthSupplier
; }
373 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */