Unverified Commit 4aff6dde authored by Scott Smith's avatar Scott Smith Committed by GitHub

Support additional authorization schemes (#5845)

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.
parent bb500465
...@@ -463,6 +463,17 @@ void HTTPClient::setAuthorization(const char * auth) ...@@ -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 * set the timeout (ms) for establishing a connection to the server
* @param connectTimeout int32_t * @param connectTimeout int32_t
...@@ -1178,7 +1189,9 @@ bool HTTPClient::sendHeader(const char * type) ...@@ -1178,7 +1189,9 @@ bool HTTPClient::sendHeader(const char * type)
if(_base64Authorization.length()) { if(_base64Authorization.length()) {
_base64Authorization.replace("\n", ""); _base64Authorization.replace("\n", "");
header += F("Authorization: Basic "); header += F("Authorization: ");
header += _authorizationType;
header += " ";
header += _base64Authorization; header += _base64Authorization;
header += "\r\n"; header += "\r\n";
} }
......
...@@ -171,6 +171,7 @@ public: ...@@ -171,6 +171,7 @@ public:
void setUserAgent(const String& userAgent); void setUserAgent(const String& userAgent);
void setAuthorization(const char * user, const char * password); void setAuthorization(const char * user, const char * password);
void setAuthorization(const char * auth); void setAuthorization(const char * auth);
void setAuthorizationType(const char * authType);
void setConnectTimeout(int32_t connectTimeout); void setConnectTimeout(int32_t connectTimeout);
void setTimeout(uint16_t timeout); void setTimeout(uint16_t timeout);
...@@ -251,6 +252,7 @@ protected: ...@@ -251,6 +252,7 @@ protected:
String _headers; String _headers;
String _userAgent = "ESP32HTTPClient"; String _userAgent = "ESP32HTTPClient";
String _base64Authorization; String _base64Authorization;
String _authorizationType = "Basic";
/// Response handling /// Response handling
RequestArgument* _currentHeaders = nullptr; RequestArgument* _currentHeaders = nullptr;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment