2016-03-31 01:48:34 -05: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/.
|
|
|
|
*/
|
|
|
|
|
loolwsd: include cleanup and organization
A source file (.cpp) must include its own header first.
This insures that the header is self-contained and
doesn't depend on arbitrary (and accidental) includes
before it to compile.
Furthermore, system headers should go next, followed by
C then C++ headers, then libraries (Poco, etc) and, finally,
project headers come last.
This makes sure that headers and included in the same dependency
order to avoid side-effects. For example, Poco should never rely on
anything from our project in the same way that a C header should
never rely on anything in C++, Poco, or project headers.
Also, includes ought to be sorted where possible, to improve
readability and avoid accidental duplicates (of which there
were a few).
Change-Id: I62cc1343e4a091d69195e37ed659dba20cfcb1ef
Reviewed-on: https://gerrit.libreoffice.org/25262
Reviewed-by: Ashod Nakashian <ashnakash@gmail.com>
Tested-by: Ashod Nakashian <ashnakash@gmail.com>
2016-05-21 09:23:07 -05:00
|
|
|
#include "Auth.hpp"
|
|
|
|
#include "config.h"
|
|
|
|
|
2016-03-31 01:48:34 -05:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <Poco/Base64Decoder.h>
|
loolwsd: include cleanup and organization
A source file (.cpp) must include its own header first.
This insures that the header is self-contained and
doesn't depend on arbitrary (and accidental) includes
before it to compile.
Furthermore, system headers should go next, followed by
C then C++ headers, then libraries (Poco, etc) and, finally,
project headers come last.
This makes sure that headers and included in the same dependency
order to avoid side-effects. For example, Poco should never rely on
anything from our project in the same way that a C header should
never rely on anything in C++, Poco, or project headers.
Also, includes ought to be sorted where possible, to improve
readability and avoid accidental duplicates (of which there
were a few).
Change-Id: I62cc1343e4a091d69195e37ed659dba20cfcb1ef
Reviewed-on: https://gerrit.libreoffice.org/25262
Reviewed-by: Ashod Nakashian <ashnakash@gmail.com>
Tested-by: Ashod Nakashian <ashnakash@gmail.com>
2016-05-21 09:23:07 -05:00
|
|
|
#include <Poco/Base64Encoder.h>
|
2016-03-31 01:48:34 -05:00
|
|
|
#include <Poco/Crypto/RSADigestEngine.h>
|
|
|
|
#include <Poco/Crypto/RSAKey.h>
|
2016-03-31 02:40:22 -05:00
|
|
|
#include <Poco/Dynamic/Var.h>
|
2016-03-31 01:48:34 -05:00
|
|
|
#include <Poco/JSON/Object.h>
|
2016-03-31 02:40:22 -05:00
|
|
|
#include <Poco/JSON/Parser.h>
|
2016-03-31 01:48:34 -05:00
|
|
|
#include <Poco/LineEndingConverter.h>
|
|
|
|
#include <Poco/Net/HTTPClientSession.h>
|
|
|
|
#include <Poco/Net/HTTPRequest.h>
|
|
|
|
#include <Poco/Net/HTTPResponse.h>
|
loolwsd: include cleanup and organization
A source file (.cpp) must include its own header first.
This insures that the header is self-contained and
doesn't depend on arbitrary (and accidental) includes
before it to compile.
Furthermore, system headers should go next, followed by
C then C++ headers, then libraries (Poco, etc) and, finally,
project headers come last.
This makes sure that headers and included in the same dependency
order to avoid side-effects. For example, Poco should never rely on
anything from our project in the same way that a C header should
never rely on anything in C++, Poco, or project headers.
Also, includes ought to be sorted where possible, to improve
readability and avoid accidental duplicates (of which there
were a few).
Change-Id: I62cc1343e4a091d69195e37ed659dba20cfcb1ef
Reviewed-on: https://gerrit.libreoffice.org/25262
Reviewed-by: Ashod Nakashian <ashnakash@gmail.com>
Tested-by: Ashod Nakashian <ashnakash@gmail.com>
2016-05-21 09:23:07 -05:00
|
|
|
#include <Poco/Net/NetException.h>
|
2016-03-31 01:48:34 -05:00
|
|
|
#include <Poco/StringTokenizer.h>
|
|
|
|
#include <Poco/Timestamp.h>
|
|
|
|
#include <Poco/URI.h>
|
|
|
|
|
loolwsd: include cleanup and organization
A source file (.cpp) must include its own header first.
This insures that the header is self-contained and
doesn't depend on arbitrary (and accidental) includes
before it to compile.
Furthermore, system headers should go next, followed by
C then C++ headers, then libraries (Poco, etc) and, finally,
project headers come last.
This makes sure that headers and included in the same dependency
order to avoid side-effects. For example, Poco should never rely on
anything from our project in the same way that a C header should
never rely on anything in C++, Poco, or project headers.
Also, includes ought to be sorted where possible, to improve
readability and avoid accidental duplicates (of which there
were a few).
Change-Id: I62cc1343e4a091d69195e37ed659dba20cfcb1ef
Reviewed-on: https://gerrit.libreoffice.org/25262
Reviewed-by: Ashod Nakashian <ashnakash@gmail.com>
Tested-by: Ashod Nakashian <ashnakash@gmail.com>
2016-05-21 09:23:07 -05:00
|
|
|
#include "Log.hpp"
|
2016-03-31 01:48:34 -05:00
|
|
|
#include "Util.hpp"
|
|
|
|
|
2016-03-31 02:40:22 -05:00
|
|
|
using Poco::Base64Decoder;
|
|
|
|
using Poco::Base64Encoder;
|
|
|
|
using Poco::OutputLineEndingConverter;
|
2016-03-31 01:48:34 -05:00
|
|
|
|
|
|
|
const std::string JWTAuth::getAccessToken()
|
|
|
|
{
|
|
|
|
std::string encodedHeader = createHeader();
|
|
|
|
std::string encodedPayload = createPayload();
|
|
|
|
|
|
|
|
// trim '=' from end of encoded header
|
|
|
|
encodedHeader.erase(std::find_if(encodedHeader.rbegin(), encodedHeader.rend(),
|
|
|
|
[](char& ch)->bool {return ch != '='; }).base(), encodedHeader.end());
|
|
|
|
// trim '=' from end of encoded payload
|
|
|
|
encodedPayload.erase(std::find_if(encodedPayload.rbegin(), encodedPayload.rend(),
|
|
|
|
[](char& ch)->bool { return ch != '='; }).base(), encodedPayload.end());
|
|
|
|
Log::info("Encoded JWT header: " + encodedHeader);
|
|
|
|
Log::info("Encoded JWT payload: " + encodedPayload);
|
|
|
|
|
|
|
|
// Convert to a URL and filename safe variant:
|
|
|
|
// Replace '+' with '-' && '/' with '_'
|
|
|
|
std::replace(encodedHeader.begin(), encodedHeader.end(), '+','-');
|
|
|
|
std::replace(encodedHeader.begin(), encodedHeader.end(), '/','_');
|
|
|
|
|
|
|
|
std::replace(encodedPayload.begin(), encodedPayload.end(), '+','-');
|
|
|
|
std::replace(encodedPayload.begin(), encodedPayload.end(), '/','_');
|
|
|
|
|
|
|
|
std::string encodedBody = encodedHeader + "." + encodedPayload;
|
|
|
|
|
|
|
|
// sign the encoded body
|
|
|
|
_digestEngine.update(encodedBody.c_str(), static_cast<unsigned>(encodedBody.length()));
|
|
|
|
Poco::Crypto::DigestEngine::Digest digest = _digestEngine.signature();
|
|
|
|
|
|
|
|
// The signature generated contains CRLF line endings.
|
|
|
|
// Use a line ending converter to remove these CRLF
|
|
|
|
std::ostringstream ostr;
|
2016-03-31 02:40:22 -05:00
|
|
|
OutputLineEndingConverter lineEndingConv(ostr, "");
|
|
|
|
Base64Encoder encoder(lineEndingConv);
|
2016-03-31 01:48:34 -05:00
|
|
|
encoder << std::string(digest.begin(), digest.end());
|
|
|
|
encoder.close();
|
|
|
|
std::string encodedSig = ostr.str();
|
|
|
|
|
|
|
|
// trim '=' from end of encoded signature
|
|
|
|
encodedSig.erase(std::find_if(encodedSig.rbegin(), encodedSig.rend(),
|
|
|
|
[](char& ch)->bool { return ch != '='; }).base(), encodedSig.end());
|
|
|
|
|
|
|
|
// Be URL and filename safe
|
|
|
|
std::replace(encodedSig.begin(), encodedSig.end(), '+','-');
|
|
|
|
std::replace(encodedSig.begin(), encodedSig.end(), '/','_');
|
|
|
|
|
|
|
|
Log::info("Sig generated is : " + encodedSig);
|
|
|
|
|
|
|
|
const std::string jwtToken = encodedBody + "." + encodedSig;
|
|
|
|
Log::info("JWT token generated: " + jwtToken);
|
|
|
|
|
|
|
|
return jwtToken;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool JWTAuth::verify(const std::string& accessToken)
|
|
|
|
{
|
|
|
|
Poco::StringTokenizer tokens(accessToken, ".", Poco::StringTokenizer::TOK_IGNORE_EMPTY | Poco::StringTokenizer::TOK_TRIM);
|
|
|
|
|
2016-03-31 02:40:22 -05:00
|
|
|
try
|
|
|
|
{
|
|
|
|
std::string encodedBody = tokens[0] + "." + tokens[1];
|
|
|
|
_digestEngine.update(encodedBody.c_str(), static_cast<unsigned>(encodedBody.length()));
|
|
|
|
Poco::Crypto::DigestEngine::Digest digest = _digestEngine.signature();
|
|
|
|
|
|
|
|
std::ostringstream ostr;
|
|
|
|
OutputLineEndingConverter lineEndingConv(ostr, "");
|
|
|
|
Base64Encoder encoder(lineEndingConv);
|
|
|
|
|
|
|
|
encoder << std::string(digest.begin(), digest.end());
|
|
|
|
encoder.close();
|
|
|
|
std::string encodedSig = ostr.str();
|
|
|
|
|
|
|
|
// trim '=' from end of encoded signature.
|
|
|
|
encodedSig.erase(std::find_if(encodedSig.rbegin(), encodedSig.rend(),
|
|
|
|
[](char& ch)->bool { return ch != '='; }).base(), encodedSig.end());
|
|
|
|
|
|
|
|
// Make the encoded sig URL and filename safe
|
|
|
|
std::replace(encodedSig.begin(), encodedSig.end(), '+', '-');
|
|
|
|
std::replace(encodedSig.begin(), encodedSig.end(), '/', '_');
|
|
|
|
|
|
|
|
if (encodedSig != tokens[2])
|
|
|
|
{
|
|
|
|
Log::info("JWTAuth: verification failed; Expected: " + encodedSig + ", Received: " + tokens[2]);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::istringstream istr(tokens[1]);
|
|
|
|
std::string decodedPayload;
|
|
|
|
Base64Decoder decoder(istr);
|
|
|
|
decoder >> decodedPayload;
|
|
|
|
|
|
|
|
Log::info("JWTAuth:verify: decoded payload: " + decodedPayload);
|
|
|
|
|
|
|
|
// Verify if the token is not already expired
|
|
|
|
Poco::JSON::Parser parser;
|
|
|
|
Poco::Dynamic::Var result = parser.parse(decodedPayload);
|
|
|
|
Poco::JSON::Object::Ptr object = result.extract<Poco::JSON::Object::Ptr>();
|
|
|
|
std::time_t decodedExptime = object->get("exp").convert<std::time_t>();
|
|
|
|
|
|
|
|
std::time_t curtime = Poco::Timestamp().epochTime();
|
|
|
|
if (curtime > decodedExptime)
|
|
|
|
{
|
|
|
|
Log::info("JWTAuth:verify: JWT expired; curtime:" + std::to_string(curtime) + ", exp:" + std::to_string(decodedExptime));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(Poco::Exception& exc)
|
2016-03-31 01:48:34 -05:00
|
|
|
{
|
2016-03-31 02:40:22 -05:00
|
|
|
Log::warn("JWTAuth:verify: Exception: " + exc.displayText());
|
2016-03-31 01:48:34 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string JWTAuth::createHeader()
|
|
|
|
{
|
|
|
|
// TODO: Some sane code to represent JSON objects
|
|
|
|
std::string header = "{\"alg\":\""+_alg+"\",\"typ\":\""+_typ+"\"}";
|
|
|
|
|
|
|
|
Log::info("JWT Header: " + header);
|
|
|
|
std::ostringstream ostr;
|
2016-03-31 02:40:22 -05:00
|
|
|
OutputLineEndingConverter lineEndingConv(ostr, "");
|
|
|
|
Base64Encoder encoder(lineEndingConv);
|
2016-03-31 01:48:34 -05:00
|
|
|
encoder << header;
|
|
|
|
encoder.close();
|
|
|
|
|
|
|
|
return ostr.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string JWTAuth::createPayload()
|
|
|
|
{
|
|
|
|
std::time_t curtime = Poco::Timestamp().epochTime();
|
2016-03-31 02:40:22 -05:00
|
|
|
std::string exptime = std::to_string(curtime + 1800);
|
2016-03-31 01:48:34 -05:00
|
|
|
|
|
|
|
// TODO: Some sane code to represent JSON objects
|
|
|
|
std::string payload = "{\"iss\":\""+_iss+"\",\"sub\":\""+_sub+"\",\"aud\":\""+_aud+"\",\"nme\":\""+_name+"\",\"exp\":\""+exptime+"\"}";
|
|
|
|
|
|
|
|
Log::info("JWT Payload: " + payload);
|
|
|
|
std::ostringstream ostr;
|
2016-03-31 02:40:22 -05:00
|
|
|
OutputLineEndingConverter lineEndingConv(ostr, "");
|
|
|
|
Base64Encoder encoder(lineEndingConv);
|
2016-03-31 01:48:34 -05:00
|
|
|
encoder << payload;
|
|
|
|
encoder.close();
|
|
|
|
|
|
|
|
return ostr.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: This MUST be done over TLS to protect the token.
|
|
|
|
const std::string OAuth::getAccessToken()
|
|
|
|
{
|
|
|
|
std::string url = _tokenEndPoint
|
|
|
|
+ "?client_id=" + _clientId
|
|
|
|
+ "&client_secret=" + _clientSecret
|
|
|
|
+ "&grant_type=authorization_code"
|
|
|
|
+ "&code=" + _authorizationCode;
|
|
|
|
// + "&redirect_uri="
|
|
|
|
|
|
|
|
Poco::URI uri(url);
|
|
|
|
Poco::Net::HTTPClientSession session(uri.getHost(), uri.getPort());
|
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST, url, Poco::Net::HTTPMessage::HTTP_1_1);
|
|
|
|
Poco::Net::HTTPResponse response;
|
|
|
|
session.sendRequest(request);
|
|
|
|
std::istream& rs = session.receiveResponse(response);
|
|
|
|
Log::info() << "Status: " << response.getStatus() << " " << response.getReason() << Log::end;
|
|
|
|
std::string reply(std::istreambuf_iterator<char>(rs), {});
|
|
|
|
Log::info("Response: " + reply);
|
|
|
|
//TODO: Parse the token.
|
|
|
|
|
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OAuth::verify(const std::string& token)
|
|
|
|
{
|
|
|
|
const std::string url = _authVerifyUrl + token;
|
|
|
|
Log::debug("Verifying authorization token from: " + url);
|
|
|
|
Poco::URI uri(url);
|
|
|
|
Poco::Net::HTTPClientSession session(uri.getHost(), uri.getPort());
|
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, url, Poco::Net::HTTPMessage::HTTP_1_1);
|
|
|
|
Poco::Net::HTTPResponse response;
|
|
|
|
session.sendRequest(request);
|
|
|
|
std::istream& rs = session.receiveResponse(response);
|
|
|
|
Log::info() << "Status: " << response.getStatus() << " " << response.getReason() << Log::end;
|
|
|
|
std::string reply(std::istreambuf_iterator<char>(rs), {});
|
|
|
|
Log::info("Response: " + reply);
|
|
|
|
|
|
|
|
//TODO: Parse the response.
|
|
|
|
/*
|
|
|
|
// This is used for the demo site.
|
|
|
|
const auto lastLogTime = std::strtoul(reply.c_str(), nullptr, 0);
|
|
|
|
if (lastLogTime < 1)
|
|
|
|
{
|
|
|
|
//TODO: Redirect to login page.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|