Changed token response in controller

This commit is contained in:
Filip Izydorczyk
2020-12-07 21:46:47 +01:00
parent 3061fb1edf
commit f9baed5df7
7 changed files with 52 additions and 52 deletions

View File

@ -2,6 +2,7 @@ package com.plannaplan.controllers;
import com.plannaplan.entities.User;
import com.plannaplan.exceptions.UserNotFoundException;
import com.plannaplan.responses.models.TokenResponse;
import com.plannaplan.security.cas.CasUserIdentity;
import com.plannaplan.security.cas.CasValidationExcepiton;
import com.plannaplan.security.cas.CasValidator;
@ -38,23 +39,25 @@ public class TokenController {
@GetMapping("/token")
@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")
public ResponseEntity<String> getToken(
public ResponseEntity<TokenResponse> getToken(
@RequestParam("ticket") @ApiParam(value = "Ticket get from CAS system. It should look like ST-1376572-wo41gty5R0JCZFKMMie2-cas.amu.edu.psl") final String ticket) {
final CasValidator validator = isDev ? new DefaultUAMCasValidator(serviceUrl, ticket) : new CustomUAMCasValidator(serviceUrl, ticket);
final CasValidator validator = isDev ? new DefaultUAMCasValidator(serviceUrl, ticket)
: new CustomUAMCasValidator(serviceUrl, ticket);
try {
final CasUserIdentity casUserIdentity = validator.validate();
final String usosId = casUserIdentity.getUsosId();
final String authority = casUserIdentity.getEmail();
final User user = this.userService.checkForUser(authority, usosId);
String token = this.userService.login(user);
return new ResponseEntity<>(token, HttpStatus.OK);
User user = this.userService.checkForUser(authority, usosId);
user = this.userService.login(user);
return new ResponseEntity<>(new TokenResponse(user), HttpStatus.OK);
} catch (CasValidationExcepiton e) {
return new ResponseEntity<>("Wrong ticket", HttpStatus.UNAUTHORIZED);
return new ResponseEntity<>(null, HttpStatus.UNAUTHORIZED);
} catch (UserNotFoundException e) {
return new ResponseEntity<>("User not found", HttpStatus.NOT_FOUND);
return new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
} catch (Exception e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}