Search in sources :

Example 1 with Authenticator

use of org.wso2.carbon.identity.api.server.application.management.v1.Authenticator in project azure-sdk-for-java by Azure.

the class KeyVaultCredentials method applyCredentialsFilter.

@Override
public void applyCredentialsFilter(OkHttpClient.Builder clientBuilder) {
    clientBuilder.addInterceptor(new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            HttpUrl url = chain.request().url();
            Map<String, String> challengeMap = cache.getCachedChallenge(url);
            if (challengeMap != null) {
                // Get the bearer token
                String credential = getAuthenticationCredentials(challengeMap);
                Request newRequest = chain.request().newBuilder().header(AUTHENTICATE, BEARER_TOKEP_REFIX + credential).build();
                return chain.proceed(newRequest);
            } else {
                // response
                return chain.proceed(chain.request());
            }
        }
    });
    // Caches the challenge for failed request and re-send the request with
    // access token.
    clientBuilder.authenticator(new Authenticator() {

        @Override
        public Request authenticate(Route route, Response response) throws IOException {
            // if challenge is not cached then extract and cache it
            String authenticateHeader = response.header(WWW_AUTHENTICATE);
            Map<String, String> challengeMap = extractChallenge(authenticateHeader, BEARER_TOKEP_REFIX);
            // Cache the challenge
            cache.addCachedChallenge(response.request().url(), challengeMap);
            // Get the bearer token from the callback by providing the
            // challenges
            String credential = getAuthenticationCredentials(challengeMap);
            if (credential == null) {
                return null;
            }
            // be cached anywhere in our code.
            return response.request().newBuilder().header(AUTHENTICATE, BEARER_TOKEP_REFIX + credential).build();
        }
    });
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request) IOException(java.io.IOException) Interceptor(okhttp3.Interceptor) Map(java.util.Map) HashMap(java.util.HashMap) HttpUrl(okhttp3.HttpUrl) Authenticator(okhttp3.Authenticator) Route(okhttp3.Route)

Example 2 with Authenticator

use of org.wso2.carbon.identity.api.server.application.management.v1.Authenticator in project couchbase-lite-android by couchbase.

the class CBLWebSocket method setupOkHttpClient.

// -------------------------------------------------------------------------
// private methods
// -------------------------------------------------------------------------
private OkHttpClient setupOkHttpClient() throws GeneralSecurityException {
    OkHttpClient.Builder builder = new OkHttpClient.Builder();
    // timeouts
    builder.connectTimeout(10, TimeUnit.SECONDS).readTimeout(30, TimeUnit.SECONDS).writeTimeout(30, TimeUnit.SECONDS);
    // redirection
    builder.followRedirects(true).followSslRedirects(true);
    // authenticator
    Authenticator authenticator = setupAuthenticator();
    if (authenticator != null)
        builder.authenticator(authenticator);
    // trusted certificate (pinned certificate)
    setupTrustedCertificate(builder);
    return builder.build();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) Authenticator(okhttp3.Authenticator)

Example 3 with Authenticator

use of org.wso2.carbon.identity.api.server.application.management.v1.Authenticator in project couchbase-lite-android by couchbase.

the class CBLWebSocket method setupAuthenticator.

private Authenticator setupAuthenticator() {
    if (options != null && options.containsKey(kC4ReplicatorOptionAuthentication)) {
        Map<String, Object> auth = (Map<String, Object>) options.get(kC4ReplicatorOptionAuthentication);
        if (auth != null) {
            final String username = (String) auth.get(kC4ReplicatorAuthUserName);
            final String password = (String) auth.get(kC4ReplicatorAuthPassword);
            if (username != null && password != null) {
                return new Authenticator() {

                    @Override
                    public Request authenticate(Route route, Response response) throws IOException {
                        // http://www.ietf.org/rfc/rfc2617.txt
                        Log.v(TAG, "Authenticating for response: " + response);
                        // If failed 3 times, give up.
                        if (responseCount(response) >= 3)
                            return null;
                        List<Challenge> challenges = response.challenges();
                        Log.v(TAG, "Challenges: " + challenges);
                        if (challenges != null) {
                            for (Challenge challenge : challenges) {
                                if (challenge.scheme().equals("Basic")) {
                                    String credential = Credentials.basic(username, password);
                                    return response.request().newBuilder().header("Authorization", credential).build();
                                }
                            // NOTE: Not implemented Digest authentication
                            // https://github.com/rburgst/okhttp-digest
                            // else if(challenge.scheme().equals("Digest")){
                            // }
                            }
                        }
                        return null;
                    }
                };
            }
        }
    }
    return null;
}
Also used : Response(okhttp3.Response) ByteString(okio.ByteString) HashMap(java.util.HashMap) Map(java.util.Map) Authenticator(okhttp3.Authenticator) Route(okhttp3.Route) Challenge(okhttp3.Challenge)

Example 4 with Authenticator

use of org.wso2.carbon.identity.api.server.application.management.v1.Authenticator in project hub-fortify-ssc-integration-service by blackducksoftware.

