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

122 lines
6.2 KiB
Java
Raw Normal View History

2017-12-15 17:57:41 +01:00
package eu.eudat.controllers;
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;
2017-12-19 10:02:25 +01:00
import eu.eudat.services.AuthenticationService;
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;
2017-12-19 10:02:25 +01:00
private AuthenticationService authenticationService;
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-01-11 12:13:01 +01:00
@Autowired
2018-02-23 11:36:51 +01:00
public Login(CustomAuthenticationProvider customAuthenticationProvider, AuthenticationService authenticationService, TwitterTokenValidator twitterTokenValidator, B2AccessTokenValidator b2AccessTokenValidator) {
2018-01-11 12:13:01 +01:00
this.customAuthenticationProvider = customAuthenticationProvider;
this.authenticationService = authenticationService;
this.twitterTokenValidator = twitterTokenValidator;
2018-02-23 11:36:51 +01:00
this.b2AccessTokenValidator = b2AccessTokenValidator;
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-01-31 16:46:00 +01:00
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<Principal>().payload(UserManager.authenticate(this.authenticationService, credentials)).status(ApiMessageCode.SUCCESS_MESSAGE));
} 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-01-23 16:21:38 +01:00
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<Principal>().payload(this.authenticationService.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 {
this.authenticationService.Logout(principal.getToken());
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) {
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
}