25 const std::string& password,
27 : user_(user), user_file_(
""), password_(password),
28 password_file_(
""), password_file_only_(false) {
35 const std::string& user_file,
36 const std::string& password,
37 const std::string& password_file,
38 bool password_file_only,
40 : user_(user), user_file_(user_file), password_(password),
41 password_file_(password_file), password_file_only_(password_file_only) {
55 if (!password_file_.empty()) {
62 if (!password_file_only_) {
63 if (!user_file_.empty()) {
75 const std::string& user_file,
76 const std::string& password,
77 const std::string& password_file,
78 bool password_file_only,
82 password_file, password_file_only,
95 return (map_.empty());
103 if (path.empty() || (path[path.size() - 1] !=
'/')) {
107 if (file_name.empty() || (file_name[0] !=
'/')) {
110 path += file_name.substr(1);
138 for (
auto const& client : list_) {
139 clients->add(client.toElement());
141 result->set(
"clients", clients);
153 <<
config->getPosition() <<
")");
160 <<
config->getPosition() <<
")");
164 << type->getPosition() <<
")");
166 if (type->stringValue() !=
"basic") {
168 <<
"supported: type is '" << type->stringValue()
169 <<
"' not 'basic' (" << type->getPosition() <<
")");
177 << realm->getPosition() <<
")");
187 << directory->getPosition() <<
")");
194 if (user_context_cfg) {
197 << user_context_cfg->getPosition() <<
")");
209 << clients->getPosition() <<
")");
213 for (
auto const& client : clients->listValue()) {
216 << client->getPosition() <<
")");
225 << password_cfg->getPosition() <<
")");
227 password = password_cfg->stringValue();
232 "password must not be a default one ("
233 << password_cfg->getPosition() <<
")");
238 << password_cfg->getPosition() <<
")");
241 .arg(password_cfg->getPosition().str());
246 string password_file;
248 if (password_file_cfg) {
251 << password_cfg->getPosition()
252 <<
") and password-file ("
253 << password_file_cfg->getPosition()
254 <<
") are mutually exclusive");
258 << password_file_cfg->getPosition() <<
")");
260 password_file = password_file_cfg->stringValue();
265 bool password_file_only =
false;
266 if (!user_cfg && !user_file_cfg) {
267 if (password_file_cfg) {
268 password_file_only =
true;
271 <<
"items (" << client->getPosition() <<
")");
280 <<
") and user-file ("
281 << user_file_cfg->getPosition()
282 <<
") are mutually exclusive");
286 << user_cfg->getPosition() <<
")");
288 user = user_cfg->stringValue();
291 << user_cfg->getPosition() <<
")");
293 if (user.find(
':') != string::npos) {
295 << user <<
"' (" << user_cfg->getPosition() <<
")");
300 << user_cfg->getPosition() <<
")");
303 .arg(user_cfg->getPosition().str());
312 << user_file_cfg->getPosition() <<
")");
314 user_file = user_file_cfg->stringValue();
318 <<
"from user-file '" << user_file <<
"' ("
319 << user_file_cfg->getPosition() <<
")");
321 if (user.find(
':') != string::npos) {
323 <<
"from user-file '" << user_file <<
"' ("
324 << user_file_cfg->getPosition() <<
")");
329 if (password_file_cfg) {
330 if (password_file_only) {
332 auto pos = content.find(
':');
333 if (pos == string::npos) {
335 <<
"in password-file '" << password_file <<
"' ("
336 << password_file_cfg->getPosition() <<
")");
338 user = content.substr(0, pos);
339 password = content.substr(pos + 1);
350 << user_context->getPosition() <<
")");
356 add(user, user_file, password, password_file, password_file_only,
358 }
catch (
const std::exception& ex) {
360 << client->getPosition() <<
")");
369 bool authentic =
false;
370 if (credentials.empty()) {
373 string value = request->getHeaderValue(
"Authorization");
376 if (value.size() < 8) {
380 string scheme = value.substr(0, 5);
382 if (scheme !=
"basic") {
386 value = value.substr(5);
389 auto const it = credentials.find(value);
390 if (it != credentials.end()) {
394 request->setBasicAuth(it->second);
411 const string& scheme =
"Basic";
415 response->context()->headers_.push_back(
417 scheme +
" realm=\"" + realm +
"\""));
418 response->finalize();
419 return (boost::dynamic_pointer_cast<HttpResponseJson>(response));
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.
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.
Exception thrown on attempt to use a default credential.
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.
static bool shouldEnforceSecurity()
Indicates security checks should be enforced.
#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_PASSWORD_SECURITY_WARNING
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_USER_SECURITY_WARNING
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.
static void check(const std::string &value)
Check if the value is a default credential.
void contextToElement(data::ElementPtr map) const
Merge unparse a user_context object.
void setContext(const data::ConstElementPtr &ctx)
Sets user context.