Search in sources :

Example 1 with AuthorizationCodeOAuthDancer

use of org.mule.runtime.oauth.api.AuthorizationCodeOAuthDancer in project mule by mulesoft.

the class DefaultExtensionsOAuthManager method createDancer.

private AuthorizationCodeOAuthDancer createDancer(OAuthConfig config) throws MuleException {
    OAuthAuthorizationCodeDancerBuilder dancerBuilder = oauthService.get().authorizationCodeGrantTypeDancerBuilder(lockId -> lockFactory.createLock(lockId), new LazyObjectStoreToMapAdapter(getObjectStoreSupplier(config)), expressionEvaluator);
    final AuthCodeConfig authCodeConfig = config.getAuthCodeConfig();
    final AuthorizationCodeGrantType grantType = config.getGrantType();
    final OAuthCallbackConfig callbackConfig = config.getCallbackConfig();
    dancerBuilder.encoding(getDefaultEncoding(muleContext)).clientCredentials(authCodeConfig.getConsumerKey(), authCodeConfig.getConsumerSecret()).tokenUrl(authCodeConfig.getAccessTokenUrl()).responseExpiresInExpr(grantType.getExpirationRegex()).responseRefreshTokenExpr(grantType.getRefreshTokenExpr()).responseAccessTokenExpr(grantType.getAccessTokenExpr()).resourceOwnerIdTransformer(ownerId -> ownerId + "-" + config.getOwnerConfigName());
    String scopes = authCodeConfig.getScope().orElseGet(() -> grantType.getDefaultScope().orElse(null));
    if (scopes != null) {
        dancerBuilder.scopes(scopes);
    }
    HttpServer httpServer;
    try {
        httpServer = httpService.get().getServerFactory().lookup(callbackConfig.getListenerConfig());
    } catch (ServerNotFoundException e) {
        throw new MuleRuntimeException(createStaticMessage(format("Connector '%s' defines '%s' as the http:listener-config to use for provisioning callbacks, but no such definition " + "exists in the application configuration", config.getOwnerConfigName(), callbackConfig.getListenerConfig())), e);
    }
    dancerBuilder.localCallback(httpServer, callbackConfig.getCallbackPath()).externalCallbackUrl(getExternalCallback(httpServer, callbackConfig)).authorizationUrl(authCodeConfig.getAuthorizationUrl()).localAuthorizationUrlPath(callbackConfig.getLocalAuthorizePath()).localAuthorizationUrlResourceOwnerId("#[attributes.queryParams.resourceOwnerId]").state("#[attributes.queryParams.state]").customParameters(config.getCustomParameters()).customParametersExtractorsExprs(getParameterExtractors(config));
    Pair<Optional<Flow>, Optional<Flow>> listenerFlows = getListenerFlows(config);
    listenerFlows.getFirst().ifPresent(flow -> dancerBuilder.beforeDanceCallback(beforeCallback(config, flow)));
    listenerFlows.getSecond().ifPresent(flow -> dancerBuilder.afterDanceCallback(afterCallback(config, flow)));
    AuthorizationCodeOAuthDancer dancer = dancerBuilder.build();
    if (started) {
        start(dancer);
    }
    return dancer;
}
Also used : LazyObjectStoreToMapAdapter(org.mule.runtime.module.extension.internal.store.LazyObjectStoreToMapAdapter) Optional(java.util.Optional) HttpServer(org.mule.runtime.http.api.server.HttpServer) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) AuthorizationCodeGrantType(org.mule.runtime.extension.api.connectivity.oauth.AuthorizationCodeGrantType) ServerNotFoundException(org.mule.runtime.http.api.server.ServerNotFoundException) AuthorizationCodeOAuthDancer(org.mule.runtime.oauth.api.AuthorizationCodeOAuthDancer) OAuthAuthorizationCodeDancerBuilder(org.mule.runtime.oauth.api.builder.OAuthAuthorizationCodeDancerBuilder)

Example 2 with AuthorizationCodeOAuthDancer

use of org.mule.runtime.oauth.api.AuthorizationCodeOAuthDancer in project mule by mulesoft.

