Imported Upstream version 17.23.2
[platform/upstream/libzypp.git] / zypp / media / CredentialManager.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/media/CredentialManager.h
10  *
11  */
12 #ifndef ZYPP_MEDIA_CREDENTIALMANAGER_H
13 #define ZYPP_MEDIA_CREDENTIALMANAGER_H
14
15 #include <set>
16
17 #include "zypp/Pathname.h"
18 #include "zypp/media/MediaUserAuth.h"
19
20 //////////////////////////////////////////////////////////////////////
21 namespace zypp
22 { ////////////////////////////////////////////////////////////////////
23
24   class Url;
25
26   //////////////////////////////////////////////////////////////////////
27   namespace media
28   { ////////////////////////////////////////////////////////////////////
29
30
31   //////////////////////////////////////////////////////////////////////
32   //
33   // CLASS NAME : CredManagerOptions
34   //
35   /**
36    * \todo configurable cred file locations
37    */
38   struct CredManagerOptions
39   {
40     CredManagerOptions(const Pathname & rootdir = "");
41
42     Pathname globalCredFilePath;
43     Pathname userCredFilePath;
44     Pathname customCredFileDir;
45   };
46   //////////////////////////////////////////////////////////////////////
47
48   // comparator for CredentialSet
49   struct AuthDataComparator
50   {
51     bool operator()(const AuthData_Ptr & lhs, const AuthData_Ptr & rhs) const;
52   };
53
54   //////////////////////////////////////////////////////////////////////
55   //
56   // CLASS NAME : CredentialManager
57   //
58   /**
59    * \todo better method names
60    * \todo delete(AuthData) method
61    */
62   class CredentialManager
63   {
64   public:
65     typedef std::set<AuthData_Ptr, AuthDataComparator> CredentialSet;
66     typedef CredentialSet::size_type                   CredentialSize;
67     typedef CredentialSet::const_iterator              CredentialIterator;
68
69
70     CredentialManager(const CredManagerOptions & opts = CredManagerOptions());
71
72     ~CredentialManager()
73     {}
74
75   public:
76     /**
77      * Get credentials for the specified \a url.
78      *
79      * If the URL contains also username, it will be used to find the match
80      * for this user (in case mutliple are available).
81      *
82      * \param url URL to find credentials for.
83      * \return Pointer to retrieved authentication data on success or an empty
84      *         AuthData_Ptr otherwise.
85      * \todo return a copy instead?
86      */
87     AuthData_Ptr getCred(const Url & url);
88
89     /**
90      * Read credentials from a file.
91      */
92     AuthData_Ptr getCredFromFile(const Pathname & file);
93
94     /**
95      * Add new global credentials.
96      */
97     void addGlobalCred(const AuthData & cred);
98
99     /**
100      * Add new user credentials.
101      */
102     void addUserCred(const AuthData & cred);
103
104     /**
105      * Add new credentials with user callbacks.
106      *
107      * If the cred->url() contains 'credentials' query parameter, the
108      * credentials will be automatically saved to the specified file using the
109      * \ref saveInFile() method.
110      *
111      * Otherwise a callback will be called asking whether to save to custom
112      * file, or to global or user's credentials catalog.
113      *
114      * \todo Currently no callback is called, credentials are automatically
115      *       saved to user's credentials.cat if no 'credentials' parameter
116      *       has been specified
117      */
118     void addCred(const AuthData & cred);
119
120     /**
121      * Saves any unsaved credentials added via \ref addUserCred() or
122      * \a addGlobalCred() methods.
123      */
124     void save();
125
126     /**
127      * Saves given \a cred to global credentials file.
128      *
129      * \note Use this method to add just one piece of credentials. To add
130      *       multiple items at once, use addGlobalCred() followed
131      *       by save()
132      */
133     void saveInGlobal(const AuthData & cred);
134
135     /**
136      * Saves given \a cred to user's credentials file.
137      *
138      * \note Use this method to add just one piece of credentials. To add
139      *       multiple items at once, use addUserCred() followed
140      *       by save()
141      */
142     void saveInUser(const AuthData & cred);
143
144     /**
145      * Saves given \a cred to user specified credentials file.
146      *
147      * If the credFile path is absolute, it will be saved at that precise
148      * location. If \a credFile is just a filename, it will be saved
149      * in \ref CredManagerOptions::customCredFileDir. Otherwise the current
150      * working directory will be prepended to the file path.
151      */
152     void saveInFile(const AuthData &, const Pathname & credFile);
153
154     /**
155      * Remove all global or user credentials from memory and disk.
156      *
157      * \param global  Whether to remove global or user credentials.
158      */
159     void clearAll(bool global = false);
160
161
162     CredentialIterator credsGlobalBegin() const;
163     CredentialIterator credsGlobalEnd()   const;
164     CredentialSize     credsGlobalSize()  const;
165     bool               credsGlobalEmpty() const;
166
167     CredentialIterator credsUserBegin() const;
168     CredentialIterator credsUserEnd()   const;
169     CredentialSize     credsUserSize()  const;
170     bool               credsUserEmpty() const;
171
172     struct Impl;
173   private:
174     RW_pointer<Impl> _pimpl;
175   };
176   //////////////////////////////////////////////////////////////////////
177
178
179     ////////////////////////////////////////////////////////////////////
180   } // media
181   //////////////////////////////////////////////////////////////////////
182   ////////////////////////////////////////////////////////////////////
183 } // zypp
184 //////////////////////////////////////////////////////////////////////
185
186 #endif /* ZYPP_MEDIA_CREDENTIALMANAGER_H */
187