* Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
*/
#ifndef MCP_AUTH_INCLUDE_H_
#define MCP_AUTH_INCLUDE_H_
#include <memory>
#include <optional>
#include <string>
#include <unordered_map>
#include <unordered_set>
namespace Mcp {
class AuthProvider {
public:
virtual ~AuthProvider() = default;
virtual void Apply(std::unordered_map<std::string, std::string>& headers) = 0;
};
class BearerTokenProvider : public AuthProvider {
public:
explicit BearerTokenProvider(const std::string& token = "");
~BearerTokenProvider() override;
void SetToken(const std::string& token);
const std::string& GetToken() const;
void Apply(std::unordered_map<std::string, std::string>& headers) override;
private:
std::string token_;
};
struct AuthContext {
std::optional<std::string> clientId;
std::optional<std::string> scopes;
};
struct AuthenticationResult {
bool authenticated{false};
std::optional<AuthContext> context;
std::optional<std::string> errorDescription;
};
class TokenVerifier {
public:
virtual ~TokenVerifier() = default;
virtual AuthenticationResult VerifyToken(const std::string& token) const = 0;
};
class Authenticator {
public:
virtual ~Authenticator() = default;
virtual AuthenticationResult Authenticate(const std::unordered_map<std::string, std::string>& headers) const = 0;
};
class NoAuthAuthenticator : public Authenticator {
public:
AuthenticationResult Authenticate(const std::unordered_map<std::string, std::string>& headers) const override;
};
class BearerTokenAuthenticator : public Authenticator {
public:
explicit BearerTokenAuthenticator(std::shared_ptr<TokenVerifier> verifier);
AuthenticationResult Authenticate(const std::unordered_map<std::string, std::string>& headers) const override;
private:
std::shared_ptr<TokenVerifier> verifier_;
};
class Authorizer {
public:
virtual ~Authorizer() = default;
virtual bool Authorize(const AuthenticationResult& authResult) const = 0;
};
class ScopeBasedAuthorizer : public Authorizer {
public:
* @brief Construct a scope-based authorizer.
*
* @param requiredScopes Space-separated list of required scopes.
*
* @throw std::invalid_argument If @p requiredScopes is empty or contains only whitespace.
*/
explicit ScopeBasedAuthorizer(const std::string& requiredScopes);
* @brief Update required scopes.
*
* @param scopes Space-separated list of required scopes.
*
* @throw std::invalid_argument If @p scopes is empty or contains only whitespace.
*/
void SetRequiredScopes(const std::string& scopes);
* @brief Get the currently required authorization scopes.
*
* @return A space-separated string of required scopes.
*/
std::string GetRequiredScopes() const;
* @brief Check whether the authentication result contains all required scopes.
*
* @param authResult The authentication result to check.
* @return Returns true if the authentication result contains all required scopes, otherwise false.
*/
bool Authorize(const AuthenticationResult& authResult) const override;
private:
std::unordered_set<std::string> requiredScopes_;
};
}
#endif