Authentication from network optics plugin
AnsweredHi Everybody,
I want to ask a question about c++ rest. In order to communicate from network optics to a device, I need to use digest authentication. Could anybody help. Is that the right way to add digest header in Qt QnetworkAccessManager?
Thanks,
QString G_username = QString::fromUtf8(_userName.c_str());
QString G_realm = QString::fromUtf8(_realm.c_str());
QString G_nonce = QString::fromUtf8(_nonce.c_str());
QString G_uri = QString::fromUtf8(urlAfterIp.c_str());
QString G_response = QString::fromUtf8(ha3.c_str());
QString G_qop = QString::fromUtf8(_qop.c_str());
QString G_nc = QString::fromUtf8((_ncString).c_str());
QString G_cnonce = QString::fromUtf8(_cnonce.c_str());
QEventLoop loop;
QNetworkAccessManager* manager = new QNetworkAccessManager();
QNetworkRequest networkRequest;
networkRequest.setUrl(QUrl("http://172.16.101.68/reset"));
networkRequest.setRawHeader(QByteArray("Digest username"), G_username.toUtf8());
networkRequest.setRawHeader(QByteArray("realm"), G_realm.toUtf8());
networkRequest.setRawHeader(QByteArray("nonce"), G_nonce.toUtf8());
networkRequest.setRawHeader(QByteArray("uri"), G_uri.toUtf8());
networkRequest.setRawHeader(QByteArray("algorithm"), "MD5");
networkRequest.setRawHeader(QByteArray("response"), G_response.toUtf8());
networkRequest.setRawHeader(QByteArray("qop"), G_qop.toUtf8());
networkRequest.setRawHeader(QByteArray("nc"), G_nc.toUtf8());
networkRequest.setRawHeader(QByteArray("cnonce"), G_cnonce.toUtf8());
QLabel *label = new QLabel();
QNetworkReply *reply = manager->get(networkRequest);
QObject::connect(reply, SIGNAL(readyRead()),
label, SLOT(slotReadyRead()));
QObject::connect(reply, SIGNAL(finished()),
label, SLOT(slotOnRequestCompleted()));
QObject::connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
label, SLOT(slotOnRequestError(QNetworkReply::NetworkError)));
QObject::connect(reply, SIGNAL(sslErrors(QList<QSslError>)),
label, SLOT(slotSslErrors(QList<QSslError>)));
QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
QByteArray dataReceived = reply->readAll();
QString dataReply(dataReceived);
-
The right format, to add the header, should be:
digestResponse = "Digest username=\"" + _userName + "\"" + ", realm=\"" + _realm + "\"" + ", nonce=\"" + _nonce + "\"" + ", uri=\"" + urlAfterIp +"\"" + ", algorithm=MD5" + ", response=" + "\"" + ha3 +"\"" + ", qop=" + _qop + ", nc=" + _ncString + ", cnonce=\"" +_cnonce + "\"";
networkRequest.setRawHeader(QByteArray("Authorization"), digestResponse.c_str());but it is not working.
0 -
Hi Ali,
In order to communicate from network optics to a device, I need to use digest authentication.
Could you please provide more details about your third party system which you need to communicate with? Understanding your use case scenario might allow me to provide you with more simple solution to your problem.
but it is not working.
Could you please log and show me full reply from your server(one where server provides you with nonce) and your request containing proper Authorization header.
0 -
Hi Anton,
We are working with Bosch Cameras and these devices accepts digest authentication by default. In order to make a connection from Plugin to Bosch devices, digest authentication with C++ has to be written in the plugin. Network optics is using Qt library by default, therefore It is better to write, digest authentication in c++ with Qt library.
Digest authentication is a two way communication by itself. First client asks a token > Server sends the token > And client is connects to device with the combination of token + username + password.
I couldn't find a clear sample of digest authentication in qt. Then I copy from c# language, how digest authentication can be done in detailed way. And I have written it in detailed way in c++(Qt). Below the link shows how digest authentication in c#.
https://stackoverflow.com/questions/3172510/how-can-i-do-digest-authentication-with-httpwebrequest
As I mentioned in my previous post, what I couldn't manage to authorize although I am sending the right string data. I am comparing the string header data with c# and c++. It is the same string. It is working in c# but not working in c++. Below is the code, I am using, is not working.
digestResponse = "Digest username=\"" + _userName + "\"" + ", realm=\"" + _realm + "\"" + ", nonce=\"" + _nonce + "\""
+ ", uri=\"" + urlAfterIp +"\"" + ", algorithm=MD5" + ", response=" + "\"" + ha3 +"\"" + ", qop=" + _qop + ", nc="
+ _ncString + ", cnonce=\"" +_cnonce + "\"";QEventLoop loop;
QNetworkAccessManager* manager = new QNetworkAccessManager();
QNetworkRequest networkRequest;
networkRequest.setUrl(QUrl("http://172.16.101.68/reset"));networkRequest.setRawHeader("Authorization", digestResponse.c_str()); //.is where I send header data****
QLabel *label = new QLabel();
QNetworkReply *reply = manager->get(networkRequest);QObject::connect(reply, SIGNAL(readyRead()),
label, SLOT(slotReadyRead()));
QObject::connect(reply, SIGNAL(finished()),
label, SLOT(slotOnRequestCompleted()));
QObject::connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
label, SLOT(slotOnRequestError(QNetworkReply::NetworkError)));
QObject::connect(reply, SIGNAL(sslErrors(QList<QSslError>)),
label, SLOT(slotSslErrors(QList<QSslError>)));QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));loop.exec();
QByteArray dataReceived = reply->readAll();
QString dataReply(dataReceived)I receive a message unauthorize below
"<HTML><HEAD><TITLE>401 Authorization Required</TITLE></HEAD><BODY><H1>Authorization Required</H1>this server could not verify that you are authorized to access the document you requested. Either you supplied the wrong credentials (e.g., bad password), or your browser doesnt understand how to supply the credentials required.</BODY></HTML>"
Thanks for help
0 -
We are working with Bosch Cameras and these devices accepts digest authentication by default. In order to make a connection from Plugin to Bosch devices, Digest authentication with C++ has to be written in the plugin. Network optics is using Qt library by default, therefore It is better to write, digest authentication in c++ with Qt library.
Just out of curiosity, what kind of info you wish to pull from Bosch Camera?
As I mentioned in my previous post, what I couldn't manage to authorize although I am sending the right string data. I am comparing the string header data with c# and c++. It is the same string. It is working in c# but not working in c++. Below is the code, I am using, is not working.
Sorry, but we are not using QT for generating auth headers, so I'm not able to tell you what's wrong just by viewing at your code. I'd recommend to collect WireShark capture, it would show what exactly is wrong with the Authorization header in your request and will point us at the problem. This article might help you to setup Wireshark capture.
By the way, have you tried using authenticationRequired signal to set login/pass credentials?
https://doc.qt.io/qt-5/qnetworkaccessmanager.html#authenticationRequired
0 -
For my case, I am thinking the worst scenario. If I give up with Qt which library, would you advise me to use. And also which library are you using? I had tried to integrate Poco and Curl but I had difficulties to add these libraries to cmake project.(win 64bit)
Thanks in advance,
0 -
I don't think switching libraries would be helpful. Please collect and show me WireShark capture of communication between your plugin and your camera. It will show which lines in your code causes the issue.
Could you please tell me how you plan to interact with Bosh camera? Latest patch of mediaserver has build-in analytic plugin for Bosch cameras which can receive some metadata from camera. Just want to be sure that you're not developing something that already exists in the product.
0 -
Hi Anton,
Thanks for help,
I found my failure. Wireshark was helpful for me. There is nothing wrong about Qt library and the codes above. In my previous codes, I got a bug in refining received nonce value. That was causing error. Now I achieve the connection. It works properly.
Thanks
0
Please sign in to leave a comment.
Comments
7 comments