Commit c1deebfc authored by Quxl's avatar Quxl

x

parent d68cf3a3
......@@ -35,7 +35,7 @@ public interface OAuthApi {
final String OAUTH_TOKEN_SESSION = "OAUTH2_TOKEN_SESSION_KEY";
default boolean isLogin() {
default boolean isExistsOAuthToken() {
return getOAuthToken() != null;
}
......@@ -88,31 +88,25 @@ public interface OAuthApi {
default void callback() {
try {
HttpServletRequest request = this.getHttpServletRequest();
HttpSession session = request.getSession();
OAuthToken token = (OAuthToken)session.getAttribute(OAUTH_TOKEN_SESSION);
if(token == null) {
OAuthConfig config = this.getOAuthConfig();
OAuthAuthzResponse oauthAuthzResponse = OAuthAuthzResponse.oauthCodeAuthzResponse(request);
String code = oauthAuthzResponse.getCode();
TokenRequestBuilder builder = OAuthClientRequest.tokenLocation(config.getAccessTokenUrl());
builder.setGrantType(GrantType.AUTHORIZATION_CODE);
builder.setClientId(config.getClientId());
builder.setClientSecret(config.getClientSecret());
builder.setRedirectURI(config.getRediretUrl());
builder.setCode(code);
OAuthClientRequest oauthClientRequest = builder.buildQueryMessage();
OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());
OAuthJSONAccessTokenResponse oAuthResponse = oAuthClient.accessToken(oauthClientRequest, OAuth.HttpMethod.POST);
String accessToken = oAuthResponse.getAccessToken();
String refreshToken = oAuthResponse.getRefreshToken();
Long expiresIn = oAuthResponse.getExpiresIn();
String idToken = oAuthResponse.getParam("id_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");
}
OAuthConfig config = this.getOAuthConfig();
OAuthAuthzResponse oauthAuthzResponse = OAuthAuthzResponse.oauthCodeAuthzResponse(request);
String code = oauthAuthzResponse.getCode();
TokenRequestBuilder builder = OAuthClientRequest.tokenLocation(config.getAccessTokenUrl());
builder.setGrantType(GrantType.AUTHORIZATION_CODE);
builder.setClientId(config.getClientId());
builder.setClientSecret(config.getClientSecret());
builder.setRedirectURI(config.getRediretUrl());
builder.setCode(code);
OAuthClientRequest oauthClientRequest = builder.buildQueryMessage();
OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());
OAuthJSONAccessTokenResponse oAuthResponse = oAuthClient.accessToken(oauthClientRequest, OAuth.HttpMethod.POST);
String accessToken = oAuthResponse.getAccessToken();
String refreshToken = oAuthResponse.getRefreshToken();
Long expiresIn = oAuthResponse.getExpiresIn();
String idToken = oAuthResponse.getParam("id_token");
String tokenType = oAuthResponse.getTokenType();
OAuthToken token = new OAuthToken(accessToken, refreshToken, idToken, tokenType, expiresIn);
saveOAuthToken(token);
} catch (OAuthApiException e) {
throw e;
} catch (Throwable e) {
......@@ -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() {
try {
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