Václav Kubernát | babbab9 | 2021-01-27 09:25:05 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 13 | namespace velia::system { |
| 14 | struct User { |
| 15 | std::string name; |
| 16 | std::vector<std::string> authorizedKeys; |
| 17 | std::optional<std::string> lastPasswordChange; |
| 18 | }; |
| 19 | |
| 20 | class AuthException : public std::runtime_error { |
| 21 | public: |
| 22 | using std::runtime_error::runtime_error; |
| 23 | }; |
| 24 | |
| 25 | namespace impl { |
Tomáš Pecka | d9e741f | 2021-02-10 15:51:17 +0100 | [diff] [blame] | 26 | void changePassword(const std::string& name, const std::string& password, const std::string& etc_shadow); |
Václav Kubernát | babbab9 | 2021-01-27 09:25:05 +0100 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | class Authentication { |
| 30 | public: |
Tomáš Pecka | d9e741f | 2021-02-10 15:51:17 +0100 | [diff] [blame] | 31 | using ChangePassword = std::function<void(const std::string& name, const std::string& password, const std::string& etc_shadow)>; |
Václav Kubernát | babbab9 | 2021-01-27 09:25:05 +0100 | [diff] [blame] | 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 | |
| 35 | private: |
| 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 | } |