Files
memgraph/src/auth/auth.hpp
niko4299 e15576f56c [E129-MG <-T0982-MG] implement edge type filtering (#489)
* GRANT, REVOKE, DENY and access_checker DONE

* Added AccessChecker to ExecutionContext

* grammar expanded; (#462)

* current

* T0954 mg expand user and role to hold permissions on labels (#465)

* added FineGrainedAccessPermissions class to model

* expanded user and role with fine grained access permissions

* fixed grammar

* [E129 < T0953-MG] GRANT, DENY, REVOKE added in interpreter and mainVisitor (#464)

* GRANT, DENY, REVOKE added in interpreter and mainVisitor

* Commented labelPermissons

* remove labelsPermission adding

* Fixed

* Removed extra lambda

* fixed

* [E129<-T0955-MG] Expand ExecutionContext with label related information (#467)

* added

* Added FineGrainedAccessChecker to Context

* fixed

* Added filtering

* testing

* Added edge filtering to storage, need to add filtering in simple Expand in operator.cpp

* Removed storage changes

* MATCH filtering working

* EdgeTypeFiltering working, just need to test everything again

* Removed FineGrainedAccessChecker

* Removed Expand Path

* Fix

* Tested FineGrainedAccessHandler, need to test AuthChecker

* Added integration test for lba

* Fixed merge conflicts

* PR fix

* fixed

* PR fix

* Fix test

* removed .vscode, .cache, .githooks

* githooks

* added tests

* fixed build

* Changed ast.lcp and User pointer to value in context.hpp

* Fixed test

* Remove denies on grant all

* AuthChecker

* Pr fix, auth_checker still not fixed

* Create mg-glue and extract UserBasedAuthChecker from AuthChecker

* Build fixed, need to fix test

* e2e tests

* e2e test working

* Added unit test, e2e and FineGrainedChecker

* Mege E129, auth_checker tests

* Fixed test

* e2e fix

Co-authored-by: Boris Taševski <36607228+BorisTasevski@users.noreply.github.com>
Co-authored-by: josipmrden <josip.mrden@external-basf.com>
Co-authored-by: János Benjamin Antal <benjamin.antal@memgraph.io>
2022-08-16 15:57:23 +02:00

166 lines
4.4 KiB
C++

// Copyright 2022 Memgraph Ltd.
//
// 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.
//
//
#pragma once
#include <mutex>
#include <optional>
#include <vector>
#include "auth/exceptions.hpp"
#include "auth/models.hpp"
#include "auth/module.hpp"
#include "kvstore/kvstore.hpp"
#include "utils/settings.hpp"
namespace memgraph::auth {
/**
* This class serves as the main Authentication/Authorization storage.
* It provides functions for managing Users, Roles, Permissions and FineGrainedAccessPermissions.
* NOTE: The non-const functions in this class aren't thread safe.
* TODO (mferencevic): Disable user/role modification functions when they are
* being managed by the auth module.
*/
class Auth final {
public:
explicit Auth(const std::string &storage_directory);
/**
* Authenticates a user using his username and password.
*
* @param username
* @param password
*
* @return a user when the username and password match, nullopt otherwise
* @throw AuthException if unable to authenticate for whatever reason.
*/
std::optional<User> Authenticate(const std::string &username, const std::string &password);
/**
* Gets a user from the storage.
*
* @param username
*
* @return a user when the user exists, nullopt otherwise
* @throw AuthException if unable to load user data.
*/
std::optional<User> GetUser(const std::string &username) const;
/**
* Saves a user object to the storage.
*
* @param user
*
* @throw AuthException if unable to save the user.
*/
void SaveUser(const User &user);
/**
* Creates a user if the user doesn't exist.
*
* @param username
* @param password
*
* @return a user when the user is created, nullopt if the user exists
* @throw AuthException if unable to save the user.
*/
std::optional<User> AddUser(const std::string &username, const std::optional<std::string> &password = std::nullopt);
/**
* Removes a user from the storage.
*
* @param username
*
* @return `true` if the user existed and was removed, `false` if the user
* doesn't exist
* @throw AuthException if unable to remove the user.
*/
bool RemoveUser(const std::string &username);
/**
* Gets all users from the storage.
*
* @return a list of users
* @throw AuthException if unable to load user data.
*/
std::vector<User> AllUsers() const;
/**
* Returns whether there are users in the storage.
*
* @return `true` if the storage contains any users, `false` otherwise
*/
bool HasUsers() const;
/**
* Gets a role from the storage.
*
* @param rolename
*
* @return a role when the role exists, nullopt otherwise
* @throw AuthException if unable to load role data.
*/
std::optional<Role> GetRole(const std::string &rolename) const;
/**
* Saves a role object to the storage.
*
* @param role
*
* @throw AuthException if unable to save the role.
*/
void SaveRole(const Role &role);
/**
* Creates a role if the role doesn't exist.
*
* @param rolename
*
* @return a role when the role is created, nullopt if the role exists
* @throw AuthException if unable to save the role.
*/
std::optional<Role> AddRole(const std::string &rolename);
/**
* Removes a role from the storage.
*
* @param rolename
*
* @return `true` if the role existed and was removed, `false` if the role
* doesn't exist
* @throw AuthException if unable to remove the role.
*/
bool RemoveRole(const std::string &rolename);
/**
* Gets all roles from the storage.
*
* @return a list of roles
* @throw AuthException if unable to load role data.
*/
std::vector<Role> AllRoles() const;
/**
* Gets all users for a role from the storage.
*
* @param rolename
*
* @return a list of roles
* @throw AuthException if unable to load user data.
*/
std::vector<User> AllUsersForRole(const std::string &rolename) const;
private:
// Even though the `kvstore::KVStore` class is guaranteed to be thread-safe,
// Auth is not thread-safe because modifying users and roles might require
// more than one operation on the storage.
kvstore::KVStore storage_;
auth::Module module_;
};
} // namespace memgraph::auth