backend/restservice/src/main/java/com/plannaplan/controllers/TokenController.java

43 lines
1.6 KiB
Java
Raw Normal View History

2020-09-11 14:12:13 +02:00
package com.plannaplan.controllers;
2020-09-14 12:55:47 +02:00
import com.plannaplan.exceptions.UserNotFoundException;
2020-09-11 14:33:29 +02:00
import com.plannaplan.security.CasValidationExcepiton;
2020-09-11 14:12:13 +02:00
import com.plannaplan.security.CasValidator;
2020-09-14 12:55:47 +02:00
import com.plannaplan.services.UserService;
2020-09-11 14:12:13 +02:00
2020-09-14 12:55:47 +02:00
import org.springframework.beans.factory.annotation.Autowired;
2020-09-11 14:33:29 +02:00
import org.springframework.http.HttpStatus;
2020-09-11 14:12:13 +02:00
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@CrossOrigin
public class TokenController {
public static String SERVICE_URL = "http://localhost:3000";
2020-09-14 12:55:47 +02:00
@Autowired
private UserService userService;
2020-09-11 14:33:29 +02:00
@GetMapping("/token")
public ResponseEntity<String> getToken(@RequestParam("ticket") final String ticket) {
2020-09-14 12:55:47 +02:00
CasValidator validator = new CasValidator(SERVICE_URL, ticket);
2020-09-11 14:33:29 +02:00
2020-09-14 12:55:47 +02:00
try {
2020-09-11 14:33:29 +02:00
String authority = validator.validate();
2020-09-14 12:55:47 +02:00
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);
2020-09-11 14:33:29 +02:00
}
}
2020-09-11 14:12:13 +02:00
}