2017-12-15 17:57:41 +01:00
|
|
|
package eu.eudat.controllers;
|
|
|
|
|
|
|
|
import eu.eudat.models.login.Credentials;
|
2017-12-17 22:34:24 +01:00
|
|
|
import eu.eudat.models.helpers.responses.ResponseItem;
|
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;
|
2017-12-19 10:02:25 +01:00
|
|
|
import eu.eudat.services.AuthenticationService;
|
2017-12-15 17:57:41 +01:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
2017-12-19 17:22:30 +01:00
|
|
|
import javax.transaction.Transactional;
|
|
|
|
|
2017-12-15 17:57:41 +01:00
|
|
|
/**
|
|
|
|
* Created by ikalyvas on 12/15/2017.
|
|
|
|
*/
|
|
|
|
@RestController
|
|
|
|
@CrossOrigin
|
2017-12-19 10:02:25 +01:00
|
|
|
@RequestMapping(value = "/auth")
|
2017-12-15 17:57:41 +01:00
|
|
|
public class Login {
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private CustomAuthenticationProvider customAuthenticationProvider;
|
|
|
|
|
2017-12-19 10:02:25 +01:00
|
|
|
@Autowired
|
|
|
|
private AuthenticationService authenticationService;
|
2017-12-19 17:22:30 +01:00
|
|
|
|
|
|
|
@Transactional
|
2017-12-18 16:55:12 +01:00
|
|
|
@RequestMapping(method = RequestMethod.POST, value = { "/externallogin" }, consumes = "application/json", produces="application/json")
|
|
|
|
public @ResponseBody ResponseItem<Principal> googleLogin(@RequestBody LoginInfo credentials) {
|
2017-12-15 17:57:41 +01:00
|
|
|
try {
|
|
|
|
return new ResponseItem<Principal>().payload(customAuthenticationProvider.authenticate(credentials)).status(HttpStatus.OK);
|
|
|
|
} catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
return new ResponseItem<Principal>().status(HttpStatus.BAD_REQUEST).message(ex.getMessage());
|
|
|
|
}
|
|
|
|
}
|
2017-12-19 10:02:25 +01:00
|
|
|
|
|
|
|
@RequestMapping(method = RequestMethod.POST, value = { "/me" }, consumes = "application/json", produces="application/json")
|
|
|
|
public @ResponseBody ResponseItem<Principal> authMe(Principal principal) {
|
|
|
|
try {
|
|
|
|
return new ResponseItem<Principal>().payload(this.authenticationService.Touch(principal.getToken())).status(HttpStatus.OK);
|
|
|
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
return new ResponseItem<Principal>().status(HttpStatus.BAD_REQUEST).message(ex.getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@RequestMapping(method = RequestMethod.POST, value = { "/logout" }, consumes = "application/json", produces="application/json")
|
|
|
|
public @ResponseBody ResponseItem<Principal> logout(Principal principal) {
|
|
|
|
try {
|
|
|
|
this.authenticationService.Logout(principal.getToken());
|
|
|
|
return new ResponseItem<Principal>().status(HttpStatus.OK);
|
|
|
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
return new ResponseItem<Principal>().status(HttpStatus.BAD_REQUEST).message(ex.getMessage());
|
|
|
|
}
|
|
|
|
}
|
2017-12-15 17:57:41 +01:00
|
|
|
}
|