Kea  1.9.9-git
basic_auth.cc
Go to the documentation of this file.
1 // Copyright (C) 2020 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #include <config.h>
8 
9 #include <http/basic_auth.h>
10 #include <util/encode/base64.h>
11 #include <util/encode/utf8.h>
12 
13 using namespace isc::util::encode;
14 using namespace std;
15 
16 namespace isc {
17 namespace http {
18 
19 BasicHttpAuth::BasicHttpAuth(const std::string& user,
20  const std::string& password)
21  : user_(user), password_(password) {
22  if (user.find(':') != string::npos) {
23  isc_throw(BadValue, "user '" << user << "' must not contain a ':'");
24  }
25  buildSecret();
26  buildCredential();
27 }
28 
29 BasicHttpAuth::BasicHttpAuth(const std::string& secret) : secret_(secret) {
30  if (secret.find(':') == string::npos) {
31  isc_throw(BadValue, "secret '" << secret << "' must contain a ':");
32  }
33  buildCredential();
34 }
35 
36 void BasicHttpAuth::buildSecret() {
37  secret_ = user_ + ":" + password_;
38 }
39 
40 void BasicHttpAuth::buildCredential() {
41  credential_ = encodeBase64(encodeUtf8(secret_));
42 }
43 
44 } // end of namespace isc::http
45 } // end of namespace isc
std::vector< uint8_t > encodeUtf8(const std::string &value)
Encode value string into UTF-8.
Definition: utf8.cc:15
STL namespace.
#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...
Defines the logger used by the top-level component of kea-dhcp-ddns.
std::string encodeBase64(const std::vector< uint8_t > &binary)
Encode binary data in the base64 format.
Definition: base_n.cc:449
BasicHttpAuth(const std::string &user, const std::string &password)
Constructor.
Definition: basic_auth.cc:19