File indexing completed on 2024-04-14 03:52:48

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      */
0071     ~AuthInfo();
0072 
0073     /**
0074      * Custom assignment operator.
0075      */
0076     AuthInfo &operator=(const AuthInfo &info);
0077 
0078     /**
0079      * Use this method to check if the object was modified.
0080      * @return true if the object has been modified
0081      */
0082     bool isModified() const;
0083 
0084     /**
0085      * Use this method to indicate that this object has been modified.
0086      * @param flag true to mark the object as modified, false to clear
0087      */
0088     void setModified(bool flag);
0089 
0090     /**
0091      * The URL for which authentication is to be stored.
0092      *
0093      * This field is required when attempting to cache authorization
0094      * and retrieve it.  However, it is not needed when prompting
0095      * the user for authorization info.
0096      *
0097      * This setting is @em required except when prompting the
0098      * user for password.
0099      */
0100     QUrl url;
0101 
0102     /**
0103      * This is @em required for caching.
0104      */
0105     QString username;
0106 
0107     /**
0108      * This is @em required for caching.
0109      */
0110     QString password;
0111 
0112     /**
0113      * Information to be displayed when prompting
0114      * the user for authentication information.
0115      *
0116      * @note If this field is not set, the authentication
0117      *    dialog simply displays the preset default prompt.
0118      *
0119      * This setting is @em optional and empty by default.
0120      */
0121     QString prompt;
0122 
0123     /**
0124      * The text to displayed in the title bar of
0125      * the password prompting dialog.
0126      *
0127      * @note If this field is not set, the authentication
0128      *    dialog simply displays the preset default caption.
0129      *
0130      * This setting is @em optional and empty by default.
0131      */
0132     QString caption;
0133 
0134     /**
0135      * Additional comment to be displayed when prompting
0136      * the user for authentication information.
0137      *
0138      * This field allows you to display a short (no more than
0139      * 80 characters) extra description in the password prompt
0140      * dialog.  For example, this field along with the
0141      * commentLabel can be used to describe the server that
0142      * requested the authentication:
0143      *
0144      *  \code
0145      *  Server:   Squid Proxy @ foo.com
0146      *  \endcode
0147      *
0148      * where "Server:" is the commentLabel and the rest is the
0149      * actual comment.  Note that it is always better to use
0150      * the @p commentLabel field as it will be placed properly
0151      * in the dialog rather than to include it within the actual
0152      * comment.
0153      *
0154      * This setting is @em optional and empty by default.
0155      */
0156     QString comment;
0157 
0158     /**
0159      * Descriptive label to be displayed in front of the
0160      * comment when prompting the user for password.
0161      *
0162      * This setting is @em optional and only applicable when
0163      * the comment field is also set.
0164      */
0165     QString commentLabel;
0166 
0167     /**
0168      * A unique identifier that allows caching of multiple
0169      * passwords for different resources in the same server.
0170      *
0171      * Mostly this setting is applicable to the HTTP protocol
0172      * whose authentication scheme explicitly defines the use
0173      * of such a unique key.  However, any protocol that can
0174      * generate or supply a unique id can effectively use it
0175      * to distinguish passwords.
0176      *
0177      * This setting is @em optional and not set by default.
0178      */
0179     QString realmValue;
0180 
0181     /**
0182      * Field to store any extra authentication information for
0183      * protocols that need it.
0184      *
0185      * This setting is @em optional and mostly applicable for HTTP
0186      * protocol.  However, any protocol can make use of it to
0187      * store extra info.
0188      */
0189     QString digestInfo;
0190 
0191     /**
0192      * Flag that, if set, indicates whether a path match should be
0193      * performed when requesting for cached authorization.
0194      *
0195      * A path is deemed to be a match if it is equal to or is a subset
0196      * of the cached path.  For example, if stored path is "/foo/bar"
0197      * and the request's path set to "/foo/bar/acme", then it is a match
0198      * whereas it would not if the request's path was set to "/foo".
0199      *
0200      * This setting is @em optional and false by default.
0201      */
0202     bool verifyPath;
0203 
0204     /**
0205      * Flag which if set forces the username field to be read-only.
0206      *
0207      * This setting is @em optional and false by default.
0208      */
0209     bool readOnly;
0210 
0211     /**
0212      * Flag to indicate the persistence of the given password.
0213      *
0214      * This is a two-way flag, when set before calling openPasswordDialog
0215      * it makes the "keep Password" check box visible to the user.
0216      * In return the flag will indicate the state of the check box.
0217      * By default if the flag is checked the password will be cached
0218      * for the entire life of the current KDE session otherwise the
0219      * cached password is deleted right after the application using
0220      * it has been closed.
0221      */
0222     bool keepPassword;
0223 
0224     /**
0225      * Flags for extra fields
0226      */
0227     enum FieldFlags {
0228         ExtraFieldNoFlags = 0,
0229         ExtraFieldReadOnly = 1 << 1,
0230         ExtraFieldMandatory = 1 << 2,
0231     };
0232 
0233     /**
0234      * Set Extra Field Value.
0235      * Currently supported extra-fields:
0236      *    "domain" (QString),
0237      *    "anonymous" (bool)
0238      * Setting it to an invalid QVariant() will disable the field.
0239      * Extra Fields are disabled by default.
0240      */
0241     void setExtraField(const QString &fieldName, const QVariant &value);
0242 
0243     /**
0244      * Set Extra Field Flags
0245      */
0246     void setExtraFieldFlags(const QString &fieldName, const FieldFlags flags);
0247 
0248     /**
0249      * Get Extra Field Value
0250      * Check QVariant::isValid() to find out if the field exists.
0251      */
0252     QVariant getExtraField(const QString &fieldName) const;
0253 
0254     /**
0255      * Get Extra Field Flags
0256      */
0257     AuthInfo::FieldFlags getExtraFieldFlags(const QString &fieldName) const;
0258 
0259     /**
0260      * Register the meta-types for AuthInfo. This is called from
0261      * AuthInfo's constructor but needed by daemons on the D-Bus such
0262      * as kpasswdserver.
0263      */
0264     static void registerMetaTypes();
0265 
0266 protected:
0267     bool modified;
0268 
0269 private:
0270     friend class ::KIO::AuthInfoPrivate;
0271     std::unique_ptr<AuthInfoPrivate> const d;
0272 };
0273 
0274 KIOCORE_EXPORT QDataStream &operator<<(QDataStream &s, const AuthInfo &a);
0275 KIOCORE_EXPORT QDataStream &operator>>(QDataStream &s, AuthInfo &a);
0276 
0277 KIOCORE_EXPORT QDBusArgument &operator<<(QDBusArgument &argument, const AuthInfo &a);
0278 KIOCORE_EXPORT const QDBusArgument &operator>>(const QDBusArgument &argument, AuthInfo &a);
0279 }
0280 
0281 Q_DECLARE_METATYPE(KIO::AuthInfo)
0282 
0283 #endif