libzypp  13.10.6
CredentialFileReader.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #include <iostream>
13 
14 #include "zypp/base/Logger.h"
15 #include "zypp/base/InputStream.h"
16 #include "zypp/parser/IniDict.h"
17 
19 
20 using std::endl;
21 
22 #undef ZYPP_BASE_LOGGER_LOGGROUP
23 #define ZYPP_BASE_LOGGER_LOGGROUP "parser"
24 
25 
27 namespace zypp
28 {
29  namespace media
31  {
32 
33 
35  //
36  // CLASS NAME : CredentialFileReader
37  //
39 
41  const Pathname & crfile,
42  const ProcessCredentials & callback)
43  {
44  InputStream is(crfile);
45  parser::IniDict dict(is);
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 
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 
91  {}
92 
93 
95  } // media
98 } // zypp
100 
MapKVIteratorTraits< SectionSet >::Key_const_iterator section_const_iterator
Definition: IniDict.h:46
Base class for all URL exceptions.
Definition: UrlException.h:31
Helper to create and pass std::istream.
Definition: InputStream.h:56
bool isValid() const
Verifies the Url.
Definition: Url.cc:483
function< bool(AuthData_Ptr &)> ProcessCredentials
Callback definition.
#define ERR
Definition: Logger.h:49
CredentialFileReader(const Pathname &crfile, const ProcessCredentials &callback)
section_const_iterator sectionsBegin() const
Definition: IniDict.cc:94
entry_const_iterator entriesEnd(const std::string &section) const
Definition: IniDict.cc:82
Class for handling media authentication data.
Definition: MediaUserAuth.h:30
Parses a INI file and offers its structure as a dictionary.
Definition: IniDict.h:40
shared_ptr< AuthData > AuthData_Ptr
Definition: MediaUserAuth.h:69
entry_const_iterator entriesBegin(const std::string &section) const
Definition: IniDict.cc:71
EntrySet::const_iterator entry_const_iterator
Definition: IniDict.h:47
section_const_iterator sectionsEnd() const
Definition: IniDict.cc:99
Url manipulation class.
Definition: Url.h:87