2017-12-15 17:57:41 +01:00
|
|
|
package eu.eudat.controllers;
|
|
|
|
|
2020-01-28 17:33:50 +01:00
|
|
|
|
2020-04-06 17:16:05 +02:00
|
|
|
import eu.eudat.exceptions.security.ExpiredTokenException;
|
|
|
|
import eu.eudat.exceptions.security.NonValidTokenException;
|
2019-07-12 15:58:17 +02:00
|
|
|
import eu.eudat.exceptions.security.NullEmailException;
|
2018-06-27 12:29:21 +02:00
|
|
|
import eu.eudat.logic.managers.UserManager;
|
2019-11-13 16:32:55 +01:00
|
|
|
import eu.eudat.logic.proxy.config.configloaders.ConfigLoader;
|
2018-06-27 12:29:21 +02:00
|
|
|
import eu.eudat.logic.security.CustomAuthenticationProvider;
|
2019-11-13 16:32:55 +01:00
|
|
|
import eu.eudat.logic.security.customproviders.ConfigurableProvider.models.ConfigurableProvidersModel;
|
2018-06-27 12:29:21 +02:00
|
|
|
import eu.eudat.logic.security.validators.b2access.B2AccessTokenValidator;
|
|
|
|
import eu.eudat.logic.security.validators.b2access.helpers.B2AccessRequest;
|
|
|
|
import eu.eudat.logic.security.validators.b2access.helpers.B2AccessResponseToken;
|
2019-11-13 16:32:55 +01:00
|
|
|
import eu.eudat.logic.security.validators.configurableProvider.ConfigurableProviderTokenValidator;
|
|
|
|
import eu.eudat.logic.security.validators.configurableProvider.helpers.ConfigurableProviderRequest;
|
|
|
|
import eu.eudat.logic.security.validators.configurableProvider.helpers.ConfigurableProviderResponseToken;
|
2019-09-30 09:27:42 +02:00
|
|
|
import eu.eudat.logic.security.validators.linkedin.LinkedInTokenValidator;
|
|
|
|
import eu.eudat.logic.security.validators.linkedin.helpers.LinkedInRequest;
|
|
|
|
import eu.eudat.logic.security.validators.linkedin.helpers.LinkedInResponseToken;
|
2019-10-23 12:38:27 +02:00
|
|
|
import eu.eudat.logic.security.validators.openaire.OpenAIRETokenValidator;
|
|
|
|
import eu.eudat.logic.security.validators.openaire.helpers.OpenAIRERequest;
|
|
|
|
import eu.eudat.logic.security.validators.openaire.helpers.OpenAIREResponseToken;
|
2019-07-12 15:58:17 +02:00
|
|
|
import eu.eudat.logic.security.validators.orcid.ORCIDTokenValidator;
|
|
|
|
import eu.eudat.logic.security.validators.orcid.helpers.ORCIDRequest;
|
|
|
|
import eu.eudat.logic.security.validators.orcid.helpers.ORCIDResponseToken;
|
2018-06-27 12:29:21 +02:00
|
|
|
import eu.eudat.logic.security.validators.twitter.TwitterTokenValidator;
|
2020-04-03 17:40:03 +02:00
|
|
|
import eu.eudat.logic.security.validators.zenodo.ZenodoTokenValidator;
|
|
|
|
import eu.eudat.logic.security.validators.zenodo.helpers.ZenodoRequest;
|
|
|
|
import eu.eudat.logic.security.validators.zenodo.helpers.ZenodoResponseToken;
|
2019-07-12 15:58:17 +02:00
|
|
|
import eu.eudat.logic.services.operations.authentication.AuthenticationService;
|
2018-08-31 16:12:31 +02:00
|
|
|
import eu.eudat.models.data.helpers.responses.ResponseItem;
|
|
|
|
import eu.eudat.models.data.login.Credentials;
|
|
|
|
import eu.eudat.models.data.login.LoginInfo;
|
|
|
|
import eu.eudat.models.data.security.Principal;
|
2018-01-23 16:21:38 +01:00
|
|
|
import eu.eudat.types.ApiMessageCode;
|
2020-01-28 17:33:50 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
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;
|
2020-04-06 17:16:05 +02:00
|
|
|
import java.io.IOException;
|
2018-06-27 12:29:21 +02:00
|
|
|
import java.security.GeneralSecurityException;
|
2017-12-19 17:22:30 +01:00
|
|
|
|
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 {
|
2020-01-28 17:33:50 +01:00
|
|
|
private static final Logger logger = LoggerFactory.getLogger(Login.class);
|
2017-12-15 17:57:41 +01:00
|
|
|
|
|
|
|
private CustomAuthenticationProvider customAuthenticationProvider;
|
2019-07-12 15:58:17 +02:00
|
|
|
private AuthenticationService nonVerifiedUserAuthenticationService;
|
2018-01-11 12:13:01 +01:00
|
|
|
private TwitterTokenValidator twitterTokenValidator;
|
2018-02-23 11:36:51 +01:00
|
|
|
private B2AccessTokenValidator b2AccessTokenValidator;
|
2019-07-12 15:58:17 +02:00
|
|
|
private ORCIDTokenValidator orcidTokenValidator;
|
2019-09-30 09:27:42 +02:00
|
|
|
private LinkedInTokenValidator linkedInTokenValidator;
|
2019-10-23 12:38:27 +02:00
|
|
|
private OpenAIRETokenValidator openAIRETokenValidator;
|
2019-11-13 16:32:55 +01:00
|
|
|
private ConfigurableProviderTokenValidator configurableProviderTokenValidator;
|
2020-04-03 17:40:03 +02:00
|
|
|
private ZenodoTokenValidator zenodoTokenValidator;
|
2019-11-13 16:32:55 +01:00
|
|
|
private ConfigLoader configLoader;
|
2018-02-23 11:36:51 +01:00
|
|
|
|
2020-01-28 17:33:50 +01:00
|
|
|
// private Logger logger;
|
2018-06-05 10:18:01 +02:00
|
|
|
|
2019-03-05 16:33:59 +01:00
|
|
|
private UserManager userManager;
|
2020-04-03 17:40:03 +02:00
|
|
|
|
2018-01-11 12:13:01 +01:00
|
|
|
@Autowired
|
2020-04-03 17:40:03 +02:00
|
|
|
public Login(CustomAuthenticationProvider customAuthenticationProvider,
|
|
|
|
AuthenticationService nonVerifiedUserAuthenticationService, TwitterTokenValidator twitterTokenValidator,
|
|
|
|
B2AccessTokenValidator b2AccessTokenValidator, ORCIDTokenValidator orcidTokenValidator,
|
|
|
|
LinkedInTokenValidator linkedInTokenValidator, OpenAIRETokenValidator openAIRETokenValidator,
|
|
|
|
ConfigurableProviderTokenValidator configurableProviderTokenValidator, ZenodoTokenValidator zenodoTokenValidator,
|
|
|
|
ConfigLoader configLoader, UserManager userManager) {
|
2018-01-11 12:13:01 +01:00
|
|
|
this.customAuthenticationProvider = customAuthenticationProvider;
|
2019-07-12 15:58:17 +02:00
|
|
|
this.nonVerifiedUserAuthenticationService = nonVerifiedUserAuthenticationService;
|
2018-01-11 12:13:01 +01:00
|
|
|
this.twitterTokenValidator = twitterTokenValidator;
|
2018-02-23 11:36:51 +01:00
|
|
|
this.b2AccessTokenValidator = b2AccessTokenValidator;
|
2019-07-12 15:58:17 +02:00
|
|
|
this.orcidTokenValidator = orcidTokenValidator;
|
2020-04-03 17:40:03 +02:00
|
|
|
this.linkedInTokenValidator = linkedInTokenValidator;
|
2019-10-23 12:38:27 +02:00
|
|
|
this.openAIRETokenValidator = openAIRETokenValidator;
|
2019-11-13 16:32:55 +01:00
|
|
|
this.configurableProviderTokenValidator = configurableProviderTokenValidator;
|
2020-04-03 17:40:03 +02:00
|
|
|
this.zenodoTokenValidator = zenodoTokenValidator;
|
2019-11-13 16:32:55 +01:00
|
|
|
this.configLoader = configLoader;
|
2019-03-05 16:33:59 +01:00
|
|
|
this.userManager = userManager;
|
2018-01-11 12:13:01 +01:00
|
|
|
}
|
|
|
|
|
2020-04-03 17:40:03 +02: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
|
2019-07-12 15:58:17 +02:00
|
|
|
ResponseEntity<ResponseItem<Principal>> externallogin(@RequestBody LoginInfo credentials) throws GeneralSecurityException, NullEmailException {
|
2020-01-28 17:33:50 +01:00
|
|
|
logger.info("Trying To Login With " + credentials.getProvider());
|
2018-08-31 16:12:31 +02:00
|
|
|
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<Principal>().payload(customAuthenticationProvider.authenticate(credentials)).status(ApiMessageCode.SUCCESS_MESSAGE));
|
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
|
2019-07-12 15:58:17 +02:00
|
|
|
ResponseEntity<ResponseItem<Principal>> nativelogin(@RequestBody Credentials credentials) throws NullEmailException {
|
2020-01-28 17:33:50 +01:00
|
|
|
logger.info(credentials.getUsername() + " Trying To Login");
|
2019-07-12 15:58:17 +02:00
|
|
|
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<Principal>().payload(userManager.authenticate(this.nonVerifiedUserAuthenticationService, credentials)).status(ApiMessageCode.SUCCESS_MESSAGE));
|
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-08-31 16:12:31 +02:00
|
|
|
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<OAuthToken>().payload(this.twitterTokenValidator.getRequestToken()).status(ApiMessageCode.NO_MESSAGE));
|
2019-09-30 09:27:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@RequestMapping(method = RequestMethod.POST, value = {"/linkedInRequestToken"}, produces = "application/json", consumes = "application/json")
|
|
|
|
public @ResponseBody
|
|
|
|
ResponseEntity<ResponseItem<LinkedInResponseToken>> linkedInRequestToken(@RequestBody LinkedInRequest linkedInRequest) {
|
|
|
|
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<LinkedInResponseToken>().payload(this.linkedInTokenValidator.getAccessToken(linkedInRequest)).status(ApiMessageCode.NO_MESSAGE));
|
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) {
|
2018-08-31 16:12:31 +02:00
|
|
|
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<B2AccessResponseToken>().payload(this.b2AccessTokenValidator.getAccessToken(b2AccessRequest)).status(ApiMessageCode.NO_MESSAGE));
|
2018-02-23 11:36:51 +01:00
|
|
|
}
|
|
|
|
|
2019-07-12 16:22:57 +02:00
|
|
|
@RequestMapping(method = RequestMethod.POST, value = {"/orcidRequestToken"}, produces = "application/json", consumes = "application/json")
|
|
|
|
public @ResponseBody
|
|
|
|
ResponseEntity<ResponseItem<ORCIDResponseToken>> ORCIDRequestToken(@RequestBody ORCIDRequest orcidRequest) {
|
|
|
|
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<ORCIDResponseToken>().payload(this.orcidTokenValidator.getAccessToken(orcidRequest)).status(ApiMessageCode.NO_MESSAGE));
|
|
|
|
}
|
|
|
|
|
2019-10-23 12:38:27 +02:00
|
|
|
@RequestMapping(method = RequestMethod.POST, value = {"/openAireRequestToken"}, produces = "application/json", consumes = "application/json")
|
|
|
|
public @ResponseBody
|
|
|
|
ResponseEntity<ResponseItem<OpenAIREResponseToken>> openAIRERequestToken(@RequestBody OpenAIRERequest openAIRERequest) {
|
|
|
|
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<OpenAIREResponseToken>().payload(this.openAIRETokenValidator.getAccessToken(openAIRERequest)).status(ApiMessageCode.NO_MESSAGE));
|
|
|
|
}
|
|
|
|
|
2019-11-13 16:32:55 +01:00
|
|
|
@RequestMapping(method = RequestMethod.POST, value = {"/configurableProviderRequestToken"}, produces = "application/json", consumes = "application/json")
|
|
|
|
public @ResponseBody
|
|
|
|
ResponseEntity<ResponseItem<ConfigurableProviderResponseToken>> configurableProviderRequestToken(@RequestBody ConfigurableProviderRequest configurableProviderRequest) {
|
|
|
|
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<ConfigurableProviderResponseToken>().payload(this.configurableProviderTokenValidator.getAccessToken(configurableProviderRequest)).status(ApiMessageCode.NO_MESSAGE));
|
|
|
|
}
|
|
|
|
|
2020-04-03 17:40:03 +02:00
|
|
|
@RequestMapping(method = RequestMethod.POST, value = {"/zenodoRequestToken"}, produces = "application/json", consumes = "application/json")
|
|
|
|
public @ResponseBody
|
|
|
|
ResponseEntity<ResponseItem<ZenodoResponseToken>> ZenodoRequestToken(@RequestBody ZenodoRequest zenodoRequest) {
|
|
|
|
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<ZenodoResponseToken>().payload(this.zenodoTokenValidator.getAccessToken(zenodoRequest)).status(ApiMessageCode.NO_MESSAGE));
|
|
|
|
}
|
|
|
|
|
2018-01-31 16:46:00 +01:00
|
|
|
@RequestMapping(method = RequestMethod.POST, value = {"/me"}, consumes = "application/json", produces = "application/json")
|
|
|
|
public @ResponseBody
|
2019-07-12 15:58:17 +02:00
|
|
|
ResponseEntity<ResponseItem<Principal>> authMe(Principal principal) throws NullEmailException {
|
2020-01-28 17:33:50 +01:00
|
|
|
logger.info(principal + " Getting Me");
|
2019-07-12 15:58:17 +02:00
|
|
|
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<Principal>().payload(this.nonVerifiedUserAuthenticationService.Touch(principal.getToken())).status(ApiMessageCode.NO_MESSAGE));
|
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) {
|
2019-07-12 15:58:17 +02:00
|
|
|
this.nonVerifiedUserAuthenticationService.Logout(principal.getToken());
|
2020-01-28 17:33:50 +01:00
|
|
|
logger.info(principal + " Logged Out");
|
2018-08-31 16:12:31 +02:00
|
|
|
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<Principal>().status(ApiMessageCode.NO_MESSAGE));
|
2017-12-19 10:02:25 +01:00
|
|
|
}
|
2019-11-13 16:32:55 +01:00
|
|
|
|
|
|
|
@RequestMapping(method = RequestMethod.GET, value = {"/configurableLogin"}, consumes = "application/json", produces = "application/json")
|
|
|
|
public @ResponseBody
|
|
|
|
ResponseEntity<ResponseItem<ConfigurableProvidersModel>> getConfigurableProviders() {
|
|
|
|
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<ConfigurableProvidersModel>().payload(new ConfigurableProvidersModel().fromDataModel(configLoader.getConfigurableProviders())).status(ApiMessageCode.NO_MESSAGE));
|
|
|
|
}
|
2017-12-15 17:57:41 +01:00
|
|
|
}
|