Skip to content
This repository was archived by the owner on Apr 30, 2025. It is now read-only.

Commit 4f84eeb

Browse files
committed
Bearer Token auth support. Fix yhirose#484
1 parent a5b4cfa commit 4f84eeb

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

httplib.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,7 @@ class ClientImpl {
789789
void set_write_timeout(time_t sec, time_t usec = 0);
790790

791791
void set_basic_auth(const char *username, const char *password);
792+
void set_bearer_token_auth(const char *token);
792793
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
793794
void set_digest_auth(const char *username, const char *password);
794795
#endif
@@ -804,6 +805,7 @@ class ClientImpl {
804805

805806
void set_proxy(const char *host, int port);
806807
void set_proxy_basic_auth(const char *username, const char *password);
808+
void set_proxy_bearer_token_auth(const char *token);
807809
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
808810
void set_proxy_digest_auth(const char *username, const char *password);
809811
#endif
@@ -849,6 +851,7 @@ class ClientImpl {
849851

850852
std::string basic_auth_username_;
851853
std::string basic_auth_password_;
854+
std::string bearer_token_auth_token_;
852855
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
853856
std::string digest_auth_username_;
854857
std::string digest_auth_password_;
@@ -870,6 +873,7 @@ class ClientImpl {
870873

871874
std::string proxy_basic_auth_username_;
872875
std::string proxy_basic_auth_password_;
876+
std::string proxy_bearer_token_auth_token_;
873877
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
874878
std::string proxy_digest_auth_username_;
875879
std::string proxy_digest_auth_password_;
@@ -887,6 +891,7 @@ class ClientImpl {
887891
write_timeout_usec_ = rhs.write_timeout_usec_;
888892
basic_auth_username_ = rhs.basic_auth_username_;
889893
basic_auth_password_ = rhs.basic_auth_password_;
894+
bearer_token_auth_token_ = rhs.bearer_token_auth_token_;
890895
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
891896
digest_auth_username_ = rhs.digest_auth_username_;
892897
digest_auth_password_ = rhs.digest_auth_password_;
@@ -902,6 +907,7 @@ class ClientImpl {
902907
proxy_port_ = rhs.proxy_port_;
903908
proxy_basic_auth_username_ = rhs.proxy_basic_auth_username_;
904909
proxy_basic_auth_password_ = rhs.proxy_basic_auth_password_;
910+
proxy_bearer_token_auth_token_ = rhs.proxy_bearer_token_auth_token_;
905911
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
906912
proxy_digest_auth_username_ = rhs.proxy_digest_auth_username_;
907913
proxy_digest_auth_password_ = rhs.proxy_digest_auth_password_;
@@ -1046,6 +1052,7 @@ class Client {
10461052
void set_write_timeout(time_t sec, time_t usec = 0);
10471053

10481054
void set_basic_auth(const char *username, const char *password);
1055+
void set_bearer_token_auth(const char *token);
10491056
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
10501057
void set_digest_auth(const char *username, const char *password);
10511058
#endif
@@ -1061,6 +1068,7 @@ class Client {
10611068

10621069
void set_proxy(const char *host, int port);
10631070
void set_proxy_basic_auth(const char *username, const char *password);
1071+
void set_proxy_bearer_token_auth(const char *token);
10641072
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
10651073
void set_proxy_digest_auth(const char *username, const char *password);
10661074
#endif
@@ -3320,6 +3328,14 @@ make_basic_authentication_header(const std::string &username,
33203328
return std::make_pair(key, field);
33213329
}
33223330

3331+
inline std::pair<std::string, std::string>
3332+
make_bearer_token_authentication_header(const std::string &token,
3333+
bool is_proxy = false) {
3334+
auto field = "Bearer " + token;
3335+
auto key = is_proxy ? "Proxy-Authorization" : "Authorization";
3336+
return std::make_pair(key, field);
3337+
}
3338+
33233339
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
33243340
inline std::pair<std::string, std::string> make_digest_authentication_header(
33253341
const Request &req, const std::map<std::string, std::string> &auth,
@@ -4761,6 +4777,16 @@ inline bool ClientImpl::write_request(Stream &strm, const Request &req,
47614777
proxy_basic_auth_username_, proxy_basic_auth_password_, true));
47624778
}
47634779

4780+
if (!bearer_token_auth_token_.empty()) {
4781+
headers.insert(make_bearer_token_authentication_header(
4782+
bearer_token_auth_token_, false));
4783+
}
4784+
4785+
if (!proxy_bearer_token_auth_token_.empty()) {
4786+
headers.insert(make_bearer_token_authentication_header(
4787+
proxy_bearer_token_auth_token_, true));
4788+
}
4789+
47644790
detail::write_headers(bstrm, req, headers);
47654791

47664792
// Flush buffer
@@ -5253,6 +5279,10 @@ inline void ClientImpl::set_basic_auth(const char *username,
52535279
basic_auth_password_ = password;
52545280
}
52555281

5282+
inline void ClientImpl::set_bearer_token_auth(const char *token) {
5283+
bearer_token_auth_token_ = token;
5284+
}
5285+
52565286
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
52575287
inline void ClientImpl::set_digest_auth(const char *username,
52585288
const char *password) {
@@ -5288,6 +5318,10 @@ inline void ClientImpl::set_proxy_basic_auth(const char *username,
52885318
proxy_basic_auth_password_ = password;
52895319
}
52905320

5321+
inline void ClientImpl::set_proxy_bearer_token_auth(const char *token) {
5322+
proxy_bearer_token_auth_token_ = token;
5323+
}
5324+
52915325
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
52925326
inline void ClientImpl::set_proxy_digest_auth(const char *username,
52935327
const char *password) {
@@ -6186,6 +6220,9 @@ inline void Client::set_write_timeout(time_t sec, time_t usec) {
61866220
inline void Client::set_basic_auth(const char *username, const char *password) {
61876221
cli_->set_basic_auth(username, password);
61886222
}
6223+
inline void Client::set_bearer_token_auth(const char *token) {
6224+
cli_->set_bearer_token_auth(token);
6225+
}
61896226
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
61906227
inline void Client::set_digest_auth(const char *username,
61916228
const char *password) {
@@ -6213,6 +6250,9 @@ inline void Client::set_proxy_basic_auth(const char *username,
62136250
const char *password) {
62146251
cli_->set_proxy_basic_auth(username, password);
62156252
}
6253+
inline void Client::set_proxy_bearer_token_auth(const char *token) {
6254+
cli_->set_proxy_bearer_token_auth(token);
6255+
}
62166256
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
62176257
inline void Client::set_proxy_digest_auth(const char *username,
62186258
const char *password) {

0 commit comments

Comments
 (0)