the class FortifyService method getHeader.

public static Builder getHeader(String userName, String password) {
    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    logging.setLevel(Level.BASIC);
    OkHttpClient.Builder okBuilder = new OkHttpClient.Builder();
    okBuilder.authenticator(new Authenticator() {

        @Override
        public Request authenticate(Route route, Response response) throws IOException {
            String credential = Credentials.basic(userName, password);
            if (credential.equals(response.request().header("Authorization"))) {
                try {
                    FortifyExceptionUtil.verifyFortifyResponseCode(response.code(), "Unauthorized access of Fortify Api");
                } catch (IntegrationException e) {
                    throw new IOException(e);
                }
                return null;
            }
            return response.request().newBuilder().header("Authorization", credential).build();
        }
    });
    okBuilder.addInterceptor(logging);
    return okBuilder;
}
Also used : Builder(okhttp3.OkHttpClient.Builder) Response(okhttp3.Response) OkHttpClient(okhttp3.OkHttpClient) IntegrationException(com.blackducksoftware.integration.exception.IntegrationException) Builder(okhttp3.OkHttpClient.Builder) Request(okhttp3.Request) IOException(java.io.IOException) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) Authenticator(okhttp3.Authenticator) Route(okhttp3.Route)

Example 5 with Authenticator

use of org.wso2.carbon.identity.api.server.application.management.v1.Authenticator in project carbon-apimgt by wso2.

the class APIAuthenticationHandler method isAuthenticate.

/**
 * Authenticates the given request using the authenticators which have been initialized.
 *
 * @param messageContext The message to be authenticated
 * @return true if the authentication is successful (never returns false)
 * @throws APISecurityException If an authentication failure or some other error occurs
 */
protected boolean isAuthenticate(MessageContext messageContext) throws APISecurityException, APIManagementException {
    boolean authenticated = false;
    AuthenticationResponse authenticationResponse;
    List<AuthenticationResponse> authResponses = new ArrayList<>();
    for (Authenticator authenticator : authenticators) {
        authenticationResponse = authenticator.authenticate(messageContext);
        if (authenticationResponse.isMandatoryAuthentication()) {
            // Update authentication status only if the authentication is a mandatory one
            authenticated = authenticationResponse.isAuthenticated();
        }
        if (!authenticationResponse.isAuthenticated()) {
            authResponses.add(authenticationResponse);
        }
        if (!authenticationResponse.isContinueToNextAuthenticator()) {
            break;
        }
    }
    if (!authenticated) {
        Pair<Integer, String> error = getError(authResponses);
        throw new APISecurityException(error.getKey(), error.getValue());
    }
    return true;
}
Also used : ArrayList(java.util.ArrayList) OAuthAuthenticator(org.wso2.carbon.apimgt.gateway.handlers.security.oauth.OAuthAuthenticator) BasicAuthAuthenticator(org.wso2.carbon.apimgt.gateway.handlers.security.basicauth.BasicAuthAuthenticator) InternalAPIKeyAuthenticator(org.wso2.carbon.apimgt.gateway.handlers.security.authenticator.InternalAPIKeyAuthenticator) MutualSSLAuthenticator(org.wso2.carbon.apimgt.gateway.handlers.security.authenticator.MutualSSLAuthenticator) ApiKeyAuthenticator(org.wso2.carbon.apimgt.gateway.handlers.security.apikey.ApiKeyAuthenticator)

Aggregations

Authenticator (okhttp3.Authenticator)28 FederatedAuthenticatorConfig (org.wso2.carbon.identity.application.common.model.FederatedAuthenticatorConfig)27 IdentityProvider (org.wso2.carbon.identity.application.common.model.IdentityProvider)25 ArrayList (java.util.ArrayList)24 HashMap (java.util.HashMap)24 Test (org.testng.annotations.Test)23 IdentityProviderManagementException (org.wso2.carbon.idp.mgt.IdentityProviderManagementException)23 AuthenticatorConfig (org.wso2.carbon.identity.application.authentication.framework.config.model.AuthenticatorConfig)22 IOException (java.io.IOException)21 ApplicationAuthenticator (org.wso2.carbon.identity.application.authentication.framework.ApplicationAuthenticator)19 StepConfig (org.wso2.carbon.identity.application.authentication.framework.config.model.StepConfig)19 OkHttpClient (okhttp3.OkHttpClient)18 SequenceConfig (org.wso2.carbon.identity.application.authentication.framework.config.model.SequenceConfig)16 FrameworkException (org.wso2.carbon.identity.application.authentication.framework.exception.FrameworkException)15 LocalAuthenticatorConfig (org.wso2.carbon.identity.application.common.model.LocalAuthenticatorConfig)15 ISIntegrationTest (org.wso2.identity.integration.common.utils.ISIntegrationTest)15 Map (java.util.Map)14 Route (okhttp3.Route)14 Response (okhttp3.Response)13 FederatedApplicationAuthenticator (org.wso2.carbon.identity.application.authentication.framework.FederatedApplicationAuthenticator)12