summaryrefslogtreecommitdiffstats
path: root/svl/source/passwordcontainer/syscreds.cxx
blob: a3354b853a8236651a7fa3ab73fd7b36e27660b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include "syscreds.hxx"
#include <osl/diagnose.h>
#include <comphelper/sequence.hxx>

using namespace com::sun::star;

SysCredentialsConfigItem::SysCredentialsConfigItem(
    SysCredentialsConfig * pOwner )
: utl::ConfigItem( "Office.Common/Passwords", ConfigItemMode::NONE ),
  m_bInited( false ),
  m_pOwner( pOwner )
{
    uno::Sequence<OUString> aNode { "Office.Common/Passwords/AuthenticateUsingSystemCredentials" };
    EnableNotification( aNode );
}

//virtual
void SysCredentialsConfigItem::Notify(
    const uno::Sequence< OUString > & /*seqPropertyNames*/ )
{
    {
        ::osl::MutexGuard aGuard( m_aMutex );
        m_bInited = false;
        // rebuild m_seqURLs
        getSystemCredentialsURLs();
    }
    m_pOwner->persistentConfigChanged();
}

void SysCredentialsConfigItem::ImplCommit()
{
    // does nothing
}

uno::Sequence< OUString >
SysCredentialsConfigItem::getSystemCredentialsURLs()
{
    ::osl::MutexGuard aGuard( m_aMutex );
    if ( !m_bInited )
    {
        // read config item
        uno::Sequence<OUString> aPropNames { "AuthenticateUsingSystemCredentials" };
        uno::Sequence< uno::Any > aAnyValues(
            utl::ConfigItem::GetProperties( aPropNames ) );

        OSL_ENSURE(
            aAnyValues.getLength() == 1,
            "SysCredentialsConfigItem::getSystemCredentialsURLs: "
            "Error reading config item!" );

        uno::Sequence< OUString > aValues;
        if ( ( aAnyValues[ 0 ] >>= aValues ) ||
             ( !aAnyValues[ 0 ].hasValue() ) )
        {
            m_seqURLs = aValues;
            m_bInited = true;
        }
    }
    return m_seqURLs;
}

void SysCredentialsConfigItem::setSystemCredentialsURLs(
    const uno::Sequence< OUString > & seqURLList )
{
    ::osl::MutexGuard aGuard( m_aMutex );

    // write config item.
    uno::Sequence< OUString > aPropNames{ "AuthenticateUsingSystemCredentials" };
    uno::Sequence< uno::Any > aPropValues{ uno::Any(seqURLList) };

    utl::ConfigItem::SetModified();
    utl::ConfigItem::PutProperties( aPropNames, aPropValues );

    m_seqURLs = seqURLList;
    m_bInited = true;
}


namespace
{
    // TODO: This code is actually copied from svl/source/passwordcontainer.cxx
    bool removeLastSegment( OUString & aURL )
    {
        sal_Int32 aInd = aURL.lastIndexOf( '/' );

        if( aInd > 0  )
        {
            sal_Int32 aPrevInd = aURL.lastIndexOf( '/', aInd );
            if ( aURL.indexOf( "://" ) != aPrevInd - 2 ||
                 aInd != aURL.getLength() - 1 )
            {
                aURL = aURL.copy( 0, aInd );
                return true;
            }
        }

        return false;
    }

    bool findURL( std::set<OUString> const & rContainer, OUString const & aURL, OUString & aResult )
    {
        // TODO: This code is actually copied from svl/source/passwordcontainer.cxx
        if( !rContainer.empty() && !aURL.isEmpty() )
        {
            OUString aUrl( aURL );

            // each iteration remove last '/...' section from the aUrl
            // while it's possible, up to the most left '://'
            do
            {
                // first look for <url>/somename and then look for <url>/somename/...
                auto aIter = rContainer.find( aUrl );
                if( aIter != rContainer.end() )
                {
                    aResult = *aIter;
                    return true;
                }
                else
                {
                    OUString tmpUrl( aUrl );
                    if ( !tmpUrl.endsWith("/") )
                      tmpUrl += "/";

                    aIter = rContainer.lower_bound( tmpUrl );
                    if( aIter != rContainer.end() && aIter->match( tmpUrl ) )
                    {
                        aResult = *aIter;
                        return true;
                    }
                }
            }
            while( removeLastSegment( aUrl ) && !aUrl.isEmpty() );
        }
        aResult.clear();
        return false;
    }

} // namespace

SysCredentialsConfig::SysCredentialsConfig()
: m_aConfigItem( this ),
  m_bCfgInited( false )
{
}

void SysCredentialsConfig::initCfg()
{
    osl::MutexGuard aGuard( m_aMutex );
    if ( !m_bCfgInited )
    {
        const uno::Sequence< OUString > aURLs(
            m_aConfigItem.getSystemCredentialsURLs() );
        m_aCfgContainer.insert( aURLs.begin(), aURLs.end() );
        m_bCfgInited = true;
    }
}

void SysCredentialsConfig::writeCfg()
{
    osl::MutexGuard aGuard( m_aMutex );

    OSL_ENSURE( m_bCfgInited, "SysCredentialsConfig::writeCfg : not initialized!" );

    m_aConfigItem.setSystemCredentialsURLs( comphelper::containerToSequence(m_aCfgContainer) );
}

OUString SysCredentialsConfig::find( OUString const & aURL )
{
    osl::MutexGuard aGuard( m_aMutex );
    OUString aResult;
    if ( findURL( m_aMemContainer, aURL, aResult ) )
        return aResult;

    initCfg();
    if ( findURL( m_aCfgContainer, aURL, aResult ) )
        return aResult;

    return OUString();
}

void SysCredentialsConfig::add( OUString const & rURL, bool bPersistent )
{
    ::osl::MutexGuard aGuard( m_aMutex );

    if ( bPersistent )
    {
        m_aMemContainer.erase( rURL );

        initCfg();
        m_aCfgContainer.insert( rURL );
        writeCfg();
    }
    else
    {
        initCfg();
        if ( m_aCfgContainer.erase( rURL ) > 0 )
            writeCfg();

        m_aMemContainer.insert( rURL );
    }
}

void SysCredentialsConfig::remove( OUString const & rURL )
{
    m_aMemContainer.erase( rURL );

    initCfg();
    if ( m_aCfgContainer.erase( rURL ) > 0 )
        writeCfg();
}

uno::Sequence< OUString > SysCredentialsConfig::list( bool bOnlyPersistent )
{
    initCfg();
    sal_Int32 nCount = m_aCfgContainer.size()
                     + ( bOnlyPersistent ? 0 : m_aMemContainer.size() );
    uno::Sequence< OUString > aResult( nCount );
    auto aResultRange = asNonConstRange(aResult);
    sal_Int32 n = 0;

    for ( const auto& rItem : m_aCfgContainer )
    {
        aResultRange[ n ] = rItem;
        ++n;
    }

    if ( !bOnlyPersistent )
    {
        for ( const auto& rItem : m_aMemContainer )
        {
            aResultRange[ n ] = rItem;
            ++n;
        }
    }
    return aResult;
}

void SysCredentialsConfig::persistentConfigChanged()
{
    ::osl::MutexGuard aGuard( m_aMutex );
    m_bCfgInited = false; // re-init on demand.
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */