1 /*
2   Copyright (c) 2018, Oracle and/or its affiliates. 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, 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 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  02110-1301  USA
23 */
24 
25 #ifndef ROUTER_HTTP_AUTH_METHOD_BASIC_INCLUDED
26 #define ROUTER_HTTP_AUTH_METHOD_BASIC_INCLUDED
27 
28 #include <string>
29 #include <system_error>
30 
31 #include "http_auth_method.h"
32 #include "mysqlrouter/http_server_export.h"
33 
34 /**
35  * Basic Authentication for HTTP.
36  *
37  * Credentials (username:password) are wrapped in Base64. Not encrypted, must be
38  * over secure channel.
39  *
40  * @see RFC 7235
41  *
42  * @startuml
43  * participant C
44  * participant S
45  *
46  * C->S: GET / HTTP/1.1
47  * S->C: HTTP/1.1 401 Unauthed\nWWW-Authenticate: Basic realm="..."
48  *
49  * C->S: GET / HTTP/1.1\nAuthorization: Basic 34850872634
50  * alt success
51  * S->C: HTTP/1.1 200 Ok
52  * else failed
53  * S->C: HTTP/1.1 403 Forbidden
54  * end
55  * @enduml
56  */
57 class HTTP_SERVER_EXPORT HttpAuthMethodBasic : public HttpAuthMethod {
58  public:
59   static constexpr char kMethodName[] = "Basic";
60   struct AuthData {
61     std::string username;
62     std::string password;
63   };
64 
65   static AuthData decode_authorization(const std::string &http_auth_data,
66                                        std::error_code &ec);
67 
68   static std::string encode_authorization(const AuthData &auth_data);
69 };
70 
71 #endif
72