use of ca.uhn.fhir.rest.client.interceptor.BasicAuthInterceptor in project cqf-ruler by DBCG.
the class ClientsTest method testRegisterAuth.
@Test
public void testRegisterAuth() {
IGenericClient client = Clients.forUrl(FhirContext.forR4Cached(), "http://test.com");
Clients.registerBasicAuth(client, "user", "password");
List<Object> interceptors = client.getInterceptorService().getAllRegisteredInterceptors();
Object authInterceptor = interceptors.get(0);
assertTrue(authInterceptor instanceof BasicAuthInterceptor);
}
use of ca.uhn.fhir.rest.client.interceptor.BasicAuthInterceptor in project cqf-ruler by DBCG.
the class CacheValueSetsProvider method cacheValuesets.
/**
* Using basic authentication this {@link Operation Operation} will update
* any {@link ValueSet Valueset} listed given the {@link Endpoint Endpoint}
* provided.
* Any Valuesets that require expansion will be expanded.
*
* @param details the {@link RequestDetails RequestDetails}
* @param endpointId the {@link Endpoint Endpoint} id
* @param valuesets the {@link StringAndListParam list} of {@link ValueSet
* Valueset} ids
* @param userName the userName
* @param password the password
* @return the {@link OperationOutcome OperationOutcome} or the resulting
* {@link Bundle Bundle}
*/
@Description(shortDefinition = "$cache-valuesets", value = "Using basic authentication this Operation will update any Valueset listed given the Endpoint provided. Any Valuesets that require expansion will be expanded.", example = "Endpoint/example-id/$cache-valuesets?valuesets=valuesetId1&valuesets=valuesetId2&user=user&password=password")
@Operation(name = "$cache-valuesets", idempotent = true, type = Endpoint.class)
public Resource cacheValuesets(RequestDetails details, @IdParam IdType endpointId, @OperationParam(name = "valuesets") StringAndListParam valuesets, @OperationParam(name = "user") String userName, @OperationParam(name = "pass") String password) {
Endpoint endpoint = null;
try {
endpoint = this.endpointDao.read(endpointId);
if (endpoint == null) {
return createErrorOutcome("Could not find Endpoint/" + endpointId);
}
} catch (Exception e) {
return createErrorOutcome("Could not find Endpoint/" + endpointId + "\n" + e);
}
IGenericClient client = Clients.forEndpoint(ourCtx, endpoint);
if (userName != null || password != null) {
if (userName == null) {
return createErrorOutcome("Password was provided, but not a user name.");
} else if (password == null) {
return createErrorOutcome("User name was provided, but not a password.");
}
BasicAuthInterceptor basicAuth = new BasicAuthInterceptor(userName, password);
client.registerInterceptor(basicAuth);
// TODO - more advanced security like bearer tokens, etc...
}
try {
Bundle bundleToPost = new Bundle();
for (StringOrListParam params : valuesets.getValuesAsQueryTokens()) {
for (StringParam valuesetId : params.getValuesAsQueryTokens()) {
bundleToPost.addEntry().setRequest(new Bundle.BundleEntryRequestComponent().setMethod(Bundle.HTTPVerb.PUT).setUrl("ValueSet/" + valuesetId.getValue())).setResource(resolveValueSet(client, valuesetId.getValue()));
}
}
return (Resource) systemDao.transaction(details, bundleToPost);
} catch (Exception e) {
return createErrorOutcome(e.getMessage());
}
}
use of ca.uhn.fhir.rest.client.interceptor.BasicAuthInterceptor in project cqf-ruler by DBCG.
the class CacheValueSetsProvider method cacheValuesets.
/**
* Using basic authentication this {@link Operation Operation} will update
* any {@link ValueSet Valueset} listed given the {@link Endpoint Endpoint}
* provided.
* Any Valuesets that require expansion will be expanded.
*
* @param details the {@link RequestDetails RequestDetails}
* @param endpointId the {@link Endpoint Endpoint} id
* @param valuesets the {@link StringAndListParam list} of {@link ValueSet
* Valueset} ids
* @param userName the userName
* @param password the password
* @return the {@link OperationOutcome OperationOutcome} or the resulting
* {@link Bundle Bundle}
*/
@Description(shortDefinition = "$cache-valuesets", value = "Using basic authentication this Operation will update any Valueset listed given the Endpoint provided. Any Valuesets that require expansion will be expanded.", example = "Endpoint/example-id/$cache-valuesets?valuesets=valuesetId1&valuesets=valuesetId2&user=user&password=password")
@Operation(name = "cache-valuesets", idempotent = true, type = Endpoint.class)
public Resource cacheValuesets(RequestDetails details, @IdParam IdType endpointId, @OperationParam(name = "valuesets") StringAndListParam valuesets, @OperationParam(name = "user") String userName, @OperationParam(name = "pass") String password) {
Endpoint endpoint = null;
try {
endpoint = this.endpointDao.read(endpointId);
if (endpoint == null) {
return createErrorOutcome("Could not find Endpoint/" + endpointId);
}
} catch (Exception e) {
return createErrorOutcome("Could not find Endpoint/" + endpointId + "\n" + e);
}
IGenericClient client = Clients.forEndpoint(ourCtx, endpoint);
if (userName != null || password != null) {
if (userName == null) {
return createErrorOutcome("Password was provided, but not a user name.");
} else if (password == null) {
return createErrorOutcome("User name was provided, but not a password.");
}
BasicAuthInterceptor basicAuth = new BasicAuthInterceptor(userName, password);
client.registerInterceptor(basicAuth);
// TODO - more advanced security like bearer tokens, etc...
}
try {
Bundle bundleToPost = new Bundle();
for (StringOrListParam params : valuesets.getValuesAsQueryTokens()) {
for (StringParam valuesetId : params.getValuesAsQueryTokens()) {
bundleToPost.addEntry().setRequest(new Bundle.BundleEntryRequestComponent().setMethod(Bundle.HTTPVerb.PUT).setUrl("ValueSet/" + valuesetId.getValue())).setResource(resolveValueSet(client, valuesetId.getValue()));
}
}
return (Resource) systemDao.transaction(details, bundleToPost);
} catch (Exception e) {
return createErrorOutcome(e.getMessage());
}
}
use of ca.uhn.fhir.rest.client.interceptor.BasicAuthInterceptor in project ipf by oehf.
the class FhirProducer method getClient.
protected IGenericClient getClient(Exchange exchange) {
var context = getEndpoint().getContext();
var clientFactory = context.getRestfulClientFactory();
var config = getEndpoint().getInterceptableConfiguration();
var securityInformation = config.getSecurityInformation();
// For the producer, the path is supposed to be the server URL
var path = config.getPath();
path = (securityInformation != null && securityInformation.isSecure() ? "https://" : "http://") + path;
var client = clientFactory.newGenericClient(path);
if (securityInformation != null && securityInformation.getUsername() != null) {
client.registerInterceptor(new BasicAuthInterceptor(securityInformation.getUsername(), securityInformation.getPassword()));
}
// deploy user-defined HAPI interceptors
var factories = config.getHapiClientInterceptorFactories();
if (factories != null) {
for (var factory : factories) {
client.registerInterceptor(factory.newInstance(getEndpoint(), exchange));
}
}
return client;
}
use of ca.uhn.fhir.rest.client.interceptor.BasicAuthInterceptor in project odm2fhir by num-codex.
the class HTTPHelper method createAuthInterceptor.
public static IClientInterceptor createAuthInterceptor(String basicauthUsername, String basicauthPassword, String oauth2TokenURL, String oauth2ClientId, String oauth2ClientSecret) throws IOException {
IClientInterceptor clientInterceptor = new BasicAuthInterceptor(basicauthUsername, basicauthPassword);
if (isNoneBlank(oauth2TokenURL, oauth2ClientId, oauth2ClientSecret)) {
var httpPost = RequestBuilder.post(oauth2TokenURL).addParameter("grant_type", "client_credentials").addParameter("client_id", oauth2ClientId).addParameter("client_secret", oauth2ClientSecret).build();
var content = HTTP_CLIENT.execute(httpPost).getEntity().getContent();
var token = new ObjectMapper().readTree(content).get("access_token").textValue();
clientInterceptor = new BearerTokenAuthInterceptor(token);
}
return clientInterceptor;
}
Aggregations