package eu.eudat.controllers; import eu.eudat.core.logger.Logger; import eu.eudat.exceptions.security.UnauthorisedException; import eu.eudat.logic.managers.UserManager; 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; import eu.eudat.logic.security.CustomAuthenticationProvider; 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; import eu.eudat.logic.security.validators.twitter.TwitterTokenValidator; import eu.eudat.logic.services.operations.AuthenticationServiceImpl; import eu.eudat.types.ApiMessageCode; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.social.oauth1.OAuthToken; import org.springframework.web.bind.annotation.*; import javax.transaction.Transactional; import java.security.GeneralSecurityException; @RestController @CrossOrigin @RequestMapping(value = "api/auth") public class Login { private CustomAuthenticationProvider customAuthenticationProvider; private AuthenticationServiceImpl authenticationServiceImpl; private TwitterTokenValidator twitterTokenValidator; private B2AccessTokenValidator b2AccessTokenValidator; private Logger logger; @Autowired public Login(CustomAuthenticationProvider customAuthenticationProvider, AuthenticationServiceImpl authenticationServiceImpl, TwitterTokenValidator twitterTokenValidator, B2AccessTokenValidator b2AccessTokenValidator, Logger logger) { this.customAuthenticationProvider = customAuthenticationProvider; this.authenticationServiceImpl = authenticationServiceImpl; this.twitterTokenValidator = twitterTokenValidator; this.b2AccessTokenValidator = b2AccessTokenValidator; this.logger = logger; } @Transactional @RequestMapping(method = RequestMethod.POST, value = {"/externallogin"}, consumes = "application/json", produces = "application/json") public @ResponseBody ResponseEntity> externallogin(@RequestBody LoginInfo credentials) throws GeneralSecurityException { try { this.logger.info(credentials, "Trying To Login With " + credentials.getProvider()); return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem().payload(customAuthenticationProvider.authenticate(credentials)).status(ApiMessageCode.SUCCESS_MESSAGE)); } catch (UnauthorisedException ex) { this.logger.info(ex, "Failed To Login"); throw ex; }/* catch (Exception ex) { this.logger.error(ex, ex.getMessage()); ex.printStackTrace(); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem().status(ApiMessageCode.DEFAULT_ERROR_MESSAGE).message(ex.getMessage())); }*/ } @Transactional @RequestMapping(method = RequestMethod.POST, value = {"/nativelogin"}, consumes = "application/json", produces = "application/json") public @ResponseBody ResponseEntity> nativelogin(@RequestBody Credentials credentials) { try { this.logger.info(credentials.getUsername(), "Trying To Login"); return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem().payload(UserManager.authenticate(this.authenticationServiceImpl, credentials)).status(ApiMessageCode.SUCCESS_MESSAGE)); } catch (UnauthorisedException ex) { this.logger.info(ex, "Failed To Login"); throw ex; }/* catch (Exception ex) { ex.printStackTrace(); this.logger.error(ex, ex.getMessage()); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem().status(ApiMessageCode.DEFAULT_ERROR_MESSAGE).message(ex.getMessage())); }*/ } @RequestMapping(method = RequestMethod.GET, value = {"/twitterRequestToken"}, produces = "application/json") public @ResponseBody ResponseEntity> twitterRequestToken() { //try { return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem().payload(this.twitterTokenValidator.getRequestToken()).status(ApiMessageCode.NO_MESSAGE)); /*} catch (Exception ex) { ex.printStackTrace(); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem().status(ApiMessageCode.DEFAULT_ERROR_MESSAGE).message(ex.getMessage())); }*/ } @RequestMapping(method = RequestMethod.POST, value = {"/b2AccessRequestToken"}, produces = "application/json", consumes = "application/json") public @ResponseBody ResponseEntity> b2AccessRequestToken(@RequestBody B2AccessRequest b2AccessRequest) { //try { return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem().payload(this.b2AccessTokenValidator.getAccessToken(b2AccessRequest)).status(ApiMessageCode.NO_MESSAGE)); /*} catch (Exception ex) { ex.printStackTrace(); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem().status(ApiMessageCode.DEFAULT_ERROR_MESSAGE).message(ex.getMessage())); }*/ } @RequestMapping(method = RequestMethod.POST, value = {"/me"}, consumes = "application/json", produces = "application/json") public @ResponseBody ResponseEntity> authMe(Principal principal) { //try { this.logger.info(principal, "Getting Me"); return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem().payload(this.authenticationServiceImpl.Touch(principal.getToken())).status(ApiMessageCode.NO_MESSAGE)); /*} catch (Exception ex) { this.logger.debug(ex, ex.getMessage()); ex.printStackTrace(); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem().status(ApiMessageCode.DEFAULT_ERROR_MESSAGE).message(ex.getMessage())); }*/ } @Transactional @RequestMapping(method = RequestMethod.POST, value = {"/logout"}, consumes = "application/json", produces = "application/json") public @ResponseBody ResponseEntity> logout(Principal principal) { //try { this.authenticationServiceImpl.Logout(principal.getToken()); this.logger.info(principal, "Logged Out"); return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem().status(ApiMessageCode.NO_MESSAGE)); /*} catch (Exception ex) { this.logger.debug(ex, ex.getMessage()); ex.printStackTrace(); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem().status(ApiMessageCode.DEFAULT_ERROR_MESSAGE).message(ex.getMessage())); }*/ } }