Saml20AutoConfiguration.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * Copyright [2020] [MaxKey of copyright http://www.maxkey.top]
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.maxkey.autoconfigure;
  17. import java.io.IOException;
  18. import java.util.Properties;
  19. import org.apache.velocity.app.VelocityEngine;
  20. import org.apache.velocity.exception.VelocityException;
  21. import org.maxkey.authz.saml.common.EndpointGenerator;
  22. import org.maxkey.authz.saml.service.IDService;
  23. import org.maxkey.authz.saml.service.TimeService;
  24. import org.maxkey.authz.saml20.binding.decoder.OpenHTTPPostDecoder;
  25. import org.maxkey.authz.saml20.binding.decoder.OpenHTTPPostSimpleSignDecoder;
  26. import org.maxkey.authz.saml20.binding.decoder.OpenHTTPRedirectDecoder;
  27. import org.maxkey.authz.saml20.binding.impl.ExtractPostBindingAdapter;
  28. import org.maxkey.authz.saml20.binding.impl.ExtractRedirectBindingAdapter;
  29. import org.maxkey.authz.saml20.binding.impl.PostBindingAdapter;
  30. import org.maxkey.authz.saml20.binding.impl.PostSimpleSignBindingAdapter;
  31. import org.maxkey.authz.saml20.provider.xml.AuthnResponseGenerator;
  32. import org.maxkey.authz.saml20.xml.SAML2ValidatorSuite;
  33. import org.maxkey.constants.ConstantsProperties;
  34. import org.maxkey.crypto.keystore.KeyStoreLoader;
  35. import org.maxkey.domain.Saml20Metadata;
  36. import org.opensaml.common.binding.security.IssueInstantRule;
  37. import org.opensaml.common.binding.security.MessageReplayRule;
  38. import org.opensaml.util.storage.MapBasedStorageService;
  39. import org.opensaml.util.storage.ReplayCache;
  40. import org.opensaml.xml.ConfigurationException;
  41. import org.opensaml.xml.parse.BasicParserPool;
  42. import org.slf4j.Logger;
  43. import org.slf4j.LoggerFactory;
  44. import org.springframework.beans.factory.InitializingBean;
  45. import org.springframework.beans.factory.annotation.Value;
  46. import org.springframework.context.annotation.Bean;
  47. import org.springframework.context.annotation.ComponentScan;
  48. import org.springframework.context.annotation.Configuration;
  49. import org.springframework.context.annotation.PropertySource;
  50. import org.springframework.ui.velocity.VelocityEngineFactoryBean;
  51. @Configuration
  52. @ComponentScan(basePackages = {
  53. "org.maxkey.authz.saml20.provider.endpoint",
  54. "org.maxkey.authz.saml20.metadata.endpoint",
  55. })
  56. @PropertySource(ConstantsProperties.applicationPropertySource)
  57. @PropertySource(ConstantsProperties.maxKeyPropertySource)
  58. public class Saml20AutoConfiguration implements InitializingBean {
  59. private static final Logger _logger = LoggerFactory.getLogger(Saml20AutoConfiguration.class);
  60. /**
  61. * samlBootstrapInitializer.
  62. * @return samlBootstrapInitializer
  63. * @throws ConfigurationException
  64. */
  65. @Bean(name = "samlBootstrapInitializer")
  66. public String samlBootstrapInitializer() throws ConfigurationException {
  67. org.opensaml.DefaultBootstrap.bootstrap();
  68. return "";
  69. }
  70. /**
  71. * TimeService.
  72. * @return timeService
  73. */
  74. @Bean(name = "timeService")
  75. public TimeService TimeService() {
  76. TimeService timeService = new TimeService();
  77. return timeService;
  78. }
  79. /**
  80. * IDService.
  81. * @return idService
  82. */
  83. @Bean(name = "idService")
  84. public IDService idService() {
  85. IDService idService = new IDService();
  86. return idService;
  87. }
  88. /**
  89. * EndpointGenerator.
  90. * @return endpointGenerator
  91. */
  92. @Bean(name = "endpointGenerator")
  93. public EndpointGenerator endpointGenerator() {
  94. EndpointGenerator generator = new EndpointGenerator();
  95. return generator;
  96. }
  97. /**
  98. * AuthnResponseGenerator.
  99. * @return authnResponseGenerator
  100. */
  101. @Bean(name = "authnResponseGenerator")
  102. public AuthnResponseGenerator authnResponseGenerator(TimeService timeService,IDService idService,
  103. @Value("${config.saml.v20.idp.issuer}") String issuerEntityName) {
  104. AuthnResponseGenerator generator = new AuthnResponseGenerator(issuerEntityName,timeService,idService);
  105. return generator;
  106. }
  107. /**
  108. * IssuerEntityName.
  109. * @return issuerEntityName
  110. */
  111. @Bean(name = "issuerEntityName")
  112. public String issuerEntityName(
  113. @Value("${config.saml.v20.idp.issuer}") String issuerEntityName) {
  114. return issuerEntityName;
  115. }
  116. /**
  117. * Saml20Metadata.
  118. * @return saml20Metadata
  119. */
  120. @Bean(name = "saml20Metadata")
  121. public Saml20Metadata saml20Metadata(
  122. @Value("${config.saml.v20.metadata.orgName}") String orgName,
  123. @Value("${config.saml.v20.metadata.orgDisplayName}") String orgDisplayName,
  124. @Value("${config.saml.v20.metadata.orgURL}") String orgURL,
  125. @Value("${config.saml.v20.metadata.company}") String company,
  126. @Value("${config.saml.v20.metadata.contactType}") String contactType,
  127. @Value("${config.saml.v20.metadata.givenName}") String givenName,
  128. @Value("${config.saml.v20.metadata.surName}") String surName,
  129. @Value("${config.saml.v20.metadata.emailAddress}") String emailAddress,
  130. @Value("${config.saml.v20.metadata.telephoneNumber}") String telephoneNumber) {
  131. Saml20Metadata metadata = new Saml20Metadata();
  132. metadata.setOrgName(orgName);
  133. metadata.setOrgDisplayName(orgDisplayName);
  134. metadata.setOrgURL(orgURL);
  135. metadata.setCompany(company);
  136. metadata.setContactType(contactType);
  137. metadata.setGivenName(givenName);
  138. metadata.setSurName(surName);
  139. metadata.setEmailAddress(emailAddress);
  140. metadata.setTelephoneNumber(telephoneNumber);
  141. return metadata;
  142. }
  143. /**
  144. * SAML2ValidatorSuite.
  145. * @return samlValidaotrSuite
  146. */
  147. @Bean(name = "samlValidaotrSuite")
  148. public SAML2ValidatorSuite validatorSuite() {
  149. SAML2ValidatorSuite validatorSuite = new SAML2ValidatorSuite();
  150. return validatorSuite;
  151. }
  152. /**
  153. * MapBasedStorageService.
  154. * @return mapBasedStorageService
  155. */
  156. @Bean(name = "mapBasedStorageService")
  157. public MapBasedStorageService mapBasedStorageService() {
  158. MapBasedStorageService mapBasedStorageService = new MapBasedStorageService();
  159. return mapBasedStorageService;
  160. }
  161. /**
  162. * VelocityEngineFactoryBean.
  163. * @return velocityEngine
  164. * @throws IOException
  165. * @throws VelocityException
  166. */
  167. @Bean(name = "velocityEngine")
  168. public VelocityEngine velocityEngine() throws VelocityException, IOException {
  169. VelocityEngineFactoryBean factory = new VelocityEngineFactoryBean();
  170. factory.setPreferFileSystemAccess(false);
  171. Properties velocityProperties = new Properties();
  172. velocityProperties.put("resource.loader", "classpath");
  173. velocityProperties.put("classpath.resource.loader.class",
  174. "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
  175. factory.setVelocityProperties(velocityProperties);
  176. return factory.createVelocityEngine();
  177. }
  178. /**
  179. * ReplayCache.
  180. * @return replayCache
  181. */
  182. @Bean(name = "replayCache")
  183. public ReplayCache replayCache(MapBasedStorageService mapBasedStorageService,
  184. @Value("${config.saml.v20.replay.cache.life.in.millis}") long duration) {
  185. ReplayCache replayCache = new ReplayCache(mapBasedStorageService,duration);
  186. return replayCache;
  187. }
  188. /**
  189. * MessageReplayRule.
  190. * @return messageReplayRule
  191. */
  192. @Bean(name = "messageReplayRule")
  193. public MessageReplayRule messageReplayRule(ReplayCache replayCache) {
  194. MessageReplayRule messageReplayRule = new MessageReplayRule(replayCache);
  195. return messageReplayRule;
  196. }
  197. /**
  198. * BasicParserPool.
  199. * @return samlParserPool
  200. */
  201. @Bean(name = "samlParserPool")
  202. public BasicParserPool samlParserPool(
  203. @Value("${config.saml.v20.max.parser.pool.size}") int maxPoolSize) {
  204. BasicParserPool samlParserPool = new BasicParserPool();
  205. samlParserPool.setMaxPoolSize(maxPoolSize);
  206. return samlParserPool;
  207. }
  208. /**
  209. * IssueInstantRule.
  210. * @return issueInstantRule
  211. */
  212. @Bean(name = "issueInstantRule")
  213. public IssueInstantRule issueInstantRule(
  214. @Value("${config.saml.v20.issue.instant.check.clock.skew.in.seconds}") int newClockSkew,
  215. @Value("${config.saml.v20.issue.instant.check.validity.time.in.seconds}") int newExpires) {
  216. IssueInstantRule decoder = new IssueInstantRule(newClockSkew,newExpires);
  217. decoder.setRequiredRule(true);
  218. return decoder;
  219. }
  220. /**
  221. * OpenHTTPPostSimpleSignDecoder.
  222. * @return openHTTPPostSimpleSignDecoder
  223. */
  224. @Bean(name = "openHTTPPostSimpleSignDecoder")
  225. public OpenHTTPPostSimpleSignDecoder openHTTPPostSimpleSignDecoder(BasicParserPool samlParserPool,
  226. @Value("${config.saml.v20.idp.receiver.endpoint}") String receiverEndpoint) {
  227. OpenHTTPPostSimpleSignDecoder decoder = new OpenHTTPPostSimpleSignDecoder(samlParserPool);
  228. decoder.setReceiverEndpoint(receiverEndpoint);
  229. return decoder;
  230. }
  231. /**
  232. * OpenHTTPPostDecoder.
  233. * @return openHTTPPostDecoder
  234. */
  235. @Bean(name = "openHTTPPostDecoder")
  236. public OpenHTTPPostDecoder openHTTPPostDecoder(BasicParserPool samlParserPool,
  237. @Value("${config.saml.v20.idp.receiver.endpoint}") String receiverEndpoint) {
  238. OpenHTTPPostDecoder decoder = new OpenHTTPPostDecoder(samlParserPool);
  239. decoder.setReceiverEndpoint(receiverEndpoint);
  240. return decoder;
  241. }
  242. /**
  243. * OpenHTTPRedirectDecoder.
  244. * @return openHTTPRedirectDecoder
  245. */
  246. @Bean(name = "openHTTPRedirectDecoder")
  247. public OpenHTTPRedirectDecoder openHTTPRedirectDecoder(BasicParserPool samlParserPool,
  248. @Value("${config.saml.v20.idp.receiver.endpoint}") String receiverEndpoint) {
  249. OpenHTTPRedirectDecoder decoder = new OpenHTTPRedirectDecoder(samlParserPool);
  250. decoder.setReceiverEndpoint(receiverEndpoint);
  251. return decoder;
  252. }
  253. /**
  254. * ExtractPostBindingAdapter.
  255. * @return extractPostBindingAdapter
  256. */
  257. @Bean(name = "extractPostBindingAdapter")
  258. public ExtractPostBindingAdapter extractPostBindingAdapter(OpenHTTPPostDecoder openHTTPPostDecoder,
  259. KeyStoreLoader keyStoreLoader,IssueInstantRule issueInstantRule,MessageReplayRule messageReplayRule) {
  260. ExtractPostBindingAdapter adapter = new ExtractPostBindingAdapter(openHTTPPostDecoder);
  261. adapter.setIssueInstantRule(issueInstantRule);
  262. adapter.setKeyStoreLoader(keyStoreLoader);
  263. adapter.setMessageReplayRule(messageReplayRule);
  264. return adapter;
  265. }
  266. /**
  267. * ExtractRedirectBindingAdapter.
  268. * @return extractRedirectBindingAdapter
  269. */
  270. @Bean(name = "extractRedirectBindingAdapter")
  271. public ExtractRedirectBindingAdapter extractRedirectBindingAdapter(OpenHTTPRedirectDecoder openHTTPRedirectDecoder,
  272. KeyStoreLoader keyStoreLoader,IssueInstantRule issueInstantRule,MessageReplayRule messageReplayRule) {
  273. ExtractRedirectBindingAdapter adapter = new ExtractRedirectBindingAdapter(openHTTPRedirectDecoder);
  274. adapter.setIssueInstantRule(issueInstantRule);
  275. adapter.setKeyStoreLoader(keyStoreLoader);
  276. adapter.setMessageReplayRule(messageReplayRule);
  277. return adapter;
  278. }
  279. /**
  280. * PostSimpleSignBindingAdapter.
  281. * @return postSimpleSignBindingAdapter
  282. */
  283. @Bean(name = "postSimpleSignBindingAdapter")
  284. public PostSimpleSignBindingAdapter postSimpleSignBindingAdapter(VelocityEngine velocityEngine,
  285. @Value("${config.saml.v20.idp.issuer}") String issuerEntityName) {
  286. PostSimpleSignBindingAdapter adapter = new PostSimpleSignBindingAdapter();
  287. adapter.setVelocityEngine(velocityEngine);
  288. adapter.setIssuerEntityName(issuerEntityName);
  289. return adapter;
  290. }
  291. /**
  292. * PostBindingAdapter.
  293. * @return postBindingAdapter
  294. */
  295. @Bean(name = "postBindingAdapter")
  296. public PostBindingAdapter postBindingAdapter(VelocityEngine velocityEngine,
  297. @Value("${config.saml.v20.idp.issuer}") String issuerEntityName) {
  298. PostBindingAdapter adapter = new PostBindingAdapter();
  299. adapter.setVelocityEngine(velocityEngine);
  300. adapter.setIssuerEntityName(issuerEntityName);
  301. return adapter;
  302. }
  303. @Override
  304. public void afterPropertiesSet() throws Exception {
  305. // TODO Auto-generated method stub
  306. }
  307. }