KIOSlave
httpauthentication.h
Go to the documentation of this file.00001 /* This file is part of the KDE libraries 00002 Copyright (C) 2008, 2009 Andreas Hartmetz <ahartmetz@gmail.com> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 Boston, MA 02110-1301, USA. 00018 */ 00019 00020 #ifndef HTTPAUTHENTICATION_H 00021 #define HTTPAUTHENTICATION_H 00022 00023 #ifndef HTTP_H_ // if we're included from http.cpp all necessary headers are already included 00024 #include <QtCore/QByteArray> 00025 #include <QtCore/QString> 00026 #include <QtCore/QList> 00027 #include <kio/authinfo.h> 00028 #endif 00029 00030 namespace KIO { 00031 class AuthInfo; 00032 } 00033 00034 class KAbstractHttpAuthentication 00035 { 00036 public: 00037 KAbstractHttpAuthentication() 00038 { 00039 reset(); 00040 } 00041 00042 static QByteArray bestOffer(const QList<QByteArray> &offers); 00043 static KAbstractHttpAuthentication *newAuth(const QByteArray &offer); 00044 00045 virtual ~KAbstractHttpAuthentication() {} 00046 00047 // reset to state after default construction 00048 void reset(); 00049 // the authentication scheme: "Digest", "Basic", "NTLM" 00050 virtual QByteArray scheme() const = 0; 00051 // initiate next step with challenge string c 00052 void setChallenge(const QByteArray &c, const KUrl &resource, const QByteArray &httpMethod); 00053 // KIO compatible data to find cached credentials. Note that username and/or password 00054 // as well as UI text will NOT be filled in. 00055 virtual void fillKioAuthInfo(KIO::AuthInfo *ai) const = 0; 00056 // what to do in response to challenge 00057 virtual void generateResponse(const QString &user, 00058 const QString &password) = 0; 00059 00060 // the following accessors contain useful data after 00061 // clients process the following fields top to bottom: highest priority is on top 00062 00063 // malformed challenge and similar problems - it is advisable to reconnect 00064 bool isError() const { return m_isError; } 00065 // do not assume wrong credentials if authentication does not succeed. 00066 // another roundtrip is needed for internal reasons. 00067 bool retryWithSameCredentials() const { return m_retryWithSameCredentials; } 00068 // force keep-alive connection because the authentication method requires it 00069 bool forceKeepAlive() const { return m_forceKeepAlive; } 00070 // force disconnection because the authentication method requires it 00071 bool forceDisconnect() const { return m_forceDisconnect; } 00072 00073 // insert this into the next request header after "Authorization: " or "Proxy-Authorization: " 00074 QString headerFragment() const { return m_headerFragment; } 00075 // this is mainly for GUI shown to the user 00076 QString realm() const; 00077 00078 protected: 00079 void authInfoBoilerplate(KIO::AuthInfo *a) const; 00080 void generateResponseCommon(const QString &user, const QString &password); 00081 00082 QByteArray m_scheme; // this is parsed from the header and not necessarily == scheme(). 00083 QList<QByteArray> m_challenge; 00084 KUrl m_resource; 00085 QByteArray m_httpMethod; 00086 00087 bool m_isError; 00088 bool m_retryWithSameCredentials; 00089 bool m_forceKeepAlive; 00090 bool m_forceDisconnect; 00091 QString m_headerFragment; 00092 00093 QString m_username; 00094 QString m_password; 00095 }; 00096 00097 00098 class KHttpBasicAuthentication : public KAbstractHttpAuthentication 00099 { 00100 public: 00101 virtual QByteArray scheme() const; 00102 virtual void fillKioAuthInfo(KIO::AuthInfo *ai) const; 00103 virtual void generateResponse(const QString &user, const QString &password); 00104 private: 00105 friend class KAbstractHttpAuthentication; 00106 KHttpBasicAuthentication() 00107 : KAbstractHttpAuthentication() {} 00108 }; 00109 00110 00111 class KHttpDigestAuthentication : public KAbstractHttpAuthentication 00112 { 00113 public: 00114 virtual QByteArray scheme() const; 00115 virtual void fillKioAuthInfo(KIO::AuthInfo *ai) const; 00116 virtual void generateResponse(const QString &user, const QString &password); 00117 private: 00118 friend class KAbstractHttpAuthentication; 00119 KHttpDigestAuthentication() 00120 : KAbstractHttpAuthentication() {} 00121 }; 00122 00123 00124 class KHttpNtlmAuthentication : public KAbstractHttpAuthentication 00125 { 00126 public: 00127 virtual QByteArray scheme() const; 00128 virtual void fillKioAuthInfo(KIO::AuthInfo *ai) const; 00129 virtual void generateResponse(const QString &user, const QString &password); 00130 private: 00131 friend class KAbstractHttpAuthentication; 00132 KHttpNtlmAuthentication() 00133 : KAbstractHttpAuthentication() {} 00134 }; 00135 00136 #endif // HTTPAUTHENTICATION_H