aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/poco/Net/src/HTTPCredentials.cpp
blob: ae1d6ea0b620724d1328ee8bd5096e9c6a420456 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//
// HTTPCredentials.cpp
//
// 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.
//
// SPDX-License-Identifier:	BSL-1.0
//


#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);
	_digest.reset();
}


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

	extractCredentials(uri, username, password);
	setUsername(username);
	setPassword(password);
	_digest.reset();
}


void HTTPCredentials::authenticate(HTTPRequest& request, const HTTPResponse& response)
{
	for (HTTPResponse::ConstIterator iter = response.find(HTTPAuthenticationParams::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);
		}
	}
}


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


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

		if (isBasicCredentials(authorization)) 
		{
			HTTPBasicCredentials(_digest.getUsername(), _digest.getPassword()).proxyAuthenticate(request);
		} 
		else if (isDigestCredentials(authorization)) 
		{
			_digest.updateProxyAuthInfo(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));
}


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


bool HTTPCredentials::hasProxyDigestCredentials(const HTTPRequest& request)
{
	return request.has(HTTPRequest::PROXY_AUTHORIZATION) && isDigestCredentials(request.get(HTTPRequest::PROXY_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