Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix digest authentication #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/xop/Authenticator.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Authenticator
Authenticator() {};
virtual ~Authenticator() {};

virtual bool Authenticate(std::shared_ptr<RtspRequest> request, std::string &nonce) = 0;
virtual bool Authenticate(std::shared_ptr<RtspRequest> request) = 0;
virtual size_t GetFailedResponse(std::shared_ptr<RtspRequest> request, std::shared_ptr<char> buf, size_t size) = 0;

private:
Expand Down
14 changes: 4 additions & 10 deletions src/xop/DigestAuthenticator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(<username>:<realm> : <password>) :<nonce> : md5(<cmd>:<url>))
Expand All @@ -32,13 +27,12 @@ std::string DigestAuthenticator::GetResponse(std::string nonce, std::string cmd,
}

bool DigestAuthenticator::Authenticate(
std::shared_ptr<RtspRequest> rtsp_request,
std::string &nonce)
std::shared_ptr<RtspRequest> 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
Expand All @@ -52,6 +46,6 @@ size_t DigestAuthenticator::GetFailedResponse(
std::shared_ptr<char> 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());
}
11 changes: 5 additions & 6 deletions src/xop/DigestAuthenticator.h
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -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<RtspRequest> request, std::string &nonce);
size_t GetFailedResponse(std::shared_ptr<RtspRequest> request, std::shared_ptr<char> buf, size_t size);
bool Authenticate(std::shared_ptr<RtspRequest> request);
size_t GetFailedResponse(std::shared_ptr<RtspRequest> request, std::shared_ptr<char> buf, size_t size);

private:
std::string realm_;
std::string username_;
std::string password_;

std::string nonce_;
};

}
Expand Down
2 changes: 1 addition & 1 deletion src/xop/RtspConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<char> res(new char[4096], std::default_delete<char[]>());
Expand Down