/src/main/java/com/google/ie/common/openid/GuiceModule.java

http://thoughtsite.googlecode.com/ · Java · 165 lines · 113 code · 25 blank · 27 comment · 4 complexity · 44ef33e43b41d706078b4b8b59e49b61 MD5 · raw file

  1. /* Copyright 2010 Google Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS.
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License
  14. */
  15. package com.google.ie.common.openid;
  16. import com.google.appengine.api.urlfetch.URLFetchService;
  17. import com.google.appengine.api.urlfetch.URLFetchServiceFactory;
  18. import com.google.ie.common.openid.appengine.AppEngineHttpFetcher;
  19. import com.google.ie.common.openid.appengine.AppEngineTrustsRootProvider;
  20. import com.google.ie.common.openid.appengine.Openid4javaFetcher;
  21. import com.google.ie.common.openid.appengine.SerialExecutorService;
  22. import com.google.inject.AbstractModule;
  23. import com.google.inject.CreationException;
  24. import com.google.inject.Provides;
  25. import com.google.inject.Scopes;
  26. import com.google.inject.Singleton;
  27. import com.google.inject.name.Named;
  28. import com.google.inject.name.Names;
  29. import com.google.step2.consumer.OAuthProviderInfoStore;
  30. import com.google.step2.discovery.DefaultHostMetaFetcher;
  31. import com.google.step2.discovery.HostMetaFetcher;
  32. import com.google.step2.discovery.ParallelHostMetaFetcher;
  33. import com.google.step2.http.HttpFetcher;
  34. import com.google.step2.hybrid.HybridOauthMessage;
  35. import com.google.step2.openid.ax2.AxMessage2;
  36. import com.google.step2.xmlsimplesign.CertValidator;
  37. import com.google.step2.xmlsimplesign.CnConstraintCertValidator;
  38. import com.google.step2.xmlsimplesign.DefaultCertValidator;
  39. import com.google.step2.xmlsimplesign.DisjunctiveCertValidator;
  40. import com.google.step2.xmlsimplesign.TrustRootsProvider;
  41. import org.openid4java.consumer.ConsumerAssociationStore;
  42. import org.openid4java.consumer.InMemoryConsumerAssociationStore;
  43. import org.openid4java.message.Message;
  44. import org.openid4java.message.MessageException;
  45. import java.util.concurrent.ExecutorService;
  46. import java.util.concurrent.Executors;
  47. /**
  48. *
  49. * @author Dirk Balfanz (dirk.balfanz@gmail.com)
  50. * @author Breno de Medeiros (breno.demedeiros@gmail.com)
  51. */
  52. public class GuiceModule extends AbstractModule {
  53. @Override
  54. protected void configure() {
  55. try {
  56. Message.addExtensionFactory(AxMessage2.class);
  57. } catch (MessageException e) {
  58. throw new CreationException(null);
  59. }
  60. try {
  61. Message.addExtensionFactory(HybridOauthMessage.class);
  62. } catch (MessageException e) {
  63. throw new CreationException(null);
  64. }
  65. bind(ConsumerAssociationStore.class)
  66. .to(InMemoryConsumerAssociationStore.class)
  67. .in(Scopes.SINGLETON);
  68. bind(OAuthProviderInfoStore.class)
  69. .to(SimpleProviderInfoStore.class).in(Scopes.SINGLETON);
  70. if (isRunningOnAppengine()) {
  71. install(new AppEngineModule());
  72. } else {
  73. install(new JettyModule());
  74. }
  75. }
  76. private boolean isRunningOnAppengine() {
  77. if (System.getSecurityManager() == null) {
  78. return false;
  79. }
  80. return System.getSecurityManager().getClass().getCanonicalName()
  81. .startsWith("com.google");
  82. }
  83. // we're using a cert validator that will validate certs either if they
  84. // belong to the expected signer of the XRD, or if they're signed
  85. // by Google.
  86. @Provides
  87. @Singleton
  88. public CertValidator provideCertValidator(DefaultCertValidator defaultValidator) {
  89. CertValidator hardCodedValidator = new CnConstraintCertValidator() {
  90. @Override
  91. protected String getRequiredCn(String authority) {
  92. return "hosted-id.google.com";
  93. }
  94. };
  95. return new DisjunctiveCertValidator(defaultValidator, hardCodedValidator);
  96. }
  97. // we're using a ParallelHostMetaFetcher to fetch host-metas both from their
  98. // default location, and from a special location at Google.
  99. @Provides
  100. @Singleton
  101. public HostMetaFetcher provideHostMetaFetcher(
  102. @Named("HostMetaFetcherExecutor") ExecutorService executor,
  103. DefaultHostMetaFetcher fetcher1,
  104. GoogleHostedHostMetaFetcher fetcher2) {
  105. // we're waiting at most 10 seconds for the two host-meta fetchers to
  106. // find
  107. // a host-meta
  108. long hostMetatimeout = 10; // seconds.
  109. return new ParallelHostMetaFetcher(executor, hostMetatimeout,
  110. fetcher1, fetcher2);
  111. }
  112. public static class JettyModule extends AbstractModule {
  113. @Override
  114. protected void configure() {
  115. bind(ExecutorService.class)
  116. .annotatedWith(Names.named("HostMetaFetcherExecutor"))
  117. .toInstance(Executors.newFixedThreadPool(20));
  118. }
  119. }
  120. public static class AppEngineModule extends AbstractModule {
  121. @Override
  122. protected void configure() {
  123. bind(HttpFetcher.class)
  124. .to(AppEngineHttpFetcher.class).in(Scopes.SINGLETON);
  125. bind(TrustRootsProvider.class)
  126. .to(AppEngineTrustsRootProvider.class).in(Scopes.SINGLETON);
  127. bind(ExecutorService.class)
  128. .annotatedWith(Names.named("HostMetaFetcherExecutor"))
  129. .to(SerialExecutorService.class).in(Scopes.SINGLETON);
  130. bind(org.openid4java.util.HttpFetcher.class)
  131. .to(Openid4javaFetcher.class)
  132. .in(Scopes.SINGLETON);
  133. }
  134. }
  135. @Provides
  136. @Singleton
  137. public URLFetchService provideUrlFetchService() {
  138. return URLFetchServiceFactory.getURLFetchService();
  139. }
  140. }