|
|
|
@ -17,37 +17,23 @@ import java.util.Collections;
|
|
|
|
|
public class AuthorizationUtils {
|
|
|
|
|
private final Logger log = Logger.getLogger(this.getClass());
|
|
|
|
|
private final SecurityConfig securityConfig;
|
|
|
|
|
private final static String TOKEN = "AccessToken";
|
|
|
|
|
private final static String SESSION = "OpenAIRESession";
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
AuthorizationUtils(SecurityConfig securityConfig) {
|
|
|
|
|
this.securityConfig = securityConfig;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String getToken(HttpServletRequest request) {
|
|
|
|
|
if (request.getCookies() == null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
for (Cookie c : request.getCookies()) {
|
|
|
|
|
if (c.getName().equals(TOKEN)) {
|
|
|
|
|
return c.getValue();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public UserInfo getUserInfo(HttpServletRequest request) {
|
|
|
|
|
String url = securityConfig.getUserInfoUrl() + (securityConfig.isDeprecated()?getToken(request):"");
|
|
|
|
|
String url = securityConfig.getUserInfoUrl();
|
|
|
|
|
RestTemplate restTemplate = new RestTemplate();
|
|
|
|
|
try {
|
|
|
|
|
if(hasCookie(request)) {
|
|
|
|
|
ResponseEntity<UserInfo> response = restTemplate.exchange(url, HttpMethod.GET, createHeaders(request), UserInfo.class);
|
|
|
|
|
return response.getBody();
|
|
|
|
|
return response.getBody();
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
} catch (RestClientException e) {
|
|
|
|
|
log.error(url + ":" + e.getMessage());
|
|
|
|
|
log.error(url + ": " + e.getMessage());
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -55,11 +41,7 @@ public class AuthorizationUtils {
|
|
|
|
|
private boolean hasCookie(HttpServletRequest request) {
|
|
|
|
|
Cookie[] cookies = request.getCookies();
|
|
|
|
|
if(cookies != null) {
|
|
|
|
|
if (securityConfig.isDeprecated()) {
|
|
|
|
|
return Arrays.stream(cookies).anyMatch(cookie -> cookie.getName().equalsIgnoreCase(TOKEN));
|
|
|
|
|
} else {
|
|
|
|
|
return Arrays.stream(cookies).anyMatch(cookie -> cookie.getName().equalsIgnoreCase(SESSION));
|
|
|
|
|
}
|
|
|
|
|
return Arrays.stream(cookies).anyMatch(cookie -> cookie.getName().equalsIgnoreCase(this.securityConfig.getSession()));
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|