Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
ziqian zhang
Grasscutter
Commits
d15c32df
Commit
d15c32df
authored
May 17, 2022
by
YukariChiba
Committed by
Melledy
May 17, 2022
Browse files
Add OAuth in AuthenticationSystem
parent
0c652180
Changes
5
Show whitespace changes
Inline
Side-by-side
src/main/java/emu/grasscutter/auth/AuthenticationSystem.java
View file @
d15c32df
...
...
@@ -60,6 +60,12 @@ public interface AuthenticationSystem {
*/
ExternalAuthenticator
getExternalAuthenticator
();
/**
* This is the authenticator used for handling OAuth authentication requests.
* @return An authenticator.
*/
OAuthAuthenticator
getOAuthAuthenticator
();
/**
* A data container that holds relevant data for authenticating a client.
*/
...
...
@@ -125,4 +131,16 @@ public interface AuthenticationSystem {
return
AuthenticationRequest
.
builder
().
request
(
request
)
.
response
(
response
).
build
();
}
/**
* Generates an authentication request from a {@link Response} object.
* @param request The Express request.
* @param jsonData The JSON data.
* @return An authentication request.
*/
static
AuthenticationRequest
fromOAuthRequest
(
Request
request
,
Response
response
)
{
return
AuthenticationRequest
.
builder
().
request
(
request
)
.
response
(
response
).
build
();
}
}
src/main/java/emu/grasscutter/auth/DefaultAuthentication.java
View file @
d15c32df
...
...
@@ -17,6 +17,7 @@ public final class DefaultAuthentication implements AuthenticationSystem {
private
final
Authenticator
<
LoginResultJson
>
tokenAuthenticator
=
new
TokenAuthenticator
();
private
final
Authenticator
<
ComboTokenResJson
>
sessionKeyAuthenticator
=
new
SessionKeyAuthenticator
();
private
final
ExternalAuthenticator
externalAuthenticator
=
new
ExternalAuthentication
();
private
final
OAuthAuthenticator
oAuthAuthenticator
=
new
OAuthAuthentication
();
@Override
public
void
createAccount
(
String
username
,
String
password
)
{
...
...
@@ -53,4 +54,9 @@ public final class DefaultAuthentication implements AuthenticationSystem {
public
ExternalAuthenticator
getExternalAuthenticator
()
{
return
this
.
externalAuthenticator
;
}
@Override
public
OAuthAuthenticator
getOAuthAuthenticator
()
{
return
this
.
oAuthAuthenticator
;
}
}
src/main/java/emu/grasscutter/auth/DefaultAuthenticators.java
View file @
d15c32df
...
...
@@ -174,4 +174,29 @@ public final class DefaultAuthenticators {
request
.
getResponse
().
send
(
"Authentication is not available with the default authentication method."
);
}
}
/**
* Handles authentication requests from OAuth sources.
*/
public
static
class
OAuthAuthentication
implements
OAuthAuthenticator
{
@Override
public
void
handleLogin
(
AuthenticationRequest
request
)
{
assert
request
.
getResponse
()
!=
null
;
request
.
getResponse
().
send
(
"Authentication is not available with the default authentication method."
);
}
@Override
public
void
handleDesktopRedirection
(
AuthenticationRequest
request
)
{
assert
request
.
getResponse
()
!=
null
;
request
.
getResponse
().
send
(
"Authentication is not available with the default authentication method."
);
}
@Override
public
void
handleMobileRedirection
(
AuthenticationRequest
request
)
{
assert
request
.
getResponse
()
!=
null
;
request
.
getResponse
().
send
(
"Authentication is not available with the default authentication method."
);
}
@Override
public
void
handleTokenProcess
(
AuthenticationRequest
request
)
{
assert
request
.
getResponse
()
!=
null
;
request
.
getResponse
().
send
(
"Authentication is not available with the default authentication method."
);
}
}
}
src/main/java/emu/grasscutter/auth/OAuthAuthenticator.java
0 → 100644
View file @
d15c32df
package
emu.grasscutter.auth
;
import
emu.grasscutter.auth.AuthenticationSystem.AuthenticationRequest
;
/**
* Handles authentication via OAuth routes.
*/
public
interface
OAuthAuthenticator
{
/**
* Called when an OAuth login request is made.
* @param request The authentication request.
*/
void
handleLogin
(
AuthenticationRequest
request
);
/**
* Called when an client requests to redirect to login page.
* @param request The authentication request.
*/
void
handleDesktopRedirection
(
AuthenticationRequest
request
);
void
handleMobileRedirection
(
AuthenticationRequest
request
);
/**
* Called when an OAuth login requests callback.
* @param request The authentication request.
*/
void
handleTokenProcess
(
AuthenticationRequest
request
);
}
src/main/java/emu/grasscutter/server/http/dispatch/DispatchHandler.java
View file @
d15c32df
...
...
@@ -33,6 +33,14 @@ public final class DispatchHandler implements Router {
.
handleAccountCreation
(
AuthenticationSystem
.
fromExternalRequest
(
request
,
response
)));
express
.
post
(
"/authentication/change_password"
,
(
request
,
response
)
->
Grasscutter
.
getAuthenticationSystem
().
getExternalAuthenticator
()
.
handlePasswordReset
(
AuthenticationSystem
.
fromExternalRequest
(
request
,
response
)));
// OAuth login
express
.
post
(
"/hk4e_global/mdk/shield/api/loginByThirdparty"
,
(
request
,
response
)
->
Grasscutter
.
getAuthenticationSystem
().
getOAuthAuthenticator
().
handleLogin
(
AuthenticationSystem
.
fromOAuthRequest
(
request
,
response
)));
// OAuth querystring convert redirection
express
.
get
(
"/authentication/openid/redirect"
,
(
request
,
response
)
->
Grasscutter
.
getAuthenticationSystem
().
getOAuthAuthenticator
().
handleTokenProcess
(
AuthenticationSystem
.
fromOAuthRequest
(
request
,
response
)));
// OAuth redirection
express
.
get
(
"/Api/twitter_login"
,
(
request
,
response
)
->
Grasscutter
.
getAuthenticationSystem
().
getOAuthAuthenticator
().
handleDesktopRedirection
(
AuthenticationSystem
.
fromOAuthRequest
(
request
,
response
)));
express
.
get
(
"/sdkTwitterLogin.html"
,
(
request
,
response
)
->
Grasscutter
.
getAuthenticationSystem
().
getOAuthAuthenticator
().
handleMobileRedirection
(
AuthenticationSystem
.
fromOAuthRequest
(
request
,
response
)));
}
/**
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment