AuthenticationSystem.java 4.31 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
import emu.grasscutter.server.http.objects.*;
5
import io.javalin.http.Context;
KingRainbow44's avatar
KingRainbow44 committed
6
7
8
9
10
11
12
13
14
15
16
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 {
17

KingRainbow44's avatar
KingRainbow44 committed
18
19
20
21
22
23
24
25
26
27
28
29
30
    /**
     * 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);

31
32
    /**
     * Called by plugins to internally verify a user's identity.
33
34
     * @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.
35
     */
36
    Account verifyUser(String details);
37

KingRainbow44's avatar
KingRainbow44 committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
    /**
     * 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
56
57
58
59
60
61
    /**
     * This is the authenticator used for handling external authentication requests.
     * @return An authenticator.
     */
    ExternalAuthenticator getExternalAuthenticator();

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

KingRainbow44's avatar
KingRainbow44 committed
68
69
70
71
72
    /**
     * A data container that holds relevant data for authenticating a client.
     */
    @Builder @AllArgsConstructor @Getter
    class AuthenticationRequest {
73
74
        private final Context context;

KingRainbow44's avatar
KingRainbow44 committed
75
76
77
78
79
80
81
82
        @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.
83
     * @param ctx The Javalin context.
KingRainbow44's avatar
KingRainbow44 committed
84
85
86
     * @param jsonData The JSON data.
     * @return An authentication request.
     */
87
    static AuthenticationRequest fromPasswordRequest(Context ctx, LoginAccountRequestJson jsonData) {
KingRainbow44's avatar
KingRainbow44 committed
88
        return AuthenticationRequest.builder()
89
                .context(ctx)
KingRainbow44's avatar
KingRainbow44 committed
90
91
92
93
94
95
                .passwordRequest(jsonData)
                .build();
    }

    /**
     * Generates an authentication request from a {@link LoginTokenRequestJson} object.
96
     * @param ctx The Javalin context.
KingRainbow44's avatar
KingRainbow44 committed
97
98
99
     * @param jsonData The JSON data.
     * @return An authentication request.
     */
100
    static AuthenticationRequest fromTokenRequest(Context ctx, LoginTokenRequestJson jsonData) {
KingRainbow44's avatar
KingRainbow44 committed
101
        return AuthenticationRequest.builder()
102
                .context(ctx)
KingRainbow44's avatar
KingRainbow44 committed
103
104
105
106
107
108
                .tokenRequest(jsonData)
                .build();
    }

    /**
     * Generates an authentication request from a {@link ComboTokenReqJson} object.
109
     * @param ctx The Javalin context.
KingRainbow44's avatar
KingRainbow44 committed
110
111
112
     * @param jsonData The JSON data.
     * @return An authentication request.
     */
113
    static AuthenticationRequest fromComboTokenRequest(Context ctx, ComboTokenReqJson jsonData,
KingRainbow44's avatar
KingRainbow44 committed
114
115
                                                       ComboTokenReqJson.LoginTokenData tokenData) {
        return AuthenticationRequest.builder()
116
                .context(ctx)
KingRainbow44's avatar
KingRainbow44 committed
117
118
119
120
                .sessionKeyRequest(jsonData)
                .sessionKeyData(tokenData)
                .build();
    }
KingRainbow44's avatar
KingRainbow44 committed
121
122

    /**
123
124
     * Generates an authentication request from a {@link Context} object.
     * @param ctx The Javalin context.
KingRainbow44's avatar
KingRainbow44 committed
125
126
     * @return An authentication request.
     */
127
128
    static AuthenticationRequest fromExternalRequest(Context ctx) {
        return AuthenticationRequest.builder().context(ctx).build();
KingRainbow44's avatar
KingRainbow44 committed
129
    }
KingRainbow44's avatar
KingRainbow44 committed
130
}