use of won.owner.service.impl.KeystoreEnabledUserDetails in project webofneeds by researchstudio-sat.
the class RestUserController method isSignedIn.
/**
* Method only accessible if the user's still signed in / the session's still
* valid {@literal ->} Use it to check the session cookie.
*/
// * @param user user object
// * @param request
// * @param response
// * @return
//
@ResponseBody
@RequestMapping(value = "/isSignedIn", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET)
@Transactional(propagation = Propagation.REQUIRED)
public // TODO: move transactionality annotation into the service layer
ResponseEntity isSignedIn(HttpServletRequest request, HttpServletResponse response) {
logger.debug("processing request to /isSignedIn");
// Execution will only get here, if the session is still valid, so sending OK
// here is enough. Spring sends an error
// code by itself if the session isn't valid any more
SecurityContext context = SecurityContextHolder.getContext();
Authentication authentication = null;
if (context != null) {
authentication = context.getAuthentication();
}
if (authentication == null) {
authentication = rememberMeServices.autoLogin(request, response);
} else if (authentication instanceof AnonymousAuthenticationToken) {
// if we're anonymous, try to see if we can reactivate a remember-me session
Authentication anonAuth = authentication;
authentication = rememberMeServices.autoLogin(request, response);
if (authentication == null) {
authentication = anonAuth;
}
}
if (authentication == null || "anonymousUser".equals(authentication.getPrincipal())) {
return generateStatusResponse(RestStatusResponse.USER_NOT_SIGNED_IN);
} else {
SecurityContextHolder.getContext().setAuthentication(authentication);
User authUser = ((KeystoreEnabledUserDetails) authentication.getPrincipal()).getUser();
return generateUserResponse(userService.getByUsername(authUser.getUsername()));
}
}
use of won.owner.service.impl.KeystoreEnabledUserDetails in project webofneeds by researchstudio-sat.
the class RestUserController method exportAccount.
@ResponseBody
@RequestMapping(value = "/exportAccount", method = RequestMethod.POST)
public ResponseEntity exportAccount(@RequestParam(name = "keyStorePassword", required = false) String keyStorePassword) {
logger.debug("processing request to /exportAccount");
SecurityContext securityContext = SecurityContextHolder.getContext();
User authUser = ((KeystoreEnabledUserDetails) securityContext.getAuthentication().getPrincipal()).getUser();
User user = userService.getByUsername(authUser.getUsername());
String responseEmail = null;
if (user.isEmailVerified()) {
responseEmail = user.getEmail();
} else {
return generateStatusResponse(RestStatusResponse.EXPORT_NOT_VERIFIED);
}
eventPublisher.publishEvent(new OnExportUserEvent(securityContext.getAuthentication(), keyStorePassword, responseEmail));
return generateStatusResponse(RestStatusResponse.EXPORT_SUCCESS);
}
use of won.owner.service.impl.KeystoreEnabledUserDetails in project webofneeds by researchstudio-sat.
the class ExportListener method onApplicationEvent.
@Override
public void onApplicationEvent(OnExportUserEvent onExportUserEvent) {
Authentication authentication = onExportUserEvent.getAuthentication();
KeystoreEnabledUserDetails userDetails = ((KeystoreEnabledUserDetails) authentication.getPrincipal());
String password = onExportUserEvent.getKeyStorePassword();
User user = userService.getByUsername(userDetails.getUsername());
String responseMail = onExportUserEvent.getResponseEmail();
File tmpFile = null;
try {
tmpFile = File.createTempFile("won", null);
tmpFile.deleteOnExit();
ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(tmpFile), Charset.forName("UTF-8"));
ZipEntry atomsEntry = new ZipEntry("export.nq");
zip.putNextEntry(atomsEntry);
user.getUserAtoms().stream().parallel().map(userAtom -> fetchAtomData(authentication, userAtom.getUri())).forEach(dataset -> {
RDFDataMgr.write(zip, dataset, RDFFormat.NQUADS_UTF8);
});
zip.closeEntry();
ZipEntry keystoreEntry = new ZipEntry("keystore.jks");
zip.putNextEntry(keystoreEntry);
if (password != null && !password.isEmpty()) {
ByteArrayOutputStream tmpStream = new ByteArrayOutputStream();
userDetails.getKeyStore().store(tmpStream, password.toCharArray());
tmpStream.writeTo(zip);
} else {
zip.write("You need to supply a keyStorePassword to get your keystore for security reasons".getBytes());
}
zip.closeEntry();
zip.close();
emailSender.sendExportMessage(onExportUserEvent.getResponseEmail(), tmpFile);
} catch (LinkedDataFetchingException e) {
logger.warn(e.getMessage());
emailSender.sendExportFailedMessage(responseMail);
} catch (KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException e) {
emailSender.sendExportFailedMessage(responseMail);
throw new RuntimeException(e);
} catch (Exception e) {
emailSender.sendExportFailedMessage(responseMail);
throw e;
} finally {
if (tmpFile != null) {
tmpFile.delete();
}
}
}
Aggregations