From b2306987ec12ca1a81041e35d2a3d45803f25e59 Mon Sep 17 00:00:00 2001 From: Scott Smith Date: Wed, 3 Nov 2021 12:23:46 -0700 Subject: [PATCH 1/2] Support additional authorization schemes The client always appends "Basic" to the authorization header, however there are other auth schemes that can be used: https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication For example "Bearer" when using OAuth. This PR adds a `setAuthorizationType` method to the HTTPClient which allows this scheme to be configured by the caller. Authorization type is set to "Basic" by default so this will have no impact on existing usecases. --- libraries/HTTPClient/src/HTTPClient.cpp | 16 ++++++++++++++-- libraries/HTTPClient/src/HTTPClient.h | 2 ++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/libraries/HTTPClient/src/HTTPClient.cpp b/libraries/HTTPClient/src/HTTPClient.cpp index cc0a31e2b6b..18d6746f60e 100644 --- a/libraries/HTTPClient/src/HTTPClient.cpp +++ b/libraries/HTTPClient/src/HTTPClient.cpp @@ -463,6 +463,17 @@ void HTTPClient::setAuthorization(const char * auth) } } +/** + * set the Authorization type for the http request + * @param authType const char * + */ +void HTTPClient::setAuthorizationType(const char * authType) +{ + if(authType) { + _authorizationType = authType; + } +} + /** * set the timeout (ms) for establishing a connection to the server * @param connectTimeout int32_t @@ -1177,8 +1188,9 @@ bool HTTPClient::sendHeader(const char * type) } if(_base64Authorization.length()) { - _base64Authorization.replace("\n", ""); - header += F("Authorization: Basic "); + header += F("Authorization: "); + header += _authorizationType; + header += " "; header += _base64Authorization; header += "\r\n"; } diff --git a/libraries/HTTPClient/src/HTTPClient.h b/libraries/HTTPClient/src/HTTPClient.h index 1b454e332a9..1bb84d6d6b5 100644 --- a/libraries/HTTPClient/src/HTTPClient.h +++ b/libraries/HTTPClient/src/HTTPClient.h @@ -171,6 +171,7 @@ class HTTPClient void setUserAgent(const String& userAgent); void setAuthorization(const char * user, const char * password); void setAuthorization(const char * auth); + void setAuthorizationType(const char * authType); void setConnectTimeout(int32_t connectTimeout); void setTimeout(uint16_t timeout); @@ -251,6 +252,7 @@ class HTTPClient String _headers; String _userAgent = "ESP32HTTPClient"; String _base64Authorization; + String _authorizationType = "Basic"; /// Response handling RequestArgument* _currentHeaders = nullptr; From fa5f388bd63fcd763b0ba57b7ca74197e13348b9 Mon Sep 17 00:00:00 2001 From: Scott Smith Date: Wed, 3 Nov 2021 12:50:31 -0700 Subject: [PATCH 2/2] add base64Auth string replace back accidentally removed this in original commit --- libraries/HTTPClient/src/HTTPClient.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/libraries/HTTPClient/src/HTTPClient.cpp b/libraries/HTTPClient/src/HTTPClient.cpp index 18d6746f60e..c99c633264e 100644 --- a/libraries/HTTPClient/src/HTTPClient.cpp +++ b/libraries/HTTPClient/src/HTTPClient.cpp @@ -1188,6 +1188,7 @@ bool HTTPClient::sendHeader(const char * type) } if(_base64Authorization.length()) { + _base64Authorization.replace("\n", ""); header += F("Authorization: "); header += _authorizationType; header += " ";