navid/veins / 849788d17ec456024834ad26378fdb084ec75cd2f7cdfef156cbb58b3db1f4bf / xveins/src/auth.h

auth.h

← xveins/src · 2383 bytes · raw · download

#pragma once

#include "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;

// ---------------------------------------------------------------------------
// Client keys
// ---------------------------------------------------------------------------

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