Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package it.inaf.ia2.transfer.auth;
import java.util.Collections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
@Component
public class GmsClient {
@Value("${gms_base_url}")
private String gmsBaseUrl;
private final RestTemplate restTemplate;
@Autowired
public GmsClient() {
restTemplate = new RestTemplate();
}
@Cacheable("gms_cache")
public boolean isMemberOf(String token, String group) {
String url = gmsBaseUrl + "/vo/search/" + group;
String gmsResponse = restTemplate.exchange(url, HttpMethod.GET, getEntity(token), String.class).getBody();
if (gmsResponse == null) {
return false;
}
return group.equals(gmsResponse.replace("\n", ""));
}
private <T> HttpEntity<T> getEntity(String token) {
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Collections.singletonList(MediaType.TEXT_PLAIN));
headers.add("Authorization", "Bearer " + token);
return new HttpEntity<>(null, headers);
}
}