This repository has been archived by the owner on Nov 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CredentialsProvider.java
66 lines (56 loc) · 2.43 KB
/
CredentialsProvider.java
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package se.nbis.lega.inbox.sftp;
import org.apache.http.HttpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import se.nbis.lega.inbox.pojo.Credentials;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Base64;
/**
* Component that queries CEGA for user credentials.
*/
@Component
public class CredentialsProvider {
private String cegaEndpoint;
private String cegaCredentials;
private RestTemplate restTemplate;
/**
* Queries CEGA REST endpoint to obtain user credentials.
*
* @param username Target user name.
* @return User's credentials.
* @throws IOException In case we can't read from remote endpoint.
* @throws URISyntaxException In case URL is in a wrong format.
*/
public Credentials getCredentials(String username) throws IOException, URISyntaxException {
URL url = new URL(String.format(cegaEndpoint, username));
org.springframework.http.HttpHeaders headers = new org.springframework.http.HttpHeaders();
headers.set(HttpHeaders.AUTHORIZATION, "Basic " + Base64.getEncoder().encodeToString(cegaCredentials.getBytes()));
ResponseEntity<Credentials> response = restTemplate.exchange(url.toURI(), HttpMethod.GET, new HttpEntity<>(headers), Credentials.class);
HttpStatus statusCode = (HttpStatus) response.getStatusCode();
if (!HttpStatus.OK.equals(statusCode)) {
throw new RestClientException(String.format("Bad response from CentralEGA: %s, %s", statusCode.value(), statusCode.getReasonPhrase()));
}
return response.getBody();
}
@Value("${inbox.cega.endpoint}")
public void setCegaEndpoint(String cegaEndpoint) {
this.cegaEndpoint = cegaEndpoint;
}
@Value("${inbox.cega.credentials}")
public void setCegaCredentials(String cegaCredentials) {
this.cegaCredentials = cegaCredentials;
}
@Autowired
public void setRestTemplate(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
}