2022-02-11 18:29:41 +08:00
|
|
|
// Copyright 2022 Memgraph Ltd.
|
2021-10-03 18:07:04 +08:00
|
|
|
//
|
|
|
|
// Licensed as a Memgraph Enterprise file under the Memgraph Enterprise
|
|
|
|
// License (the "License"); by using this file, you agree to be bound by the terms of the License, and you may not use
|
|
|
|
// this file except in compliance with the License. You may obtain a copy of the License at https://memgraph.com/legal.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
2018-07-27 16:54:20 +08:00
|
|
|
#pragma once
|
|
|
|
|
2019-04-23 17:00:49 +08:00
|
|
|
#include <optional>
|
2018-07-27 16:54:20 +08:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <json/json.hpp>
|
|
|
|
|
2022-03-09 22:53:33 +08:00
|
|
|
namespace memgraph::auth {
|
2018-08-14 17:34:00 +08:00
|
|
|
// These permissions must have values that are applicable for usage in a
|
|
|
|
// bitmask.
|
2021-01-19 19:10:06 +08:00
|
|
|
// clang-format off
|
2018-07-27 16:54:20 +08:00
|
|
|
enum class Permission : uint64_t {
|
2022-02-11 18:29:41 +08:00
|
|
|
MATCH = 1,
|
|
|
|
CREATE = 1U << 1U,
|
|
|
|
MERGE = 1U << 2U,
|
|
|
|
DELETE = 1U << 3U,
|
|
|
|
SET = 1U << 4U,
|
|
|
|
REMOVE = 1U << 5U,
|
|
|
|
INDEX = 1U << 6U,
|
|
|
|
STATS = 1U << 7U,
|
|
|
|
CONSTRAINT = 1U << 8U,
|
|
|
|
DUMP = 1U << 9U,
|
|
|
|
REPLICATION = 1U << 10U,
|
|
|
|
DURABILITY = 1U << 11U,
|
|
|
|
READ_FILE = 1U << 12U,
|
|
|
|
FREE_MEMORY = 1U << 13U,
|
|
|
|
TRIGGER = 1U << 14U,
|
|
|
|
CONFIG = 1U << 15U,
|
|
|
|
AUTH = 1U << 16U,
|
|
|
|
STREAM = 1U << 17U,
|
|
|
|
MODULE_READ = 1U << 18U,
|
2022-02-17 17:35:48 +08:00
|
|
|
MODULE_WRITE = 1U << 19U,
|
2022-07-11 15:20:15 +08:00
|
|
|
WEBSOCKET = 1U << 20U,
|
|
|
|
SCHEMA = 1U << 21U
|
2018-08-14 17:34:00 +08:00
|
|
|
};
|
2021-01-19 19:10:06 +08:00
|
|
|
// clang-format on
|
2018-07-27 16:54:20 +08:00
|
|
|
|
2018-08-14 17:34:00 +08:00
|
|
|
// Function that converts a permission to its string representation.
|
|
|
|
std::string PermissionToString(Permission permission);
|
2018-07-27 16:54:20 +08:00
|
|
|
|
2018-08-14 17:34:00 +08:00
|
|
|
// Class that indicates what permission level the user/role has.
|
2018-07-27 16:54:20 +08:00
|
|
|
enum class PermissionLevel {
|
2018-08-14 17:34:00 +08:00
|
|
|
GRANT,
|
|
|
|
NEUTRAL,
|
|
|
|
DENY,
|
2018-07-27 16:54:20 +08:00
|
|
|
};
|
|
|
|
|
2018-08-22 16:59:46 +08:00
|
|
|
// Function that converts a permission level to its string representation.
|
|
|
|
std::string PermissionLevelToString(PermissionLevel level);
|
|
|
|
|
2018-07-27 16:54:20 +08:00
|
|
|
class Permissions final {
|
|
|
|
public:
|
|
|
|
Permissions(uint64_t grants = 0, uint64_t denies = 0);
|
|
|
|
|
|
|
|
PermissionLevel Has(Permission permission) const;
|
|
|
|
|
|
|
|
void Grant(Permission permission);
|
|
|
|
|
|
|
|
void Revoke(Permission permission);
|
|
|
|
|
|
|
|
void Deny(Permission permission);
|
|
|
|
|
2018-08-14 17:34:00 +08:00
|
|
|
std::vector<Permission> GetGrants() const;
|
|
|
|
|
|
|
|
std::vector<Permission> GetDenies() const;
|
|
|
|
|
2018-07-27 16:54:20 +08:00
|
|
|
nlohmann::json Serialize() const;
|
|
|
|
|
2020-01-15 20:57:58 +08:00
|
|
|
/// @throw AuthException if unable to deserialize.
|
2018-07-27 16:54:20 +08:00
|
|
|
static Permissions Deserialize(const nlohmann::json &data);
|
|
|
|
|
|
|
|
uint64_t grants() const;
|
|
|
|
uint64_t denies() const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
uint64_t grants_{0};
|
|
|
|
uint64_t denies_{0};
|
|
|
|
};
|
|
|
|
|
|
|
|
bool operator==(const Permissions &first, const Permissions &second);
|
|
|
|
|
|
|
|
bool operator!=(const Permissions &first, const Permissions &second);
|
|
|
|
|
|
|
|
class Role final {
|
|
|
|
public:
|
|
|
|
Role(const std::string &rolename);
|
|
|
|
|
|
|
|
Role(const std::string &rolename, const Permissions &permissions);
|
|
|
|
|
|
|
|
const std::string &rolename() const;
|
|
|
|
const Permissions &permissions() const;
|
|
|
|
Permissions &permissions();
|
|
|
|
|
|
|
|
nlohmann::json Serialize() const;
|
|
|
|
|
2020-01-15 20:57:58 +08:00
|
|
|
/// @throw AuthException if unable to deserialize.
|
2018-07-27 16:54:20 +08:00
|
|
|
static Role Deserialize(const nlohmann::json &data);
|
|
|
|
|
|
|
|
friend bool operator==(const Role &first, const Role &second);
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string rolename_;
|
|
|
|
Permissions permissions_;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool operator==(const Role &first, const Role &second);
|
|
|
|
|
|
|
|
// TODO (mferencevic): Implement password expiry.
|
|
|
|
class User final {
|
|
|
|
public:
|
|
|
|
User(const std::string &username);
|
|
|
|
|
2021-02-18 22:32:43 +08:00
|
|
|
User(const std::string &username, const std::string &password_hash, const Permissions &permissions);
|
2018-07-27 16:54:20 +08:00
|
|
|
|
2020-01-15 20:57:58 +08:00
|
|
|
/// @throw AuthException if unable to verify the password.
|
2018-07-27 16:54:20 +08:00
|
|
|
bool CheckPassword(const std::string &password);
|
|
|
|
|
2020-01-15 20:57:58 +08:00
|
|
|
/// @throw AuthException if unable to set the password.
|
2021-02-18 22:32:43 +08:00
|
|
|
void UpdatePassword(const std::optional<std::string> &password = std::nullopt);
|
2018-07-27 16:54:20 +08:00
|
|
|
|
|
|
|
void SetRole(const Role &role);
|
|
|
|
|
2018-08-14 17:34:00 +08:00
|
|
|
void ClearRole();
|
|
|
|
|
2021-07-22 22:22:08 +08:00
|
|
|
Permissions GetPermissions() const;
|
2018-07-27 16:54:20 +08:00
|
|
|
|
|
|
|
const std::string &username() const;
|
|
|
|
|
2018-08-22 16:59:46 +08:00
|
|
|
const Permissions &permissions() const;
|
2018-07-27 16:54:20 +08:00
|
|
|
Permissions &permissions();
|
|
|
|
|
2021-07-22 22:22:08 +08:00
|
|
|
const Role *role() const;
|
2018-07-27 16:54:20 +08:00
|
|
|
|
|
|
|
nlohmann::json Serialize() const;
|
|
|
|
|
2020-01-15 20:57:58 +08:00
|
|
|
/// @throw AuthException if unable to deserialize.
|
2018-07-27 16:54:20 +08:00
|
|
|
static User Deserialize(const nlohmann::json &data);
|
|
|
|
|
|
|
|
friend bool operator==(const User &first, const User &second);
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string username_;
|
|
|
|
std::string password_hash_;
|
|
|
|
Permissions permissions_;
|
2019-04-23 17:00:49 +08:00
|
|
|
std::optional<Role> role_;
|
2018-07-27 16:54:20 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
bool operator==(const User &first, const User &second);
|
2022-03-09 22:53:33 +08:00
|
|
|
} // namespace memgraph::auth
|