24 const std::string& password,
26 : user_(user), user_file_(
""), password_(password),
27 password_file_(
""), password_file_only_(false) {
34 const std::string& user_file,
35 const std::string& password,
36 const std::string& password_file,
37 bool password_file_only,
39 : user_(user), user_file_(user_file), password_(password),
40 password_file_(password_file), password_file_only_(password_file_only) {
54 if (!password_file_.empty()) {
61 if (!password_file_only_) {
62 if (!user_file_.empty()) {
74 const std::string& user_file,
75 const std::string& password,
76 const std::string& password_file,
77 bool password_file_only,
81 password_file, password_file_only,
94 return (map_.empty());
102 if (path.empty() || (path[path.size() - 1] !=
'/')) {
106 if (file_name.empty() || (file_name[0] !=
'/')) {
109 path += file_name.substr(1);
137 for (
auto const& client : list_) {
138 clients->add(client.toElement());
140 result->set(
"clients", clients);
152 << config->getPosition() <<
")");
159 << config->getPosition() <<
")");
163 << type->getPosition() <<
")");
165 if (type->stringValue() !=
"basic") {
167 <<
"supported: type is '" << type->stringValue()
168 <<
"' not 'basic' (" << type->getPosition() <<
")");
176 << realm->getPosition() <<
")");
186 << directory->getPosition() <<
")");
193 if (user_context_cfg) {
196 << user_context_cfg->getPosition() <<
")");
208 << clients->getPosition() <<
")");
212 for (
auto const& client : clients->listValue()) {
215 << client->getPosition() <<
")");
224 << password_cfg->getPosition() <<
")");
226 password = password_cfg->stringValue();
230 string password_file;
232 if (password_file_cfg) {
235 << password_cfg->getPosition()
236 <<
") and password-file ("
237 << password_file_cfg->getPosition()
238 <<
") are mutually exclusive");
242 << password_file_cfg->getPosition() <<
")");
244 password_file = password_file_cfg->stringValue();
249 bool password_file_only =
false;
250 if (!user_cfg && !user_file_cfg) {
251 if (password_file_cfg) {
252 password_file_only =
true;
255 <<
"items (" << client->getPosition() <<
")");
264 <<
") and user-file ("
265 << user_file_cfg->getPosition()
266 <<
") are mutually exclusive");
270 << user_cfg->getPosition() <<
")");
272 user = user_cfg->stringValue();
275 << user_cfg->getPosition() <<
")");
277 if (user.find(
':') != string::npos) {
279 << user <<
"' (" << user_cfg->getPosition() <<
")");
288 << user_file_cfg->getPosition() <<
")");
290 user_file = user_file_cfg->stringValue();
294 <<
"from user-file '" << user_file <<
"' ("
295 << user_file_cfg->getPosition() <<
")");
297 if (user.find(
':') != string::npos) {
299 <<
"from user-file '" << user_file <<
"' ("
300 << user_file_cfg->getPosition() <<
")");
305 if (password_file_cfg) {
306 if (password_file_only) {
308 auto pos = content.find(
':');
309 if (pos == string::npos) {
311 <<
"in password-file '" << password_file <<
"' ("
312 << password_file_cfg->getPosition() <<
")");
314 user = content.substr(0, pos);
315 password = content.substr(pos + 1);
326 << user_context->getPosition() <<
")");
332 add(user, user_file, password, password_file, password_file_only,
334 }
catch (
const std::exception& ex) {
336 << client->getPosition() <<
")");
345 bool authentic =
false;
346 if (credentials.empty()) {
349 string value = request->getHeaderValue(
"Authorization");
352 if (value.size() < 8) {
356 string scheme = value.substr(0, 5);
358 if (scheme !=
"basic") {
362 value = value.substr(5);
365 auto const it = credentials.find(value);
366 if (it != credentials.end()) {
370 request->setBasicAuth(it->second);
387 const string& scheme =
"Basic";
391 response->context()->headers_.push_back(
393 scheme +
" realm=\"" + realm +
"\""));
394 response->finalize();
395 return (boost::dynamic_pointer_cast<HttpResponseJson>(response));
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
static ElementPtr create(const Position &pos=ZERO_POSITION())
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
static ElementPtr createList(const Position &pos=ZERO_POSITION())
Creates an empty ListElement type ElementPtr.
To be removed. Please use ConfigError instead.
Basic HTTP authentication client configuration.
BasicHttpAuthClient(const std::string &user, const std::string &password, const isc::data::ConstElementPtr &user_context)
Constructor (legacy).
virtual isc::data::ElementPtr toElement() const
Unparses basic HTTP authentication client configuration.
std::string getFileContent(const std::string &file_name) const
Get the content of {directory}/{file-name} regular file.
virtual isc::data::ElementPtr toElement() const
Unparses basic HTTP authentication configuration.
void add(const std::string &user, const std::string &user_file, const std::string &password, const std::string &password_file, bool password_file_only=false, const isc::data::ConstElementPtr &user_context=isc::data::ConstElementPtr())
Add a client configuration.
void parse(const isc::data::ConstElementPtr &config)
Parses basic HTTP authentication configuration.
virtual isc::http::HttpResponseJsonPtr checkAuth(const isc::http::HttpResponseCreator &creator, const isc::http::HttpRequestPtr &request) const
Validate HTTP request.
virtual void clear()
Clear configuration.
virtual bool empty() const
Empty predicate.
const BasicHttpAuthMap & getCredentialMap() const
Returns the credential and user id map.
Represents a basic HTTP authentication.
const std::string & getCredential() const
Returns the credential (base64 of the UTF-8 secret).
const std::string & getRealm() const
Returns the realm.
const std::string & getDirectory() const
Returns the common part for file paths (usually a directory).
void setDirectory(const std::string &directory)
Set the common part for file paths (usually a directory).
void setRealm(const std::string &realm)
Set the realm.
static bool recordBasicAuth_
Record basic auth.
Specifies an interface for classes creating HTTP responses from HTTP requests.
virtual HttpResponsePtr createStockHttpResponse(const HttpRequestPtr &request, const HttpStatusCode &status_code) const =0
Creates implementation specific HTTP response.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
#define LOG_INFO(LOGGER, MESSAGE)
Macro to conveniently test info output and log it.
boost::shared_ptr< const Element > ConstElementPtr
boost::shared_ptr< Element > ElementPtr
std::unordered_map< std::string, std::string > BasicHttpAuthMap
Type of basic HTTP authentication credential and user id map, e.g.
const isc::log::MessageID HTTP_CLIENT_REQUEST_NOT_AUTHORIZED
boost::shared_ptr< HttpResponseJson > HttpResponseJsonPtr
Pointer to the HttpResponseJson object.
const isc::log::MessageID HTTP_CLIENT_REQUEST_BAD_AUTH_HEADER
const isc::log::MessageID HTTP_CLIENT_REQUEST_AUTHORIZED
boost::shared_ptr< HttpResponse > HttpResponsePtr
Pointer to the HttpResponse object.
const isc::log::MessageID HTTP_CLIENT_REQUEST_NO_AUTH_HEADER
boost::shared_ptr< HttpRequest > HttpRequestPtr
Pointer to the HttpRequest object.
isc::log::Logger auth_logger("auth")
Defines the logger used by the HTTP authentication.
string getContent(string const &file_name)
Get the content of a regular file.
void lowercase(string &text)
Convert string to lowercase.
string trim(const string &input)
Trim leading and trailing spaces.
Defines the logger used by the top-level component of kea-lfc.
void contextToElement(data::ElementPtr map) const
Merge unparse a user_context object.
void setContext(const data::ConstElementPtr &ctx)
Sets user context.