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;
}
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);
}
}
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);
}
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);
}
Aggregations