use of io.cdap.cdap.security.spi.authenticator.RemoteAuthenticator in project cdap by caskdata.
the class RemoteClientAuthenticatorTest method testRemoteClientWithRemoteAuthenticatorIncludesAuthorizationHeader.
@Test
public void testRemoteClientWithRemoteAuthenticatorIncludesAuthorizationHeader() throws Exception {
String mockAuthenticatorName = "mock-remote-authenticator";
Credential expectedCredential = new Credential("test-credential", Credential.CredentialType.EXTERNAL_BEARER);
RemoteAuthenticator mockRemoteAuthenticator = mock(RemoteAuthenticator.class);
when(mockRemoteAuthenticator.getName()).thenReturn(mockAuthenticatorName);
when(mockRemoteAuthenticator.getCredentials()).thenReturn(expectedCredential);
mockRemoteAuthenticatorProvider.setAuthenticator(mockRemoteAuthenticator);
RemoteClientFactory remoteClientFactory = injector.getInstance(RemoteClientFactory.class);
RemoteClient remoteClient = remoteClientFactory.createRemoteClient(TEST_SERVICE, new HttpRequestConfig(15000, 15000, false), "/");
HttpURLConnection conn = remoteClient.openConnection(HttpMethod.GET, "");
int responseCode = conn.getResponseCode();
// Verify that the request received the expected headers.
HttpHeaders headers = testHttpHandler.getRequest().headers();
Assert.assertEquals(HttpResponseStatus.OK.code(), responseCode);
Assert.assertEquals(String.format("%s %s", expectedCredential.getType().getQualifiedName(), expectedCredential.getValue()), headers.get(javax.ws.rs.core.HttpHeaders.AUTHORIZATION));
}
use of io.cdap.cdap.security.spi.authenticator.RemoteAuthenticator in project cdap by caskdata.
the class DefaultRemoteAuthenticatorProvider method get.
/**
* Retrieves the current {@link RemoteAuthenticator} from the extension loader using the authenticator name or
* {@code null} if there is no current authenticator available.
*/
@Override
public RemoteAuthenticator get() {
if (defaultAuthenticatorName == null) {
return new NoOpRemoteAuthenticator();
}
// Try to get the current authenticator from the extension loader.
RemoteAuthenticator extensionRemoteAuthenticator = remoteAuthenticatorExtensionLoader.get(defaultAuthenticatorName);
if (extensionRemoteAuthenticator != null) {
return extensionRemoteAuthenticator;
}
// Once a local remote authenticator has been instantiated, return that instead.
if (localRemoteAuthenticator != null) {
return localRemoteAuthenticator;
}
// Check if remote authenticator binding is a local authenticator.
RemoteAuthenticator localAuthenticator = getLocalAuthenticator(defaultAuthenticatorName);
if (localAuthenticator != null) {
localRemoteAuthenticator = localAuthenticator;
return localRemoteAuthenticator;
}
// If no remote authenticator was found, return a NoOpRemoteAuthenticator.
localRemoteAuthenticator = new NoOpRemoteAuthenticator();
return localRemoteAuthenticator;
}
Aggregations