navid/veins / 639918f360ca6c705b7f24f2279600affb9acd756e42c9f3df7d054dfb080205 / xveins/src/auth/auth.h

auth.h

← xveins/src/auth · 2219 bytes · raw · download

#pragma once

#include "crypto/crypto.h"

#include <cstdint>
#include <optional>
#include <string>
#include <string_view>
#include <vector>

namespace vn::auth {

constexpr std::uint8_t ROLE_ANONYMOUS = 0;
constexpr std::uint8_t ROLE_READ = 1;
constexpr std::uint8_t ROLE_WRITE = 2;
constexpr std::uint8_t ROLE_UNKNOWN = 255;

struct Key {
    std::string name;
    std::vector<std::uint8_t> seed;
    PublicKey public_key{};
    std::string fingerprint;
};

fs::path key_dir();
fs::path key_path(std::string_view name);
std::optional<Key> load_key(std::string_view name);
std::optional<Key> generate_key(std::string_view name);
std::vector<std::string> list_key_names();

// ---------------------------------------------------------------------------
// Per-user key selection
// ---------------------------------------------------------------------------

fs::path auth_dir();
std::string normalize_target(std::string_view target);
bool set_key(std::string_view target, std::string_view key_name);
bool remove_key(std::string_view target);
std::vector<std::string> list_credential_lines();
bool local_key_exists(std::string_view name);
std::optional<std::string> key_for_url(std::string_view url);

// ---------------------------------------------------------------------------
// Server access policy
// ---------------------------------------------------------------------------

struct AccessEntry {
    PublicKey public_key{};
    std::string name;
    std::uint8_t role = ROLE_READ;
};

struct Access {
    bool valid = true;
    std::int64_t error_line = 0;
    bool anonymous_read = false;
    std::vector<AccessEntry> entries;
};

Access empty_access();
fs::path access_path(const fs::path& repo_root);
Access load_access(const fs::path& repo_root);
bool write_access(const fs::path& repo_root, const Access& access);
bool add_access(const fs::path& repo_root, std::uint8_t role, const PublicKey& public_key,
                std::string_view name);
bool remove_access(const fs::path& repo_root, std::string_view fingerprint, std::string_view name);
bool set_anonymous(const fs::path& repo_root, bool enabled);
std::uint8_t role_for(const Access& access, const PublicKey* public_key);

} // namespace vn::auth