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 .
20 #include "syscreds.hxx"
21 #include <osl/diagnose.h>
22 #include <comphelper/sequence.hxx>
24 using namespace com::sun::star
;
26 SysCredentialsConfigItem::SysCredentialsConfigItem(
27 SysCredentialsConfig
* pOwner
)
28 : utl::ConfigItem( u
"Office.Common/Passwords"_ustr
, ConfigItemMode::NONE
),
32 uno::Sequence
<OUString
> aNode
{ u
"Office.Common/Passwords/AuthenticateUsingSystemCredentials"_ustr
};
33 EnableNotification( aNode
);
37 void SysCredentialsConfigItem::Notify(
38 const uno::Sequence
< OUString
> & /*seqPropertyNames*/ )
41 std::unique_lock
aGuard( m_aMutex
);
44 getSystemCredentialsURLs(aGuard
);
46 m_pOwner
->persistentConfigChanged();
49 void SysCredentialsConfigItem::ImplCommit()
54 uno::Sequence
< OUString
>
55 SysCredentialsConfigItem::getSystemCredentialsURLs()
57 std::unique_lock
aGuard(m_aMutex
);
58 return getSystemCredentialsURLs(aGuard
);
61 const uno::Sequence
< OUString
> &
62 SysCredentialsConfigItem::getSystemCredentialsURLs(std::unique_lock
<std::mutex
>& /*rGuard*/)
67 uno::Sequence
<OUString
> aPropNames
{ u
"AuthenticateUsingSystemCredentials"_ustr
};
68 uno::Sequence
< uno::Any
> aAnyValues(
69 utl::ConfigItem::GetProperties( aPropNames
) );
72 aAnyValues
.getLength() == 1,
73 "SysCredentialsConfigItem::getSystemCredentialsURLs: "
74 "Error reading config item!" );
76 uno::Sequence
< OUString
> aValues
;
77 if ( ( aAnyValues
[ 0 ] >>= aValues
) ||
78 ( !aAnyValues
[ 0 ].hasValue() ) )
80 m_seqURLs
= std::move(aValues
);
87 void SysCredentialsConfigItem::setSystemCredentialsURLs(
88 const uno::Sequence
< OUString
> & seqURLList
)
91 uno::Sequence
< OUString
> aPropNames
{ u
"AuthenticateUsingSystemCredentials"_ustr
};
92 uno::Sequence
< uno::Any
> aPropValues
{ uno::Any(seqURLList
) };
94 utl::ConfigItem::SetModified();
95 utl::ConfigItem::PutProperties( aPropNames
, aPropValues
);
97 std::unique_lock
aGuard( m_aMutex
);
99 m_seqURLs
= seqURLList
;
106 // TODO: This code is actually copied from svl/source/passwordcontainer.cxx
107 bool removeLastSegment( OUString
& aURL
)
109 sal_Int32 aInd
= aURL
.lastIndexOf( '/' );
113 sal_Int32 aPrevInd
= aURL
.lastIndexOf( '/', aInd
);
114 if ( aURL
.indexOf( "://" ) != aPrevInd
- 2 ||
115 aInd
!= aURL
.getLength() - 1 )
117 aURL
= aURL
.copy( 0, aInd
);
125 bool findURL( std::set
<OUString
> const & rContainer
, OUString
const & aURL
, OUString
& aResult
)
127 // TODO: This code is actually copied from svl/source/passwordcontainer.cxx
128 if( !rContainer
.empty() && !aURL
.isEmpty() )
130 OUString
aUrl( aURL
);
132 // each iteration remove last '/...' section from the aUrl
133 // while it's possible, up to the most left '://'
136 // first look for <url>/somename and then look for <url>/somename/...
137 auto aIter
= rContainer
.find( aUrl
);
138 if( aIter
!= rContainer
.end() )
145 OUString
tmpUrl( aUrl
);
146 if ( !tmpUrl
.endsWith("/") )
149 aIter
= rContainer
.lower_bound( tmpUrl
);
150 if( aIter
!= rContainer
.end() && aIter
->match( tmpUrl
) )
157 while( removeLastSegment( aUrl
) && !aUrl
.isEmpty() );
165 SysCredentialsConfig::SysCredentialsConfig()
166 : m_aConfigItem( this ),
167 m_bCfgInited( false )
171 void SysCredentialsConfig::initCfg(std::unique_lock
<std::mutex
>& /*rGuard*/)
175 const uno::Sequence
< OUString
> aURLs(
176 m_aConfigItem
.getSystemCredentialsURLs() );
177 m_aCfgContainer
.insert( aURLs
.begin(), aURLs
.end() );
182 void SysCredentialsConfig::writeCfg(std::unique_lock
<std::mutex
>& /*rGuard*/)
184 OSL_ENSURE( m_bCfgInited
, "SysCredentialsConfig::writeCfg : not initialized!" );
186 m_aConfigItem
.setSystemCredentialsURLs( comphelper::containerToSequence(m_aCfgContainer
) );
189 OUString
SysCredentialsConfig::find( OUString
const & aURL
)
191 std::unique_lock
aGuard( m_aMutex
);
193 if ( findURL( m_aMemContainer
, aURL
, aResult
) )
197 if ( findURL( m_aCfgContainer
, aURL
, aResult
) )
203 void SysCredentialsConfig::add( OUString
const & rURL
, bool bPersistent
)
205 std::unique_lock
aGuard( m_aMutex
);
209 m_aMemContainer
.erase( rURL
);
212 m_aCfgContainer
.insert( rURL
);
218 if ( m_aCfgContainer
.erase( rURL
) > 0 )
221 m_aMemContainer
.insert( rURL
);
225 void SysCredentialsConfig::remove( OUString
const & rURL
)
227 std::unique_lock
aGuard(m_aMutex
);
229 m_aMemContainer
.erase( rURL
);
232 if ( m_aCfgContainer
.erase( rURL
) > 0 )
236 uno::Sequence
< OUString
> SysCredentialsConfig::list( bool bOnlyPersistent
)
238 std::unique_lock
aGuard(m_aMutex
);
240 sal_Int32 nCount
= m_aCfgContainer
.size()
241 + ( bOnlyPersistent
? 0 : m_aMemContainer
.size() );
242 uno::Sequence
< OUString
> aResult( nCount
);
243 auto aResultRange
= asNonConstRange(aResult
);
246 for ( const auto& rItem
: m_aCfgContainer
)
248 aResultRange
[ n
] = rItem
;
252 if ( !bOnlyPersistent
)
254 for ( const auto& rItem
: m_aMemContainer
)
256 aResultRange
[ n
] = rItem
;
263 void SysCredentialsConfig::persistentConfigChanged()
265 std::unique_lock
aGuard( m_aMutex
);
266 m_bCfgInited
= false; // re-init on demand.
269 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */