defuze.me
Client
|
00001 #include "authenticator.hpp" 00002 #include "logger.hpp" 00003 00004 using namespace Network; 00005 00006 Authenticator::Authenticator(NetworkCore& net) : network(net), authenticating(false) 00007 { 00008 setParamsName("authenticator"); 00009 } 00010 00011 Authenticator::~Authenticator() 00012 { 00013 } 00014 00015 void Authenticator::defineParams() 00016 { 00017 } 00018 00019 void Authenticator::invalidate() 00020 { 00021 setParameter("token", QVariant()); 00022 getToken(); 00023 } 00024 00025 QVariantMap Authenticator::getRadioInfo() const 00026 { 00027 return getParameter("radio", QVariant()).toMap(); 00028 } 00029 00030 void Authenticator::receiveResponse() 00031 { 00032 if (reply->error()) 00033 { 00034 QVariant data = network.apiParser().parse(reply->readAll()); 00035 QString error = data.toMap()["error"].toString(); 00036 Logger::log(QString("Authenticator: can't authenticate: %1").arg(reply->errorString())); 00037 password.clear(); 00038 setParameter("token", QVariant()); 00039 emit failed(error); 00040 } 00041 else 00042 { 00043 QVariant data = network.apiParser().parse(reply->readAll()); 00044 setParameter("token", data.toMap()["token"].toString()); 00045 setParameter("radio", data.toMap()["radio"].toMap()); 00046 Logger::log(QString("Authenticator: new token received: %1").arg(getParameter("login").toString())); 00047 commitParameters(); 00048 emit authenticated(); 00049 } 00050 authenticating = false; 00051 reply->deleteLater(); 00052 } 00053 00054 void Authenticator::authenticate(const QString& login, const QString& password) 00055 { 00056 QNetworkRequest request; 00057 QVariantMap data; 00058 00059 // store login to autofill forms 00060 setParameter("login", login); 00061 this->password = password; 00062 setParameter("token", "pending"); 00063 00064 // build POST data hash 00065 data["login"] = login; 00066 data["password"] = password; 00067 00068 // create request 00069 request = network.apiRequest("session"); 00070 00071 Logger::log(QString("Authenticator: authenticating user %1").arg(login)); 00072 00073 // send request to web service 00074 reply = network.web().post(request, network.apiParser().serialize(data)); 00075 00076 // handle events 00077 connect(reply, SIGNAL(finished()), SLOT(receiveResponse())); 00078 } 00079 00080 void Authenticator::cancel() 00081 { 00082 if (getParameter("token", "").toString() == "pending") 00083 reply->abort(); 00084 setParameter("token", QVariant()); 00085 authenticating = false; 00086 Logger::log("Authenticator: authentication cancelled by user"); 00087 } 00088 00089 const QString Authenticator::getToken() 00090 { 00091 if (!hasToken() && !authenticating) 00092 { 00093 authenticating = true; 00094 // Try to authenticate if no token is stored 00095 if (getParameter("login", "").toString().length() == 0 || password.length() == 0) 00096 { 00097 // If we need some credentials, ask them to the user 00098 // Change the token to a fake value not to request credentials several times 00099 emit needCredentials(getParameter("login", "").toString()); 00100 } 00101 else 00102 { 00103 // If we have cached credentials, just use them 00104 // Change the token to a fake value not to request token several times 00105 authenticate(getParameter("login", "").toString(), password); 00106 } 00107 } 00108 // Remember token fetching is asynchronous, so is no token is stored 00109 // getToken will never return it synchronously 00110 return getParameter("token", "").toString(); 00111 } 00112 00113 bool Authenticator::hasToken() const 00114 { 00115 return getParameter("token", "").toString().length() > 0; 00116 } 00117 00118