Commit 2499d0e4 authored by Quxl's avatar Quxl

x

parent 8b82e539
......@@ -17,17 +17,28 @@ import org.apache.oltu.oauth2.client.response.OAuthJSONAccessTokenResponse;
import org.apache.oltu.oauth2.client.response.OAuthResourceResponse;
import org.apache.oltu.oauth2.common.OAuth;
import org.apache.oltu.oauth2.common.exception.OAuthProblemException;
import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
import org.apache.oltu.oauth2.common.message.types.GrantType;
public interface OAuthApi {
OAuthConfig getOAuthConfig();
HttpServletRequest getHttpServletRequest();
HttpServletResponse getHttpServletResponse();
final String OAUTH_TOKEN_SESSION = "OAUTH2_TOKEN_SESSION_KEY";
default boolean isLogin() {
HttpServletRequest request = this.getHttpServletRequest();
HttpSession session = request.getSession();
OAuthToken token = (OAuthToken)session.getAttribute(OAUTH_TOKEN_SESSION);
return token != null;
}
default void sendOAuthLoginRedirect(HttpServletResponse response) {
default void sendOAuthLoginRedirect() {
try {
HttpServletResponse response = this.getHttpServletResponse();
OAuthConfig config = this.getOAuthConfig();
AuthenticationRequestBuilder builder = OAuthClientRequest.authorizationLocation(config.getAuthorizeUrl());
builder.setResponseType(OAuth.OAUTH_CODE);
......@@ -41,14 +52,9 @@ public interface OAuthApi {
}
}
default boolean isLogin(HttpServletRequest request) {
HttpSession session = request.getSession();
OAuthToken token = (OAuthToken)session.getAttribute(OAUTH_TOKEN_SESSION);
return token != null;
}
default OAuthToken getOAuthToken(HttpServletRequest request) throws OAuthProblemException, OAuthSystemException {
default OAuthToken getOAuthToken() {
try {
HttpServletRequest request = this.getHttpServletRequest();
HttpSession session = request.getSession();
OAuthToken token = (OAuthToken)session.getAttribute(OAUTH_TOKEN_SESSION);
if(token == null) {
......@@ -78,8 +84,9 @@ public interface OAuthApi {
}
}
default OAuthToken refreshOAuthToken(HttpServletRequest request) {
default OAuthToken refreshOAuthToken() {
try {
HttpServletRequest request = this.getHttpServletRequest();
HttpSession session = request.getSession();
OAuthToken token = (OAuthToken)session.getAttribute(OAUTH_TOKEN_SESSION);
OAuthConfig config = this.getOAuthConfig();
......@@ -104,33 +111,25 @@ public interface OAuthApi {
}
}
default String doRequest(String method, String url, HttpServletRequest req) {
default String doRequest(String method, String url, Map<String, Object> parameters) {
try {
HttpSession session = req.getSession();
HttpServletRequest request = this.getHttpServletRequest();
HttpSession session = request.getSession();
OAuthToken token = (OAuthToken)session.getAttribute(OAUTH_TOKEN_SESSION);
Map<String, String[]> parameters = req.getParameterMap();
OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());
String accessToken = token.getAccessToken();
OAuthClientRequest request = new OAuthBearerClientRequest(url).setAccessToken(accessToken).buildQueryMessage();
OAuthClientRequest clientRequest = new OAuthBearerClientRequest(url).setAccessToken(accessToken).buildQueryMessage();
String body = StringUtil.toQueryString(parameters);
request.setBody(body);
OAuthResourceResponse resourceResponse = oAuthClient.resource(request, method, OAuthResourceResponse.class);
clientRequest.setBody(body);
OAuthResourceResponse resourceResponse = oAuthClient.resource(clientRequest, method, OAuthResourceResponse.class);
String resBody = resourceResponse.getBody();
return resBody;
} catch (OAuthProblemException e) {
this.refreshOAuthToken(req);
return doRequest(method, url, req);
this.refreshOAuthToken();
return doRequest(method, url, parameters);
} catch (Exception e) {
throw new OAuthApiException(e.getMessage(), e);
}
}
default String doGetResource(String url, HttpServletRequest req) {
return this.doRequest("GET", url, req);
}
default String doPostResource(String url, HttpServletRequest req) {
return this.doRequest("POST", url, req);
}
}
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