File indexing completed on 2023-10-03 03:19:51
0001 /* 0002 This file is part of the KDE libraries 0003 SPDX-FileCopyrightText: 2000-2001 Dawit Alemayehu <adawit@kde.org> 0004 0005 SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 0008 #ifndef KIO_AUTHINFO_H 0009 #define KIO_AUTHINFO_H 0010 0011 #include "kiocore_export.h" 0012 0013 #include <QList> 0014 #include <QMap> 0015 #include <QStringList> 0016 #include <QUrl> 0017 #include <QVariant> // Q_DECLARE_METATYPE 0018 0019 #include <memory> 0020 0021 class QDBusArgument; 0022 0023 namespace KIO 0024 { 0025 class AuthInfoPrivate; 0026 0027 /** 0028 * @class KIO::AuthInfo authinfo.h <KIO/AuthInfo> 0029 * 0030 * This class is intended to make it easier to prompt for, cache 0031 * and retrieve authorization information. 0032 * 0033 * When using this class to cache, retrieve or prompt authentication 0034 * information, you only need to set the necessary attributes. For 0035 * example, to check whether a password is already cached, the only 0036 * required information is the URL of the resource and optionally 0037 * whether or not a path match should be performed. Similarly, to 0038 * prompt for password you only need to optionally set the prompt, 0039 * username (if already supplied), comment and commentLabel fields. 0040 * 0041 * <em>SPECIAL NOTE:</em> If you extend this class to add additional 0042 * parameters do not forget to overload the stream insertion and 0043 * extraction operators ("<<" and ">>") so that the added data can 0044 * be correctly serialized. 0045 * 0046 * @short A two way messaging class for passing authentication information. 0047 * @author Dawit Alemayehu <adawit@kde.org> 0048 */ 0049 class KIOCORE_EXPORT AuthInfo 0050 { 0051 KIOCORE_EXPORT friend QDataStream &operator<<(QDataStream &s, const AuthInfo &a); 0052 KIOCORE_EXPORT friend QDataStream &operator>>(QDataStream &s, AuthInfo &a); 0053 0054 KIOCORE_EXPORT friend QDBusArgument &operator<<(QDBusArgument &argument, const AuthInfo &a); 0055 KIOCORE_EXPORT friend const QDBusArgument &operator>>(const QDBusArgument &argument, AuthInfo &a); 0056 0057 public: 0058 /** 0059 * Default constructor. 0060 */ 0061 AuthInfo(); 0062 0063 /** 0064 * Copy constructor. 0065 */ 0066 AuthInfo(const AuthInfo &info); 0067 0068 /** 0069 * Destructor 0070 * @since 4.1 0071 */ 0072 ~AuthInfo(); 0073 0074 /** 0075 * Custom assignment operator. 0076 */ 0077 AuthInfo &operator=(const AuthInfo &info); 0078 0079 /** 0080 * Use this method to check if the object was modified. 0081 * @return true if the object has been modified 0082 */ 0083 bool isModified() const; 0084 0085 /** 0086 * Use this method to indicate that this object has been modified. 0087 * @param flag true to mark the object as modified, false to clear 0088 */ 0089 void setModified(bool flag); 0090 0091 /** 0092 * The URL for which authentication is to be stored. 0093 * 0094 * This field is required when attempting to cache authorization 0095 * and retrieve it. However, it is not needed when prompting 0096 * the user for authorization info. 0097 * 0098 * This setting is @em required except when prompting the 0099 * user for password. 0100 */ 0101 QUrl url; 0102 0103 /** 0104 * This is @em required for caching. 0105 */ 0106 QString username; 0107 0108 /** 0109 * This is @em required for caching. 0110 */ 0111 QString password; 0112 0113 /** 0114 * Information to be displayed when prompting 0115 * the user for authentication information. 0116 * 0117 * @note If this field is not set, the authentication 0118 * dialog simply displays the preset default prompt. 0119 * 0120 * This setting is @em optional and empty by default. 0121 */ 0122 QString prompt; 0123 0124 /** 0125 * The text to displayed in the title bar of 0126 * the password prompting dialog. 0127 * 0128 * @note If this field is not set, the authentication 0129 * dialog simply displays the preset default caption. 0130 * 0131 * This setting is @em optional and empty by default. 0132 */ 0133 QString caption; 0134 0135 /** 0136 * Additional comment to be displayed when prompting 0137 * the user for authentication information. 0138 * 0139 * This field allows you to display a short (no more than 0140 * 80 characters) extra description in the password prompt 0141 * dialog. For example, this field along with the 0142 * commentLabel can be used to describe the server that 0143 * requested the authentication: 0144 * 0145 * \code 0146 * Server: Squid Proxy @ foo.com 0147 * \endcode 0148 * 0149 * where "Server:" is the commentLabel and the rest is the 0150 * actual comment. Note that it is always better to use 0151 * the @p commentLabel field as it will be placed properly 0152 * in the dialog rather than to include it within the actual 0153 * comment. 0154 * 0155 * This setting is @em optional and empty by default. 0156 */ 0157 QString comment; 0158 0159 /** 0160 * Descriptive label to be displayed in front of the 0161 * comment when prompting the user for password. 0162 * 0163 * This setting is @em optional and only applicable when 0164 * the comment field is also set. 0165 */ 0166 QString commentLabel; 0167 0168 /** 0169 * A unique identifier that allows caching of multiple 0170 * passwords for different resources in the same server. 0171 * 0172 * Mostly this setting is applicable to the HTTP protocol 0173 * whose authentication scheme explicitly defines the use 0174 * of such a unique key. However, any protocol that can 0175 * generate or supply a unique id can effectively use it 0176 * to distinguish passwords. 0177 * 0178 * This setting is @em optional and not set by default. 0179 */ 0180 QString realmValue; 0181 0182 /** 0183 * Field to store any extra authentication information for 0184 * protocols that need it. 0185 * 0186 * This setting is @em optional and mostly applicable for HTTP 0187 * protocol. However, any protocol can make use of it to 0188 * store extra info. 0189 */ 0190 QString digestInfo; 0191 0192 /** 0193 * Flag that, if set, indicates whether a path match should be 0194 * performed when requesting for cached authorization. 0195 * 0196 * A path is deemed to be a match if it is equal to or is a subset 0197 * of the cached path. For example, if stored path is "/foo/bar" 0198 * and the request's path set to "/foo/bar/acme", then it is a match 0199 * whereas it would not if the request's path was set to "/foo". 0200 * 0201 * This setting is @em optional and false by default. 0202 */ 0203 bool verifyPath; 0204 0205 /** 0206 * Flag which if set forces the username field to be read-only. 0207 * 0208 * This setting is @em optional and false by default. 0209 */ 0210 bool readOnly; 0211 0212 /** 0213 * Flag to indicate the persistence of the given password. 0214 * 0215 * This is a two-way flag, when set before calling openPasswordDialog 0216 * it makes the "keep Password" check box visible to the user. 0217 * In return the flag will indicate the state of the check box. 0218 * By default if the flag is checked the password will be cached 0219 * for the entire life of the current KDE session otherwise the 0220 * cached password is deleted right after the application using 0221 * it has been closed. 0222 */ 0223 bool keepPassword; 0224 0225 /** 0226 * Flags for extra fields 0227 * @since 4.1 0228 */ 0229 enum FieldFlags { 0230 ExtraFieldNoFlags = 0, 0231 ExtraFieldReadOnly = 1 << 1, 0232 ExtraFieldMandatory = 1 << 2, 0233 }; 0234 0235 /** 0236 * Set Extra Field Value. 0237 * Currently supported extra-fields: 0238 * "domain" (QString), 0239 * "anonymous" (bool) 0240 * Setting it to an invalid QVariant() will disable the field. 0241 * Extra Fields are disabled by default. 0242 * @since 4.1 0243 */ 0244 void setExtraField(const QString &fieldName, const QVariant &value); 0245 0246 /** 0247 * Set Extra Field Flags 0248 * @since 4.1 0249 */ 0250 void setExtraFieldFlags(const QString &fieldName, const FieldFlags flags); 0251 0252 /** 0253 * Get Extra Field Value 0254 * Check QVariant::isValid() to find out if the field exists. 0255 * @since 4.1 0256 */ 0257 QVariant getExtraField(const QString &fieldName) const; 0258 0259 /** 0260 * Get Extra Field Flags 0261 * @since 4.1 0262 */ 0263 AuthInfo::FieldFlags getExtraFieldFlags(const QString &fieldName) const; 0264 0265 /** 0266 * Register the meta-types for AuthInfo. This is called from 0267 * AuthInfo's constructor but needed by daemons on the D-Bus such 0268 * as kpasswdserver. 0269 * @since 4.3 0270 */ 0271 static void registerMetaTypes(); 0272 0273 protected: 0274 bool modified; 0275 0276 private: 0277 friend class ::KIO::AuthInfoPrivate; 0278 std::unique_ptr<AuthInfoPrivate> const d; 0279 }; 0280 0281 KIOCORE_EXPORT QDataStream &operator<<(QDataStream &s, const AuthInfo &a); 0282 KIOCORE_EXPORT QDataStream &operator>>(QDataStream &s, AuthInfo &a); 0283 0284 KIOCORE_EXPORT QDBusArgument &operator<<(QDBusArgument &argument, const AuthInfo &a); 0285 KIOCORE_EXPORT const QDBusArgument &operator>>(const QDBusArgument &argument, AuthInfo &a); 0286 0287 /** 0288 * A Singleton class that provides access to passwords 0289 * stored in .netrc files for automatic login purposes. 0290 * This is only meant to address backward compatibility 0291 * with old automated ftp client style logins... 0292 * 0293 * @short An interface to the ftp .netrc files 0294 * @author Dawit Alemayehu <adawit@kde.org> 0295 */ 0296 class KIOCORE_EXPORT NetRC 0297 { 0298 public: 0299 /** 0300 * Specifies the mode to be used when searching for a 0301 * matching automatic login info for a given site : 0302 * 0303 * @li exactOnly search entries with exact host name matches. 0304 * @li defaultOnly search entries that are specified as "default". 0305 * @li presetOnly search entries that are specified as "preset". 0306 * 0307 * @see lookup 0308 * @see LookUpMode 0309 */ 0310 enum LookUpModeFlag { 0311 exactOnly = 0x0002, 0312 defaultOnly = 0x0004, 0313 presetOnly = 0x0008, 0314 }; 0315 /** 0316 * Stores a combination of #LookUpModeFlag values. 0317 */ 0318 Q_DECLARE_FLAGS(LookUpMode, LookUpModeFlag) 0319 0320 /** 0321 * Contains auto login information. 0322 * @see lookup() 0323 */ 0324 struct AutoLogin { 0325 QString type; 0326 QString machine; 0327 QString login; 0328 QString password; 0329 QMap<QString, QStringList> macdef; 0330 }; 0331 0332 /** 0333 * A reference to the instance of the class. 0334 * @return the class 0335 */ 0336 static NetRC *self(); 0337 0338 /** 0339 * Looks up the @p login information for the given @p url. 0340 * 0341 * @param url the url whose login information will be checked 0342 * @param login the login information will be writte here 0343 * @param userealnetrc if true, use $HOME/.netrc file 0344 * @param type the type of the login. If null, the @p url's protocol 0345 * will be taken 0346 * @param mode the LookUpMode flags (ORed) for the query 0347 */ 0348 bool lookup(const QUrl &url, 0349 AutoLogin &login, 0350 bool userealnetrc = false, 0351 const QString &type = QString(), 0352 LookUpMode mode = LookUpMode(exactOnly) | defaultOnly); 0353 /** 0354 * Reloads the auto login information. 0355 */ 0356 void reload(); 0357 0358 protected: 0359 bool parse(const QString &fileName); 0360 0361 private: 0362 KIOCORE_NO_EXPORT NetRC(); 0363 KIOCORE_NO_EXPORT ~NetRC(); 0364 0365 NetRC(const NetRC &) = delete; 0366 NetRC &operator=(const NetRC &) = delete; 0367 0368 private: 0369 static NetRC *instance; 0370 0371 class NetRCPrivate; 0372 std::unique_ptr<NetRCPrivate> const d; 0373 }; 0374 0375 Q_DECLARE_OPERATORS_FOR_FLAGS(NetRC::LookUpMode) 0376 0377 } 0378 0379 Q_DECLARE_METATYPE(KIO::AuthInfo) 0380 0381 #endif