Commit 31c5abfd authored by Quxl's avatar Quxl

x

parent 57d14644
...@@ -10,6 +10,7 @@ import org.apache.oltu.oauth2.client.OAuthClient; ...@@ -10,6 +10,7 @@ import org.apache.oltu.oauth2.client.OAuthClient;
import org.apache.oltu.oauth2.client.URLConnectionClient; import org.apache.oltu.oauth2.client.URLConnectionClient;
import org.apache.oltu.oauth2.client.request.OAuthBearerClientRequest; import org.apache.oltu.oauth2.client.request.OAuthBearerClientRequest;
import org.apache.oltu.oauth2.client.request.OAuthClientRequest; import org.apache.oltu.oauth2.client.request.OAuthClientRequest;
import org.apache.oltu.oauth2.client.request.OAuthClientRequest.AuthenticationRequestBuilder;
import org.apache.oltu.oauth2.client.request.OAuthClientRequest.TokenRequestBuilder; import org.apache.oltu.oauth2.client.request.OAuthClientRequest.TokenRequestBuilder;
import org.apache.oltu.oauth2.client.response.OAuthAuthzResponse; import org.apache.oltu.oauth2.client.response.OAuthAuthzResponse;
import org.apache.oltu.oauth2.client.response.OAuthJSONAccessTokenResponse; import org.apache.oltu.oauth2.client.response.OAuthJSONAccessTokenResponse;
...@@ -33,7 +34,7 @@ public interface OAuthApi { ...@@ -33,7 +34,7 @@ public interface OAuthApi {
default boolean isLogin() { default boolean isLogin() {
HttpServletRequest request = this.getHttpServletRequest(); HttpServletRequest request = this.getHttpServletRequest();
HttpSession session = request.getSession(); HttpSession session = request.getSession();
OAuthToken token = (OAuthToken) session.getAttribute(OAUTH_TOKEN_SESSION); OAuthToken token = (OAuthToken)session.getAttribute(OAUTH_TOKEN_SESSION);
return token != null; return token != null;
} }
...@@ -41,14 +42,14 @@ public interface OAuthApi { ...@@ -41,14 +42,14 @@ public interface OAuthApi {
try { try {
HttpServletResponse response = this.getHttpServletResponse(); HttpServletResponse response = this.getHttpServletResponse();
OAuthConfig config = this.getOAuthConfig(); OAuthConfig config = this.getOAuthConfig();
UrlBuilder split = new UrlBuilder(config.getAuthorizeUrl()) AuthenticationRequestBuilder builder = OAuthClientRequest.authorizationLocation(config.getAuthorizeUrl());
.setParameter("scope", config.getScope()) builder.setResponseType(OAuth.OAUTH_CODE);
.setParameter("response_type", OAuth.OAUTH_CODE) builder.setClientId(config.getClientId());
.setParameter("realm", config.getRealm()) builder.setRedirectURI(config.getRediretUrl());
.setParameter("client_id", config.getClientId()) builder.setScope(config.getScope());
.setParameter("redirect_uri", config.getRediretUrl()); builder.setParameter("realm", config.getRealm());
String urlString = split.toUrlString(); OAuthClientRequest oauthResponse = builder.buildQueryMessage();
response.sendRedirect(urlString); response.sendRedirect(oauthResponse.getLocationUri());
} catch (Exception e) { } catch (Exception e) {
throw new OAuthApiException(e.getMessage(), e); throw new OAuthApiException(e.getMessage(), e);
} }
...@@ -58,8 +59,8 @@ public interface OAuthApi { ...@@ -58,8 +59,8 @@ public interface OAuthApi {
try { try {
HttpServletRequest request = this.getHttpServletRequest(); HttpServletRequest request = this.getHttpServletRequest();
HttpSession session = request.getSession(); HttpSession session = request.getSession();
OAuthToken token = (OAuthToken) session.getAttribute(OAUTH_TOKEN_SESSION); OAuthToken token = (OAuthToken)session.getAttribute(OAUTH_TOKEN_SESSION);
if (token == null) { if(token == null) {
OAuthConfig config = this.getOAuthConfig(); OAuthConfig config = this.getOAuthConfig();
OAuthAuthzResponse oauthAuthzResponse = OAuthAuthzResponse.oauthCodeAuthzResponse(request); OAuthAuthzResponse oauthAuthzResponse = OAuthAuthzResponse.oauthCodeAuthzResponse(request);
String code = oauthAuthzResponse.getCode(); String code = oauthAuthzResponse.getCode();
...@@ -71,8 +72,7 @@ public interface OAuthApi { ...@@ -71,8 +72,7 @@ public interface OAuthApi {
builder.setCode(code); builder.setCode(code);
OAuthClientRequest oauthClientRequest = builder.buildQueryMessage(); OAuthClientRequest oauthClientRequest = builder.buildQueryMessage();
OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient()); OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());
OAuthJSONAccessTokenResponse oAuthResponse = oAuthClient.accessToken(oauthClientRequest, OAuthJSONAccessTokenResponse oAuthResponse = oAuthClient.accessToken(oauthClientRequest, OAuth.HttpMethod.POST);
OAuth.HttpMethod.POST);
String accessToken = oAuthResponse.getAccessToken(); String accessToken = oAuthResponse.getAccessToken();
String refreshToken = oAuthResponse.getRefreshToken(); String refreshToken = oAuthResponse.getRefreshToken();
Long expiresIn = oAuthResponse.getExpiresIn(); Long expiresIn = oAuthResponse.getExpiresIn();
...@@ -99,7 +99,7 @@ public interface OAuthApi { ...@@ -99,7 +99,7 @@ public interface OAuthApi {
try { try {
HttpServletRequest request = this.getHttpServletRequest(); HttpServletRequest request = this.getHttpServletRequest();
HttpSession session = request.getSession(); HttpSession session = request.getSession();
OAuthToken token = (OAuthToken) session.getAttribute(OAUTH_TOKEN_SESSION); OAuthToken token = (OAuthToken)session.getAttribute(OAUTH_TOKEN_SESSION);
OAuthConfig config = this.getOAuthConfig(); OAuthConfig config = this.getOAuthConfig();
TokenRequestBuilder builder = OAuthClientRequest.tokenLocation(config.getAccessTokenUrl()); TokenRequestBuilder builder = OAuthClientRequest.tokenLocation(config.getAccessTokenUrl());
builder.setGrantType(GrantType.REFRESH_TOKEN); builder.setGrantType(GrantType.REFRESH_TOKEN);
...@@ -108,8 +108,7 @@ public interface OAuthApi { ...@@ -108,8 +108,7 @@ public interface OAuthApi {
builder.setClientSecret(config.getClientSecret()); builder.setClientSecret(config.getClientSecret());
OAuthClientRequest oauthClientRequest = builder.buildQueryMessage(); OAuthClientRequest oauthClientRequest = builder.buildQueryMessage();
OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient()); OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());
OAuthJSONAccessTokenResponse oAuthResponse = oAuthClient.accessToken(oauthClientRequest, OAuthJSONAccessTokenResponse oAuthResponse = oAuthClient.accessToken(oauthClientRequest, OAuth.HttpMethod.POST);
OAuth.HttpMethod.POST);
String accessToken = oAuthResponse.getAccessToken(); String accessToken = oAuthResponse.getAccessToken();
String refreshToken = oAuthResponse.getRefreshToken(); String refreshToken = oAuthResponse.getRefreshToken();
Long expiresIn = oAuthResponse.getExpiresIn(); Long expiresIn = oAuthResponse.getExpiresIn();
...@@ -136,15 +135,13 @@ public interface OAuthApi { ...@@ -136,15 +135,13 @@ public interface OAuthApi {
headers.put("X-SE-IFW-ApplicationName", applicationName); headers.put("X-SE-IFW-ApplicationName", applicationName);
HttpServletRequest request = this.getHttpServletRequest(); HttpServletRequest request = this.getHttpServletRequest();
HttpSession session = request.getSession(); HttpSession session = request.getSession();
OAuthToken token = (OAuthToken) session.getAttribute(OAUTH_TOKEN_SESSION); OAuthToken token = (OAuthToken)session.getAttribute(OAUTH_TOKEN_SESSION);
OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient()); OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());
String accessToken = token.getAccessToken(); String accessToken = token.getAccessToken();
OAuthClientRequest clientRequest = new OAuthBearerClientRequest(url).setAccessToken(accessToken) OAuthClientRequest clientRequest = new OAuthBearerClientRequest(url).setAccessToken(accessToken).buildQueryMessage();
.buildQueryMessage();
clientRequest.setBody(data.toString()); clientRequest.setBody(data.toString());
clientRequest.setHeaders(headers); clientRequest.setHeaders(headers);
OAuthResourceResponse resourceResponse = oAuthClient.resource(clientRequest, "POST", OAuthResourceResponse resourceResponse = oAuthClient.resource(clientRequest, "POST", OAuthResourceResponse.class);
OAuthResourceResponse.class);
String resBody = resourceResponse.getBody(); String resBody = resourceResponse.getBody();
return resBody; return resBody;
} catch (OAuthProblemException e) { } catch (OAuthProblemException 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