libzypp: zypp/media/CredentialFileReader.cc Source File

libzypp 17.25.10

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
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
26 namespace zypp
27 {
29 namespace media
30 {
32 namespace
33 {
34 // Looks like INI but allows multiple sections for the same URL
35 // but different user (in .cat files). So don't use an Ini
36 // Also support a global section without '[URL]' which is used
37 // in credential files.
38 // -------------------------------------
39 // username = emptyUSER
40 // password = emptyPASS
41 // -------------------------------------
42 // [http://server/tmp/sumafake222]
43 // username = USER
44 // password = PASS
45 //
46 // [http://server/tmp/sumafake222]
47 // username = USER2
48 // password = PASS
49 // -------------------------------------
50 struct CredentialFileReaderImpl : public parser::IniParser
51 {
52 typedef CredentialFileReader::ProcessCredentials ProcessCredentials;
53
54 struct StopParsing {};
55
56 CredentialFileReaderImpl( const Pathname & input_r, const ProcessCredentials & callback_r )
57 : _input( input_r )
58 , _callback( callback_r )
59 {
60 try
61 {
62 parse( input_r );
63 }
64 catch ( StopParsing )
65 { /* NO error but consumer aborted parsing */ }
66 }
67
68 // NO-OP; new sections are opened in consume()
69 virtual void beginParse()
70 { /*EMPTY*/ }
71
72 // start a new section [url]
73 virtual void consume( const std::string & section_r )
74 {
75 endParse(); // close any open section
76 _secret.reset( new AuthData );
77 try
78 {
79 _secret->setUrl( Url(section_r) );
80 }
81 catch ( const url::UrlException & )
82 {
83 ERR << "Ignore invalid URL '" << section_r << "' in file " << _input << endl;
84 _secret.reset(); // ignore this section
85 }
86 }
87
88 virtual void consume( const std::string & section_r, const std::string & key_r, const std::string & value_r )
89 {
90 if ( !_secret && section_r.empty() )
91 _secret.reset( new AuthData ); // a initial global section without [URL]
92
93 if ( _secret )
94 {
95 if ( key_r == "username" )
96 _secret->setUsername( value_r );
97 else if ( key_r == "password" )
98 _secret->setPassword( value_r );
99 else
100 WAR << "Ignore unknown attribute '" << key_r << "=" << value_r << "' in file " << _input << endl;
101 }
102 // else: ignored section due to wrong URL
103 }
104
105 // send any valid pending section
106 virtual void endParse()
107 {
108 if ( _secret )
109 {
110 if ( _secret->valid() )
111 {
112 if ( !_callback( _secret ) )
113 throw( StopParsing() );
114 }
115 else
116 ERR << "Ignore invalid credentials for URL '" << _secret->url() << "' in file " << _input << endl;
117 }
118 }
119
120 private:
121 const Pathname & _input;
122 const ProcessCredentials & _callback;
123 AuthData_Ptr _secret;
124 };
125 } // namespace
127
129 //
130 // CLASS NAME : CredentialFileReader
131 //
133
134 CredentialFileReader::CredentialFileReader( const Pathname & crfile_r, const ProcessCredentials & callback_r )
135 { CredentialFileReaderImpl( crfile_r, callback_r ); }
136
137 CredentialFileReader::~CredentialFileReader()
138 {}
139
140 } // namespace media
142 } // namespace zypp
144
_input
const Pathname & _input
Definition: CredentialFileReader.cc:121
_callback
const ProcessCredentials & _callback
Definition: CredentialFileReader.cc:122
_secret
AuthData_Ptr _secret
Definition: CredentialFileReader.cc:123
CredentialFileReader.h
IniDict.h
InputStream.h
Logger.h
ERR
#define ERR
Definition: Logger.h:93
WAR
#define WAR
Definition: Logger.h:92
zypp::Url
Url manipulation class.
Definition: Url.h:92
zypp::filesystem::Pathname
Pathname.
Definition: Pathname.h:45
zypp::media::AuthData
Class for handling media authentication data.
Definition: MediaUserAuth.h:31
zypp::media::CredentialFileReader::~CredentialFileReader
~CredentialFileReader()
Definition: CredentialFileReader.cc:137
zypp::media::CredentialFileReader::ProcessCredentials
function< bool(AuthData_Ptr &)> ProcessCredentials
Callback invoked for each entry found in the file.
Definition: CredentialFileReader.h:36
zypp::media::CredentialFileReader::CredentialFileReader
CredentialFileReader(const Pathname &crfile_r, const ProcessCredentials &callback_r)
Definition: CredentialFileReader.cc:134
zypp::parser::IniParser
Simple INI-file parser.
Definition: IniParser.h:42
zypp::url::UrlException
Base class for all URL exceptions.
Definition: UrlException.h:32
zypp::media::AuthData_Ptr
shared_ptr< AuthData > AuthData_Ptr
Definition: MediaUserAuth.h:69
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2