the class DefaultExtensionsOAuthManager method refreshToken.

/**
 * {@inheritDoc}
 */
@Override
public void refreshToken(String ownerConfigName, String resourceOwnerId, OAuthConnectionProviderWrapper connectionProvider) {
    AuthorizationCodeOAuthDancer dancer = dancers.get(ownerConfigName);
    try {
        dancer.refreshToken(resourceOwnerId).get();
        connectionProvider.updateAuthState();
    } catch (Exception e) {
        throw new MuleRuntimeException(createStaticMessage(format("Could not refresh token for resourceOwnerId '%s' using config '%s'", resourceOwnerId, ownerConfigName)), e);
    }
}
Also used : MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) AuthorizationCodeOAuthDancer(org.mule.runtime.oauth.api.AuthorizationCodeOAuthDancer) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) ServerNotFoundException(org.mule.runtime.http.api.server.ServerNotFoundException) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) MuleException(org.mule.runtime.api.exception.MuleException) MalformedURLException(java.net.MalformedURLException)

Example 3 with AuthorizationCodeOAuthDancer

use of org.mule.runtime.oauth.api.AuthorizationCodeOAuthDancer in project mule by mulesoft.

the class DefaultExtensionsOAuthManager method invalidate.

/**
 * {@inheritDoc}
 */
@Override
public void invalidate(String ownerConfigName, String resourceOwnerId) {
    AuthorizationCodeOAuthDancer dancer = dancers.get(ownerConfigName);
    if (dancer == null) {
        return;
    }
    dancer.invalidateContext(resourceOwnerId);
}
Also used : AuthorizationCodeOAuthDancer(org.mule.runtime.oauth.api.AuthorizationCodeOAuthDancer)

Example 4 with AuthorizationCodeOAuthDancer

use of org.mule.runtime.oauth.api.AuthorizationCodeOAuthDancer in project mule by mulesoft.

the class DefaultExtensionsOAuthManager method getOAuthContext.

/**
 * {@inheritDoc}
 */
@Override
public Optional<ResourceOwnerOAuthContext> getOAuthContext(OAuthConfig config) {
    AuthorizationCodeOAuthDancer dancer = dancers.get(config.getOwnerConfigName());
    if (dancer == null) {
        return empty();
    }
    ResourceOwnerOAuthContext contextForResourceOwner = dancer.getContextForResourceOwner(config.getAuthCodeConfig().getResourceOwnerId());
    if (contextForResourceOwner == null || contextForResourceOwner.getAccessToken() == null) {
        return empty();
    }
    return of(contextForResourceOwner);
}
Also used : AuthorizationCodeOAuthDancer(org.mule.runtime.oauth.api.AuthorizationCodeOAuthDancer) ResourceOwnerOAuthContext(org.mule.runtime.oauth.api.state.ResourceOwnerOAuthContext)

Aggregations

AuthorizationCodeOAuthDancer (org.mule.runtime.oauth.api.AuthorizationCodeOAuthDancer)4 MuleRuntimeException (org.mule.runtime.api.exception.MuleRuntimeException)2 ServerNotFoundException (org.mule.runtime.http.api.server.ServerNotFoundException)2 MalformedURLException (java.net.MalformedURLException)1 Optional (java.util.Optional)1 MuleException (org.mule.runtime.api.exception.MuleException)1 InitialisationException (org.mule.runtime.api.lifecycle.InitialisationException)1 AuthorizationCodeGrantType (org.mule.runtime.extension.api.connectivity.oauth.AuthorizationCodeGrantType)1 HttpServer (org.mule.runtime.http.api.server.HttpServer)1 LazyObjectStoreToMapAdapter (org.mule.runtime.module.extension.internal.store.LazyObjectStoreToMapAdapter)1 OAuthAuthorizationCodeDancerBuilder (org.mule.runtime.oauth.api.builder.OAuthAuthorizationCodeDancerBuilder)1 ResourceOwnerOAuthContext (org.mule.runtime.oauth.api.state.ResourceOwnerOAuthContext)1