blob: 4e73d97a59f0b498a6f91adb8f43cbde9e3b3034 [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
Václav Kubernát7efd6d52021-11-09 01:31:11 +010010#include <map>
Jan Kundrát1c3b8812021-05-17 13:06:03 +020011#include <optional>
12#include <string>
Václav Kubernátbabbab92021-01-27 09:25:05 +010013#include <sysrepo-cpp/Session.hpp>
Jan Kundrát1c3b8812021-05-17 13:06:03 +020014#include <vector>
Václav Kubernátbabbab92021-01-27 09:25:05 +010015#include "utils/log-fwd.h"
16
17namespace velia::system {
18struct User {
19 std::string name;
20 std::vector<std::string> authorizedKeys;
21 std::optional<std::string> lastPasswordChange;
22};
23
24class AuthException : public std::runtime_error {
25public:
26 using std::runtime_error::runtime_error;
27};
28
29namespace impl {
Tomáš Peckad9e741f2021-02-10 15:51:17 +010030void changePassword(const std::string& name, const std::string& password, const std::string& etc_shadow);
Václav Kubernátbabbab92021-01-27 09:25:05 +010031}
32
33class Authentication {
34public:
Tomáš Peckad9e741f2021-02-10 15:51:17 +010035 using ChangePassword = std::function<void(const std::string& name, const std::string& password, const std::string& etc_shadow)>;
Václav Kubernátbabbab92021-01-27 09:25:05 +010036
Václav Kubernát7efd6d52021-11-09 01:31:11 +010037 Authentication(sysrepo::Session srSess, const std::string& etc_passwd, const std::string& etc_shadow, const std::string& authorized_keys_format, ChangePassword changePassword);
Václav Kubernátbabbab92021-01-27 09:25:05 +010038
39private:
40 std::vector<std::string> listKeys(const std::string& username);
41 std::string authorizedKeysPath(const std::string& username);
42 std::vector<User> listUsers();
43 void addKey(const std::string& username, const std::string& key);
44 void removeKey(const std::string& username, const int index);
45 std::string homeDirectory(const std::string& username);
Václav Kubernát8ea630e2021-02-18 16:55:25 +010046 std::map<std::string, std::optional<std::string>> lastPasswordChanges();
Václav Kubernátbabbab92021-01-27 09:25:05 +010047
48
49 velia::Log m_log;
50 std::string m_etc_passwd;
51 std::string m_etc_shadow;
52 std::string m_authorized_keys_format;
Václav Kubernát7efd6d52021-11-09 01:31:11 +010053 sysrepo::Session m_session;
54 std::optional<sysrepo::Subscription> m_sub;
Václav Kubernátbabbab92021-01-27 09:25:05 +010055};
56}