Files
aevolution/include/evo/identity/identity.hpp
T
aevolution-gitops-bot ce1b5b8140
Foundation / checks (checks) (push) Successful in 32m12s
Foundation / checks (client) (push) Failing after 10m0s
Foundation / checks (deploy) (push) Successful in 3m19s
Foundation / checks (ecs) (push) Successful in 1m56s
Foundation / checks (network) (push) Successful in 13m21s
Foundation / checks (postgres) (push) Successful in 44m18s
Foundation / checks (tsan) (push) Successful in 7m58s
Foundation / postgres-tools (push) Skipped
Foundation / image (push) Skipped
Foundation / server-image (push) Skipped
Extend DEV M10 grant lifetime to four hours
2026-10-07 13:41:36 -04:00

127 lines
4.9 KiB
C++

#pragma once
#include "evo/network/session_grant.hpp"
#include <compare>
#include <cstddef>
#include <cstdint>
#include <expected>
#include <map>
#include <span>
#include <string>
#include <utility>
// M10.3 backend identity adapter (ADR-0091). An identity provider authenticates an
// external credential to a provider subject; the account directory maps subjects to
// provider-independent AccountIds and orders their grants; the issuer signs short-lived
// region-scoped session grants. Library only: no service, no Steam. Production
// providers plug in behind IdentityProvider later. Credentials are never stored or
// logged here.
namespace evo::identity {
using network::AccountId;
using network::DeploymentEnvironment;
enum class ProviderKind : std::uint8_t { dev_test = 1 };
struct ProviderSubject {
ProviderKind provider{};
std::string subject; // Provider-scoped, 1..128 bytes; never an account id.
auto operator<=>(const ProviderSubject&) const = default;
};
enum class IdentityError : std::uint8_t {
not_dev_environment,
wrong_environment,
unauthenticated,
invalid_request,
directory_full,
serial_exhausted,
signing
};
class IdentityProvider {
public:
IdentityProvider() = default;
IdentityProvider(const IdentityProvider&) = delete;
IdentityProvider& operator=(const IdentityProvider&) = delete;
virtual ~IdentityProvider() = default;
[[nodiscard]] virtual DeploymentEnvironment environment() const noexcept = 0;
[[nodiscard]] virtual std::expected<ProviderSubject, IdentityError>
authenticate(std::span<const std::uint8_t> credential) = 0;
protected:
IdentityProvider(IdentityProvider&&) = default;
IdentityProvider& operator=(IdentityProvider&&) = default;
};
// Dev-only test identities: the credential "dev-test:<name>" (name 1..32 of [a-z0-9-])
// authenticates as that name without any secret. It cannot be created outside dev.
class DevTestProvider final : public IdentityProvider {
public:
[[nodiscard]] static std::expected<DevTestProvider, IdentityError>
create(DeploymentEnvironment environment);
[[nodiscard]] DeploymentEnvironment environment() const noexcept override {
return DeploymentEnvironment::dev;
}
[[nodiscard]] std::expected<ProviderSubject, IdentityError>
authenticate(std::span<const std::uint8_t> credential) override;
private:
DevTestProvider() = default;
};
// Serial and issue time of the next grant for an account: both non-decreasing, serial
// strictly increasing (regions rely on this to bound how long serials are remembered).
struct GrantSequence {
std::uint64_t serial{}, issued_at{};
};
class AccountDirectory {
public:
AccountDirectory() = default;
AccountDirectory(const AccountDirectory&) = delete;
AccountDirectory& operator=(const AccountDirectory&) = delete;
virtual ~AccountDirectory() = default;
// The subject's account, created with a fresh random id on first sight.
[[nodiscard]] virtual std::expected<AccountId, IdentityError>
resolve(const ProviderSubject& subject) = 0;
[[nodiscard]] virtual std::expected<GrantSequence, IdentityError>
next_grant(const AccountId& account, std::uint64_t now_seconds) = 0;
protected:
AccountDirectory(AccountDirectory&&) = default;
AccountDirectory& operator=(AccountDirectory&&) = default;
};
// Bounded in-memory directory for tests and local tools; not durable.
class MemoryAccountDirectory final : public AccountDirectory {
public:
explicit MemoryAccountDirectory(std::size_t max_accounts) : max_accounts_{max_accounts} {}
[[nodiscard]] std::expected<AccountId, IdentityError>
resolve(const ProviderSubject& subject) override;
[[nodiscard]] std::expected<GrantSequence, IdentityError>
next_grant(const AccountId& account, std::uint64_t now_seconds) override;
private:
std::size_t max_accounts_;
std::map<ProviderSubject, AccountId> subjects_;
std::map<AccountId, GrantSequence> sequences_;
};
class GrantIssuer {
public:
// The provider and signing key must belong to the same environment. Lifetime is
// 1..14400 s; regions refuse lifetimes above their own maximum (300 s by default).
[[nodiscard]] static std::expected<GrantIssuer, IdentityError>
create(network::GrantSigningKey key, IdentityProvider& provider, AccountDirectory& directory,
std::uint64_t lifetime_seconds);
[[nodiscard]] std::expected<network::SessionGrant, IdentityError>
issue(std::span<const std::uint8_t> credential, world::WorldIdentity world,
std::uint64_t region, std::uint64_t now_seconds);
private:
GrantIssuer(network::GrantSigningKey key, IdentityProvider& provider,
AccountDirectory& directory, std::uint64_t lifetime)
: key_{std::move(key)}, provider_{&provider}, directory_{&directory}, lifetime_{lifetime} {}
network::GrantSigningKey key_;
IdentityProvider* provider_;
AccountDirectory* directory_;
std::uint64_t lifetime_;
};
} // namespace evo::identity