Skip to content

Commit 63e7f57

Browse files
优化简单模式和授权码模式
1 parent e8e6123 commit 63e7f57

File tree

1 file changed

+21
-25
lines changed

1 file changed

+21
-25
lines changed

src/main/java/net/ymate/module/oauth/controller/OAuthSnsController.java

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import net.ymate.framework.webmvc.support.UserSessionBean;
2323
import net.ymate.module.oauth.IOAuth;
2424
import net.ymate.module.oauth.OAuth;
25-
import net.ymate.module.oauth.OAuthCode;
2625
import net.ymate.module.oauth.intercept.SnsAccessTokenCheckInterceptor;
2726
import net.ymate.module.oauth.support.OAuthResponseUtils;
2827
import net.ymate.platform.core.beans.annotation.Before;
@@ -96,23 +95,7 @@ public IView authorize(@RequestParam(defaultValue = "false") Boolean authorized)
9695
.setParam(org.apache.oltu.oauth2.common.OAuth.OAUTH_STATE, _state)
9796
.buildQueryMessage();
9897
} else {
99-
switch (_responseType) {
100-
case CODE:
101-
OAuthCode _authzCode = _authzHelper.createOrUpdateAuthCode(_redirectURI, _scope);
102-
//
103-
_response = OAuthASResponse.authorizationResponse(_request, HttpServletResponse.SC_FOUND)
104-
.location(_redirectURI)
105-
.setCode(_authzCode.getCode())
106-
.setParam(org.apache.oltu.oauth2.common.OAuth.OAUTH_STATE, _state)
107-
.buildQueryMessage();
108-
break;
109-
case TOKEN:
110-
IOAuth.IOAuthTokenHelper _tokenHelper = OAuth.get().tokenHelper(_oauthRequest.getClientId(), _oauthRequest.getClientSecret(), _oauthRequest.getParam(org.apache.oltu.oauth2.common.OAuth.OAUTH_CODE), _uid);
111-
_response = OAuthResponseUtils.tokenToResponse(_tokenHelper.createOrUpdateAccessToken(), _state);
112-
break;
113-
default:
114-
_response = OAuthResponseUtils.badRequest(OAuthError.CodeResponse.UNSUPPORTED_RESPONSE_TYPE);
115-
}
98+
_response = __parseResponseType(_request, _responseType, _authzHelper, _oauthRequest, _redirectURI, _scope, _uid, _state);
11699
}
117100
} else {
118101
_response = OAuthResponseUtils.badRequest(OAuthError.TokenResponse.INVALID_REQUEST);
@@ -126,13 +109,7 @@ public IView authorize(@RequestParam(defaultValue = "false") Boolean authorized)
126109
.addAttribute("client_icon", _authzHelper.getOAuthClient().getIconUrl())
127110
.addAttribute("client_domain", _authzHelper.getOAuthClient().getDomain());
128111
} else {
129-
OAuthCode _authzCode = _authzHelper.createOrUpdateAuthCode(_redirectURI, _scope);
130-
//
131-
_response = OAuthASResponse.authorizationResponse(_request, HttpServletResponse.SC_FOUND)
132-
.location(_redirectURI)
133-
.setCode(_authzCode.getCode())
134-
.setParam(org.apache.oltu.oauth2.common.OAuth.OAUTH_STATE, _state)
135-
.buildQueryMessage();
112+
_response = __parseResponseType(_request, _responseType, _authzHelper, _oauthRequest, _redirectURI, _scope, _uid, _state);
136113
}
137114
}
138115
return View.httpStatusView(_response.getResponseStatus()).addHeader("Location", _response.getLocationUri());
@@ -148,6 +125,25 @@ public IView authorize(@RequestParam(defaultValue = "false") Boolean authorized)
148125
return WebUtils.buildErrorView(WebContext.getContext().getOwner(), 0, _response.getBody());
149126
}
150127

128+
private OAuthResponse __parseResponseType(HttpServletRequest request, ResponseType _responseType, IOAuth.IOAuthAuthzHelper _authzHelper, OAuthAuthzRequest _oauthRequest, String _redirectURI, String _scope, String uid, String state) throws Exception {
129+
OAuthResponse _response;
130+
switch (_responseType) {
131+
case CODE:
132+
_response = OAuthASResponse.authorizationResponse(request, HttpServletResponse.SC_FOUND)
133+
.location(_redirectURI)
134+
.setCode(_authzHelper.createOrUpdateAuthCode(_redirectURI, _scope).getCode())
135+
.setParam(org.apache.oltu.oauth2.common.OAuth.OAUTH_STATE, state)
136+
.buildQueryMessage();
137+
break;
138+
case TOKEN:
139+
_response = OAuthResponseUtils.tokenToResponse(OAuth.get().tokenHelper(_oauthRequest.getClientId(), _oauthRequest.getClientSecret(), _oauthRequest.getParam(org.apache.oltu.oauth2.common.OAuth.OAUTH_CODE), uid).createOrUpdateAccessToken(), state);
140+
break;
141+
default:
142+
_response = OAuthResponseUtils.badRequest(OAuthError.CodeResponse.UNSUPPORTED_RESPONSE_TYPE);
143+
}
144+
return _response;
145+
}
146+
151147
/**
152148
* @return 返回访问凭证 (grant_type=[authorization_code|password])
153149
* @throws Exception 可能产生的任何异常

0 commit comments

Comments
 (0)