/src/kea/src/lib/http/basic_auth.h
Line | Count | Source |
1 | | // Copyright (C) 2020-2022 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 | | #ifndef BASIC_HTTP_AUTH_H |
8 | | #define BASIC_HTTP_AUTH_H |
9 | | |
10 | | #include <http/header_context.h> |
11 | | #include <exceptions/exceptions.h> |
12 | | #include <boost/shared_ptr.hpp> |
13 | | #include <unordered_set> |
14 | | |
15 | | namespace isc { |
16 | | namespace http { |
17 | | |
18 | | /// @brief Represents a basic HTTP authentication. |
19 | | /// |
20 | | /// It computes the credential from user and password. |
21 | | class BasicHttpAuth { |
22 | | public: |
23 | | |
24 | | /// @brief Constructor. |
25 | | /// |
26 | | /// @param user User id |
27 | | /// @param password Password |
28 | | /// @throw BadValue if user contains the ':' character. |
29 | | BasicHttpAuth(const std::string& user, const std::string& password); |
30 | | |
31 | | /// @brief Constructor. |
32 | | /// |
33 | | /// @param secret user:password string |
34 | | /// @throw BadValue if secret does not contain the ':' character. |
35 | | BasicHttpAuth(const std::string& secret); |
36 | | |
37 | | /// @brief Returns the secret. |
38 | 0 | const std::string& getSecret() const { |
39 | 0 | return (secret_); |
40 | 0 | } |
41 | | |
42 | | /// @brief Returns the credential (base64 of the UTF-8 secret). |
43 | 0 | const std::string& getCredential() const { |
44 | 0 | return (credential_); |
45 | 0 | } |
46 | | |
47 | | private: |
48 | | |
49 | | /// @brief Build the secret from user and password. |
50 | | void buildSecret(); |
51 | | |
52 | | /// @brief Build the credential from the secret. |
53 | | void buildCredential(); |
54 | | |
55 | | /// @brief User id e.g. johndoe. |
56 | | std::string user_; |
57 | | |
58 | | /// @brief Password e.g. secret1. |
59 | | std::string password_; |
60 | | |
61 | | /// @brief Secret e.g. johndoe:secret1. |
62 | | std::string secret_; |
63 | | |
64 | | /// @brief Credential: base64 encoding of UTF-8 secret, |
65 | | /// e.g. am9obmRvZTpzZWNyZXQx. |
66 | | std::string credential_; |
67 | | }; |
68 | | |
69 | | /// @brief Type of pointers to basic HTTP authentication objects. |
70 | | typedef boost::shared_ptr<BasicHttpAuth> BasicHttpAuthPtr; |
71 | | |
72 | | /// @brief Represents basic HTTP authentication header. |
73 | | struct BasicAuthHttpHeaderContext : public HttpHeaderContext { |
74 | | |
75 | | /// @brief Constructor. |
76 | | /// |
77 | | /// @param basic_auth Basic HTTP authentication object. |
78 | | explicit BasicAuthHttpHeaderContext(const BasicHttpAuth& basic_auth) |
79 | 0 | : HttpHeaderContext("Authorization", |
80 | 0 | "Basic " + basic_auth.getCredential()) { |
81 | 0 | } |
82 | | }; |
83 | | |
84 | | } // end of namespace isc::http |
85 | | } // end of namespace isc |
86 | | |
87 | | #endif // endif BASIC_HTTP_AUTH_H |