20#ifndef ORES_SECURITY_JWT_JWT_AUTHENTICATOR_HPP
21#define ORES_SECURITY_JWT_JWT_AUTHENTICATOR_HPP
26#include "ores.security/jwt/jwt_claims.hpp"
27#include "ores.security/jwt/jwt_error.hpp"
28#include "ores.logging/make_logger.hpp"
30namespace ores::security::jwt {
53 const std::string& issuer =
"",
54 const std::string& audience =
"");
63 const std::string& private_key_pem,
64 const std::string& issuer =
"",
65 const std::string& audience =
"");
74 const std::string& public_key_pem,
75 const std::string& issuer =
"",
76 const std::string& audience =
"");
81 std::expected<jwt_claims, jwt_error>
validate(
const std::string& token)
const;
89 std::expected<jwt_claims, jwt_error>
114 enum class algorithm_type { hs256, rs256_signer, rs256_verifier };
116 jwt_authenticator() =
default;
118 inline static std::string_view logger_name =
119 "ores.security.jwt.jwt_authenticator";
123 static auto instance = make_logger(logger_name);
127 bool configured_ =
false;
128 algorithm_type algorithm_ = algorithm_type::hs256;
130 std::string private_key_;
131 std::string public_key_;
133 std::string audience_;
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