Search in sources :

Example 1 with OAuthAccessTokenManager

use of nl.nn.adapterframework.http.authentication.OAuthAccessTokenManager in project iaf by ibissource.

the class OAuthAccessTokenManagerTest method testRetrieveAccessTokenNoScope.

@Test
public void testRetrieveAccessTokenNoScope() throws Exception {
    String tokenEndpoint = TOKENENDPOINT;
    String scope = null;
    String clientId = clientClientId;
    String clientSecret = clientClientSecret;
    CredentialFactory client_cf = new CredentialFactory(null, clientId, clientSecret);
    OAuthAccessTokenManager accessTokenManager = new OAuthAccessTokenManager(tokenEndpoint, scope, client_cf, true, httpSender);
    String accessToken = accessTokenManager.getAccessToken(null);
    assertThat(accessToken, startsWith("Bearer"));
}
Also used : CredentialFactory(nl.nn.adapterframework.util.CredentialFactory) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) OAuthAccessTokenManager(nl.nn.adapterframework.http.authentication.OAuthAccessTokenManager) Test(org.junit.Test)

Example 2 with OAuthAccessTokenManager

use of nl.nn.adapterframework.http.authentication.OAuthAccessTokenManager in project iaf by ibissource.

the class OAuthAccessTokenManagerTest method testRetrieveAccessTokenWrongCredentials.

@Test
public void testRetrieveAccessTokenWrongCredentials() throws Exception {
    String tokenEndpoint = TOKENENDPOINT;
    String scope = "email";
    String clientId = clientClientId;
    String clientSecret = "xxx";
    CredentialFactory client_cf = new CredentialFactory(null, clientId, clientSecret);
    OAuthAccessTokenManager accessTokenManager = new OAuthAccessTokenManager(tokenEndpoint, scope, client_cf, true, httpSender);
    HttpAuthenticationException exception = assertThrows(HttpAuthenticationException.class, () -> accessTokenManager.getAccessToken(null));
    assertThat(exception.getMessage(), containsString("unauthorized_client"));
}
Also used : CredentialFactory(nl.nn.adapterframework.util.CredentialFactory) HttpAuthenticationException(nl.nn.adapterframework.http.authentication.HttpAuthenticationException) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) OAuthAccessTokenManager(nl.nn.adapterframework.http.authentication.OAuthAccessTokenManager) Test(org.junit.Test)

Example 3 with OAuthAccessTokenManager

use of nl.nn.adapterframework.http.authentication.OAuthAccessTokenManager in project iaf by ibissource.

the class OAuthAccessTokenManagerTest method testRetrieveAccessTokenWrongTokenEndpoint.

@Test
public void testRetrieveAccessTokenWrongTokenEndpoint() throws Exception {
    String tokenEndpoint = TOKENENDPOINT + "x";
    String scope = "email";
    String clientId = clientClientId;
    String clientSecret = clientClientSecret;
    CredentialFactory client_cf = new CredentialFactory(null, clientId, clientSecret);
    OAuthAccessTokenManager accessTokenManager = new OAuthAccessTokenManager(tokenEndpoint, scope, client_cf, true, httpSender);
    HttpAuthenticationException exception = assertThrows(HttpAuthenticationException.class, () -> accessTokenManager.getAccessToken(null));
    assertThat(exception.getMessage(), containsString("404"));
}
Also used : CredentialFactory(nl.nn.adapterframework.util.CredentialFactory) HttpAuthenticationException(nl.nn.adapterframework.http.authentication.HttpAuthenticationException) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) OAuthAccessTokenManager(nl.nn.adapterframework.http.authentication.OAuthAccessTokenManager) Test(org.junit.Test)

Example 4 with OAuthAccessTokenManager

use of nl.nn.adapterframework.http.authentication.OAuthAccessTokenManager in project iaf by ibissource.

the class HttpSenderBase method setupAuthentication.

