1 /*
2  * Copyright (C) 2019 Emeric Poupon
3  *
4  * This file is part of LMS.
5  *
6  * LMS is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * LMS is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with LMS.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #pragma once
21 
22 #include <string_view>
23 
24 #include <Wt/WDateTime.h>
25 #include <Wt/Dbo/ptr.h>
26 
27 #include <boost/asio/ip/address.hpp>
28 #include "auth/Types.hpp"
29 #include "database/Types.hpp"
30 
31 namespace Database
32 {
33 	class Session;
34 	class User;
35 }
36 
37 namespace Auth
38 {
39 
40 	class IAuthTokenService;
41 
42 	class IPasswordService
43 	{
44 		public:
45 			virtual ~IPasswordService() = default;
46 
47 			struct CheckResult
48 			{
49 				enum class State
50 				{
51 					Granted,
52 					Denied,
53 					Throttled,
54 				};
55 				State state {State::Denied};
56 				std::optional<Database::IdType> userId {};
57 				std::optional<Wt::WDateTime> expiry {};
58 			};
59 			virtual CheckResult				checkUserPassword(Database::Session& session,
60 													const boost::asio::ip::address& clientAddress,
61 													std::string_view loginName,
62 													std::string_view password) = 0;
63 
64 			class PasswordTooWeakException : public Auth::Exception
65 			{
66 				public:
PasswordTooWeakException()67 					PasswordTooWeakException() : Auth::Exception {"Password too weak"} {}
68 			};
69 
70 			virtual bool					canSetPasswords() const = 0;
71 			virtual bool					isPasswordSecureEnough(std::string_view username, std::string_view password) const = 0;
72 			virtual void					setPassword(Database::Session& session, Database::IdType userId, std::string_view newPassword) = 0;
73 	};
74 
75 	std::unique_ptr<IPasswordService>	createPasswordService(std::string_view authPasswordBackend, std::size_t maxThrottlerEntryCount, IAuthTokenService& authTokenService);
76 }
77 
78