Line data Source code
1 : // Snap Websites Server -- handle creating / encrypting password
2 : // Copyright (c) 2011-2019 Made to Order Software Corp. All Rights Reserved
3 : //
4 : // This program is free software; you can redistribute it and/or modify
5 : // it under the terms of the GNU General Public License as published by
6 : // the Free Software Foundation; either version 2 of the License, or
7 : // (at your option) any later version.
8 : //
9 : // This program is distributed in the hope that it will be useful,
10 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : // GNU General Public License for more details.
13 : //
14 : // You should have received a copy of the GNU General Public License
15 : // along with this program; if not, write to the Free Software
16 : // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 : #pragma once
18 :
19 : // snapwebsites lib
20 : //
21 : #include "snapwebsites/file_content.h"
22 : #include "snapwebsites/snap_exception.h"
23 :
24 :
25 : namespace snap
26 : {
27 :
28 0 : class password_exception : public snap::snap_exception
29 : {
30 : public:
31 0 : password_exception(char const * what_msg) : snap_exception(what_msg) {}
32 : password_exception(std::string const & what_msg) : snap_exception(what_msg) {}
33 0 : password_exception(QString const & what_msg) : snap_exception(what_msg) {}
34 : };
35 :
36 0 : class password_exception_function_failure : public password_exception
37 : {
38 : public:
39 : password_exception_function_failure(char const * what_msg) : password_exception(what_msg) {}
40 : password_exception_function_failure(std::string const & what_msg) : password_exception(what_msg) {}
41 0 : password_exception_function_failure(QString const & what_msg) : password_exception(what_msg) {}
42 : };
43 :
44 0 : class password_exception_invalid_parameter : public password_exception
45 : {
46 : public:
47 0 : password_exception_invalid_parameter(char const * what_msg) : password_exception(what_msg) {}
48 : password_exception_invalid_parameter(std::string const & what_msg) : password_exception(what_msg) {}
49 0 : password_exception_invalid_parameter(QString const & what_msg) : password_exception(what_msg) {}
50 : };
51 :
52 0 : class password_exception_digest_not_available : public password_exception
53 : {
54 : public:
55 0 : password_exception_digest_not_available(char const * what_msg) : password_exception(what_msg) {}
56 : password_exception_digest_not_available(std::string const & what_msg) : password_exception(what_msg) {}
57 : password_exception_digest_not_available(QString const & what_msg) : password_exception(what_msg) {}
58 : };
59 :
60 0 : class password_exception_encryption_failed : public password_exception
61 : {
62 : public:
63 0 : password_exception_encryption_failed(char const * what_msg) : password_exception(what_msg) {}
64 : password_exception_encryption_failed(std::string const & what_msg) : password_exception(what_msg) {}
65 : password_exception_encryption_failed(QString const & what_msg) : password_exception(what_msg) {}
66 : };
67 :
68 :
69 :
70 : class password
71 : {
72 : public:
73 : password();
74 : ~password();
75 :
76 : void set_digest(std::string const & digest);
77 : std::string const & get_digest() const;
78 :
79 : void generate_password(int min_length = 64);
80 : void set_plain_password(std::string const & plain_password, std::string const & salt = std::string());
81 : std::string const & get_plain_password() const;
82 : bool get_password_from_console(std::string const & salt = std::string());
83 :
84 : std::string const & get_salt() const;
85 :
86 : void set_encrypted_password(std::string const & encrypted_password, std::string const & salt = std::string());
87 : std::string const & get_encrypted_password() const;
88 :
89 : bool operator == (password const & rhs) const;
90 : bool operator < (password const & rhs) const;
91 :
92 : static void clear_string(std::string & str);
93 :
94 : private:
95 : void generate_password_salt();
96 : void encrypt_password();
97 :
98 : std::string f_plain_password = std::string();
99 : std::string f_encrypted_password = std::string();
100 : std::string f_salt = std::string();
101 : std::string f_digest = std::string("sha512");
102 : };
103 :
104 :
105 : class password_file
106 : {
107 : public:
108 : password_file(std::string const & password_filename);
109 : ~password_file();
110 :
111 : bool find(std::string const & name, password & p);
112 : bool save(std::string const & name, password const & p);
113 : bool remove(std::string const & name);
114 : std::string next(password & p);
115 : void rewind();
116 :
117 : private:
118 : bool load_passwords();
119 :
120 : bool f_file_loaded = false;
121 : std::string::size_type f_next = 0;
122 : file_content f_passwords;
123 : };
124 :
125 : } // snap namespace
126 : // vim: ts=4 sw=4 et
|