Drop the AGPL license in favor of a source-available license. See the blog post [1] for details. [1] https://www.scylladb.com/2024/12/18/why-were-moving-to-a-source-available-license/
74 lines
1.6 KiB
C++
74 lines
1.6 KiB
C++
/*
|
|
* Copyright (C) 2016-present ScyllaDB
|
|
*
|
|
* Modified by ScyllaDB
|
|
*/
|
|
|
|
/*
|
|
* SPDX-License-Identifier: (LicenseRef-ScyllaDB-Source-Available-1.0 and Apache-2.0)
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <string_view>
|
|
#include <functional>
|
|
#include <optional>
|
|
|
|
#include <seastar/core/sstring.hh>
|
|
|
|
#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<sstring> 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<auth::authenticated_user> : fmt::formatter<string_view> {
|
|
template <typename FormatContext>
|
|
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<auth::authenticated_user> final {
|
|
size_t operator()(const auth::authenticated_user &u) const {
|
|
return std::hash<std::optional<sstring>>()(u.name);
|
|
}
|
|
};
|
|
|
|
}
|