Endpoint returnung token if valid data

This commit is contained in:
Filip Izydorczyk 2020-09-14 12:55:47 +02:00
parent 11f3625684
commit 478ea27480
4 changed files with 29 additions and 20 deletions

View File

@ -20,12 +20,13 @@ public class UserService extends EventWatcher {
} }
public String login(String authority) throws UserNotFoundException { public String login(String authority) throws UserNotFoundException {
User user = this.repo.getByAuthority(authority); User user = this.repo.getByAuthority(authority.replace("\n", "").trim());
if (user == null) { if (user == null) {
throw new UserNotFoundException("Can not find user with given authority"); throw new UserNotFoundException("Can not find user with given authority");
} }
String token = UUID.randomUUID().toString(); String token = UUID.randomUUID().toString();
user.setToken(token); user.setToken(token);
this.repo.save(user);
return token; return token;
} }
@ -34,7 +35,7 @@ public class UserService extends EventWatcher {
} }
public User getUserByEmail(String email) { public User getUserByEmail(String email) {
return this.repo.getByAuthority(email); return this.repo.getByAuthority(email.replace("\n", "").trim());
} }
} }

View File

@ -45,6 +45,7 @@ public class UserServiceTest {
String token = this.userService.login(TEST_USER_MAIL); String token = this.userService.login(TEST_USER_MAIL);
System.out.println("Returned token: " + token); System.out.println("Returned token: " + token);
assertTrue(token != null); assertTrue(token != null);
assertTrue(this.userService.getUserByEmail(TEST_USER_MAIL).getToken() != null);
} catch (UserNotFoundException e) { } catch (UserNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
assertTrue(false); assertTrue(false);

View File

@ -24,13 +24,13 @@ public class App {
SpringApplication.run(App.class, args); SpringApplication.run(App.class, args);
} }
// @EventListener(ApplicationReadyEvent.class) @EventListener(ApplicationReadyEvent.class)
// public void importData() { public void importData() {
// User testUser = new User(); User testUser = new User();
// testUser.setEmail("noteexisitingmail@notexistingdomain.com"); testUser.setEmail("filizy@st.amu.edu.pl");
// testUser.setName("Tom"); testUser.setName("Filip");
// testUser.setSurname("Kovalsky"); testUser.setSurname("Izydorczyk");
// testUser.setRole(UserRoles.TEST_USER); testUser.setRole(UserRoles.STUDENT);
// this.userService.save(testUser); this.userService.save(testUser);
// } }
} }

View File

@ -1,8 +1,11 @@
package com.plannaplan.controllers; package com.plannaplan.controllers;
import com.plannaplan.exceptions.UserNotFoundException;
import com.plannaplan.security.CasValidationExcepiton; import com.plannaplan.security.CasValidationExcepiton;
import com.plannaplan.security.CasValidator; import com.plannaplan.security.CasValidator;
import com.plannaplan.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.CrossOrigin;
@ -16,19 +19,23 @@ public class TokenController {
public static String SERVICE_URL = "http://localhost:3000"; public static String SERVICE_URL = "http://localhost:3000";
@Autowired
private UserService userService;
@GetMapping("/token") @GetMapping("/token")
public ResponseEntity<String> getToken(@RequestParam("ticket") final String ticket) { public ResponseEntity<String> getToken(@RequestParam("ticket") final String ticket) {
CasValidator validator = new CasValidator(SERVICE_URL,ticket); CasValidator validator = new CasValidator(SERVICE_URL, ticket);
try{ try {
String authority = validator.validate(); String authority = validator.validate();
return new ResponseEntity<>(authority,HttpStatus.OK); String token = this.userService.login(authority);
} return new ResponseEntity<>(token, HttpStatus.OK);
catch(CasValidationExcepiton e){ } catch (CasValidationExcepiton e) {
return new ResponseEntity<>("Wrong ticket",HttpStatus.UNAUTHORIZED); return new ResponseEntity<>("Wrong ticket", HttpStatus.UNAUTHORIZED);
} } catch (UserNotFoundException e) {
catch(Exception e){ return new ResponseEntity<>("User not found", HttpStatus.NOT_FOUND);
return new ResponseEntity<>(e.getMessage(),HttpStatus.INTERNAL_SERVER_ERROR); } catch (Exception e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
} }
} }