Commit 45ec31fe authored by Quxl's avatar Quxl

x

parent 0d6622ec
...@@ -40,10 +40,28 @@ public interface OAuthApi { ...@@ -40,10 +40,28 @@ public interface OAuthApi {
} }
default OAuthToken getOAuthToken() { default OAuthToken getOAuthToken() {
HttpServletRequest request = this.getHttpServletRequest(); try {
HttpSession session = request.getSession(); HttpServletRequest request = this.getHttpServletRequest();
OAuthToken token = (OAuthToken)session.getAttribute(OAUTH_TOKEN_SESSION); HttpSession session = request.getSession();
return token; OAuthToken token = (OAuthToken)session.getAttribute(OAUTH_TOKEN_SESSION);
return token;
} catch (OAuthApiException e) {
throw e;
} catch (Throwable e) {
throw new OAuthApiException(e.getMessage(), e);
}
}
default void doOAuthLogin() {
try {
String redirectUrl = this.getOAuthPath();
HttpServletResponse response = this.getHttpServletResponse();
response.sendRedirect(redirectUrl);
} catch (OAuthApiException e) {
throw e;
} catch (Throwable e) {
throw new OAuthApiException(e.getMessage(), e);
}
} }
default String getOAuthPath() { default String getOAuthPath() {
...@@ -60,6 +78,8 @@ public interface OAuthApi { ...@@ -60,6 +78,8 @@ public interface OAuthApi {
redirectUrl = redirectUrl.replaceAll("\\+", "%20"); redirectUrl = redirectUrl.replaceAll("\\+", "%20");
logger.debug("redirect:" + redirectUrl); logger.debug("redirect:" + redirectUrl);
return redirectUrl; return redirectUrl;
} catch (OAuthApiException e) {
throw e;
} catch (Exception e) { } catch (Exception e) {
throw new OAuthApiException(e.getMessage(), e); throw new OAuthApiException(e.getMessage(), e);
} }
...@@ -93,16 +113,24 @@ public interface OAuthApi { ...@@ -93,16 +113,24 @@ public interface OAuthApi {
} else { } else {
throw new OAuthApiException("OAuthToken already exists"); throw new OAuthApiException("OAuthToken already exists");
} }
} catch (Exception e) { } catch (OAuthApiException e) {
throw e;
} catch (Throwable e) {
throw new OAuthApiException(e.getMessage(), e); throw new OAuthApiException(e.getMessage(), e);
} }
} }
default void doLogout() { default void doLogout() {
HttpServletRequest request = this.getHttpServletRequest(); try {
HttpSession session = request.getSession(); HttpServletRequest request = this.getHttpServletRequest();
session.removeAttribute(OAUTH_TOKEN_SESSION); HttpSession session = request.getSession();
session.invalidate(); session.removeAttribute(OAUTH_TOKEN_SESSION);
session.invalidate();
} catch (OAuthApiException e) {
throw e;
} catch (Throwable e) {
throw new OAuthApiException(e.getMessage(), e);
}
} }
default void refresh() { default void refresh() {
...@@ -126,7 +154,9 @@ public interface OAuthApi { ...@@ -126,7 +154,9 @@ public interface OAuthApi {
String tokenType = oAuthResponse.getTokenType(); String tokenType = oAuthResponse.getTokenType();
OAuthToken newToken = new OAuthToken(accessToken, refreshToken, idToken, tokenType, expiresIn); OAuthToken newToken = new OAuthToken(accessToken, refreshToken, idToken, tokenType, expiresIn);
session.setAttribute(OAUTH_TOKEN_SESSION, newToken); session.setAttribute(OAUTH_TOKEN_SESSION, newToken);
} catch (Exception e) { } catch (OAuthApiException e) {
throw e;
} catch (Throwable e) {
throw new OAuthApiException(e.getMessage(), e); throw new OAuthApiException(e.getMessage(), e);
} }
} }
...@@ -154,10 +184,12 @@ public interface OAuthApi { ...@@ -154,10 +184,12 @@ public interface OAuthApi {
OAuthResourceResponse resourceResponse = oAuthClient.resource(clientRequest, "POST", OAuthResourceResponse.class); OAuthResourceResponse resourceResponse = oAuthClient.resource(clientRequest, "POST", OAuthResourceResponse.class);
String resBody = resourceResponse.getBody(); String resBody = resourceResponse.getBody();
return resBody; return resBody;
} catch (OAuthApiException e) {
throw e;
} catch (OAuthProblemException e) { } catch (OAuthProblemException e) {
this.refresh(); this.refresh();
return doPost(url, headers, data); return doPost(url, headers, data);
} catch (Exception e) { } catch (Throwable e) {
throw new OAuthApiException(e.getMessage(), e); throw new OAuthApiException(e.getMessage(), e);
} }
} }
......
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