DefaultAuthenticators.java 6.92 KB
Newer Older
KingRainbow44's avatar
KingRainbow44 committed
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package emu.grasscutter.auth;

import emu.grasscutter.Grasscutter;
import emu.grasscutter.auth.AuthenticationSystem.AuthenticationRequest;
import emu.grasscutter.database.DatabaseHelper;
import emu.grasscutter.game.Account;
import emu.grasscutter.server.http.objects.*;

import static emu.grasscutter.Configuration.*;
import static emu.grasscutter.utils.Language.translate;

/**
 * A class containing default authenticators.
 */
public final class DefaultAuthenticators {
    
    /**
     * Handles the authentication request from the username & password form.
     */
    public static class PasswordAuthenticator implements Authenticator<LoginResultJson> {
        @Override public LoginResultJson authenticate(AuthenticationRequest request) {
            var response = new LoginResultJson();
            
            var requestData = request.getPasswordRequest();
            assert requestData != null; // This should never be null.
            
            boolean successfulLogin = false; 
            String address = request.getRequest().ip();
            String responseMessage = translate("messages.dispatch.account.username_error");
            
            // Get account from database.
            Account account = DatabaseHelper.getAccountByName(requestData.account);
            
            // Check if account exists.
            if(account == null && ACCOUNT.autoCreate) {
                // This account has been created AUTOMATICALLY. There will be no permissions added.
                account = DatabaseHelper.createAccountWithId(requestData.account, 0);
                
                // Check if the account was created successfully.
                if(account == null) {
                    responseMessage = translate("messages.dispatch.account.username_create_error");
                    Grasscutter.getLogger().info(translate("messages.dispatch.account.account_login_create_error", address));
                } else {
                    // Add default permissions.
                    for (var permission : ACCOUNT.defaultPermissions)
                        account.addPermission(permission);

                    // Continue with login.
                    successfulLogin = true;

                    // Log the creation.
                    Grasscutter.getLogger().info(translate("messages.dispatch.account.account_login_create_success", address, response.data.account.uid));
                }
            } else if(account != null)
                successfulLogin = true;
            
            // Set response data.
            if(successfulLogin) {
                response.message = "OK";
                response.data.account.uid = account.getId();
                response.data.account.token = account.generateSessionKey();
                response.data.account.email = account.getEmail();
                
                // Log the login.
                Grasscutter.getLogger().info(translate("messages.dispatch.account.login_success", address, account.getId()));
            } else {
                response.retcode = -201;
                response.message = responseMessage;
                
                // Log the failure.
                Grasscutter.getLogger().info(translate("messages.dispatch.account.account_login_exist_error", address));
            }
            
            return response;
        }
    }

    /**
     * Handles the authentication request from the game when using a registry token.
     */
    public static class TokenAuthenticator implements Authenticator<LoginResultJson> {
        @Override public LoginResultJson authenticate(AuthenticationRequest request) {
            var response = new LoginResultJson();
            
            var requestData = request.getTokenRequest();
            assert requestData != null;
            
            boolean successfulLogin;
            String address = request.getRequest().ip();
            
            // Log the attempt.
            Grasscutter.getLogger().info(translate("messages.dispatch.account.login_token_attempt", address));
            
            // Get account from database.
            Account account = DatabaseHelper.getAccountById(requestData.uid);
            
            // Check if account exists/token is valid.
            successfulLogin = account != null && account.getSessionKey().equals(requestData.token);
            
            // Set response data.
            if(successfulLogin) {
                response.message = "OK";
                response.data.account.uid = account.getId();
                response.data.account.token = account.getSessionKey();
                response.data.account.email = account.getEmail();
                
                // Log the login.
                Grasscutter.getLogger().info(translate("messages.dispatch.account.login_token_success", address, requestData.uid));
            } else {
                response.retcode = -201;
                response.message = translate("messages.dispatch.account.account_cache_error");
                
                // Log the failure.
                Grasscutter.getLogger().info(translate("messages.dispatch.account.login_token_error", address));
            }
            
            return response;
        }
    }

    /**
     * Handles the authentication request from the game when using a combo token/session key.
     */
    public static class SessionKeyAuthenticator implements Authenticator<ComboTokenResJson> {
        @Override public ComboTokenResJson authenticate(AuthenticationRequest request) {
            var response  = new ComboTokenResJson();
            
            var requestData = request.getSessionKeyRequest();
            var loginData = request.getSessionKeyData();
            assert requestData != null; assert loginData != null;
            
            boolean successfulLogin;
            String address = request.getRequest().ip();
            
            // Get account from database.
            Account account = DatabaseHelper.getAccountById(loginData.uid);
            
            // Check if account exists/token is valid.
            successfulLogin = account != null && account.getSessionKey().equals(loginData.token);
            
            // Set response data.
            if(successfulLogin) {
                response.message = "OK";
                response.data.open_id = account.getId();
                response.data.combo_id = "157795300";
                response.data.combo_token = account.generateLoginToken();
                
                // Log the login.
                Grasscutter.getLogger().info(translate("messages.dispatch.account.combo_token_success", address));
            } else {
                response.retcode = -201;
                response.message = translate("messages.dispatch.account.session_key_error");
                
                // Log the failure.
                Grasscutter.getLogger().info(translate("messages.dispatch.account.combo_token_error", address));
            }
            
            return response;
        }
    }
}