argos/dmp-backend/src/main/java/eu/eudat/handlers/PrincipalArgumentResolver.java

48 lines
1.9 KiB
Java
Raw Normal View History

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;
2018-01-10 17:05:23 +01:00
2017-12-15 17:57:41 +01:00
@Override
public boolean supportsParameter(MethodParameter methodParameter) {
return methodParameter.getParameterType().equals(Principal.class);
}
@Override
2018-01-10 17:05:23 +01:00
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");
2018-01-10 17:05:23 +01:00
if (token == null) throw new UnauthorisedException("Authentication Information Is Missing");
2017-12-18 16:55:12 +01:00
UUID authToken;
2018-01-10 17:05:23 +01:00
try {
2017-12-18 16:55:12 +01:00
authToken = UUID.fromString(token);
2018-01-10 17:05:23 +01:00
} catch (IllegalArgumentException ex) {
2017-12-18 16:55:12 +01:00
throw new UnauthorisedException("Authentication Information Is Missing");
}
Principal principal = this.authenticationService.Touch(authToken);
2018-01-10 17:05:23 +01:00
if (principal == null) throw new UnauthorisedException("Authentication Information Missing");
2017-12-15 17:57:41 +01:00
return principal;
}
2018-01-10 17:05:23 +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
}