Endpoint returnung token if valid data
This commit is contained in:
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user