Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.jdriven.RoboAdvice.Controller;
- import com.jdriven.RoboAdvice.DTO.CustomStrategyDTO;
- import com.jdriven.RoboAdvice.DTO.PortfolioDTO;
- import com.jdriven.RoboAdvice.DataModel.User;
- import com.jdriven.RoboAdvice.Model.GenericResponse;
- import com.jdriven.RoboAdvice.ServiceInterface.StrategyPerAssetService;
- import com.jdriven.RoboAdvice.ServiceInterface.ToUpdateQueueService;
- import com.jdriven.RoboAdvice.ServiceInterface.UserService;
- import com.jdriven.RoboAdvice.Utils.Constant;
- import com.jdriven.RoboAdvice.Utils.StrategyBuilder;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.*;
- import javax.crypto.SecretKeyFactory;
- import javax.crypto.spec.PBEKeySpec;
- import java.security.SecureRandom;
- import java.util.Base64;
- import java.util.NoSuchElementException;
- /**
- * Created by Davide on 01/03/17.
- */
- @CrossOrigin
- @RestController
- public class SignupController {
- //https://www.quandl.com/api/v3/datasets/COM/WLD_RICE_05.json?api_key=xPf3zsdBERWBxoRwy6Tt
- @Autowired
- private UserService userService;
- @Autowired
- private ToUpdateQueueService toUpdateQueueService;
- @Autowired
- private StrategyPerAssetService strategyPerAssetService;
- /* @PersistenceContext
- EntityManager em;
- public List<AssetHeld> findAssetsHeldByUser(Long userID) {
- return em.createNamedQuery("AssetClass.findAssetsHeldByUser").setParameter("uId",userID )
- .getResultList();
- }
- public List<StrategyPerAssetClass> findActiveStrategy(Long userID) {
- return em.createNamedQuery("StrategyPerAssetClass.findActiveStrategy").setParameter("uId",userID )
- .getResultList();
- }*/
- @RequestMapping(value = "/signup")
- public GenericResponse signUp(@RequestParam(value = "firstName") String firstName,
- @RequestParam(value = "lastName") String lastName,
- @RequestParam(value = "email") String email,
- @RequestParam(value = "password") String password,
- @RequestBody CustomStrategyDTO customStrategy) {
- if (firstName == null || lastName == null || email == null || password == null || userService.findByEmail(email) != null){
- return new GenericResponse(Constant.FAIL);
- }
- try {
- byte[] salt = new byte[16];
- SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
- sr.nextBytes(salt);
- PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), salt, 65536, 512);
- SecretKeyFactory f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
- byte[] hash = f.generateSecret(spec).getEncoded();
- Base64.Encoder enc = Base64.getEncoder();
- User newUser = User.builder().email(email).password(enc.encodeToString(hash)).salt(enc.encodeToString(salt)).firstName(firstName.substring(0, 1).toUpperCase() + firstName.substring(1)).lastName(lastName.substring(0, 1).toUpperCase() + lastName.substring(1)).build();
- userService.save(newUser);
- if (customStrategy == null) {
- return new GenericResponse(Constant.FAIL);
- }
- toUpdateQueueService.changeStrategy(customStrategy,newUser);
- return new GenericResponse(Constant.OK);
- }
- catch (Exception e) {
- e.printStackTrace();
- return new GenericResponse(Constant.FAIL);
- }
- }
- /*@RequestMapping(value = "/login")
- public GenericResponse login( @RequestParam(value = "email") String email, @RequestParam(value = "password") String password) {
- try {
- User u = userService.findByEmail(email);
- Base64.Decoder dec = Base64.getDecoder();
- byte[] salt = dec.decode(u.getSalt());
- PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), salt, 65536, 512);
- SecretKeyFactory f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
- byte[] hash = f.generateSecret(spec).getEncoded();
- Base64.Encoder enc = Base64.getEncoder();
- if (u == null || !u.getEmail().equalsIgnoreCase(email) || !enc.encodeToString(hash).equals(u.getPassword())) {
- throw new BadCredentialsException("Username or Password are incorrect");
- }
- UserRole r = new UserRole();
- r.setName("USER_ROLE");
- List<UserRole> lr = new ArrayList<UserRole>();
- lr.add(r);
- Collection<? extends GrantedAuthority> authorities = lr;
- UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(u, password, authorities);
- return new GenericResponse("OK!",u);
- } catch (Exception e) {
- e.printStackTrace();
- throw new BadCredentialsException("Error during login");
- }
- }*/
- @RequestMapping(value = "/api/getPortfolio")
- public GenericResponse getPortfolio( ) {
- try {
- PortfolioDTO portfolioDTO = userService.getPortfolio( userService.getCurrentUser().getEmail());
- return new GenericResponse(Constant.OK,portfolioDTO);
- }
- catch (Exception e) {
- e.printStackTrace();
- throw new NoSuchElementException("Error during fetch");
- }
- }
- //TODO REMOVE THIS AFTER DEBUGGING
- @RequestMapping(value = "/api/getUpdates")
- public GenericResponse getUpdates( ) {
- try {
- return new GenericResponse(Constant.OK,toUpdateQueueService.getUpdatesDTO(userService.getCurrentUser().getIdUser()));
- }
- catch (Exception e) {
- e.printStackTrace();
- throw new NoSuchElementException("Error during fetch");
- }
- }
- @RequestMapping(value = "/api/getAssetClasses")
- public GenericResponse getAssetClasses( ) {
- try {
- return new GenericResponse(Constant.OK,strategyPerAssetService.getCustomStrategies());
- }
- catch (Exception e) {
- e.printStackTrace();
- throw new NoSuchElementException("Error during fetch");
- }
- }
- }
Add Comment
Please, Sign In to add comment