/applinks-diagnostics-plugin/src/main/java/com/atlassian/applinks/diagnostics/rest/DefaultApplicationLinkConfigurationService.java

https://bitbucket.org/atlassianlabs/applinks-diagnostics · Java · 183 lines · 139 code · 20 blank · 24 comment · 16 complexity · d6d4e5222a014b799d407d4ba4132656 MD5 · raw file

  1. package com.atlassian.applinks.diagnostics.rest;
  2. import com.atlassian.applinks.api.ApplicationId;
  3. import com.atlassian.applinks.api.ApplicationLink;
  4. import com.atlassian.applinks.api.TypeNotInstalledException;
  5. import com.atlassian.applinks.api.auth.AuthenticationProvider;
  6. import com.atlassian.applinks.application.generic.GenericApplicationTypeImpl;
  7. import com.atlassian.applinks.core.auth.oauth.ServiceProviderStoreService;
  8. import com.atlassian.applinks.spi.auth.AuthenticationConfigurationManager;
  9. import com.atlassian.applinks.spi.auth.AuthenticationProviderPluginModule;
  10. import com.atlassian.applinks.spi.link.MutatingApplicationLinkService;
  11. import com.atlassian.oauth.Consumer;
  12. import com.atlassian.oauth.consumer.ConsumerService;
  13. import com.atlassian.plugin.PluginAccessor;
  14. import com.atlassian.plugins.rest.common.Link;
  15. import com.google.common.base.Predicate;
  16. import com.google.common.collect.Iterables;
  17. import com.google.common.collect.Lists;
  18. import org.apache.commons.lang.StringUtils;
  19. import java.net.URI;
  20. import java.net.URISyntaxException;
  21. import java.util.ArrayList;
  22. import java.util.List;
  23. import java.util.Map;
  24. /**
  25. * @since ShipIt23
  26. */
  27. public class DefaultApplicationLinkConfigurationService implements ApplicationLinkConfigurationService {
  28. public static final String CONTEXT = "applicationlink";
  29. private final MutatingApplicationLinkService applicationLinkService;
  30. private final PluginAccessor pluginAccessor;
  31. private final AuthenticationConfigurationManager authenticationConfigurationManager;
  32. private final ServiceProviderStoreService serviceProviderStoreService;
  33. private final ConsumerService consumerService;
  34. public DefaultApplicationLinkConfigurationService(final MutatingApplicationLinkService applicationLinkService,
  35. final PluginAccessor pluginAccessor,
  36. final AuthenticationConfigurationManager authenticationConfigurationManager,
  37. final ServiceProviderStoreService serviceProviderStoreService,
  38. final ConsumerService consumerService)
  39. {
  40. this.applicationLinkService = applicationLinkService;
  41. this.pluginAccessor = pluginAccessor;
  42. this.authenticationConfigurationManager = authenticationConfigurationManager;
  43. this.serviceProviderStoreService = serviceProviderStoreService;
  44. this.consumerService = consumerService;
  45. }
  46. /**
  47. * Get filtered Configured providers for the current ApplicationLink.
  48. */
  49. public List<AuthenticationProviderEntity> getConfiguredProviders(ApplicationLink applicationLink, Predicate<AuthenticationProviderPluginModule> predicate)
  50. throws URISyntaxException
  51. {
  52. return getConfiguredProviders(applicationLink, Iterables.filter(pluginAccessor.getEnabledModulesByClass(AuthenticationProviderPluginModule.class), predicate));
  53. }
  54. /**
  55. * Get unfiltered Configured providers for the current ApplicationLink.
  56. */
  57. public List<AuthenticationProviderEntity> getConfiguredProviders(ApplicationLink applicationLink)
  58. throws URISyntaxException
  59. {
  60. return getConfiguredProviders(applicationLink, pluginAccessor.getEnabledModulesByClass(AuthenticationProviderPluginModule.class));
  61. }
  62. /**
  63. * Get Configured providers for the current ApplicationLink.
  64. */
  65. public List<AuthenticationProviderEntity> getConfiguredProviders(ApplicationLink applicationLink, Iterable<AuthenticationProviderPluginModule> pluginModules)
  66. throws URISyntaxException
  67. {
  68. // get the providers configured for this link
  69. final List<AuthenticationProviderEntity> configuredAuthProviders = new ArrayList<AuthenticationProviderEntity>();
  70. for (AuthenticationProviderPluginModule authenticationProviderPluginModule : pluginModules)
  71. {
  72. final AuthenticationProvider authenticationProvider = authenticationProviderPluginModule.getAuthenticationProvider(applicationLink);
  73. if (authenticationProvider != null)
  74. {
  75. Map<String, String> config = authenticationConfigurationManager.getConfiguration(applicationLink.getId(),authenticationProviderPluginModule.getAuthenticationProviderClass());
  76. configuredAuthProviders.add(new AuthenticationProviderEntity(
  77. Link.self(new URI(CONTEXT + "/" + applicationLink.getId().toString() + "/authentication/provider")),
  78. authenticationProviderPluginModule.getClass().getName(),
  79. authenticationProviderPluginModule.getAuthenticationProviderClass().getName(),
  80. config));
  81. }
  82. }
  83. return configuredAuthProviders;
  84. }
  85. /**
  86. * Find the ApplicationLink by its id.
  87. */
  88. public ApplicationLink findApplicationLink(final String id) throws TypeNotInstalledException
  89. {
  90. ApplicationId applicationId;
  91. try
  92. {
  93. applicationId = new ApplicationId(id);
  94. }
  95. catch(IllegalArgumentException e)
  96. {
  97. return null;
  98. }
  99. return applicationLinkService.getApplicationLink(applicationId);
  100. }
  101. /**
  102. * find an ApplicationLinks's Consumer
  103. */
  104. public Iterable<Consumer> findConsumers(ApplicationLink applicationLink, List<AuthenticationProviderEntity> configuredAuthProviders)
  105. {
  106. List<Consumer> consumers = Lists.newArrayList();
  107. Consumer consumer = serviceProviderStoreService.getConsumer(applicationLink);
  108. if(consumer != null)
  109. {
  110. // incoming consumer
  111. consumers.add(consumer);
  112. }
  113. // for generic applicationlinks the Consumer is stored in the OAuth ConsumerService using the consumerkey as the key
  114. // the consumerkey is stored as a property of the Authenticationprovider.
  115. if(applicationLink.getType() instanceof GenericApplicationTypeImpl)
  116. {
  117. for(AuthenticationProviderEntity entity : configuredAuthProviders)
  118. {
  119. if(applicationLink.getType() instanceof GenericApplicationTypeImpl)
  120. {
  121. final String consumerKey = entity.getConfig().get("consumerKey.outbound");
  122. if(StringUtils.isEmpty(consumerKey))
  123. {
  124. continue;
  125. }
  126. Consumer genericOutGoingConsumer = consumerService.getConsumerByKey(consumerKey);
  127. if(genericOutGoingConsumer != null)
  128. {
  129. // outgoing consumer
  130. consumers.add(genericOutGoingConsumer);
  131. }
  132. }
  133. }
  134. }
  135. return consumers;
  136. }
  137. public List<AuthenticationProviderPluginModule> getConfiguredProviderPluginModules(ApplicationLink applicationLink, Predicate<AuthenticationProviderPluginModule> predicate)
  138. {
  139. return getConfiguredProviderPluginModules(applicationLink, Iterables.filter(pluginAccessor.getEnabledModulesByClass(AuthenticationProviderPluginModule.class), predicate));
  140. }
  141. private List<AuthenticationProviderPluginModule> getConfiguredProviderPluginModules(ApplicationLink applicationLink, Iterable<AuthenticationProviderPluginModule> pluginModules)
  142. {
  143. // get the providers configured for this link
  144. final List<AuthenticationProviderPluginModule> configuredAuthProviders = new ArrayList<AuthenticationProviderPluginModule>();
  145. for (AuthenticationProviderPluginModule authenticationProviderPluginModule : pluginModules)
  146. {
  147. final AuthenticationProvider authenticationProvider = authenticationProviderPluginModule.getAuthenticationProvider(applicationLink);
  148. if (authenticationProvider != null)
  149. {
  150. configuredAuthProviders.add(authenticationProviderPluginModule);
  151. }
  152. }
  153. return configuredAuthProviders;
  154. }
  155. public boolean isConfigured(ApplicationLink applicationLink, AuthenticationProviderPluginModule authenticationProviderPluginModule)
  156. {
  157. if(authenticationProviderPluginModule.getAuthenticationProvider(applicationLink) != null)
  158. {
  159. return authenticationConfigurationManager.isConfigured(applicationLink.getId(), authenticationProviderPluginModule.getAuthenticationProviderClass());
  160. }
  161. return false;
  162. }
  163. }