private void setupAuthentication(CredentialFactory user_cf, CredentialFactory proxyCredentials, HttpHost proxy, Builder requestConfigBuilder) {
    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    if (StringUtils.isNotEmpty(user_cf.getUsername()) || StringUtils.isNotEmpty(getTokenEndpoint())) {
        credentialsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), getCredentials());
        AuthenticationScheme preferredAuthenticationScheme = getPreferredAuthenticationScheme();
        requestConfigBuilder.setTargetPreferredAuthSchemes(Arrays.asList(preferredAuthenticationScheme.getSchemeName()));
        requestConfigBuilder.setAuthenticationEnabled(true);
        if (preferredAuthenticationScheme == AuthenticationScheme.OAUTH) {
            CredentialFactory client_cf = new CredentialFactory(getClientAuthAlias(), getClientId(), getClientSecret());
            OAuthAccessTokenManager accessTokenManager = new OAuthAccessTokenManager(getTokenEndpoint(), getScope(), client_cf, StringUtils.isEmpty(user_cf.getUsername()), this, getTokenExpiry());
            httpClientContext.setAttribute(OAuthAuthenticationScheme.ACCESSTOKEN_MANAGER_KEY, accessTokenManager);
            httpClientBuilder.setTargetAuthenticationStrategy(new OAuthPreferringAuthenticationStrategy());
        }
    }
    if (proxy != null) {
        AuthScope scope = new AuthScope(proxy, proxyRealm, AuthScope.ANY_SCHEME);
        if (StringUtils.isNotEmpty(proxyCredentials.getUsername())) {
            Credentials credentials = new UsernamePasswordCredentials(proxyCredentials.getUsername(), proxyCredentials.getPassword());
            credentialsProvider.setCredentials(scope, credentials);
        }
        if (prefillProxyAuthCache()) {
            requestConfigBuilder.setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC));
            AuthCache authCache = httpClientContext.getAuthCache();
            if (authCache == null)
                authCache = new BasicAuthCache();
            authCache.put(proxy, new BasicScheme());
            httpClientContext.setAuthCache(authCache);
        }
    }
    httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
Also used : AuthenticationScheme(nl.nn.adapterframework.http.authentication.AuthenticationScheme) OAuthAuthenticationScheme(nl.nn.adapterframework.http.authentication.OAuthAuthenticationScheme) BasicScheme(org.apache.http.impl.auth.BasicScheme) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) OAuthPreferringAuthenticationStrategy(nl.nn.adapterframework.http.authentication.OAuthPreferringAuthenticationStrategy) CredentialFactory(nl.nn.adapterframework.util.CredentialFactory) AuthScope(org.apache.http.auth.AuthScope) AuthCache(org.apache.http.client.AuthCache) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) OAuthAccessTokenManager(nl.nn.adapterframework.http.authentication.OAuthAccessTokenManager) Credentials(org.apache.http.auth.Credentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 5 with OAuthAccessTokenManager

use of nl.nn.adapterframework.http.authentication.OAuthAccessTokenManager in project iaf by ibissource.

the class OAuthAccessTokenManagerTest method testRetrieveAccessToken.

@Test
public void testRetrieveAccessToken() throws Exception {
    String tokenEndpoint = TOKENENDPOINT;
    String scope = "email";
    String clientId = clientClientId;
    String clientSecret = clientClientSecret;
    CredentialFactory client_cf = new CredentialFactory(null, clientId, clientSecret);
    OAuthAccessTokenManager accessTokenManager = new OAuthAccessTokenManager(tokenEndpoint, scope, client_cf, true, httpSender);
    String accessToken = accessTokenManager.getAccessToken(null);
    assertThat(accessToken, startsWith("Bearer"));
}
Also used : CredentialFactory(nl.nn.adapterframework.util.CredentialFactory) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) OAuthAccessTokenManager(nl.nn.adapterframework.http.authentication.OAuthAccessTokenManager) Test(org.junit.Test)

Aggregations

OAuthAccessTokenManager (nl.nn.adapterframework.http.authentication.OAuthAccessTokenManager)5 CredentialFactory (nl.nn.adapterframework.util.CredentialFactory)5 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)4 Test (org.junit.Test)4 HttpAuthenticationException (nl.nn.adapterframework.http.authentication.HttpAuthenticationException)2 AuthenticationScheme (nl.nn.adapterframework.http.authentication.AuthenticationScheme)1 OAuthAuthenticationScheme (nl.nn.adapterframework.http.authentication.OAuthAuthenticationScheme)1 OAuthPreferringAuthenticationStrategy (nl.nn.adapterframework.http.authentication.OAuthPreferringAuthenticationStrategy)1 AuthScope (org.apache.http.auth.AuthScope)1 Credentials (org.apache.http.auth.Credentials)1 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)1 AuthCache (org.apache.http.client.AuthCache)1 CredentialsProvider (org.apache.http.client.CredentialsProvider)1 BasicScheme (org.apache.http.impl.auth.BasicScheme)1 BasicAuthCache (org.apache.http.impl.client.BasicAuthCache)1 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)1