aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/poco/Net/src/HTTPAuthenticationParams.cpp
blob: 1c01ac268f4e1c7116b45e3a967fe80b0c198dce (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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
//
// HTTPAuthenticationParams.cpp
//
// Library: Net
// Package: HTTP
// Module:	HTTPAuthenticationParams
//
// 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/Exception.h"
#include "Poco/Net/HTTPAuthenticationParams.h"
#include "Poco/Net/HTTPRequest.h"
#include "Poco/Net/HTTPResponse.h"
#include "Poco/Net/NetException.h"
#include "Poco/String.h"
#include "Poco/Ascii.h"


using Poco::icompare;
using Poco::Ascii;


namespace
{
	bool mustBeQuoted(const std::string& name)
	{
		return
			icompare(name, "cnonce") == 0 ||
			icompare(name, "domain") == 0 ||
			icompare(name, "nonce") == 0 ||
			icompare(name, "opaque") == 0 ||
			icompare(name, "qop") == 0 ||
			icompare(name, "realm") == 0 ||
			icompare(name, "response") == 0 ||
			icompare(name, "uri") == 0 ||
			icompare(name, "username") == 0;
	}


	void formatParameter(std::string& result, const std::string& name, const std::string& value)
	{
		result += name;
		result += '=';
		if (mustBeQuoted(name))
		{
			result += '"';
			result += value;
			result += '"';
		}
		else
		{
			result += value;
		}
	}
}


namespace Poco {
namespace Net {


const std::string HTTPAuthenticationParams::REALM("realm");
const std::string HTTPAuthenticationParams::WWW_AUTHENTICATE("WWW-Authenticate");
const std::string HTTPAuthenticationParams::PROXY_AUTHENTICATE("Proxy-Authenticate");


HTTPAuthenticationParams::HTTPAuthenticationParams()
{
}


HTTPAuthenticationParams::HTTPAuthenticationParams(const std::string& authInfo)
{
	fromAuthInfo(authInfo);
}


HTTPAuthenticationParams::HTTPAuthenticationParams(const HTTPRequest& request)
{
	fromRequest(request);
}


HTTPAuthenticationParams::HTTPAuthenticationParams(const HTTPResponse& response, const std::string& header)
{
	fromResponse(response, header);
}


HTTPAuthenticationParams::~HTTPAuthenticationParams()
{
}


HTTPAuthenticationParams& HTTPAuthenticationParams::operator = (const HTTPAuthenticationParams& authParams)
{
	NameValueCollection::operator = (authParams);

	return *this;
}


void HTTPAuthenticationParams::fromAuthInfo(const std::string& authInfo)
{
	parse(authInfo.begin(), authInfo.end());
}


void HTTPAuthenticationParams::fromRequest(const HTTPRequest& request)
{
	std::string scheme;
	std::string authInfo;

	request.getCredentials(scheme, authInfo);

	if (icompare(scheme, "Digest") != 0)
		throw InvalidArgumentException("Could not parse non-Digest authentication information", scheme);

	fromAuthInfo(authInfo); 
}


void HTTPAuthenticationParams::fromResponse(const HTTPResponse& response, const std::string& header)
{
	NameValueCollection::ConstIterator it = response.find(header);
	if (it == response.end())
		throw NotAuthenticatedException("HTTP response has no authentication header");

	bool found = false;
	while (!found && it != response.end() && icompare(it->first, header) == 0)
	{
		const std::string& header2 = it->second;
		if (icompare(header2, 0, 6, "Basic ") == 0)
		{
			parse(header2.begin() + 6, header2.end());
			found = true;
		}
		else if (icompare(header2, 0, 7, "Digest ") == 0)
		{
			parse(header2.begin() + 7, header2.end());
			found = true;
		}
		++it;
	}
	if (!found) throw NotAuthenticatedException("No Basic or Digest authentication header found");
}


const std::string& HTTPAuthenticationParams::getRealm() const
{
	return get(REALM);
}


void HTTPAuthenticationParams::setRealm(const std::string& realm)
{
	set(REALM, realm);
}


std::string HTTPAuthenticationParams::toString() const
{
	ConstIterator iter = begin();
	std::string result;

	if (iter != end())
	{
		formatParameter(result, iter->first, iter->second);
		++iter;
	}

	for (; iter != end(); ++iter)
	{
		result.append(", ");
		formatParameter(result, iter->first, iter->second);
	}

	return result;
}


void HTTPAuthenticationParams::parse(std::string::const_iterator first, std::string::const_iterator last)
{
	enum State
	{
		STATE_INITIAL = 0x0100,
		STATE_FINAL = 0x0200,

		STATE_SPACE = STATE_INITIAL | 0,
		STATE_TOKEN = 1,
		STATE_EQUALS = 2,
		STATE_VALUE = STATE_FINAL | 3,
		STATE_VALUE_QUOTED = 4,
		STATE_VALUE_ESCAPE = 5,
		STATE_COMMA = STATE_FINAL | 6
	};

	int state = STATE_SPACE;
	std::string token;
	std::string value;

	for (std::string::const_iterator it = first; it != last; ++it)
	{
		switch (state)
		{
		case STATE_SPACE:
			if (Ascii::isAlphaNumeric(*it) || *it == '_' || *it == '-')
			{
				token += *it;
				state = STATE_TOKEN;
			}
			else if (Ascii::isSpace(*it))
			{
				// Skip
			}
			else throw SyntaxException("Invalid authentication information");
			break;

		case STATE_TOKEN:
			if (*it == '=')
			{
				state = STATE_EQUALS;
			}
			else if (Ascii::isAlphaNumeric(*it) || *it == '_' || *it == '-')
			{
				token += *it;
			}
			else throw SyntaxException("Invalid authentication information");
			break;

		case STATE_EQUALS:
			if (Ascii::isAlphaNumeric(*it) || *it == '_')
			{
				value += *it;
				state = STATE_VALUE;
			}
			else if (*it == '"')
			{
				state = STATE_VALUE_QUOTED;
			}
			else throw SyntaxException("Invalid authentication information");
			break;

		case STATE_VALUE_QUOTED:
			if (*it == '\\')
			{
				state = STATE_VALUE_ESCAPE;
			}
			else if (*it == '"')
			{
				add(token, value);
				token.clear();
				value.clear();
				state = STATE_COMMA;
			}
			else
			{
				value += *it;
			}
			break;

		case STATE_VALUE_ESCAPE:
			value += *it;
			state = STATE_VALUE_QUOTED;
			break;

		case STATE_VALUE:
			if (Ascii::isSpace(*it))
			{
				add(token, value);
				token.clear();
				value.clear();
				state = STATE_COMMA;
			}
			else if (*it == ',')
			{
				add(token, value);
				token.clear();
				value.clear();
				state = STATE_SPACE;
			}
			else
			{
				value += *it;
			}
			break;

		case STATE_COMMA:
			if (*it == ',')
			{
				state = STATE_SPACE;
			}
			else if (Ascii::isSpace(*it))
			{
				// Skip
			}
			else throw SyntaxException("Invalid authentication information");
			break;
		}
	}

	if (state == STATE_VALUE)
		add(token, value);

	if (!(state & STATE_FINAL))
		throw SyntaxException("Invalid authentication information");
}


} } // namespace Poco::Net