1 /*
2  * Copyright (c) 2015, 2021, Oracle and/or its affiliates.
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, version 2.0,
6  * as published by the Free Software Foundation.
7  *
8  * This program is also distributed with certain software (including
9  * but not limited to OpenSSL) that is licensed under separate terms,
10  * as designated in a particular file or component or in included license
11  * documentation.  The authors of MySQL hereby grant you an additional
12  * permission to link the program and your derivative works with the
13  * separately licensed software that they have included with MySQL.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License, version 2.0, for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23  * 02110-1301  USA
24  */
25 
26 #ifndef _NGS_PROTOCOL_AUTHENTICATION_H_
27 #define _NGS_PROTOCOL_AUTHENTICATION_H_
28 
29 
30 #include "ngs_common/bind.h"
31 #include "ngs/error_code.h"
32 #include "ngs/memory.h"
33 
34 
35 namespace ngs
36 {
37   class Session_interface;
38   class Authentication_handler;
39 
40   typedef Custom_allocator<Authentication_handler>::Unique_ptr Authentication_handler_ptr;
41 
42   class Authentication_handler
43   {
44   public:
45     enum Status
46     {
47       Ongoing,
48       Succeeded,
49       Failed,
50       Error
51     };
52 
53     struct Response
54     {
55       std::string data;
56       Status status;
57       int error_code;
58     };
59 
~Authentication_handler()60     virtual ~Authentication_handler() {}
61 
62     typedef Authentication_handler_ptr (*create)(Session_interface *session);
63 
64     virtual Response handle_start(const std::string &mechanism,
65                                   const std::string &data,
66                                   const std::string &initial_response) = 0;
67 
68     virtual Response handle_continue(const std::string &data) = 0;
69 
70     virtual void done() = 0;
71 
wrap_ptr(Authentication_handler * auth)72     static ngs::Authentication_handler_ptr wrap_ptr(Authentication_handler *auth)
73     {
74       return ngs::Authentication_handler_ptr(auth, ngs::bind(&ngs::Authentication_handler::done, ngs::placeholders::_1));
75     }
76 
77   protected:
78     std::string compute_password_hash(const std::string &password);
79     bool extract_null_terminated_element(const std::string &message, std::size_t &element_position, size_t element_size, char *output);
80   };
81 };
82 
83 #endif
84