use of org.bimserver.models.store.OAuthServer 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.bimserver.models.store.OAuthServer in project BIMserver by opensourceBIM.
the class OAuthRegistrationServlet method service.
@Override
public void service(HttpServletRequest request, HttpServletResponse httpResponse) throws ServletException, IOException {
OAuthServerRegistrationRequest oauthRequest = null;
try {
oauthRequest = new OAuthServerRegistrationRequest(new JSONHttpServletRequestWrapper(request));
oauthRequest.discover();
oauthRequest.getClientUrl();
oauthRequest.getClientDescription();
oauthRequest.getRedirectURI();
try (DatabaseSession session = getBimServer().getDatabase().createSession()) {
OAuthServer oAuthServer = session.querySingle(StorePackage.eINSTANCE.getOAuthServer_RedirectUrl(), oauthRequest.getRedirectURI());
GregorianCalendar now = new GregorianCalendar();
if (oAuthServer == null) {
oAuthServer = session.create(OAuthServer.class);
oAuthServer.setClientName(oauthRequest.getClientName());
oAuthServer.setClientUrl(oauthRequest.getClientUrl());
oAuthServer.setClientDescription(oauthRequest.getClientDescription());
if (oauthRequest.getClientIcon() != null) {
try {
byte[] icon = NetUtils.getContentAsBytes(new URL(oauthRequest.getClientIcon()), 5000);
oAuthServer.setClientIcon(icon);
} catch (Exception e) {
//
}
}
oAuthServer.setRedirectUrl(oauthRequest.getRedirectURI());
// DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
GregorianCalendar expires = new GregorianCalendar();
expires.add(Calendar.YEAR, 1);
String secret = new MD5Generator().generateValue();
oAuthServer.setIssuedAt(now.getTime());
oAuthServer.setExpiresAt(expires.getTime());
oAuthServer.setClientSecret(secret);
oAuthServer.setClientId(oauthRequest.getClientName().replace(" ", "").toLowerCase());
oAuthServer.setIncoming(true);
session.commit();
}
OAuthResponse response = OAuthServerRegistrationResponse.status(HttpServletResponse.SC_OK).setClientId(oAuthServer.getClientId()).setClientSecret(oAuthServer.getClientSecret()).setIssuedAt("" + oAuthServer.getIssuedAt().getTime()).setExpiresIn(oAuthServer.getExpiresAt().getTime() - now.getTimeInMillis()).setParam("message", "OK").buildJSONMessage();
httpResponse.setStatus(response.getResponseStatus());
httpResponse.setContentType(response.getHeaders().get("Content-Type"));
httpResponse.getWriter().write(response.getBody());
} catch (BimserverDatabaseException e) {
e.printStackTrace();
} catch (ServiceException e) {
e.printStackTrace();
}
} catch (OAuthProblemException e) {
OAuthResponse response;
try {
response = OAuthServerRegistrationResponse.errorResponse(HttpServletResponse.SC_BAD_REQUEST).error(e).buildJSONMessage();
httpResponse.setStatus(response.getResponseStatus());
httpResponse.getWriter().write(response.getBody());
} catch (OAuthSystemException e1) {
e1.printStackTrace();
}
} catch (OAuthSystemException e) {
e.printStackTrace();
}
}
use of org.bimserver.models.store.OAuthServer in project BIMserver by opensourceBIM.
the class OAuthServiceImpl method listRegisteredServersLocal.
@Override
public List<SOAuthServer> listRegisteredServersLocal() throws ServerException, UserException {
try (DatabaseSession session = getBimServer().getDatabase().createSession()) {
List<OAuthServer> allOfType = session.getAllOfType(StorePackage.eINSTANCE.getOAuthServer(), OAuthServer.class, OldQuery.getDefault());
Iterator<OAuthServer> iterator = allOfType.iterator();
while (iterator.hasNext()) {
OAuthServer next = iterator.next();
if (!next.isIncoming()) {
iterator.remove();
}
}
return getBimServer().getSConverter().convertToSListOAuthServer(allOfType);
} catch (BimserverDatabaseException e) {
return handleException(e);
}
}
use of org.bimserver.models.store.OAuthServer in project BIMserver by opensourceBIM.
the class OAuthServiceImpl method listRegisteredServers.
@Override
public List<SOAuthServer> listRegisteredServers() throws ServerException, UserException {
try (DatabaseSession session = getBimServer().getDatabase().createSession()) {
List<OAuthServer> allOfType = session.getAllOfType(StorePackage.eINSTANCE.getOAuthServer(), OAuthServer.class, OldQuery.getDefault());
Iterator<OAuthServer> iterator = allOfType.iterator();
while (iterator.hasNext()) {
OAuthServer next = iterator.next();
if (next.isIncoming()) {
iterator.remove();
}
}
return getBimServer().getSConverter().convertToSListOAuthServer(allOfType);
} catch (BimserverDatabaseException e) {
return handleException(e);
}
}
use of org.bimserver.models.store.OAuthServer in project BIMserver by opensourceBIM.
the class OAuthServiceImpl method generateForwardUrl.
public String generateForwardUrl(String registrationEndpoint, String authorizeUrl, String returnUrl) throws ServerException, UserException {
try (DatabaseSession session = getBimServer().getDatabase().createSession()) {
OAuthServer oAuthServer = session.querySingle(StorePackage.eINSTANCE.getOAuthServer_RegistrationEndpoint(), registrationEndpoint);
if (oAuthServer == null) {
throw new UserException("Application not registered");
}
OAuthClientRequest request2 = OAuthClientRequest.authorizationLocation(authorizeUrl).setParameter("auth_type", "service").setClientId(oAuthServer.getClientId()).setRedirectURI(returnUrl).setResponseType(ResponseType.CODE.toString()).setState("state").buildQueryMessage();
return request2.getLocationUri();
} catch (Exception e) {
return handleException(e);
}
}
Aggregations