1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright (C) 2020-2022 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#ifndef BASIC_HTTP_AUTH_H
#define BASIC_HTTP_AUTH_H

#include <http/header_context.h>
#include <exceptions/exceptions.h>
#include <boost/shared_ptr.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <unordered_set><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

namespace isc {
namespace http {

/// @brief Represents a basic HTTP authentication.
///
/// It computes the credential from user and password.
class BasicHttpAuth {
public:

    /// @brief Constructor.
    ///
    /// @param user User id
    /// @param password Password
    /// @throw BadValue if user contains the ':' character.
    BasicHttpAuth(const std::string& user, const std::string& password);

    /// @brief Constructor.
    ///
    /// @param secret user:password string
    /// @throw BadValue if secret does not contain the ':' character.
    BasicHttpAuth(const std::string& secret);

    /// @brief Returns the secret.
    const std::string& getSecret() const {
        return (secret_);
    }

    /// @brief Returns the credential (base64 of the UTF-8 secret).
    const std::string& getCredential() const {
        return (credential_);
    }

private:

    /// @brief Build the secret from user and password.
    void buildSecret();

    /// @brief Build the credential from the secret.
    void buildCredential();

    /// @brief User id e.g. johndoe.
    std::string user_;

    /// @brief Password e.g. secret1.
    std::string password_;

    /// @brief Secret e.g. johndoe:secret1.
    std::string secret_;

    /// @brief Credential: base64 encoding of UTF-8 secret,
    /// e.g. am9obmRvZTpzZWNyZXQx.
    std::string credential_;
};

/// @brief Type of pointers to basic HTTP authentication objects.
typedef boost::shared_ptr<BasicHttpAuth> BasicHttpAuthPtr;

/// @brief Represents basic HTTP authentication header.
struct BasicAuthHttpHeaderContext : public HttpHeaderContext {

    /// @brief Constructor.
    ///
    /// @param basic_auth Basic HTTP authentication object.
    explicit BasicAuthHttpHeaderContext(const BasicHttpAuth& basic_auth)
        : HttpHeaderContext("Authorization",
                            "Basic " + basic_auth.getCredential()) {
    }
};

} // end of namespace isc::http
} // end of namespace isc

#endif // endif BASIC_HTTP_AUTH_H