From 9c988819dd9ca06466d3a866ca720f13d600c193 Mon Sep 17 00:00:00 2001 From: Spencer Stock Date: Tue, 25 Jun 2019 10:51:07 -0600 Subject: [PATCH] added ability to receive token when you signup --- .../starthere/controllers/OpenController.java | 44 ++++++++++++++----- .../lambdaschool/starthere/models/User.java | 19 ++++++++ 2 files changed, 52 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/lambdaschool/starthere/controllers/OpenController.java b/src/main/java/com/lambdaschool/starthere/controllers/OpenController.java index fa88c94..b099eb8 100644 --- a/src/main/java/com/lambdaschool/starthere/controllers/OpenController.java +++ b/src/main/java/com/lambdaschool/starthere/controllers/OpenController.java @@ -7,18 +7,16 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.http.*; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; -import org.springframework.web.servlet.support.ServletUriComponentsBuilder; +import org.springframework.web.client.RestTemplate; import javax.servlet.http.HttpServletRequest; import javax.validation.Valid; -import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; @@ -38,23 +36,47 @@ public ResponseEntity addNewUser(HttpServletRequest request, @Valid @RequestBody User newuser) throws URISyntaxException { + logger.trace(request.getRequestURI() + " accessed"); ArrayList newRoles = new ArrayList<>(); newRoles.add(new UserRoles(newuser, roleService.findByName("user"))); newuser.setUserRoles(newRoles); - newuser = userService.save(newuser); + String temp = newuser.getPasswordRaw(); + newuser = userService.save(newuser); //passwordRaw is nullified + + + String loginUrl + = "https://sgs-lambda-bookr.herokuapp.com//oauth/token"; + + RestTemplate restTemplate = new RestTemplate(); + + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); + + headers.add("Authorization", "Basic bGFtYmRhLWNsaWVudDpsYW1iZGEtc2VjcmV0"); + MultiValueMap map= new LinkedMultiValueMap<>(); + map.add("grant_type", "password"); + map.add("username", newuser.getUsername()); + map.add("password", temp); + HttpEntity> request2 = new HttpEntity<>(map, headers); + ResponseEntity response = restTemplate.postForEntity( + loginUrl, request2 , String.class); + + + - // set the location header for the newly created resource - to another controller! +/* // set the location header for the newly created resource - to another controller! HttpHeaders responseHeaders = new HttpHeaders(); - URI newRestaurantURI = ServletUriComponentsBuilder + URI createUserURI = ServletUriComponentsBuilder .fromUriString(request.getServerName() + ":" + request.getLocalPort() + "/users/user/{userId}") .buildAndExpand(newuser.getUserid()).toUri(); - responseHeaders.setLocation(newRestaurantURI); + responseHeaders.setLocation(createUserURI);*/ - return new ResponseEntity<>(null, responseHeaders, HttpStatus.CREATED); + return response; + //return new ResponseEntity<>(null, responseHeaders, HttpStatus.CREATED); } } diff --git a/src/main/java/com/lambdaschool/starthere/models/User.java b/src/main/java/com/lambdaschool/starthere/models/User.java index d169ca0..cf6c177 100644 --- a/src/main/java/com/lambdaschool/starthere/models/User.java +++ b/src/main/java/com/lambdaschool/starthere/models/User.java @@ -27,6 +27,9 @@ public class User extends Auditable @JsonProperty(access = JsonProperty.Access.WRITE_ONLY) private String password; + @Column + private String passwordRaw; + @OneToMany(mappedBy = "user", cascade = CascadeType.ALL) @JsonIgnoreProperties("user") @@ -46,6 +49,17 @@ public User(String username, String password, List userRoles) } this.userRoles = userRoles; } + public User(String username, String password, List userRoles, Boolean encrypt) + { + setUsername(username); + if(encrypt) setPassword(password); + else setPasswordNoEncrypt(password); + for (UserRoles ur : userRoles) + { + ur.setUser(this); + } + this.userRoles = userRoles; + } public long getUserid() { @@ -74,10 +88,15 @@ public String getPassword() public void setPassword(String password) { + this.passwordRaw = password; BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); this.password = passwordEncoder.encode(password); } + public String getPasswordRaw() { + return passwordRaw; + } + public void setPasswordNoEncrypt(String password) { this.password = password;