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

54 lines
2.4 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;
import com.plannaplan.security.cas.CasUserIdentity;
import com.plannaplan.security.cas.CasValidationExcepiton;
import com.plannaplan.security.cas.DefaultUAMCasValidator;
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;
2020-11-04 16:40:02 +01:00
import io.swagger.annotations.Api;
2020-11-04 16:58:26 +01:00
import io.swagger.annotations.ApiOperation;
2020-11-04 17:23:29 +01:00
import io.swagger.annotations.ApiParam;
2020-11-04 16:40:02 +01:00
2020-09-11 14:12:13 +02:00
@RestController
@CrossOrigin
2020-11-04 16:40:02 +01:00
@Api(tags = { "Token" }, value = "Token", description = "Enpoints to get authorization.")
2020-09-11 14:12:13 +02:00
public class TokenController {
2020-10-08 15:57:42 +02:00
private final static String SERVICE_URL = "http://localhost:3000";
2020-09-11 14:12:13 +02:00
2020-09-14 12:55:47 +02:00
@Autowired
private UserService userService;
2020-09-11 14:33:29 +02:00
@GetMapping("/token")
2020-11-04 16:58:26 +01:00
@ApiOperation(value = "Endpoint to access token required to call secured endpoints. In order to access token we need to provide access token comming from unviersity CAS system")
2020-11-04 17:23:29 +01:00
public ResponseEntity<String> getToken(
2020-11-05 14:32:42 +01:00
@RequestParam("ticket") @ApiParam(value = "Ticket get from CAS system. It should look like ST-1376572-wo41gty5R0JCZFKMMie2-cas.amu.edu.pl") final String ticket) {
final DefaultUAMCasValidator validator = new DefaultUAMCasValidator(SERVICE_URL, ticket);
2020-09-11 14:33:29 +02:00
2020-09-14 12:55:47 +02:00
try {
CasUserIdentity casUserIdentity = validator.validate();
String usosId = casUserIdentity.getUsosId();
String authority = casUserIdentity.getEmail();
this.userService.checkForUser(authority, usosId);
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
}