blob: f57d86f5ceb32d5a0ff016de9dd302ec7fc1c165 [file] [log] [blame]
Václav Kubernátbabbab92021-01-27 09:25:05 +01001/*
2 * Copyright (C) 2021 CESNET, https://photonics.cesnet.cz/
3 *
4 * Written by Václav Kubernát <kubernat@cesnet.cz>
5 *
6*/
7
8#pragma once
9
10#include <sysrepo-cpp/Session.hpp>
11#include "utils/log-fwd.h"
12
13namespace velia::system {
14struct User {
15 std::string name;
16 std::vector<std::string> authorizedKeys;
17 std::optional<std::string> lastPasswordChange;
18};
19
20class AuthException : public std::runtime_error {
21public:
22 using std::runtime_error::runtime_error;
23};
24
25namespace impl {
26void changePassword(const std::string& name, const std::string& password);
27}
28
29class Authentication {
30public:
31 using ChangePassword = std::function<void(const std::string& name, const std::string& password)>;
32
33 Authentication(sysrepo::S_Session srSess, const std::string& etc_passwd, const std::string& etc_shadow, const std::string& authorized_keys_format, ChangePassword changePassword);
34
35private:
36 std::vector<std::string> listKeys(const std::string& username);
37 std::string authorizedKeysPath(const std::string& username);
38 std::vector<User> listUsers();
39 void addKey(const std::string& username, const std::string& key);
40 void removeKey(const std::string& username, const int index);
41 std::string homeDirectory(const std::string& username);
42 std::optional<std::string> lastPasswordChange(const std::string& username);
43
44
45 velia::Log m_log;
46 std::string m_etc_passwd;
47 std::string m_etc_shadow;
48 std::string m_authorized_keys_format;
49 sysrepo::S_Session m_session;
50 sysrepo::S_Subscribe m_sub;
51};
52}