argos/dmp-backend/web/src/main/java/eu/eudat/controllers/Login.java

129 lines
6.5 KiB
Java
Raw Normal View History

2017-12-15 17:57:41 +01:00
package eu.eudat.controllers;
2018-06-05 10:18:01 +02:00
import eu.eudat.core.logger.Logger;
2018-02-07 10:56:30 +01:00
import eu.eudat.exceptions.security.UnauthorisedException;
2018-01-31 16:39:16 +01:00
import eu.eudat.managers.UserManager;
import eu.eudat.models.helpers.responses.ResponseItem;
2018-02-16 11:34:02 +01:00
import eu.eudat.models.login.Credentials;
2017-12-18 16:55:12 +01:00
import eu.eudat.models.login.LoginInfo;
2017-12-15 17:57:41 +01:00
import eu.eudat.models.security.Principal;
import eu.eudat.security.CustomAuthenticationProvider;
2018-02-23 11:36:51 +01:00
import eu.eudat.security.validators.b2access.B2AccessTokenValidator;
import eu.eudat.security.validators.b2access.helpers.B2AccessRequest;
import eu.eudat.security.validators.b2access.helpers.B2AccessResponseToken;
2018-01-11 12:13:01 +01:00
import eu.eudat.security.validators.twitter.TwitterTokenValidator;
2018-03-05 17:18:45 +01:00
import eu.eudat.services.operations.AuthenticationServiceImpl;
2018-01-23 16:21:38 +01:00
import eu.eudat.types.ApiMessageCode;
2017-12-15 17:57:41 +01:00
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
2018-01-22 08:41:31 +01:00
import org.springframework.http.ResponseEntity;
2018-01-11 12:13:01 +01:00
import org.springframework.social.oauth1.OAuthToken;
2017-12-15 17:57:41 +01:00
import org.springframework.web.bind.annotation.*;
2017-12-19 17:22:30 +01:00
import javax.transaction.Transactional;
2018-02-01 10:08:06 +01:00
2017-12-15 17:57:41 +01:00
@RestController
@CrossOrigin
2018-02-09 16:54:41 +01:00
@RequestMapping(value = "api/auth")
2017-12-15 17:57:41 +01:00
public class Login {
private CustomAuthenticationProvider customAuthenticationProvider;
2018-03-05 17:18:45 +01:00
private AuthenticationServiceImpl authenticationServiceImpl;
2017-12-19 17:22:30 +01:00
2018-01-11 12:13:01 +01:00
private TwitterTokenValidator twitterTokenValidator;
2018-02-23 11:36:51 +01:00
private B2AccessTokenValidator b2AccessTokenValidator;
2018-06-05 10:18:01 +02:00
private Logger logger;
2018-01-11 12:13:01 +01:00
@Autowired
2018-06-05 10:18:01 +02:00
public Login(CustomAuthenticationProvider customAuthenticationProvider, AuthenticationServiceImpl authenticationServiceImpl,
TwitterTokenValidator twitterTokenValidator, B2AccessTokenValidator b2AccessTokenValidator,Logger logger) {
2018-01-11 12:13:01 +01:00
this.customAuthenticationProvider = customAuthenticationProvider;
2018-03-05 17:18:45 +01:00
this.authenticationServiceImpl = authenticationServiceImpl;
2018-01-11 12:13:01 +01:00
this.twitterTokenValidator = twitterTokenValidator;
2018-02-23 11:36:51 +01:00
this.b2AccessTokenValidator = b2AccessTokenValidator;
2018-06-05 10:18:01 +02:00
this.logger = logger;
2018-01-11 12:13:01 +01:00
}
2017-12-19 17:22:30 +01:00
@Transactional
2018-01-31 16:46:00 +01:00
@RequestMapping(method = RequestMethod.POST, value = {"/externallogin"}, consumes = "application/json", produces = "application/json")
public @ResponseBody
ResponseEntity<ResponseItem<Principal>> externallogin(@RequestBody LoginInfo credentials) {
2017-12-15 17:57:41 +01:00
try {
2018-01-23 16:21:38 +01:00
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<Principal>().payload(customAuthenticationProvider.authenticate(credentials)).status(ApiMessageCode.SUCCESS_MESSAGE));
2018-01-31 16:46:00 +01:00
} catch (UnauthorisedException ex) {
2018-01-11 12:13:01 +01:00
throw ex;
2018-01-31 16:46:00 +01:00
} catch (Exception ex) {
2017-12-15 17:57:41 +01:00
ex.printStackTrace();
2018-01-23 16:21:38 +01:00
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem<Principal>().status(ApiMessageCode.DEFAULT_ERROR_MESSAGE).message(ex.getMessage()));
2017-12-15 17:57:41 +01:00
}
2018-01-31 16:39:16 +01:00
}
@Transactional
2018-01-31 16:46:00 +01:00
@RequestMapping(method = RequestMethod.POST, value = {"/nativelogin"}, consumes = "application/json", produces = "application/json")
public @ResponseBody
ResponseEntity<ResponseItem<Principal>> nativelogin(@RequestBody Credentials credentials) {
2018-01-31 16:39:16 +01:00
try {
2018-03-05 17:18:45 +01:00
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<Principal>().payload(UserManager.authenticate(this.authenticationServiceImpl, credentials)).status(ApiMessageCode.SUCCESS_MESSAGE));
2018-01-31 16:46:00 +01:00
} catch (UnauthorisedException ex) {
2018-01-31 16:39:16 +01:00
throw ex;
2018-01-31 16:46:00 +01:00
} catch (Exception ex) {
2018-01-31 16:39:16 +01:00
ex.printStackTrace();
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem<Principal>().status(ApiMessageCode.DEFAULT_ERROR_MESSAGE).message(ex.getMessage()));
}
2017-12-15 17:57:41 +01:00
}
2017-12-19 10:02:25 +01:00
2018-01-31 16:46:00 +01:00
@RequestMapping(method = RequestMethod.GET, value = {"/twitterRequestToken"}, produces = "application/json")
public @ResponseBody
ResponseEntity<ResponseItem<OAuthToken>> twitterRequestToken() {
2018-01-11 12:13:01 +01:00
try {
2018-01-23 16:21:38 +01:00
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<OAuthToken>().payload(this.twitterTokenValidator.getRequestToken()).status(ApiMessageCode.NO_MESSAGE));
2018-01-11 12:13:01 +01:00
} catch (Exception ex) {
ex.printStackTrace();
2018-01-23 16:21:38 +01:00
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem<OAuthToken>().status(ApiMessageCode.DEFAULT_ERROR_MESSAGE).message(ex.getMessage()));
2018-01-11 12:13:01 +01:00
}
}
2018-02-23 11:36:51 +01:00
@RequestMapping(method = RequestMethod.POST, value = {"/b2AccessRequestToken"}, produces = "application/json", consumes = "application/json")
public @ResponseBody
ResponseEntity<ResponseItem<B2AccessResponseToken>> b2AccessRequestToken(@RequestBody B2AccessRequest b2AccessRequest) {
try {
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<B2AccessResponseToken>().payload(this.b2AccessTokenValidator.getAccessToken(b2AccessRequest)).status(ApiMessageCode.NO_MESSAGE));
} catch (Exception ex) {
ex.printStackTrace();
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem<B2AccessResponseToken>().status(ApiMessageCode.DEFAULT_ERROR_MESSAGE).message(ex.getMessage()));
}
}
2018-01-31 16:46:00 +01:00
@RequestMapping(method = RequestMethod.POST, value = {"/me"}, consumes = "application/json", produces = "application/json")
public @ResponseBody
ResponseEntity<ResponseItem<Principal>> authMe(Principal principal) {
2017-12-19 10:02:25 +01:00
try {
2018-03-05 17:18:45 +01:00
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<Principal>().payload(this.authenticationServiceImpl.Touch(principal.getToken())).status(ApiMessageCode.NO_MESSAGE));
2017-12-19 10:02:25 +01:00
} catch (Exception ex) {
ex.printStackTrace();
2018-01-23 16:21:38 +01:00
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem<Principal>().status(ApiMessageCode.DEFAULT_ERROR_MESSAGE).message(ex.getMessage()));
2017-12-19 10:02:25 +01:00
}
}
2018-02-01 08:47:24 +01:00
@Transactional
2018-01-31 16:46:00 +01:00
@RequestMapping(method = RequestMethod.POST, value = {"/logout"}, consumes = "application/json", produces = "application/json")
public @ResponseBody
ResponseEntity<ResponseItem<Principal>> logout(Principal principal) {
2017-12-19 10:02:25 +01:00
try {
2018-03-05 17:18:45 +01:00
this.authenticationServiceImpl.Logout(principal.getToken());
2018-06-05 10:18:01 +02:00
this.logger.info(principal,"Logged Out");
2018-01-23 16:21:38 +01:00
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<Principal>().status(ApiMessageCode.NO_MESSAGE));
2017-12-19 10:02:25 +01:00
} catch (Exception ex) {
2018-06-05 10:18:01 +02:00
this.logger.debug(ex,ex.getMessage());
2017-12-19 10:02:25 +01:00
ex.printStackTrace();
2018-01-23 16:21:38 +01:00
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem<Principal>().status(ApiMessageCode.DEFAULT_ERROR_MESSAGE).message(ex.getMessage()));
2017-12-19 10:02:25 +01:00
}
}
2017-12-15 17:57:41 +01:00
}