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/XMultiServiceFactory.hpp>
29 #include <com/sun/star/lang/XEventListener.hpp>
30 #include <com/sun/star/lang/XComponent.hpp>
31 #include <com/sun/star/uno/XComponentContext.hpp>
32 #include <cppuhelper/implbase.hxx>
34 #include <unotools/configitem.hxx>
35 #include <ucbhelper/interactionrequest.hxx>
37 #include <rtl/ref.hxx>
38 #include <osl/mutex.hxx>
40 #include "syscreds.hxx"
42 #define MEMORY_RECORD 0
43 #define PERSISTENT_RECORD 1
50 // there are two lists of passwords, memory passwords and persistent passwords
52 ::std::vector
< OUString
> m_aMemPass
;
54 // persistent passwords are encrypted in one string
58 void InitArrays( bool bHasMemoryList
, const ::std::vector
< OUString
>& aMemoryList
,
59 bool bHasPersistentList
, const OUString
& aPersistentList
)
61 m_bHasMemPass
= bHasMemoryList
;
63 m_aMemPass
= aMemoryList
;
65 m_bHasPersPass
= bHasPersistentList
;
66 if ( bHasPersistentList
)
67 m_aPersPass
= aPersistentList
;
72 NamePassRecord( const OUString
& aName
)
74 , m_bHasMemPass( false )
75 , m_bHasPersPass( false )
79 NamePassRecord( const OUString
& aName
, const OUString
& aPersistentList
)
81 , m_bHasMemPass( false )
82 , m_bHasPersPass( true )
83 , m_aPersPass( aPersistentList
)
87 NamePassRecord( const NamePassRecord
& aRecord
)
88 : m_aName( aRecord
.m_aName
)
89 , m_bHasMemPass( false )
90 , m_bHasPersPass( false )
92 InitArrays( aRecord
.m_bHasMemPass
, aRecord
.m_aMemPass
, aRecord
.m_bHasPersPass
, aRecord
.m_aPersPass
);
95 NamePassRecord
& operator=( const NamePassRecord
& aRecord
)
99 m_aName
= aRecord
.m_aName
;
103 InitArrays( aRecord
.m_bHasMemPass
, aRecord
.m_aMemPass
, aRecord
.m_bHasPersPass
, aRecord
.m_aPersPass
);
108 const OUString
& GetUserName() const
113 bool HasPasswords( sal_Int8 nStatus
) const
115 if ( nStatus
== MEMORY_RECORD
)
116 return m_bHasMemPass
;
117 if ( nStatus
== PERSISTENT_RECORD
)
118 return m_bHasPersPass
;
123 ::std::vector
< OUString
> GetMemPasswords() const
128 return ::std::vector
< OUString
>();
131 OUString
GetPersPasswords() const
133 if ( m_bHasPersPass
)
139 void SetMemPasswords( const ::std::vector
< OUString
>& aMemList
)
141 m_aMemPass
= aMemList
;
142 m_bHasMemPass
= true;
145 void SetPersPasswords( const OUString
& aPersList
)
147 m_aPersPass
= aPersList
;
148 m_bHasPersPass
= true;
151 void RemovePasswords( sal_Int8 nStatus
)
153 if ( nStatus
== MEMORY_RECORD
)
155 m_bHasMemPass
= false;
158 else if ( nStatus
== PERSISTENT_RECORD
)
160 m_bHasPersPass
= false;
168 typedef ::std::pair
< const OUString
, ::std::vector
< NamePassRecord
> > PairUrlRecord
;
169 typedef ::std::map
< OUString
, ::std::vector
< NamePassRecord
> > PassMap
;
172 class PasswordContainer
;
175 : public ::utl::ConfigItem
178 PasswordContainer
* mainCont
;
182 virtual void ImplCommit() override
;
185 StorageItem( PasswordContainer
* point
, const OUString
& path
) :
186 ConfigItem( path
, ConfigItemMode::NONE
),
190 css::uno::Sequence
< OUString
> aNode
{ path
+ "/Store" };
191 EnableNotification( aNode
);
195 void update( const OUString
& url
, const NamePassRecord
& rec
);
196 void remove( const OUString
& url
, const OUString
& rec
);
199 bool getEncodedMP( OUString
& aResult
);
200 void setEncodedMP( const OUString
& aResult
, bool bAcceptEmpty
= false );
201 void setUseStorage( bool bUse
);
204 virtual void Notify( const css::uno::Sequence
< OUString
>& aPropertyNames
) override
;
208 class PasswordContainer
: public ::cppu::WeakImplHelper
<
209 css::task::XPasswordContainer2
,
210 css::lang::XServiceInfo
,
211 css::lang::XEventListener
>
214 PassMap m_aContainer
;
215 std::unique_ptr
<StorageItem
> m_pStorageFile
;
217 OUString m_aMasterPasswd
; // master password is set when the string is not empty
218 css::uno::Reference
< css::lang::XComponent
> mComponent
;
219 SysCredentialsConfig mUrlContainer
;
221 /// @throws css::uno::RuntimeException
222 css::uno::Sequence
< css::task::UserRecord
> CopyToUserRecordSequence(
223 const ::std::vector
< NamePassRecord
>& original
,
224 const css::uno::Reference
< css::task::XInteractionHandler
>& Handler
);
226 css::task::UserRecord
CopyToUserRecord(
227 const NamePassRecord
& aRecord
,
228 bool& io_bTryToDecode
,
229 const css::uno::Reference
< css::task::XInteractionHandler
>& aHandler
);
231 /// @throws css::uno::RuntimeException
232 css::uno::Sequence
< css::task::UserRecord
> FindUsr(
233 const ::std::vector
< NamePassRecord
>& userlist
,
234 const OUString
& name
,
235 const css::uno::Reference
< css::task::XInteractionHandler
>& Handler
);
236 /// @throws css::uno::RuntimeException
237 bool createUrlRecord(
238 const PassMap::iterator
& rIter
,
240 const OUString
& aName
,
241 const css::uno::Reference
< css::task::XInteractionHandler
>& aHandler
,
242 css::task::UrlRecord
& rRec
);
244 /// @throws css::uno::RuntimeException
245 css::task::UrlRecord
find(
246 const OUString
& aURL
,
247 const OUString
& aName
,
248 bool bName
, // only needed to support empty user names
249 const css::uno::Reference
< css::task::XInteractionHandler
>& aHandler
);
251 static OUString
GetDefaultMasterPassword();
253 static OUString
RequestPasswordFromUser(
254 css::task::PasswordRequestMode aRMode
,
255 const css::uno::Reference
< css::task::XInteractionHandler
>& xHandler
);
257 /// @throws css::uno::RuntimeException
258 OUString
const & GetMasterPassword( const css::uno::Reference
< css::task::XInteractionHandler
>& Handler
);
260 /// @throws css::uno::RuntimeException
261 void UpdateVector( const OUString
& url
, ::std::vector
< NamePassRecord
>& toUpdate
, NamePassRecord
const & rec
, bool writeFile
);
263 /// @throws css::uno::RuntimeException
264 void PrivateAdd( const OUString
& aUrl
,
265 const OUString
& aUserName
,
266 const css::uno::Sequence
< OUString
>& aPasswords
,
268 const css::uno::Reference
< css::task::XInteractionHandler
>& Handler
);
270 /// @throws css::uno::RuntimeException
271 static ::std::vector
< OUString
> DecodePasswords( const OUString
& aLine
, const OUString
& aMasterPassword
, css::task::PasswordRequestMode mode
);
273 /// @throws css::uno::RuntimeException
274 static OUString
EncodePasswords(const std::vector
< OUString
>& lines
, const OUString
& aMasterPassword
);
277 PasswordContainer( const css::uno::Reference
< css::uno::XComponentContext
>& );
278 virtual ~PasswordContainer() override
;
280 virtual void SAL_CALL
add( const OUString
& aUrl
,
281 const OUString
& aUserName
,
282 const css::uno::Sequence
< OUString
>& aPasswords
,
283 const css::uno::Reference
< css::task::XInteractionHandler
>& Handler
) override
;
285 virtual void SAL_CALL
addPersistent( const OUString
& aUrl
,
286 const OUString
& aUserName
,
287 const css::uno::Sequence
< OUString
>& aPasswords
,
288 const css::uno::Reference
< css::task::XInteractionHandler
>& Handler
) override
;
290 virtual css::task::UrlRecord SAL_CALL
291 find( const OUString
& aUrl
,
292 const css::uno::Reference
< css::task::XInteractionHandler
>& Handler
) override
;
294 virtual css::task::UrlRecord SAL_CALL
295 findForName( const OUString
& aUrl
,
296 const OUString
& aUserName
,
297 const css::uno::Reference
< css::task::XInteractionHandler
>& Handler
) override
;
299 virtual void SAL_CALL
remove( const OUString
& aUrl
,
300 const OUString
& aUserName
) override
;
302 virtual void SAL_CALL
removePersistent( const OUString
& aUrl
,
303 const OUString
& aUserName
) override
;
305 virtual void SAL_CALL
removeAllPersistent() override
;
307 virtual css::uno::Sequence
< css::task::UrlRecord
> SAL_CALL
308 getAllPersistent( const css::uno::Reference
< css::task::XInteractionHandler
>& Handler
) override
;
311 virtual OUString SAL_CALL
getImplementationName( ) override
;
312 virtual sal_Bool SAL_CALL
supportsService( const OUString
& ServiceName
) override
;
314 virtual css::uno::Sequence
< OUString
> SAL_CALL
315 getSupportedServiceNames( ) override
;
318 virtual void SAL_CALL
disposing( const css::lang::EventObject
& Source
) override
;
320 // XMasterPasswordHandling
321 virtual sal_Bool SAL_CALL
authorizateWithMasterPassword( const css::uno::Reference
< css::task::XInteractionHandler
>& xHandler
) override
;
322 virtual sal_Bool SAL_CALL
changeMasterPassword( const css::uno::Reference
< css::task::XInteractionHandler
>& xHandler
) override
;
323 virtual void SAL_CALL
removeMasterPassword() override
;
324 virtual sal_Bool SAL_CALL
hasMasterPassword( ) override
;
325 virtual sal_Bool SAL_CALL
allowPersistentStoring( sal_Bool bAllow
) override
;
326 virtual sal_Bool SAL_CALL
isPersistentStoringAllowed( ) override
;
328 // XMasterPasswordHandling2
329 virtual sal_Bool SAL_CALL
useDefaultMasterPassword( const css::uno::Reference
< css::task::XInteractionHandler
>& xHandler
) override
;
330 virtual sal_Bool SAL_CALL
isDefaultMasterPasswordUsed( ) override
;
333 virtual void SAL_CALL
addUrl( const OUString
& Url
, sal_Bool MakePersistent
) override
;
334 virtual OUString SAL_CALL
findUrl( const OUString
& Url
) override
;
335 virtual void SAL_CALL
removeUrl( const OUString
& Url
) override
;
336 virtual css::uno::Sequence
< OUString
> SAL_CALL
getUrls( sal_Bool OnlyPersistent
) override
;
342 class MasterPasswordRequest_Impl
: public ucbhelper::InteractionRequest
344 ::rtl::Reference
< ucbhelper::InteractionSupplyAuthentication
> m_xAuthSupplier
;
347 MasterPasswordRequest_Impl( css::task::PasswordRequestMode Mode
);
349 const ::rtl::Reference
< ucbhelper::InteractionSupplyAuthentication
> &
350 getAuthenticationSupplier() const { return m_xAuthSupplier
; }
357 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */