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 string password_file;
240 if (password_file_cfg) {
243 << password_cfg->getPosition()
244 <<
") and password-file ("
245 << password_file_cfg->getPosition()
246 <<
") are mutually exclusive");
250 << password_file_cfg->getPosition() <<
")");
252 password_file = password_file_cfg->stringValue();
257 bool password_file_only =
false;
258 if (!user_cfg && !user_file_cfg) {
259 if (password_file_cfg) {
260 password_file_only =
true;
263 <<
"items (" << client->getPosition() <<
")");
272 <<
") and user-file ("
273 << user_file_cfg->getPosition()
274 <<
") are mutually exclusive");
278 << user_cfg->getPosition() <<
")");
280 user = user_cfg->stringValue();
283 << user_cfg->getPosition() <<
")");
285 if (user.find(
':') != string::npos) {
287 << user <<
"' (" << user_cfg->getPosition() <<
")");
296 << user_file_cfg->getPosition() <<
")");
298 user_file = user_file_cfg->stringValue();
302 <<
"from user-file '" << user_file <<
"' ("
303 << user_file_cfg->getPosition() <<
")");
305 if (user.find(
':') != string::npos) {
307 <<
"from user-file '" << user_file <<
"' ("
308 << user_file_cfg->getPosition() <<
")");
313 if (password_file_cfg) {
314 if (password_file_only) {
316 auto pos = content.find(
':');
317 if (pos == string::npos) {
319 <<
"in password-file '" << password_file <<
"' ("
320 << password_file_cfg->getPosition() <<
")");
322 user = content.substr(0, pos);
323 password = content.substr(pos + 1);
334 << user_context->getPosition() <<
")");
340 add(user, user_file, password, password_file, password_file_only,
342 }
catch (
const std::exception& ex) {
344 << client->getPosition() <<
")");
353 bool authentic =
false;
354 if (credentials.empty()) {
357 string value = request->getHeaderValue(
"Authorization");
360 if (value.size() < 8) {
364 string scheme = value.substr(0, 5);
366 if (scheme !=
"basic") {
370 value = value.substr(5);
373 auto const it = credentials.find(value);
374 if (it != credentials.end()) {
378 request->setBasicAuth(it->second);
395 const string& scheme =
"Basic";
399 response->context()->headers_.push_back(
401 scheme +
" realm=\"" + realm +
"\""));
402 response->finalize();
403 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.
Exception thrown on attempt to use a default credential.
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.
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.