use of org.apache.oltu.oauth2.ext.dynamicreg.server.request.OAuthServerRegistrationRequest 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();
}
}
Aggregations