AuthenticationSystem.java 4.55 KB
Newer Older
KingRainbow44's avatar
KingRainbow44 committed
1
2
package emu.grasscutter.auth;

3
import emu.grasscutter.game.Account;
KingRainbow44's avatar
KingRainbow44 committed
4
5
import emu.grasscutter.server.http.objects.*;
import express.http.Request;
KingRainbow44's avatar
KingRainbow44 committed
6
import express.http.Response;
KingRainbow44's avatar
KingRainbow44 committed
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
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;

import javax.annotation.Nullable;

/**
 * Defines an authenticator for the server.
 * Can be changed by plugins.
 */
public interface AuthenticationSystem {
    
    /**
     * Called when a user requests to make an account.
     * @param username The provided username.
     * @param password The provided password. (SHA-256'ed)
     */
    void createAccount(String username, String password);

    /**
     * Called when a user requests to reset their password.
     * @param username The username of the account to reset.
     */
    void resetPassword(String username);

32
33
    /**
     * Called by plugins to internally verify a user's identity.
34
35
     * @param details A unique identifier to identify the user. (For example: a JWT token)
     * @return The user's account if the verification was successful, null if the user was unable to be verified.
36
     */
37
    Account verifyUser(String details);
38

KingRainbow44's avatar
KingRainbow44 committed
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
    /**
     * This is the authenticator used for password authentication.
     * @return An authenticator.
     */
    Authenticator<LoginResultJson> getPasswordAuthenticator();

    /**
     * This is the authenticator used for token authentication.
     * @return An authenticator.
     */
    Authenticator<LoginResultJson> getTokenAuthenticator();

    /**
     * This is the authenticator used for session authentication.
     * @return An authenticator.
     */
    Authenticator<ComboTokenResJson> getSessionKeyAuthenticator();

KingRainbow44's avatar
KingRainbow44 committed
57
58
59
60
61
62
    /**
     * This is the authenticator used for handling external authentication requests.
     * @return An authenticator.
     */
    ExternalAuthenticator getExternalAuthenticator();

63
64
65
66
67
68
    /**
     * This is the authenticator used for handling OAuth authentication requests.
     * @return An authenticator.
     */
    OAuthAuthenticator getOAuthAuthenticator();

KingRainbow44's avatar
KingRainbow44 committed
69
70
71
72
73
74
    /**
     * A data container that holds relevant data for authenticating a client.
     */
    @Builder @AllArgsConstructor @Getter
    class AuthenticationRequest {
        private final Request request;
KingRainbow44's avatar
KingRainbow44 committed
75
76
        @Nullable private final Response response;
        
KingRainbow44's avatar
KingRainbow44 committed
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
        @Nullable private final LoginAccountRequestJson passwordRequest;
        @Nullable private final LoginTokenRequestJson tokenRequest;
        @Nullable private final ComboTokenReqJson sessionKeyRequest;
        @Nullable private final ComboTokenReqJson.LoginTokenData sessionKeyData;
    }

    /**
     * Generates an authentication request from a {@link LoginAccountRequestJson} object.
     * @param request The Express request.
     * @param jsonData The JSON data.
     * @return An authentication request.
     */
    static AuthenticationRequest fromPasswordRequest(Request request, LoginAccountRequestJson jsonData) {
        return AuthenticationRequest.builder()
                .request(request)
                .passwordRequest(jsonData)
                .build();
    }

    /**
     * Generates an authentication request from a {@link LoginTokenRequestJson} object.
     * @param request The Express request.
     * @param jsonData The JSON data.
     * @return An authentication request.
     */
    static AuthenticationRequest fromTokenRequest(Request request, LoginTokenRequestJson jsonData) {
        return AuthenticationRequest.builder()
                .request(request)
                .tokenRequest(jsonData)
                .build();
    }

    /**
     * Generates an authentication request from a {@link ComboTokenReqJson} object.
     * @param request The Express request.
     * @param jsonData The JSON data.
     * @return An authentication request.
     */
    static AuthenticationRequest fromComboTokenRequest(Request request, ComboTokenReqJson jsonData, 
                                                       ComboTokenReqJson.LoginTokenData tokenData) {
        return AuthenticationRequest.builder()
                .request(request)
                .sessionKeyRequest(jsonData)
                .sessionKeyData(tokenData)
                .build();
    }
KingRainbow44's avatar
KingRainbow44 committed
123
124
125
126
127
128
129
130
131
132
133

    /**
     * Generates an authentication request from a {@link Response} object.
     * @param request The Express request.
     * @param response the Express response.
     * @return An authentication request.
     */
    static AuthenticationRequest fromExternalRequest(Request request, Response response) {
        return AuthenticationRequest.builder().request(request)
                .response(response).build();
    }
KingRainbow44's avatar
KingRainbow44 committed
134
}