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 {
User user = this.repo.getByAuthority(authority);
User user = this.repo.getByAuthority(authority.replace("\n", "").trim());
if (user == null) {
throw new UserNotFoundException("Can not find user with given authority");
}
String token = UUID.randomUUID().toString();
user.setToken(token);
this.repo.save(user);
return token;
}
@ -34,7 +35,7 @@ public class UserService extends EventWatcher {
}
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);
System.out.println("Returned token: " + token);
assertTrue(token != null);
assertTrue(this.userService.getUserByEmail(TEST_USER_MAIL).getToken() != null);
} catch (UserNotFoundException e) {
e.printStackTrace();
assertTrue(false);

View File

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

View File

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