2017-12-15 17:57:41 +01:00
|
|
|
package eu.eudat.handlers;
|
|
|
|
|
2017-12-18 16:55:12 +01:00
|
|
|
import eu.eudat.exceptions.UnauthorisedException;
|
2017-12-15 17:57:41 +01:00
|
|
|
import eu.eudat.models.security.Principal;
|
2017-12-18 16:55:12 +01:00
|
|
|
import eu.eudat.services.AuthenticationService;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
2017-12-15 17:57:41 +01:00
|
|
|
import org.springframework.core.MethodParameter;
|
|
|
|
import org.springframework.web.bind.support.WebDataBinderFactory;
|
|
|
|
import org.springframework.web.context.request.NativeWebRequest;
|
|
|
|
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
|
|
|
|
import org.springframework.web.method.support.ModelAndViewContainer;
|
|
|
|
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by ikalyvas on 12/15/2017.
|
|
|
|
*/
|
|
|
|
public final class PrincipalArgumentResolver implements HandlerMethodArgumentResolver {
|
|
|
|
|
2017-12-18 16:55:12 +01:00
|
|
|
private AuthenticationService authenticationService;
|
2017-12-15 17:57:41 +01:00
|
|
|
@Override
|
|
|
|
public boolean supportsParameter(MethodParameter methodParameter) {
|
|
|
|
return methodParameter.getParameterType().equals(Principal.class);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Object resolveArgument(MethodParameter methodParameter,ModelAndViewContainer modelAndViewContainer,NativeWebRequest nativeWebRequest,WebDataBinderFactory webDataBinderFactory) throws Exception {
|
2017-12-18 16:55:12 +01:00
|
|
|
String token = nativeWebRequest.getHeader("AuthToken");
|
|
|
|
if(token == null)throw new UnauthorisedException("Authentication Information Is Missing");
|
|
|
|
UUID authToken;
|
|
|
|
try{
|
|
|
|
authToken = UUID.fromString(token);
|
|
|
|
}catch (IllegalArgumentException ex){
|
|
|
|
throw new UnauthorisedException("Authentication Information Is Missing");
|
|
|
|
}
|
|
|
|
|
|
|
|
Principal principal = this.authenticationService.Touch(authToken);
|
|
|
|
if(principal==null)throw new UnauthorisedException("Authentication Information Missing");
|
2017-12-15 17:57:41 +01:00
|
|
|
return principal;
|
|
|
|
}
|
|
|
|
|
2017-12-19 10:02:25 +01:00
|
|
|
public PrincipalArgumentResolver(AuthenticationService authenticationService){
|
2017-12-18 16:55:12 +01:00
|
|
|
this.authenticationService = authenticationService;
|
|
|
|
}
|
|
|
|
|
2017-12-15 17:57:41 +01:00
|
|
|
}
|