2016-02-15 17:05:24 -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/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Authentication and Authorization support.
|
|
|
|
#ifndef INCLUDED_AUTH_HPP
|
|
|
|
#define INCLUDED_AUTH_HPP
|
|
|
|
|
2017-08-16 09:38:00 -05:00
|
|
|
#include <cassert>
|
2016-02-15 17:05:24 -06:00
|
|
|
#include <string>
|
|
|
|
|
2016-03-21 03:28:05 -05:00
|
|
|
#include <Poco/Crypto/RSADigestEngine.h>
|
|
|
|
#include <Poco/Crypto/RSAKey.h>
|
2017-08-16 09:38:00 -05:00
|
|
|
#include <Poco/Net/HTTPRequest.h>
|
|
|
|
#include <Poco/URI.h>
|
|
|
|
|
|
|
|
/// Class to keep the authorization data.
|
|
|
|
class Authorization
|
|
|
|
{
|
|
|
|
public:
|
2017-08-17 07:05:22 -05:00
|
|
|
enum class Type
|
|
|
|
{
|
2017-08-16 09:38:00 -05:00
|
|
|
None,
|
|
|
|
Token,
|
|
|
|
Header
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
Type _type;
|
|
|
|
std::string _data;
|
|
|
|
|
|
|
|
public:
|
|
|
|
Authorization()
|
|
|
|
: _type(Type::None)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Authorization(Type type, const std::string& data)
|
|
|
|
: _type(type)
|
|
|
|
, _data(data)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the access_token parametr to the given uri.
|
|
|
|
void authorizeURI(Poco::URI& uri) const;
|
|
|
|
|
|
|
|
/// Set the Authorization: header in request.
|
|
|
|
void authorizeRequest(Poco::Net::HTTPRequest& request) const;
|
|
|
|
};
|
2016-02-15 17:05:24 -06:00
|
|
|
|
|
|
|
/// Base class of all Authentication/Authorization implementations.
|
|
|
|
class AuthBase
|
|
|
|
{
|
|
|
|
public:
|
2017-09-12 03:03:26 -05:00
|
|
|
virtual ~AuthBase() {}
|
2016-03-19 06:17:49 -05:00
|
|
|
/// Called to acquire an access token.
|
2016-03-21 03:28:05 -05:00
|
|
|
virtual const std::string getAccessToken() = 0;
|
2016-02-15 17:05:24 -06:00
|
|
|
|
|
|
|
/// Used to verify the validity of an access token.
|
|
|
|
virtual bool verify(const std::string& token) = 0;
|
|
|
|
};
|
|
|
|
|
2016-08-13 23:01:13 -05:00
|
|
|
/// JWT Authorization.
|
2016-10-29 20:15:00 -05:00
|
|
|
class JWTAuth : public AuthBase
|
2016-03-21 03:28:05 -05:00
|
|
|
{
|
|
|
|
public:
|
2016-07-29 02:14:13 -05:00
|
|
|
JWTAuth(const std::string& keyPath, const std::string& name, const std::string& sub, const std::string& aud)
|
2016-03-21 03:28:05 -05:00
|
|
|
: _name(name),
|
|
|
|
_sub(sub),
|
|
|
|
_aud(aud),
|
|
|
|
_key(Poco::Crypto::RSAKey("", keyPath)),
|
|
|
|
_digestEngine(_key, "SHA256")
|
2017-08-17 07:05:22 -05:00
|
|
|
{
|
|
|
|
}
|
2016-03-21 03:28:05 -05:00
|
|
|
|
2016-03-31 01:48:34 -05:00
|
|
|
const std::string getAccessToken() override;
|
2016-03-21 03:28:05 -05:00
|
|
|
|
2016-03-31 01:48:34 -05:00
|
|
|
bool verify(const std::string& accessToken) override;
|
2016-03-21 03:28:05 -05:00
|
|
|
|
|
|
|
private:
|
2016-03-31 01:48:34 -05:00
|
|
|
const std::string createHeader();
|
2016-03-21 03:28:05 -05:00
|
|
|
|
2016-03-31 01:48:34 -05:00
|
|
|
const std::string createPayload();
|
2016-03-21 03:28:05 -05:00
|
|
|
|
|
|
|
private:
|
|
|
|
const std::string _alg = "RS256";
|
|
|
|
const std::string _typ = "JWT";
|
|
|
|
|
|
|
|
const std::string _iss = "lool";
|
|
|
|
const std::string _name;
|
|
|
|
const std::string _sub;
|
|
|
|
const std::string _aud;
|
|
|
|
|
|
|
|
const Poco::Crypto::RSAKey _key;
|
|
|
|
Poco::Crypto::RSADigestEngine _digestEngine;
|
|
|
|
};
|
|
|
|
|
2016-08-13 23:01:13 -05:00
|
|
|
/// OAuth Authorization.
|
2016-02-15 17:05:24 -06:00
|
|
|
class OAuth : public AuthBase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
OAuth(const std::string& clientId,
|
2016-03-19 06:17:49 -05:00
|
|
|
const std::string& clientSecret,
|
|
|
|
const std::string& tokenEndPoint,
|
|
|
|
const std::string& authVerifyUrl,
|
|
|
|
const std::string& authorizationCode) :
|
2016-02-15 17:05:24 -06:00
|
|
|
_clientId(clientId),
|
|
|
|
_clientSecret(clientSecret),
|
|
|
|
_tokenEndPoint(tokenEndPoint),
|
2016-03-19 06:17:49 -05:00
|
|
|
_authVerifyUrl(authVerifyUrl),
|
|
|
|
_authorizationCode(authorizationCode)
|
2016-02-15 17:05:24 -06:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-03-31 01:48:34 -05:00
|
|
|
const std::string getAccessToken() override;
|
2016-02-15 17:05:24 -06:00
|
|
|
|
2016-03-31 01:48:34 -05:00
|
|
|
bool verify(const std::string& token) override;
|
2016-02-15 17:05:24 -06:00
|
|
|
|
|
|
|
private:
|
|
|
|
const std::string _clientId;
|
|
|
|
const std::string _clientSecret;
|
|
|
|
const std::string _tokenEndPoint;
|
|
|
|
const std::string _authVerifyUrl;
|
2016-03-19 06:17:49 -05:00
|
|
|
const std::string _authorizationCode;
|
2016-02-15 17:05:24 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
2016-03-08 01:31:29 -06:00
|
|
|
|
2016-02-15 17:05:24 -06:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|