use of org.entando.entando.aps.system.services.oauth2.model.AuthorizationCode in project entando-core by entando.
the class AuthEndpointServlet method doGet.
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
OAuthAuthzRequest oauthRequest = null;
OAuthIssuerImpl oauthIssuerImpl = new OAuthIssuerImpl(new MD5Generator());
IApiOAuthorizationCodeManager codeManager = (IApiOAuthorizationCodeManager) ApsWebApplicationUtils.getBean(SystemConstants.OAUTH2_AUTHORIZATION_CODE_MANAGER, request);
try {
oauthRequest = new OAuthAuthzRequest(request);
if (validateClient(oauthRequest, request, response)) {
// build response according to response_type
String responseType = oauthRequest.getParam(OAuth.OAUTH_RESPONSE_TYPE) == null ? OAuth.OAUTH_RESPONSE_TYPE : oauthRequest.getParam(OAuth.OAUTH_RESPONSE_TYPE);
OAuthASResponse.OAuthAuthorizationResponseBuilder builder = OAuthASResponse.authorizationResponse(request, HttpServletResponse.SC_FOUND);
final String authorizationCode = oauthIssuerImpl.authorizationCode();
final int expires = 3;
AuthorizationCode authCode = new AuthorizationCode();
authCode.setAuthorizationCode(authorizationCode);
// gets a calendar using the default time zone and locale.
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.SECOND, expires);
authCode.setExpires(calendar.getTimeInMillis());
authCode.setClientId(oauthRequest.getClientId());
authCode.setSource(request.getRemoteAddr());
codeManager.addAuthorizationCode(authCode);
if (responseType.equals(ResponseType.CODE.toString())) {
builder.setCode(authorizationCode);
}
if (responseType.equals(ResponseType.TOKEN.toString())) {
builder.setAccessToken(authorizationCode);
builder.setExpiresIn((long) expires);
}
String redirectURI = oauthRequest.getParam(OAuth.OAUTH_REDIRECT_URI);
final OAuthResponse resp = builder.location(redirectURI).buildQueryMessage();
final int status = resp.getResponseStatus();
response.setStatus(status);
response.sendRedirect(resp.getLocationUri());
} else {
logger.warn("OAuth2 authentication failed");
response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
}
} catch (OAuthSystemException ex) {
logger.error("System exception {} ", ex.getMessage());
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
} catch (OAuthProblemException ex) {
logger.error("OAuth2 error {} ", ex.getMessage());
response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
} catch (IOException e) {
logger.error("IOException {} ", e);
}
}
Aggregations