aboutsummaryrefslogtreecommitdiff
path: root/src/server/bnetserver/REST/LoginRESTService.h
blob: 1313493e0239481217e0e41dff6e9e0da41347b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef LoginRESTService_h__
#define LoginRESTService_h__

#include "HttpService.h"
#include "Login.pb.h"
#include "LoginHttpSession.h"

namespace Battlenet
{
enum class BanMode
{
    BAN_IP = 0,
    BAN_ACCOUNT = 1
};

class LoginRESTService : public Trinity::Net::Http::HttpService<LoginHttpSession>
{
public:
    using RequestHandlerResult = Trinity::Net::Http::RequestHandlerResult;
    using HttpRequest = Trinity::Net::Http::Request;
    using HttpResponse = Trinity::Net::Http::Response;
    using HttpRequestContext = Trinity::Net::Http::RequestContext;
    using HttpSessionState = Trinity::Net::Http::SessionState;

    LoginRESTService() : HttpService("login"), _port(0), _loginTicketDuration(0) { }

    static LoginRESTService& Instance();

    bool StartNetwork(Trinity::Asio::IoContext& ioContext, std::string const& bindIp, uint16 port, int32 threadCount = 1) override;

    std::string const& GetHostnameForClient(boost::asio::ip::address const& address) const;
    uint16 GetPort() const { return _port; }

private:
    static void OnSocketAccept(boost::asio::ip::tcp::socket&& sock, uint32 threadIndex);

    static std::string ExtractAuthorization(HttpRequest const& request);

    RequestHandlerResult HandleGetForm(std::shared_ptr<LoginHttpSession> session, HttpRequestContext& context);
    RequestHandlerResult HandleGetGameAccounts(std::shared_ptr<LoginHttpSession> session, HttpRequestContext& context);
    RequestHandlerResult HandleGetPortal(std::shared_ptr<LoginHttpSession> session, HttpRequestContext& context);

    RequestHandlerResult HandlePostLogin(std::shared_ptr<LoginHttpSession> session, HttpRequestContext& context);
    RequestHandlerResult HandlePostRefreshLoginTicket(std::shared_ptr<LoginHttpSession> session, HttpRequestContext& context);

    static std::string CalculateShaPassHash(std::string const& name, std::string const& password);

    JSON::Login::FormInputs _formInputs;
    std::string _bindIP;
    uint16 _port;
    std::array<std::string, 2> _hostnames;
    std::array<boost::asio::ip::address, 2> _addresses;
    uint32 _loginTicketDuration;
};
}

#define sLoginService Battlenet::LoginRESTService::Instance()

#endif // LoginRESTService_h__