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 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame^] | 10 | #include <map> |
Jan Kundrát | 1c3b881 | 2021-05-17 13:06:03 +0200 | [diff] [blame] | 11 | #include <optional> |
| 12 | #include <string> |
Václav Kubernát | babbab9 | 2021-01-27 09:25:05 +0100 | [diff] [blame] | 13 | #include <sysrepo-cpp/Session.hpp> |
Jan Kundrát | 1c3b881 | 2021-05-17 13:06:03 +0200 | [diff] [blame] | 14 | #include <vector> |
Václav Kubernát | babbab9 | 2021-01-27 09:25:05 +0100 | [diff] [blame] | 15 | #include "utils/log-fwd.h" |
| 16 | |
| 17 | namespace velia::system { |
| 18 | struct User { |
| 19 | std::string name; |
| 20 | std::vector<std::string> authorizedKeys; |
| 21 | std::optional<std::string> lastPasswordChange; |
| 22 | }; |
| 23 | |
| 24 | class AuthException : public std::runtime_error { |
| 25 | public: |
| 26 | using std::runtime_error::runtime_error; |
| 27 | }; |
| 28 | |
| 29 | namespace impl { |
Tomáš Pecka | d9e741f | 2021-02-10 15:51:17 +0100 | [diff] [blame] | 30 | 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] | 31 | } |
| 32 | |
| 33 | class Authentication { |
| 34 | public: |
Tomáš Pecka | d9e741f | 2021-02-10 15:51:17 +0100 | [diff] [blame] | 35 | 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] | 36 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame^] | 37 | 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át | babbab9 | 2021-01-27 09:25:05 +0100 | [diff] [blame] | 38 | |
| 39 | private: |
| 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át | 8ea630e | 2021-02-18 16:55:25 +0100 | [diff] [blame] | 46 | std::map<std::string, std::optional<std::string>> lastPasswordChanges(); |
Václav Kubernát | babbab9 | 2021-01-27 09:25:05 +0100 | [diff] [blame] | 47 | |
| 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át | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame^] | 53 | sysrepo::Session m_session; |
| 54 | std::optional<sysrepo::Subscription> m_sub; |
Václav Kubernát | babbab9 | 2021-01-27 09:25:05 +0100 | [diff] [blame] | 55 | }; |
| 56 | } |