Imported Upstream version 15.0.0
[platform/upstream/libzypp.git] / zypp / media / CredentialFileReader.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/media/CredentialFileReader.cc
10  *
11  */
12 #include <iostream>
13
14 #include "zypp/base/Logger.h"
15 #include "zypp/base/InputStream.h"
16 #include "zypp/parser/IniDict.h"
17
18 #include "zypp/media/CredentialFileReader.h"
19
20 using std::endl;
21
22 #undef ZYPP_BASE_LOGGER_LOGGROUP
23 #define ZYPP_BASE_LOGGER_LOGGROUP "parser"
24
25
26 ///////////////////////////////////////////////////////////////////
27 namespace zypp
28 { /////////////////////////////////////////////////////////////////
29   ///////////////////////////////////////////////////////////////////
30   namespace media
31   { /////////////////////////////////////////////////////////////////
32
33
34   //////////////////////////////////////////////////////////////////////
35   //
36   // CLASS NAME : CredentialFileReader
37   //
38   //////////////////////////////////////////////////////////////////////
39
40   CredentialFileReader::CredentialFileReader(
41       const Pathname & crfile,
42       const ProcessCredentials & callback)
43   {
44     InputStream is(crfile);
45     parser::IniDict dict(is);
46     for (parser::IniDict::section_const_iterator its = dict.sectionsBegin();
47          its != dict.sectionsEnd();
48          ++its)
49     {
50       Url storedUrl;
51       if (!its->empty())
52       {
53         try { storedUrl = Url(*its); }
54         catch (const url::UrlException &)
55         {
56           ERR << "invalid URL '" << *its << "' in credentials in file: "
57               << crfile << endl;
58           continue;
59         }
60       }
61
62       AuthData_Ptr credentials;
63       credentials.reset(new AuthData());
64
65       // set url
66       if (storedUrl.isValid())
67         credentials->setUrl(storedUrl);
68
69       for (parser::IniDict::entry_const_iterator it = dict.entriesBegin(*its);
70            it != dict.entriesEnd(*its);
71            ++it)
72       {
73         if (it->first == "username")
74           credentials->setUsername(it->second);
75         else if (it->first == "password")
76           credentials->setPassword(it->second);
77         else
78           ERR << "Unknown attribute in [" << crfile << "]: "
79               << it->second << " ignored" << endl;
80       }
81
82       if (credentials->valid())
83         callback(credentials);
84       else
85         ERR << "invalid credentials in file: " << crfile << endl;
86     } // sections
87   }
88
89
90   CredentialFileReader::~CredentialFileReader()
91   {}
92
93
94     /////////////////////////////////////////////////////////////////
95   } // media
96   ///////////////////////////////////////////////////////////////////
97   /////////////////////////////////////////////////////////////////
98 } // zypp
99 ///////////////////////////////////////////////////////////////////
100