Commit c1deebfc authored by Quxl's avatar Quxl

x

parent d68cf3a3
...@@ -35,7 +35,7 @@ public interface OAuthApi { ...@@ -35,7 +35,7 @@ public interface OAuthApi {
final String OAUTH_TOKEN_SESSION = "OAUTH2_TOKEN_SESSION_KEY"; final String OAUTH_TOKEN_SESSION = "OAUTH2_TOKEN_SESSION_KEY";
default boolean isLogin() { default boolean isExistsOAuthToken() {
return getOAuthToken() != null; return getOAuthToken() != null;
} }
...@@ -88,31 +88,25 @@ public interface OAuthApi { ...@@ -88,31 +88,25 @@ public interface OAuthApi {
default void callback() { default void callback() {
try { try {
HttpServletRequest request = this.getHttpServletRequest(); HttpServletRequest request = this.getHttpServletRequest();
HttpSession session = request.getSession(); OAuthConfig config = this.getOAuthConfig();
OAuthToken token = (OAuthToken)session.getAttribute(OAUTH_TOKEN_SESSION); OAuthAuthzResponse oauthAuthzResponse = OAuthAuthzResponse.oauthCodeAuthzResponse(request);
if(token == null) { String code = oauthAuthzResponse.getCode();
OAuthConfig config = this.getOAuthConfig(); TokenRequestBuilder builder = OAuthClientRequest.tokenLocation(config.getAccessTokenUrl());
OAuthAuthzResponse oauthAuthzResponse = OAuthAuthzResponse.oauthCodeAuthzResponse(request); builder.setGrantType(GrantType.AUTHORIZATION_CODE);
String code = oauthAuthzResponse.getCode(); builder.setClientId(config.getClientId());
TokenRequestBuilder builder = OAuthClientRequest.tokenLocation(config.getAccessTokenUrl()); builder.setClientSecret(config.getClientSecret());
builder.setGrantType(GrantType.AUTHORIZATION_CODE); builder.setRedirectURI(config.getRediretUrl());
builder.setClientId(config.getClientId()); builder.setCode(code);
builder.setClientSecret(config.getClientSecret()); OAuthClientRequest oauthClientRequest = builder.buildQueryMessage();
builder.setRedirectURI(config.getRediretUrl()); OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());
builder.setCode(code); OAuthJSONAccessTokenResponse oAuthResponse = oAuthClient.accessToken(oauthClientRequest, OAuth.HttpMethod.POST);
OAuthClientRequest oauthClientRequest = builder.buildQueryMessage(); String accessToken = oAuthResponse.getAccessToken();
OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient()); String refreshToken = oAuthResponse.getRefreshToken();
OAuthJSONAccessTokenResponse oAuthResponse = oAuthClient.accessToken(oauthClientRequest, OAuth.HttpMethod.POST); Long expiresIn = oAuthResponse.getExpiresIn();
String accessToken = oAuthResponse.getAccessToken(); String idToken = oAuthResponse.getParam("id_token");
String refreshToken = oAuthResponse.getRefreshToken(); String tokenType = oAuthResponse.getTokenType();
Long expiresIn = oAuthResponse.getExpiresIn(); OAuthToken token = new OAuthToken(accessToken, refreshToken, idToken, tokenType, expiresIn);
String idToken = oAuthResponse.getParam("id_token"); saveOAuthToken(token);
String tokenType = oAuthResponse.getTokenType();
token = new OAuthToken(accessToken, refreshToken, idToken, tokenType, expiresIn);
session.setAttribute(OAUTH_TOKEN_SESSION, token);
} else {
throw new OAuthApiException("OAuthToken already exists");
}
} catch (OAuthApiException e) { } catch (OAuthApiException e) {
throw e; throw e;
} catch (Throwable e) { } catch (Throwable e) {
...@@ -120,6 +114,12 @@ public interface OAuthApi { ...@@ -120,6 +114,12 @@ public interface OAuthApi {
} }
} }
default void saveOAuthToken(OAuthToken token) {
HttpServletRequest request = this.getHttpServletRequest();
HttpSession session = request.getSession();
session.setAttribute(OAUTH_TOKEN_SESSION, token);
}
default void removeOAuthToken() { default void removeOAuthToken() {
try { try {
HttpServletRequest request = this.getHttpServletRequest(); HttpServletRequest request = this.getHttpServletRequest();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment