2016-02-15 17:05:24 -06:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
2023-10-30 16:58:54 -05:00
|
|
|
* Copyright the Collabora Online contributors.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
2023-11-09 12:23:00 -06:00
|
|
|
*
|
|
|
|
* 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/.
|
2016-02-15 17:05:24 -06:00
|
|
|
*/
|
|
|
|
|
|
|
|
// Authentication and Authorization support.
|
2020-04-18 03:39:50 -05:00
|
|
|
|
|
|
|
#pragma once
|
2016-02-15 17:05:24 -06:00
|
|
|
|
2017-08-16 09:38:00 -05:00
|
|
|
#include <cassert>
|
2016-02-15 17:05:24 -06:00
|
|
|
#include <string>
|
2020-01-18 10:36:50 -06:00
|
|
|
#include <memory>
|
2016-02-15 17:05:24 -06:00
|
|
|
|
2019-02-12 05:16:40 -06:00
|
|
|
#if !MOBILEAPP
|
2016-03-21 03:28:05 -05:00
|
|
|
#include <Poco/Crypto/RSADigestEngine.h>
|
|
|
|
#include <Poco/Crypto/RSAKey.h>
|
2018-09-04 04:02:12 -05:00
|
|
|
#endif
|
2017-08-16 09:38:00 -05:00
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2019-02-12 05:16:40 -06:00
|
|
|
#if !MOBILEAPP
|
2018-09-04 04:02:12 -05:00
|
|
|
|
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:
|
2018-09-28 04:54:20 -05:00
|
|
|
JWTAuth(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),
|
2020-01-18 10:36:50 -06:00
|
|
|
_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
|
|
|
|
2020-01-18 10:36:50 -06:00
|
|
|
static void cleanup();
|
|
|
|
|
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";
|
|
|
|
|
2021-11-15 10:23:40 -06:00
|
|
|
const std::string _iss = "cool";
|
2016-03-21 03:28:05 -05:00
|
|
|
const std::string _name;
|
|
|
|
const std::string _sub;
|
|
|
|
const std::string _aud;
|
|
|
|
|
2020-01-18 10:36:50 -06:00
|
|
|
static std::unique_ptr<Poco::Crypto::RSAKey> _key;
|
2016-03-21 03:28:05 -05:00
|
|
|
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: */
|