Hi there,
I'm working on an Android client with HAPI-FHIR for talking to a FHIR server and using OAuth2 for authentication.
Rather than rolling our own Oauth2 implementation, we're currently making of use Google's OAuth client library to handle initial token fetching (password grant), and then using HAPI's BearerTokenAuthInterceptor to sign requests with the access token:
val client = fhirContext.newRestfulGenericClient(serverBase)
client.registerInterceptor(BearerTokenAuthInterceptor(accessToken))
This is nice and simple, but a bit more is needed to handle refreshing the access token if it's about the expire or if the server returns the relevant error response. Google's Oauth library can handle all of this automatically... but only if you use Google's HTTP client to make requests. E.g. from the library docs:
public static HttpResponse executeGet(
HttpTransport transport, JsonFactory jsonFactory, String accessToken, GenericUrl url)
throws IOException {
Credential credential =
new Credential(BearerToken.authorizationHeaderAccessMethod()).setAccessToken(accessToken);
HttpRequestFactory requestFactory = transport.createRequestFactory(credential);
return requestFactory.buildGetRequest(url).execute();
}
So in order to work with HAPI, what I've done currently is to reimplement Google's expiry logic prior to making FHIR requests, and delegate to the library to refresh the token if needed (any needed calls will use Google HTTP client). Then once there's a valid access token, only then does it proceed to creating an (Apache) HAPI restful client to make FHIR requests.
This out-of-band handling of authorisation logic has a smell to it; it seems like it would be architecturally better to have a restful client that played nice with HAPI to handle these low-level details.
There's also the issue of having two different HTTP clients (Google's and Apache), which means both app binary and memory bloat, as well as double-handling for SSL config, etc.
So my question is, is there a RestfulClientFactory implementation out there that already handles client OAuth2 scenarios like this nicely? And if not, could it be worth writing and releasing a Google HTTP client plugin for HAPI to support this? I'm interested in hearing if this would be a worthwhile development effort. Or some third option, perhaps handling OAuth2 prior to moving control to HAPI classes isn't actually so bad :)
Cheers, Matt