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 file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* 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-11-13 03:38:14 -06:00
|
|
|
#include "Protocol.hpp"
|
2019-12-23 07:45:38 -06:00
|
|
|
#include "Log.hpp"
|
|
|
|
#include <Exceptions.hpp>
|
2018-11-22 05:03:42 -06:00
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cassert>
|
2019-11-13 03:38:14 -06:00
|
|
|
#include <regex>
|
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-06-20 10:32:09 -05:00
|
|
|
Util::setHttpHeaders(request, "Authorization: Bearer " + _data);
|
|
|
|
assert(request.has("Authorization") && "HTTPRequest missing Authorization header");
|
2018-11-22 05:03:42 -06:00
|
|
|
break;
|
|
|
|
case Type::Header:
|
|
|
|
// there might be more headers in here; like
|
|
|
|
// Authorization: Basic ....
|
|
|
|
// X-Something-Custom: Huh
|
2020-06-20 10:32:09 -05:00
|
|
|
Util::setHttpHeaders(request, _data);
|
2018-11-22 05:03:42 -06:00
|
|
|
break;
|
|
|
|
default:
|
2019-12-23 07:45:38 -06:00
|
|
|
// assert(false);
|
|
|
|
throw BadRequestException("Invalid HTTP request type");
|
|
|
|
break;
|
2018-11-22 05:03:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-20 13:09:21 -05:00
|
|
|
Authorization Authorization::create(const Poco::URI::QueryParameters& queryParams)
|
|
|
|
{
|
|
|
|
// prefer the access_token
|
|
|
|
std::string decoded;
|
|
|
|
for (const auto& param : queryParams)
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2018-11-22 05:03:42 -06:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|