Search in sources :

Example 1 with HttpServer

use of org.mule.runtime.http.api.server.HttpServer 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)

Aggregations

Optional (java.util.Optional)1 MuleRuntimeException (org.mule.runtime.api.exception.MuleRuntimeException)1 AuthorizationCodeGrantType (org.mule.runtime.extension.api.connectivity.oauth.AuthorizationCodeGrantType)1 HttpServer (org.mule.runtime.http.api.server.HttpServer)1 ServerNotFoundException (org.mule.runtime.http.api.server.ServerNotFoundException)1 LazyObjectStoreToMapAdapter (org.mule.runtime.module.extension.internal.store.LazyObjectStoreToMapAdapter)1 AuthorizationCodeOAuthDancer (org.mule.runtime.oauth.api.AuthorizationCodeOAuthDancer)1 OAuthAuthorizationCodeDancerBuilder (org.mule.runtime.oauth.api.builder.OAuthAuthorizationCodeDancerBuilder)1