2018-11-22 05:03:42 -06:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
|
|
|
* 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/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "Authorization.hpp"
|
2019-12-23 07:45:38 -06:00
|
|
|
#include "Log.hpp"
|
2022-03-29 20:37:57 -05:00
|
|
|
#include "StringVector.hpp"
|
2018-11-22 05:03:42 -06:00
|
|
|
|
2022-03-28 09:51:38 -05:00
|
|
|
#include <Poco/Net/HTTPRequest.h>
|
|
|
|
#include <Poco/URI.h>
|
2018-11-22 05:03:42 -06:00
|
|
|
|
|
|
|
void Authorization::authorizeURI(Poco::URI& uri) const
|
|
|
|
{
|
|
|
|
if (_type == Authorization::Type::Token)
|
|
|
|
{
|
|
|
|
static const std::string key("access_token");
|
|
|
|
|
|
|
|
Poco::URI::QueryParameters queryParams = uri.getQueryParameters();
|
|
|
|
for (auto& param: queryParams)
|
|
|
|
{
|
|
|
|
if (param.first == key)
|
|
|
|
{
|
|
|
|
param.second = _data;
|
|
|
|
uri.setQueryParameters(queryParams);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// it did not exist yet
|
|
|
|
uri.addQueryParameter(key, _data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Authorization::authorizeRequest(Poco::Net::HTTPRequest& request) const
|
|
|
|
{
|
|
|
|
switch (_type)
|
|
|
|
{
|
|
|
|
case Type::Token:
|
2020-09-01 12:15:30 -05:00
|
|
|
request.set("Authorization", "Bearer " + _data);
|
2018-11-22 05:03:42 -06:00
|
|
|
break;
|
|
|
|
case Type::Header:
|
2020-09-01 12:15:30 -05:00
|
|
|
{
|
2018-11-22 05:03:42 -06:00
|
|
|
// there might be more headers in here; like
|
|
|
|
// Authorization: Basic ....
|
|
|
|
// X-Something-Custom: Huh
|
2020-09-01 12:15:30 -05:00
|
|
|
// Split based on \n's or \r's and trim, to avoid nonsense in the
|
|
|
|
// headers
|
2022-03-29 20:37:57 -05:00
|
|
|
StringVector tokens(StringVector::tokenizeAnyOf(_data, "\n\r"));
|
2020-09-01 12:15:30 -05:00
|
|
|
for (auto it = tokens.begin(); it != tokens.end(); ++it)
|
|
|
|
{
|
|
|
|
std::string token = tokens.getParam(*it);
|
|
|
|
|
2020-11-15 11:03:45 -06:00
|
|
|
std::size_t separator = token.find_first_of(':');
|
2020-09-01 12:15:30 -05:00
|
|
|
if (separator != std::string::npos)
|
|
|
|
{
|
2020-11-15 11:03:45 -06:00
|
|
|
std::size_t headerStart = token.find_first_not_of(' ', 0);
|
|
|
|
std::size_t headerEnd = token.find_last_not_of(' ', separator - 1);
|
2020-09-01 12:15:30 -05:00
|
|
|
|
2020-11-15 11:03:45 -06:00
|
|
|
std::size_t valueStart = token.find_first_not_of(' ', separator + 1);
|
|
|
|
std::size_t valueEnd = token.find_last_not_of(' ');
|
2020-09-01 12:15:30 -05:00
|
|
|
|
|
|
|
// set the header
|
|
|
|
if (headerStart != std::string::npos && headerEnd != std::string::npos &&
|
|
|
|
valueStart != std::string::npos && valueEnd != std::string::npos)
|
|
|
|
{
|
2020-11-15 11:03:45 -06:00
|
|
|
std::size_t headerLength = headerEnd - headerStart + 1;
|
|
|
|
std::size_t valueLength = valueEnd - valueStart + 1;
|
2020-09-01 12:15:30 -05:00
|
|
|
|
|
|
|
request.set(token.substr(headerStart, headerLength), token.substr(valueStart, valueLength));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-22 05:03:42 -06:00
|
|
|
break;
|
2020-09-01 12:15:30 -05:00
|
|
|
}
|
2018-11-22 05:03:42 -06:00
|
|
|
default:
|
2021-02-23 07:41:59 -06:00
|
|
|
LOG_ERR("No HTTP Authorization type detected. Assuming no authorization needed. "
|
2020-10-06 22:08:13 -05:00
|
|
|
"Specify access_token to set the Authorization Bearer header.");
|
2019-12-23 07:45:38 -06:00
|
|
|
break;
|
2018-11-22 05:03:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-28 09:51:38 -05:00
|
|
|
Authorization Authorization::create(const Poco::URI& uri)
|
2020-06-20 13:09:21 -05:00
|
|
|
{
|
|
|
|
// prefer the access_token
|
|
|
|
std::string decoded;
|
2022-03-28 09:51:38 -05:00
|
|
|
for (const auto& param : uri.getQueryParameters())
|
2020-06-20 13:09:21 -05:00
|
|
|
{
|
|
|
|
if (param.first == "access_token")
|
|
|
|
{
|
|
|
|
Poco::URI::decode(param.second, decoded);
|
|
|
|
return Authorization(Authorization::Type::Token, decoded);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (param.first == "access_header")
|
|
|
|
Poco::URI::decode(param.second, decoded);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!decoded.empty())
|
|
|
|
return Authorization(Authorization::Type::Header, decoded);
|
|
|
|
|
|
|
|
return Authorization();
|
|
|
|
}
|
|
|
|
|
2022-03-28 09:51:38 -05:00
|
|
|
Authorization Authorization::create(const std::string& uri) { return create(Poco::URI(uri)); }
|
|
|
|
|
2018-11-22 05:03:42 -06:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|