
/*---------------------------------------------------------------------------*/
/* webserver_digest_authenticator.h                                          */
/* copyright (c) innovaphone 2016-2022                                       */
/*                                                                           */
/*---------------------------------------------------------------------------*/

inline std::string Trim(const char * str)
{
	std::string s = str == nullptr ? "" : str;
	size_t a = 0, b = s.length() - 1;
	while (a < b && std::isspace(s[a])) ++a;
	b -= a;
	while (b > a && std::isspace(s[b])) --b;
	s = (a > b) ? "" : s.substr(a, b - a + 1);
	return s;
}


class WebserverDigestAuthenticator : public IDigestAuthenticator {
private:
	class HTTPAuthDigest * authDigest = nullptr;
	class WebserverMessageHandler * messageHandler = nullptr;
	
	bool parseSuccess = false;
	std::string authHeaderField;
	std::string requestTarget;
	
public:
	WebserverDigestAuthenticator(HTTPAuthDigest * authDigest);
	~WebserverDigestAuthenticator();
	
	void Init(class WebserverMessageHandler * handler);
	virtual const char * User() override;
	virtual const char * AuthenticationHeaderField() override;
	virtual const char * RequestTarget() override { return this->requestTarget.c_str(); }

	virtual bool IsAuthenticated(const char * user, const char * password) override;
	virtual void SendAuthenticationRequest() override;
	virtual void SendAuthenticationRequest(const char * body, size_t bodySize, wsr_type_t respopnseType = WSP_RESPONSE_HTML) override; 
};