Fix authentication cast to OpenAIREAuthentication while it is not a instance of. Check cookie existance to avoid extra calls for get user info.

This commit is contained in:
Konstantinos Triantafyllou 2021-11-23 13:19:50 +00:00
parent 639dc02108
commit 3f59a7e387
2 changed files with 33 additions and 9 deletions

View File

@ -1,6 +1,7 @@
package eu.dnetlib.uoaauthorizationlibrary.security; package eu.dnetlib.uoaauthorizationlibrary.security;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -69,7 +70,7 @@ public class AuthorizationService {
} }
public List<String> getRoles() { public List<String> getRoles() {
OpenAIREAuthentication authentication = (OpenAIREAuthentication) SecurityContextHolder.getContext().getAuthentication(); OpenAIREAuthentication authentication = getAuthentication();
if (authentication != null && authentication.isAuthenticated()) { if (authentication != null && authentication.isAuthenticated()) {
return authentication.getAuthorities().stream().map(GrantedAuthority::getAuthority).collect(Collectors.toList()); return authentication.getAuthorities().stream().map(GrantedAuthority::getAuthority).collect(Collectors.toList());
} }
@ -77,7 +78,7 @@ public class AuthorizationService {
} }
public String getAaiId() { public String getAaiId() {
OpenAIREAuthentication authentication = (OpenAIREAuthentication) SecurityContextHolder.getContext().getAuthentication(); OpenAIREAuthentication authentication = getAuthentication();
if (authentication != null && authentication.isAuthenticated()) { if (authentication != null && authentication.isAuthenticated()) {
return authentication.getUser().getSub(); return authentication.getUser().getSub();
} }
@ -85,10 +86,19 @@ public class AuthorizationService {
} }
public String getEmail() { public String getEmail() {
OpenAIREAuthentication authentication = (OpenAIREAuthentication) SecurityContextHolder.getContext().getAuthentication(); OpenAIREAuthentication authentication = getAuthentication();
if (authentication != null && authentication.isAuthenticated()) { if (authentication != null && authentication.isAuthenticated()) {
return authentication.getUser().getEmail(); return authentication.getUser().getEmail();
} }
return null; return null;
} }
private OpenAIREAuthentication getAuthentication() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if(authentication instanceof OpenAIREAuthentication) {
return (OpenAIREAuthentication) authentication;
} else {
return null;
}
}
} }

View File

@ -10,12 +10,15 @@ import org.springframework.web.client.RestTemplate;
import javax.servlet.http.Cookie; import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
@Component @Component
public class AuthorizationUtils { public class AuthorizationUtils {
private final Logger log = Logger.getLogger(this.getClass()); private final Logger log = Logger.getLogger(this.getClass());
private final SecurityConfig securityConfig; private final SecurityConfig securityConfig;
private final static String TOKEN = "AccessToken";
private final static String SESSION = "OpenAIRESession";
@Autowired @Autowired
AuthorizationUtils(SecurityConfig securityConfig) { AuthorizationUtils(SecurityConfig securityConfig) {
@ -27,10 +30,9 @@ public class AuthorizationUtils {
return null; return null;
} }
for (Cookie c : request.getCookies()) { for (Cookie c : request.getCookies()) {
if (c.getName().equals("AccessToken")) { if (c.getName().equals(TOKEN)) {
return c.getValue(); return c.getValue();
} }
} }
return null; return null;
} }
@ -39,11 +41,23 @@ public class AuthorizationUtils {
String url = securityConfig.getUserInfoUrl() + (securityConfig.isDeprecated()?getToken(request):""); String url = securityConfig.getUserInfoUrl() + (securityConfig.isDeprecated()?getToken(request):"");
RestTemplate restTemplate = new RestTemplate(); RestTemplate restTemplate = new RestTemplate();
try { try {
ResponseEntity<UserInfo> response = restTemplate.exchange(url, HttpMethod.GET, createHeaders(request), UserInfo.class); if(hasCookie(request)) {
return response.getBody(); ResponseEntity<UserInfo> response = restTemplate.exchange(url, HttpMethod.GET, createHeaders(request), UserInfo.class);
} catch (RestClientException e) { return response.getBody();
log.error(e.getMessage()); }
return null; return null;
} catch (RestClientException e) {
log.error(url + ":" + e.getMessage());
return null;
}
}
private boolean hasCookie(HttpServletRequest request) {
Cookie[] cookies = request.getCookies();
if(securityConfig.isDeprecated()) {
return Arrays.stream(cookies).anyMatch(cookie -> cookie.getName().equalsIgnoreCase(TOKEN));
} else {
return Arrays.stream(cookies).anyMatch(cookie -> cookie.getName().equalsIgnoreCase(SESSION));
} }
} }