Coverage Report

Created: 2026-10-28 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/poco/Net/include/Poco/Net/HTTPBasicCredentials.h
Line
Count
Source
1
//
2
// HTTPBasicCredentials.h
3
//
4
// Library: Net
5
// Package: HTTP
6
// Module:  HTTPBasicCredentials
7
//
8
// Definition of the HTTPBasicCredentials class.
9
//
10
// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
11
// and Contributors.
12
//
13
// SPDX-License-Identifier: BSL-1.0
14
//
15
16
17
#ifndef Net_HTTPBasicCredentials_INCLUDED
18
#define Net_HTTPBasicCredentials_INCLUDED
19
20
21
#include "Poco/Net/Net.h"
22
23
24
namespace Poco::Net {
25
26
27
class HTTPRequest;
28
29
30
class Net_API HTTPBasicCredentials
31
  /// This is a utility class for working with
32
  /// HTTP Basic Authentication in HTTPRequest
33
  /// objects.
34
{
35
public:
36
  HTTPBasicCredentials();
37
    /// Creates an empty HTTPBasicCredentials object.
38
39
  HTTPBasicCredentials(const std::string& username, const std::string& password);
40
    /// Creates a HTTPBasicCredentials object with the given username and password.
41
42
  explicit HTTPBasicCredentials(const HTTPRequest& request);
43
    /// Creates a HTTPBasicCredentials object with the authentication information
44
    /// from the given request.
45
    ///
46
    /// Throws a NotAuthenticatedException if the request does
47
    /// not contain basic authentication information.
48
49
  explicit HTTPBasicCredentials(const std::string& authInfo);
50
    /// Creates a HTTPBasicCredentials object with the authentication information
51
    /// in the given string. The authentication information can be extracted
52
    /// from a HTTPRequest object by calling HTTPRequest::getCredentials().
53
54
  ~HTTPBasicCredentials();
55
    /// Destroys the HTTPBasicCredentials.
56
57
  void clear();
58
    /// Clears both username and password.
59
60
  void setUsername(const std::string& username);
61
    /// Sets the username.
62
63
  [[nodiscard]] const std::string& getUsername() const;
64
    /// Returns the username.
65
66
  void setPassword(const std::string& password);
67
    /// Sets the password.
68
69
  [[nodiscard]] const std::string& getPassword() const;
70
    /// Returns the password.
71
72
  [[nodiscard]] bool empty() const;
73
    /// Returns true if both username and password are empty, otherwise false.
74
75
  void authenticate(HTTPRequest& request) const;
76
    /// Adds authentication information to the given HTTPRequest.
77
78
  void proxyAuthenticate(HTTPRequest& request) const;
79
    /// Adds proxy authentication information to the given HTTPRequest.
80
81
  static const std::string SCHEME;
82
83
protected:
84
  void parseAuthInfo(const std::string& authInfo);
85
    /// Extracts username and password from Basic authentication info
86
    /// by base64-decoding authInfo and splitting the resulting
87
    /// string at the ':' delimiter.
88
89
private:
90
  HTTPBasicCredentials(const HTTPBasicCredentials&);
91
  HTTPBasicCredentials& operator = (const HTTPBasicCredentials&);
92
93
  std::string _username;
94
  std::string _password;
95
};
96
97
98
//
99
// inlines
100
//
101
inline const std::string& HTTPBasicCredentials::getUsername() const
102
0
{
103
0
  return _username;
104
0
}
105
106
107
inline const std::string& HTTPBasicCredentials::getPassword() const
108
0
{
109
0
  return _password;
110
0
}
111
112
113
inline bool HTTPBasicCredentials::empty() const
114
0
{
115
0
  return _username.empty() && _password.empty();
116
0
}
117
118
119
} // namespace Poco::Net
120
121
122
#endif // Net_HTTPBasicCredentials_INCLUDED