Search in sources :

Example 1 with BasicAuthInterceptor

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);
}
Also used : BasicAuthInterceptor(ca.uhn.fhir.rest.client.interceptor.BasicAuthInterceptor) IGenericClient(ca.uhn.fhir.rest.client.api.IGenericClient) Test(org.junit.jupiter.api.Test)

Example 2 with 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());
    }
}
Also used : Endpoint(org.hl7.fhir.dstu3.model.Endpoint) BasicAuthInterceptor(ca.uhn.fhir.rest.client.interceptor.BasicAuthInterceptor) IGenericClient(ca.uhn.fhir.rest.client.api.IGenericClient) Bundle(org.hl7.fhir.dstu3.model.Bundle) Resource(org.hl7.fhir.dstu3.model.Resource) StringParam(ca.uhn.fhir.rest.param.StringParam) StringOrListParam(ca.uhn.fhir.rest.param.StringOrListParam) Description(ca.uhn.fhir.model.api.annotation.Description) Operation(ca.uhn.fhir.rest.annotation.Operation)

Example 3 with 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());
    }
}
Also used : Endpoint(org.hl7.fhir.r4.model.Endpoint) BasicAuthInterceptor(ca.uhn.fhir.rest.client.interceptor.BasicAuthInterceptor) IGenericClient(ca.uhn.fhir.rest.client.api.IGenericClient) Bundle(org.hl7.fhir.r4.model.Bundle) Resource(org.hl7.fhir.r4.model.Resource) StringParam(ca.uhn.fhir.rest.param.StringParam) StringOrListParam(ca.uhn.fhir.rest.param.StringOrListParam) Description(ca.uhn.fhir.model.api.annotation.Description) Operation(ca.uhn.fhir.rest.annotation.Operation)

Example 4 with BasicAuthInterceptor

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;
}
Also used : BasicAuthInterceptor(ca.uhn.fhir.rest.client.interceptor.BasicAuthInterceptor)

Example 5 with BasicAuthInterceptor

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;
}
Also used : BasicAuthInterceptor(ca.uhn.fhir.rest.client.interceptor.BasicAuthInterceptor) IClientInterceptor(ca.uhn.fhir.rest.client.api.IClientInterceptor) BearerTokenAuthInterceptor(ca.uhn.fhir.rest.client.interceptor.BearerTokenAuthInterceptor) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

BasicAuthInterceptor (ca.uhn.fhir.rest.client.interceptor.BasicAuthInterceptor)9 IGenericClient (ca.uhn.fhir.rest.client.api.IGenericClient)5 IClientInterceptor (ca.uhn.fhir.rest.client.api.IClientInterceptor)3 BearerTokenAuthInterceptor (ca.uhn.fhir.rest.client.interceptor.BearerTokenAuthInterceptor)3 FhirContext (ca.uhn.fhir.context.FhirContext)2 Description (ca.uhn.fhir.model.api.annotation.Description)2 Operation (ca.uhn.fhir.rest.annotation.Operation)2 StringOrListParam (ca.uhn.fhir.rest.param.StringOrListParam)2 StringParam (ca.uhn.fhir.rest.param.StringParam)2 FhirVersionEnum (ca.uhn.fhir.context.FhirVersionEnum)1 AdditionalRequestHeadersInterceptor (ca.uhn.fhir.rest.client.interceptor.AdditionalRequestHeadersInterceptor)1 CookieInterceptor (ca.uhn.fhir.rest.client.interceptor.CookieInterceptor)1 LoggingInterceptor (ca.uhn.fhir.rest.client.interceptor.LoggingInterceptor)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 Map (java.util.Map)1 AuthScope (org.apache.http.auth.AuthScope)1 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)1 CredentialsProvider (org.apache.http.client.CredentialsProvider)1