2017-08-03 16:05:48 -05: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/.
|
|
|
|
*/
|
|
|
|
|
2017-12-20 07:06:26 -06:00
|
|
|
#include <config.h>
|
2017-08-03 16:05:48 -05:00
|
|
|
|
2017-12-20 07:06:26 -06:00
|
|
|
#include <WopiTestServer.hpp>
|
|
|
|
#include <Log.hpp>
|
|
|
|
#include <Unit.hpp>
|
|
|
|
#include <UnitHTTP.hpp>
|
|
|
|
#include <helpers.hpp>
|
2017-08-03 16:05:48 -05:00
|
|
|
#include <Poco/Net/HTTPRequest.h>
|
|
|
|
#include <Poco/Net/OAuth20Credentials.h>
|
2017-08-09 15:02:29 -05:00
|
|
|
#include <Poco/Util/LayeredConfiguration.h>
|
2017-08-03 16:05:48 -05:00
|
|
|
|
|
|
|
using Poco::Net::OAuth20Credentials;
|
|
|
|
|
2017-09-26 09:12:58 -05:00
|
|
|
class UnitOAuth : public WopiTestServer
|
2017-08-03 16:05:48 -05:00
|
|
|
{
|
2017-08-17 07:05:22 -05:00
|
|
|
enum class Phase
|
|
|
|
{
|
|
|
|
LoadToken, // loading the document with Bearer token
|
|
|
|
LoadHeader, // loading the document with Basic auth
|
2017-09-27 13:13:16 -05:00
|
|
|
Polling // just wait for the results
|
2017-08-09 15:02:29 -05:00
|
|
|
} _phase;
|
|
|
|
|
2017-08-17 07:05:22 -05:00
|
|
|
bool _finishedToken;
|
|
|
|
bool _finishedHeader;
|
2017-08-17 04:47:14 -05:00
|
|
|
|
2017-08-03 16:05:48 -05:00
|
|
|
public:
|
2020-12-20 21:11:33 -06:00
|
|
|
UnitOAuth()
|
|
|
|
: WopiTestServer("UnitOAuth")
|
|
|
|
, _phase(Phase::LoadToken)
|
|
|
|
, _finishedToken(false)
|
|
|
|
, _finishedHeader(false)
|
2017-08-03 16:05:48 -05:00
|
|
|
{
|
2017-08-09 15:02:29 -05:00
|
|
|
}
|
2017-08-03 16:05:48 -05:00
|
|
|
|
2017-09-26 09:12:58 -05:00
|
|
|
/// The actual assert of the authentication.
|
2017-08-17 04:47:14 -05:00
|
|
|
void assertRequest(const Poco::Net::HTTPRequest& request, int fileIndex)
|
|
|
|
{
|
|
|
|
// check that the request contains the Authorization: header
|
|
|
|
try {
|
|
|
|
if (fileIndex == 0)
|
|
|
|
{
|
|
|
|
OAuth20Credentials creds(request);
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT_EQUAL(std::string("s3hn3ct0k3v"), creds.getBearerToken());
|
2017-08-17 04:47:14 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
OAuth20Credentials creds(request, "Basic");
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT_EQUAL(std::string("basic=="), creds.getBearerToken());
|
2017-08-17 04:47:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const std::exception&)
|
|
|
|
{
|
|
|
|
// fail as fast as possible
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-26 09:12:58 -05:00
|
|
|
void assertCheckFileInfoRequest(const Poco::Net::HTTPRequest& request) override
|
2017-08-09 15:02:29 -05:00
|
|
|
{
|
2017-09-26 09:12:58 -05:00
|
|
|
std::string path = Poco::URI(request.getURI()).getPath();
|
|
|
|
assertRequest(request, (path == "/wopi/files/0")? 0: 1);
|
|
|
|
}
|
2017-08-03 16:05:48 -05:00
|
|
|
|
2017-09-26 09:12:58 -05:00
|
|
|
void assertGetFileRequest(const Poco::Net::HTTPRequest& request) override
|
|
|
|
{
|
|
|
|
std::string path = Poco::URI(request.getURI()).getPath();
|
|
|
|
if (path == "/wopi/files/0/contents")
|
2017-08-03 16:05:48 -05:00
|
|
|
{
|
2017-09-26 09:12:58 -05:00
|
|
|
assertRequest(request, 0);
|
|
|
|
_finishedToken = true;
|
2017-08-03 16:05:48 -05:00
|
|
|
}
|
2017-09-26 09:12:58 -05:00
|
|
|
else
|
2017-08-03 16:05:48 -05:00
|
|
|
{
|
2017-09-26 09:12:58 -05:00
|
|
|
assertRequest(request, 1);
|
|
|
|
_finishedHeader = true;
|
2017-08-09 15:02:29 -05:00
|
|
|
}
|
2017-09-27 13:13:16 -05:00
|
|
|
|
|
|
|
if (_finishedToken && _finishedHeader)
|
|
|
|
exitTest(TestResult::Ok);
|
2017-09-26 09:12:58 -05:00
|
|
|
}
|
2017-08-09 15:02:29 -05:00
|
|
|
|
2020-12-24 11:15:37 -06:00
|
|
|
void invokeWSDTest() override
|
2017-08-03 16:05:48 -05:00
|
|
|
{
|
2017-08-09 15:02:29 -05:00
|
|
|
constexpr char testName[] = "UnitOAuth";
|
|
|
|
|
|
|
|
switch (_phase)
|
|
|
|
{
|
2017-08-17 07:05:22 -05:00
|
|
|
case Phase::LoadToken:
|
|
|
|
case Phase::LoadHeader:
|
2017-08-09 15:02:29 -05:00
|
|
|
{
|
2017-09-27 12:57:22 -05:00
|
|
|
if (_phase == Phase::LoadToken)
|
|
|
|
initWebsocket("/wopi/files/0?access_token=s3hn3ct0k3v");
|
|
|
|
else
|
|
|
|
initWebsocket("/wopi/files/1?access_header=Authorization: Basic basic==");
|
2017-08-09 15:02:29 -05:00
|
|
|
|
2021-11-18 06:08:14 -06:00
|
|
|
helpers::sendTextFrame(*getWs()->getCOOLWebSocket(), "load url=" + getWopiSrc(), testName);
|
2017-09-27 13:13:16 -05:00
|
|
|
SocketPoll::wakeupWorld();
|
2017-08-09 15:02:29 -05:00
|
|
|
|
2017-08-17 07:05:22 -05:00
|
|
|
if (_phase == Phase::LoadToken)
|
|
|
|
_phase = Phase::LoadHeader;
|
2017-08-17 04:47:14 -05:00
|
|
|
else
|
2017-09-27 13:13:16 -05:00
|
|
|
_phase = Phase::Polling;
|
2017-08-09 15:02:29 -05:00
|
|
|
break;
|
|
|
|
}
|
2017-09-27 13:13:16 -05:00
|
|
|
case Phase::Polling:
|
2017-08-09 15:02:29 -05:00
|
|
|
{
|
2017-09-27 13:13:16 -05:00
|
|
|
// just wait for the results
|
2017-08-09 15:02:29 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-08-03 16:05:48 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
UnitBase *unit_create_wsd(void)
|
|
|
|
{
|
|
|
|
return new UnitOAuth();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|