22 BasicHttpAuthClient::BasicHttpAuthClient(
const std::string& user,
23 const std::string& password,
25 : user_(user), password_(password) {
39 result->set(
"user", Element::create(user_));
42 result->set(
"password", Element::create(password_));
49 const std::string& password,
64 return (map_.empty());
75 result->set(
"type", Element::create(
string(
"basic")));
78 result->set(
"realm", Element::create(
getRealm()));
82 for (
auto client : list_) {
83 clients->add(client.toElement());
85 result->set(
"clients", clients);
95 if (config->getType() != Element::map) {
97 << config->getPosition() <<
")");
104 << config->getPosition() <<
")");
106 if (type->getType() != Element::string) {
108 << type->getPosition() <<
")");
110 if (type->stringValue() !=
"basic") {
112 <<
"supported: type is '" << type->stringValue()
113 <<
"' not 'basic' (" << type->getPosition() <<
")");
119 if (realm->getType() != Element::string) {
121 << realm->getPosition() <<
")");
128 if (user_context_cfg) {
129 if (user_context_cfg->getType() != Element::map) {
131 << user_context_cfg->getPosition() <<
")");
141 if (clients->getType() != Element::list) {
143 << clients->getPosition() <<
")");
147 for (
auto client : clients->listValue()) {
148 if (client->getType() != Element::map) {
150 << client->getPosition() <<
")");
157 << client->getPosition() <<
")");
159 if (user_cfg->getType() != Element::string) {
161 << user_cfg->getPosition() <<
")");
163 string user = user_cfg->stringValue();
166 << user_cfg->getPosition() <<
")");
168 if (user.find(
':') != string::npos) {
170 << user <<
"' (" << user_cfg->getPosition() <<
")");
177 if (password_cfg->getType() != Element::string) {
179 << password_cfg->getPosition() <<
")");
181 password = password_cfg->stringValue();
187 if (user_context->getType() != Element::map) {
189 << user_context->getPosition() <<
")");
195 add(user, password, user_context);
196 }
catch (
const std::exception& ex) {
198 << client->getPosition() <<
")");
207 bool authentic =
false;
208 if (credentials.empty()) {
211 string value = request->getHeaderValue(
"Authorization");
214 if (value.size() < 8) {
218 string scheme = value.substr(0, 5);
220 if (scheme !=
"basic") {
224 value = value.substr(5);
227 const auto it = credentials.find(value);
228 if (it != credentials.end()) {
246 const string& scheme =
"Basic";
250 response->context()->headers_.push_back(
252 scheme +
" realm=\"" + realm +
"\""));
253 response->finalize();
254 return (boost::dynamic_pointer_cast<HttpResponseJson>(response));
const isc::log::MessageID HTTP_CLIENT_REQUEST_NO_AUTH_HEADER
void parse(const isc::data::ConstElementPtr &config)
Parses basic HTTP authentication configuration.
void setContext(const data::ConstElementPtr &ctx)
Sets user context.
void lowercase(std::string &text)
Lowercase String.
#define LOG_INFO(LOGGER, MESSAGE)
Macro to conveniently test info output and log it.
virtual HttpResponsePtr createStockHttpResponse(const HttpRequestPtr &request, const HttpStatusCode &status_code) const =0
Creates implementation specific HTTP response.
boost::shared_ptr< HttpResponseJson > HttpResponseJsonPtr
Pointer to the HttpResponseJson object.
void setRealm(const std::string &realm)
Set the realm.
const isc::log::MessageID HTTP_CLIENT_REQUEST_NOT_AUTHORIZED
const isc::log::MessageID HTTP_CLIENT_REQUEST_AUTHORIZED
Represents a basic HTTP authentication.
Specifies an interface for classes creating HTTP responses from HTTP requests.
boost::shared_ptr< Element > ElementPtr
const std::string & getRealm() const
Returns the realm.
void contextToElement(data::ElementPtr map) const
Merge unparse a user_context object.
virtual bool empty() const
Empty predicate.
std::unordered_map< std::string, std::string > BasicHttpAuthMap
Type of basic HTTP authentication credential and user id map, e.g.
virtual isc::http::HttpResponseJsonPtr checkAuth(const isc::http::HttpResponseCreator &creator, const isc::http::HttpRequestPtr &request) const
Validate HTTP request.
isc::log::Logger auth_logger("auth")
Defines the logger used by the HTTP authentication.
const BasicHttpAuthMap & getCredentialMap() const
Returns the credential and user id map.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
To be removed. Please use ConfigError instead.
const std::string & getCredential() const
Returns the credential (base64 of the UTF-8 secret).
boost::shared_ptr< HttpResponse > HttpResponsePtr
Pointer to the HttpResponse object.
virtual isc::data::ElementPtr toElement() const
Unparses basic HTTP authentication configuration.
virtual void clear()
Clear configuration.
boost::shared_ptr< const Element > ConstElementPtr
virtual isc::data::ElementPtr toElement() const
Unparses basic HTTP authentication client configuration.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
const isc::log::MessageID HTTP_CLIENT_REQUEST_BAD_AUTH_HEADER
Defines the logger used by the top-level component of kea-dhcp-ddns.
void add(const std::string &user, const std::string &password, const isc::data::ConstElementPtr &user_context=isc::data::ConstElementPtr())
Add a client configuration.
string trim(const string &instring)
Trim Leading and Trailing Spaces.
boost::shared_ptr< HttpRequest > HttpRequestPtr
Pointer to the HttpRequest object.
Basic HTTP authentication client configuration.