//
// HTTPCredentials.cpp
//
// $Id: //poco/1.4/Net/src/HTTPCredentials.cpp#2 $
//
// Library: Net
// Package: HTTP
// Module:	HTTPCredentials
//
// Copyright (c) 2011, Anton V. Yabchinskiy (arn at bestmx dot ru).
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
// 
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//


#include "Poco/Net/HTTPAuthenticationParams.h"
#include "Poco/Net/HTTPBasicCredentials.h"
#include "Poco/Net/HTTPCredentials.h"
#include "Poco/Net/HTTPRequest.h"
#include "Poco/Net/HTTPResponse.h"
#include "Poco/Net/NetException.h"
#include "Poco/String.h"
#include "Poco/Ascii.h"
#include "Poco/URI.h"


using Poco::icompare;


namespace Poco {
namespace Net {


HTTPCredentials::HTTPCredentials()
{
}


HTTPCredentials::HTTPCredentials(const std::string& username, const std::string& password):
	_digest(username, password)
{
}


HTTPCredentials::~HTTPCredentials()
{
}


void HTTPCredentials::fromUserInfo(const std::string& userInfo)
{
	std::string username;
	std::string password;

	extractCredentials(userInfo, username, password);
	setUsername(username);
	setPassword(password);
	// TODO: Reset digest state?
}


void HTTPCredentials::fromURI(const URI& uri)
{
	std::string username;
	std::string password;

	extractCredentials(uri, username, password);
	setUsername(username);
	setPassword(password);
	// TODO: Reset digest state?
}


void HTTPCredentials::authenticate(HTTPRequest& request, const HTTPResponse& response)
{
	for (HTTPResponse::ConstIterator iter = response.find("WWW-Authenticate"); iter != response.end(); ++iter)
	{
		if (isBasicCredentials(iter->second)) 
		{
			HTTPBasicCredentials(_digest.getUsername(), _digest.getPassword()).authenticate(request);
			return;
		} 
		else if (isDigestCredentials(iter->second)) 
		{
			_digest.authenticate(request, HTTPAuthenticationParams(iter->second.substr(7)));
			return;
		}
	}
}


void HTTPCredentials::updateAuthInfo(HTTPRequest& request)
{
	if (request.has(HTTPRequest::AUTHORIZATION)) 
	{
		const std::string& authorization = request.get(HTTPRequest::AUTHORIZATION);

		if (isBasicCredentials(authorization)) 
		{
			HTTPBasicCredentials(_digest.getUsername(), _digest.getPassword()).authenticate(request);
		} 
		else if (isDigestCredentials(authorization)) 
		{
			_digest.updateAuthInfo(request);
		}
	}
}


bool HTTPCredentials::isBasicCredentials(const std::string& header)
{
	return icompare(header, 0, 5, "Basic") == 0 && (header.size() > 5 ? Poco::Ascii::isSpace(header[5]) : true);
}


bool HTTPCredentials::isDigestCredentials(const std::string& header)
{
	return icompare(header, 0, 6, "Digest") == 0 && (header.size() > 6 ? Poco::Ascii::isSpace(header[6]) : true);
}


bool HTTPCredentials::hasBasicCredentials(const HTTPRequest& request)
{
	return request.has(HTTPRequest::AUTHORIZATION) && isBasicCredentials(request.get(HTTPRequest::AUTHORIZATION));
}


bool HTTPCredentials::hasDigestCredentials(const HTTPRequest& request)
{
	return request.has(HTTPRequest::AUTHORIZATION) && isDigestCredentials(request.get(HTTPRequest::AUTHORIZATION));
}


void HTTPCredentials::extractCredentials(const std::string& userInfo, std::string& username, std::string& password)
{
	const std::string::size_type p = userInfo.find(':');

	if (p != std::string::npos) 
	{
		username.assign(userInfo, 0, p);
		password.assign(userInfo, p + 1, std::string::npos);
	} 
	else 
	{
		username.assign(userInfo);
		password.clear();
	}
}


void HTTPCredentials::extractCredentials(const Poco::URI& uri, std::string& username, std::string& password)
{
	if (!uri.getUserInfo().empty()) 
	{
		extractCredentials(uri.getUserInfo(), username, password);
	}
}


} } // namespace Poco::Net
