ORE Studio 0.0.4
Loading...
Searching...
No Matches
jwt_authenticator.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * Copyright (C) 2025 Marco Craveiro <marco.craveiro@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 3 of the License, or (at your option) any later
8 * version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 */
20#ifndef ORES_SECURITY_JWT_JWT_AUTHENTICATOR_HPP
21#define ORES_SECURITY_JWT_JWT_AUTHENTICATOR_HPP
22
23#include <string>
24#include <optional>
25#include <expected>
26#include "ores.security/jwt/jwt_claims.hpp"
27#include "ores.security/jwt/jwt_error.hpp"
28#include "ores.logging/make_logger.hpp"
29
30namespace ores::security::jwt {
31
45class jwt_authenticator final {
46public:
52 static jwt_authenticator create_hs256(const std::string& secret,
53 const std::string& issuer = "",
54 const std::string& audience = "");
55
63 const std::string& private_key_pem,
64 const std::string& issuer = "",
65 const std::string& audience = "");
66
74 const std::string& public_key_pem,
75 const std::string& issuer = "",
76 const std::string& audience = "");
77
81 std::expected<jwt_claims, jwt_error> validate(const std::string& token) const;
82
89 std::expected<jwt_claims, jwt_error>
90 validate_allow_expired(const std::string& token) const;
91
98 std::optional<std::string> create_token(const jwt_claims& claims) const;
99
103 bool is_configured() const { return configured_; }
104
111 std::string get_public_key_pem() const;
112
113private:
114 enum class algorithm_type { hs256, rs256_signer, rs256_verifier };
115
116 jwt_authenticator() = default;
117
118 inline static std::string_view logger_name =
119 "ores.security.jwt.jwt_authenticator";
120
121 static auto& lg() {
122 using namespace ores::logging;
123 static auto instance = make_logger(logger_name);
124 return instance;
125 }
126
127 bool configured_ = false;
128 algorithm_type algorithm_ = algorithm_type::hs256;
129 std::string secret_; // HS256
130 std::string private_key_; // RS256 signer
131 std::string public_key_; // RS256 verifier
132 std::string issuer_;
133 std::string audience_;
134};
135
136}
137
138#endif
Implements logging infrastructure for ORE Studio.
Definition boost_severity.hpp:28
JWT authentication primitive.
Definition jwt_authenticator.hpp:45
std::expected< jwt_claims, jwt_error > validate(const std::string &token) const
Validates a JWT token and extracts claims.
Definition jwt_authenticator.cpp:90
static jwt_authenticator create_rs256_verifier(const std::string &public_key_pem, const std::string &issuer="", const std::string &audience="")
Creates an RS256 verifier using an RSA public key (PEM).
Definition jwt_authenticator.cpp:71
std::string get_public_key_pem() const
Extracts the RSA public key PEM from the private key.
Definition jwt_authenticator.cpp:439
static jwt_authenticator create_rs256_signer(const std::string &private_key_pem, const std::string &issuer="", const std::string &audience="")
Creates an RS256 signer using an RSA private key (PEM).
Definition jwt_authenticator.cpp:52
bool is_configured() const
Checks if the authenticator is properly configured.
Definition jwt_authenticator.hpp:103
std::optional< std::string > create_token(const jwt_claims &claims) const
Creates a new JWT token with the given claims.
Definition jwt_authenticator.cpp:322
std::expected< jwt_claims, jwt_error > validate_allow_expired(const std::string &token) const
Validates a JWT token and extracts claims, ignoring expiry.
Definition jwt_authenticator.cpp:225
static jwt_authenticator create_hs256(const std::string &secret, const std::string &issuer="", const std::string &audience="")
Creates an authenticator using a symmetric secret (HS256).
Definition jwt_authenticator.cpp:34
Represents the claims extracted from a JWT token.
Definition jwt_claims.hpp:33