From dd6d3fdd1ec5a8f123a0e90455b46733e3c3586f Mon Sep 17 00:00:00 2001 From: roleo Date: Sat, 5 Feb 2022 16:59:00 +0100 Subject: [PATCH] Fix digest authentication --- src/xop/Authenticator.h | 2 +- src/xop/DigestAuthenticator.cpp | 14 ++++---------- src/xop/DigestAuthenticator.h | 11 +++++------ src/xop/RtspConnection.cpp | 2 +- 4 files changed, 11 insertions(+), 18 deletions(-) diff --git a/src/xop/Authenticator.h b/src/xop/Authenticator.h index 63c0d40..a800b0c 100644 --- a/src/xop/Authenticator.h +++ b/src/xop/Authenticator.h @@ -13,7 +13,7 @@ class Authenticator Authenticator() {}; virtual ~Authenticator() {}; - virtual bool Authenticate(std::shared_ptr request, std::string &nonce) = 0; + virtual bool Authenticate(std::shared_ptr request) = 0; virtual size_t GetFailedResponse(std::shared_ptr request, std::shared_ptr buf, size_t size) = 0; private: diff --git a/src/xop/DigestAuthenticator.cpp b/src/xop/DigestAuthenticator.cpp index 7aae4cf..0b43291 100644 --- a/src/xop/DigestAuthenticator.cpp +++ b/src/xop/DigestAuthenticator.cpp @@ -16,11 +16,6 @@ DigestAuthenticator::~DigestAuthenticator() } -std::string DigestAuthenticator::GetNonce() -{ - return md5::generate_nonce(); -} - std::string DigestAuthenticator::GetResponse(std::string nonce, std::string cmd, std::string url) { //md5(md5(: : ) : : md5(:)) @@ -32,13 +27,12 @@ std::string DigestAuthenticator::GetResponse(std::string nonce, std::string cmd, } bool DigestAuthenticator::Authenticate( - std::shared_ptr rtsp_request, - std::string &nonce) + std::shared_ptr rtsp_request) { std::string cmd = rtsp_request->MethodToString[rtsp_request->GetMethod()]; std::string url = rtsp_request->GetRtspUrl(); - if (nonce.size() > 0 && (GetResponse(nonce, cmd, url) == rtsp_request->GetAuthResponse())) { + if (nonce_.size() > 0 && (GetResponse(nonce_, cmd, url) == rtsp_request->GetAuthResponse())) { return true; } else { #if 0 @@ -52,6 +46,6 @@ size_t DigestAuthenticator::GetFailedResponse( std::shared_ptr buf, size_t size) { - std::string nonce = md5::generate_nonce(); - return rtsp_request->BuildUnauthorizedRes(buf.get(), size, realm_.c_str(), nonce.c_str()); + nonce_ = md5::generate_nonce(); + return rtsp_request->BuildUnauthorizedRes(buf.get(), size, realm_.c_str(), nonce_.c_str()); } diff --git a/src/xop/DigestAuthenticator.h b/src/xop/DigestAuthenticator.h index a584cd1..191ad9e 100644 --- a/src/xop/DigestAuthenticator.h +++ b/src/xop/DigestAuthenticator.h @@ -1,8 +1,8 @@ //PHZ //2019-10-6 -#ifndef RTSP_DIGEST_AUTHENTICATION_H -#define RTSP_DIGEST_AUTHENTICATION_H +#ifndef RTSP_DIGEST_AUTHENTICATOR_H +#define RTSP_DIGEST_AUTHENTICATOR_H #include "Authenticator.h" @@ -27,17 +27,16 @@ class DigestAuthenticator : public Authenticator std::string GetPassword() const { return password_; } - std::string GetNonce(); std::string GetResponse(std::string nonce, std::string cmd, std::string url); - bool Authenticate(std::shared_ptr request, std::string &nonce); - size_t GetFailedResponse(std::shared_ptr request, std::shared_ptr buf, size_t size); + bool Authenticate(std::shared_ptr request); + size_t GetFailedResponse(std::shared_ptr request, std::shared_ptr buf, size_t size); private: std::string realm_; std::string username_; std::string password_; - + std::string nonce_; }; } diff --git a/src/xop/RtspConnection.cpp b/src/xop/RtspConnection.cpp index 4ce502a..b0d7b20 100644 --- a/src/xop/RtspConnection.cpp +++ b/src/xop/RtspConnection.cpp @@ -412,7 +412,7 @@ void RtspConnection::HandleCmdGetParamter() bool RtspConnection::HandleAuthentication() { if (authenticator_ != nullptr && !has_auth_) { - if (authenticator_->Authenticate(rtsp_request_, _nonce)) { + if (authenticator_->Authenticate(rtsp_request_)) { has_auth_ = true; } else { std::shared_ptr res(new char[4096], std::default_delete());