Commit 57d14644 authored by Quxl's avatar Quxl

x

parent ba192c5a
...@@ -23,17 +23,17 @@ import com.alibaba.fastjson.JSONObject; ...@@ -23,17 +23,17 @@ import com.alibaba.fastjson.JSONObject;
public interface OAuthApi { public interface OAuthApi {
OAuthConfig getOAuthConfig(); OAuthConfig getOAuthConfig();
HttpServletRequest getHttpServletRequest(); HttpServletRequest getHttpServletRequest();
HttpServletResponse getHttpServletResponse(); HttpServletResponse getHttpServletResponse();
final String OAUTH_TOKEN_SESSION = "OAUTH2_TOKEN_SESSION_KEY"; final String OAUTH_TOKEN_SESSION = "OAUTH2_TOKEN_SESSION_KEY";
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,20 +41,25 @@ public interface OAuthApi { ...@@ -41,20 +41,25 @@ 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()).setParameter("scope", config.getScope()).setParameter("response_type", OAuth.OAUTH_CODE).setParameter("realm", config.getRealm()).setParameter("client_id", config.getClientId()).setParameter("redirect_uri", config.getRediretUrl()); UrlBuilder split = new UrlBuilder(config.getAuthorizeUrl())
.setParameter("scope", config.getScope())
.setParameter("response_type", OAuth.OAUTH_CODE)
.setParameter("realm", config.getRealm())
.setParameter("client_id", config.getClientId())
.setParameter("redirect_uri", config.getRediretUrl());
String urlString = split.toUrlString(); String urlString = split.toUrlString();
response.sendRedirect(urlString); response.sendRedirect(urlString);
} catch (Exception e) { } catch (Exception e) {
throw new OAuthApiException(e.getMessage(), e); throw new OAuthApiException(e.getMessage(), e);
} }
} }
default void doLogin() { default void doLogin() {
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();
...@@ -66,7 +71,8 @@ public interface OAuthApi { ...@@ -66,7 +71,8 @@ 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, OAuth.HttpMethod.POST); OAuthJSONAccessTokenResponse oAuthResponse = oAuthClient.accessToken(oauthClientRequest,
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();
...@@ -75,25 +81,25 @@ public interface OAuthApi { ...@@ -75,25 +81,25 @@ public interface OAuthApi {
token = new OAuthToken(accessToken, refreshToken, idToken, tokenType, expiresIn); token = new OAuthToken(accessToken, refreshToken, idToken, tokenType, expiresIn);
session.setAttribute(OAUTH_TOKEN_SESSION, token); session.setAttribute(OAUTH_TOKEN_SESSION, token);
} else { } else {
throw new OAuthApiException("OAuthToken already exists"); throw new OAuthApiException("OAuthToken already exists");
} }
} catch (Exception e) { } catch (Exception e) {
throw new OAuthApiException(e.getMessage(), e); throw new OAuthApiException(e.getMessage(), e);
} }
} }
default void doLogout() { default void doLogout() {
HttpServletRequest request = this.getHttpServletRequest(); HttpServletRequest request = this.getHttpServletRequest();
HttpSession session = request.getSession(); HttpSession session = request.getSession();
session.removeAttribute(OAUTH_TOKEN_SESSION); session.removeAttribute(OAUTH_TOKEN_SESSION);
session.invalidate(); session.invalidate();
} }
default void refresh() { default void refresh() {
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);
...@@ -102,7 +108,8 @@ public interface OAuthApi { ...@@ -102,7 +108,8 @@ 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, OAuth.HttpMethod.POST); OAuthJSONAccessTokenResponse oAuthResponse = oAuthClient.accessToken(oauthClientRequest,
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();
...@@ -114,7 +121,7 @@ public interface OAuthApi { ...@@ -114,7 +121,7 @@ public interface OAuthApi {
throw new OAuthApiException(e.getMessage(), e); throw new OAuthApiException(e.getMessage(), e);
} }
} }
default String doPost(String url, Map<String, String> headers, JSONObject data) { default String doPost(String url, Map<String, String> headers, JSONObject data) {
try { try {
String accept = "application/json"; String accept = "application/json";
...@@ -129,13 +136,15 @@ public interface OAuthApi { ...@@ -129,13 +136,15 @@ 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).buildQueryMessage(); OAuthClientRequest clientRequest = new OAuthBearerClientRequest(url).setAccessToken(accessToken)
.buildQueryMessage();
clientRequest.setBody(data.toString()); clientRequest.setBody(data.toString());
clientRequest.setHeaders(headers); clientRequest.setHeaders(headers);
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 (OAuthProblemException e) { } catch (OAuthProblemException e) {
...@@ -145,5 +154,5 @@ public interface OAuthApi { ...@@ -145,5 +154,5 @@ public interface OAuthApi {
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