/* * Copyright (C) 2016-present ScyllaDB * * Modified by ScyllaDB */ /* * SPDX-License-Identifier: (LicenseRef-ScyllaDB-Source-Available-1.0 and Apache-2.0) */ #pragma once #include #include #include #include #include "seastarx.hh" namespace auth { /// /// A type-safe wrapper for the name of a logged-in user, or a nameless (anonymous) user. /// class authenticated_user final { public: /// /// An anonymous user has no name. /// std::optional name{}; /// /// An anonymous user. /// authenticated_user() = default; explicit authenticated_user(std::string_view name); friend bool operator==(const authenticated_user&, const authenticated_user&) noexcept = default; }; const authenticated_user& anonymous_user() noexcept; inline bool is_anonymous(const authenticated_user& u) noexcept { return u == anonymous_user(); } } /// /// The user name, or "anonymous". /// template <> struct fmt::formatter : fmt::formatter { template auto format(const auth::authenticated_user& u, FormatContext& ctx) const { if (u.name) { return fmt::format_to(ctx.out(), "{}", *u.name); } else { return fmt::format_to(ctx.out(), "{}", "anonymous"); } } }; namespace std { template <> struct hash final { size_t operator()(const auth::authenticated_user &u) const { return std::hash>()(u.name); } }; }