use of org.apache.oltu.oauth2.as.request.OAuthAuthzRequest in project BIMserver by opensourceBIM.
the class OAuthAuthorizationServlet method service.
@Override
public void service(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServletException, IOException {
OAuthAuthzRequest oauthRequest = null;
String authType = request.getParameter("auth_type");
if (request.getParameter("token") == null) {
String location = "/apps/bimviews/?page=OAuth&auth_type=" + authType + "&client_id=" + request.getParameter("client_id") + "&response_type=" + request.getParameter("response_type") + "&redirect_uri=" + request.getParameter("redirect_uri");
if (request.getParameter("state") != null) {
String state = request.getParameter("state");
LOGGER.info("Incoming state: " + state);
String encodedState = UrlEscapers.urlFragmentEscaper().escape(state);
LOGGER.info("Encoded state: " + encodedState);
location += "&state=" + encodedState;
}
LOGGER.info("Redirecting to " + location);
httpServletResponse.sendRedirect(location);
return;
}
OAuthAuthorizationCode oauthCode = null;
String token = request.getParameter("token");
try (DatabaseSession session = getBimServer().getDatabase().createSession()) {
OAuthServer oAuthServer = session.querySingle(StorePackage.eINSTANCE.getOAuthServer_ClientId(), request.getParameter("client_id"));
org.bimserver.webservices.authorization.Authorization realAuth = org.bimserver.webservices.authorization.Authorization.fromToken(getBimServer().getEncryptionKey(), token);
long uoid = realAuth.getUoid();
User user = session.get(uoid, OldQuery.getDefault());
for (OAuthAuthorizationCode oAuthAuthorizationCode : user.getOAuthIssuedAuthorizationCodes()) {
if (oAuthAuthorizationCode.getOauthServer() == oAuthServer) {
if (oAuthAuthorizationCode.getAuthorization() != null) {
oauthCode = oAuthAuthorizationCode;
}
}
}
try {
if (oauthCode == null) {
throw new ServletException("No auth found for token " + token);
}
oauthRequest = new OAuthAuthzRequest(request);
String responseType = oauthRequest.getParam(OAuth.OAUTH_RESPONSE_TYPE);
OAuthASResponse.OAuthAuthorizationResponseBuilder builder = OAuthASResponse.authorizationResponse(request, HttpServletResponse.SC_FOUND);
if (responseType.equals(ResponseType.CODE.toString())) {
builder.setCode(oauthCode.getCode());
// } else if (responseType.equals(ResponseType.TOKEN))) {
// builder.setAccessToken(oauthCode.get)
}
// if (responseType.equals(ResponseType.TOKEN.toString())) {
// builder.setAccessToken(oauthIssuerImpl.accessToken());
// // builder.setTokenType(OAuth.DEFAULT_TOKEN_TYPE.toString());
// builder.setExpiresIn(3600l);
// }
String redirectURI = oauthRequest.getParam(OAuth.OAUTH_REDIRECT_URI);
if (redirectURI != null && !redirectURI.equals("")) {
URI uri = makeUrl(redirectURI, oauthCode, builder);
LOGGER.info("Redirecting to " + uri);
httpServletResponse.sendRedirect(uri.toString());
} else {
URI uri = makeUrl("http://fakeaddress", oauthCode, builder);
httpServletResponse.getWriter().println("No redirectURI provided");
httpServletResponse.getWriter().println("Would have redirected to: " + uri);
}
} catch (OAuthProblemException e) {
final Response.ResponseBuilder responseBuilder = Response.status(HttpServletResponse.SC_FOUND);
String redirectUri = e.getRedirectUri();
if (OAuthUtils.isEmpty(redirectUri)) {
throw new WebApplicationException(responseBuilder.entity("OAuth callback url needs to be provided by client!!!").build());
}
try {
OAuthResponse response = OAuthASResponse.errorResponse(HttpServletResponse.SC_FOUND).error(e).location(redirectUri).buildQueryMessage();
// final URI location = new URI(response.getLocationUri());
httpServletResponse.sendRedirect(response.getLocationUri());
} catch (OAuthSystemException e1) {
e1.printStackTrace();
}
}
} catch (OAuthSystemException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (BimserverLockConflictException e2) {
e2.printStackTrace();
} catch (BimserverDatabaseException e2) {
e2.printStackTrace();
} catch (AuthenticationException e2) {
e2.printStackTrace();
}
}
use of org.apache.oltu.oauth2.as.request.OAuthAuthzRequest 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