|
16 | 16 |
|
17 | 17 | package org.springframework.security.oauth.config; |
18 | 18 |
|
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import org.springframework.beans.BeanMetadataElement; |
19 | 22 | import org.springframework.beans.factory.support.BeanDefinitionBuilder; |
| 23 | +import org.springframework.beans.factory.support.ManagedMap; |
20 | 24 | import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser; |
21 | 25 | import org.springframework.beans.factory.xml.ParserContext; |
22 | | -import org.springframework.security.core.authority.AuthorityUtils; |
23 | | -import org.springframework.security.oauth.common.signature.RSAKeySecret; |
24 | | -import org.springframework.security.oauth.common.signature.SharedConsumerSecret; |
25 | | -import org.springframework.security.oauth.provider.BaseConsumerDetails; |
26 | 26 | import org.springframework.security.oauth.provider.InMemoryConsumerDetailsService; |
27 | 27 | import org.springframework.util.StringUtils; |
28 | 28 | import org.springframework.util.xml.DomUtils; |
29 | 29 | import org.w3c.dom.Element; |
30 | 30 |
|
31 | | -import java.io.IOException; |
32 | | -import java.security.cert.Certificate; |
33 | | -import java.security.cert.CertificateException; |
34 | | -import java.security.cert.CertificateFactory; |
35 | | -import java.util.List; |
36 | | -import java.util.Map; |
37 | | -import java.util.TreeMap; |
38 | | - |
39 | 31 | /** |
40 | 32 | * @author Ryan Heaton |
41 | 33 | * @author Andrew McCall |
| 34 | + * @author Dave Syer |
42 | 35 | */ |
43 | 36 | public class ConsumerServiceBeanDefinitionParser extends AbstractSingleBeanDefinitionParser { |
44 | 37 |
|
45 | | - @Override |
46 | | - protected Class getBeanClass(Element element) { |
47 | | - return InMemoryConsumerDetailsService.class; |
48 | | - } |
| 38 | + @Override |
| 39 | + protected Class<?> getBeanClass(Element element) { |
| 40 | + return InMemoryConsumerDetailsService.class; |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { |
| 45 | + List<Element> consumerElements = DomUtils.getChildElementsByTagName(element, "consumer"); |
| 46 | + ManagedMap<String, BeanMetadataElement> consumers = new ManagedMap<String, BeanMetadataElement>(); |
| 47 | + for (Object item : consumerElements) { |
49 | 48 |
|
50 | | - @Override |
51 | | - protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { |
52 | | - List consumerElements = DomUtils.getChildElementsByTagName(element, "consumer"); |
53 | | - Map<String, BaseConsumerDetails> consumers = new TreeMap<String, BaseConsumerDetails>(); |
54 | | - for (Object item : consumerElements) { |
55 | | - BaseConsumerDetails consumer = new BaseConsumerDetails(); |
56 | | - Element consumerElement = (Element) item; |
57 | | - String key = consumerElement.getAttribute("key"); |
58 | | - if (StringUtils.hasText(key)) { |
59 | | - consumer.setConsumerKey(key); |
60 | | - } |
61 | | - else { |
62 | | - parserContext.getReaderContext().error("A consumer key must be supplied with the definition of a consumer.", consumerElement); |
63 | | - } |
| 49 | + BeanDefinitionBuilder consumer = BeanDefinitionBuilder |
| 50 | + .genericBeanDefinition(ConsumerDetailsFactoryBean.class); |
| 51 | + Element consumerElement = (Element) item; |
| 52 | + String key = consumerElement.getAttribute("key"); |
| 53 | + if (StringUtils.hasText(key)) { |
| 54 | + consumer.addPropertyValue("consumerKey", key); |
| 55 | + } |
| 56 | + else { |
| 57 | + parserContext.getReaderContext().error( |
| 58 | + "A consumer key must be supplied with the definition of a consumer.", consumerElement); |
| 59 | + } |
64 | 60 |
|
65 | | - String secret = consumerElement.getAttribute("secret"); |
66 | | - if (secret != null) { |
67 | | - String typeOfSecret = consumerElement.getAttribute("typeOfSecret"); |
68 | | - if ("rsa-cert".equals(typeOfSecret)) { |
69 | | - try { |
70 | | - Certificate cert = CertificateFactory.getInstance("X.509").generateCertificate(parserContext.getReaderContext().getResourceLoader().getResource(secret).getInputStream()); |
71 | | - consumer.setSignatureSecret(new RSAKeySecret(cert.getPublicKey())); |
72 | | - } |
73 | | - catch (IOException e) { |
74 | | - parserContext.getReaderContext().error("RSA certificate not found at " + secret + ".", consumerElement, e); |
75 | | - } |
76 | | - catch (CertificateException e) { |
77 | | - parserContext.getReaderContext().error("Invalid RSA certificate at " + secret + ".", consumerElement, e); |
78 | | - } |
79 | | - catch (NullPointerException e) { |
80 | | - parserContext.getReaderContext().error("Could not load RSA certificate at " + secret + ".", consumerElement, e); |
81 | | - } |
82 | | - } |
83 | | - else { |
84 | | - consumer.setSignatureSecret(new SharedConsumerSecret(secret)); |
85 | | - } |
86 | | - } |
87 | | - else { |
88 | | - parserContext.getReaderContext().error("A consumer secret must be supplied with the definition of a consumer.", consumerElement); |
89 | | - } |
| 61 | + String secret = consumerElement.getAttribute("secret"); |
| 62 | + if (StringUtils.hasText(secret)) { |
| 63 | + consumer.addPropertyValue("secret", secret); |
| 64 | + String typeOfSecret = consumerElement.getAttribute("typeOfSecret"); |
| 65 | + consumer.addPropertyValue("typeOfSecret", typeOfSecret); |
| 66 | + } |
| 67 | + else { |
| 68 | + parserContext.getReaderContext().error( |
| 69 | + "A consumer secret must be supplied with the definition of a consumer.", consumerElement); |
| 70 | + } |
90 | 71 |
|
91 | | - String name = consumerElement.getAttribute("name"); |
92 | | - if (StringUtils.hasText(name)) { |
93 | | - consumer.setConsumerName(name); |
94 | | - } |
| 72 | + String name = consumerElement.getAttribute("name"); |
| 73 | + if (StringUtils.hasText(name)) { |
| 74 | + consumer.addPropertyValue("consumerName", name); |
| 75 | + } |
95 | 76 |
|
96 | | - String authorities = consumerElement.getAttribute("authorities"); |
97 | | - if (authorities != null) { |
98 | | - consumer.setAuthorities(AuthorityUtils.commaSeparatedStringToAuthorityList(authorities)); |
99 | | - } |
| 77 | + String authorities = consumerElement.getAttribute("authorities"); |
| 78 | + if (StringUtils.hasText(authorities)) { |
| 79 | + consumer.addPropertyValue("authorities", authorities); |
| 80 | + } |
100 | 81 |
|
101 | | - String resourceName = consumerElement.getAttribute("resourceName"); |
102 | | - if (resourceName != null) { |
103 | | - consumer.setResourceName(resourceName); |
104 | | - } |
| 82 | + String resourceName = consumerElement.getAttribute("resourceName"); |
| 83 | + if (StringUtils.hasText(resourceName)) { |
| 84 | + consumer.addPropertyValue("resourceName", resourceName); |
| 85 | + } |
105 | 86 |
|
106 | | - String resourceDescription = consumerElement.getAttribute("resourceDescription"); |
107 | | - if (resourceDescription != null) { |
108 | | - consumer.setResourceDescription(resourceDescription); |
109 | | - } |
| 87 | + String resourceDescription = consumerElement.getAttribute("resourceDescription"); |
| 88 | + if (StringUtils.hasText(resourceDescription)) { |
| 89 | + consumer.addPropertyValue("resourceDescription", resourceDescription); |
| 90 | + } |
110 | 91 |
|
111 | | - String requiredToObtainAuthenticatedToken = consumerElement.getAttribute("requiredToObtainAuthenticatedToken"); |
112 | | - if (requiredToObtainAuthenticatedToken != null && "false".equalsIgnoreCase(requiredToObtainAuthenticatedToken)) { |
113 | | - consumer.setRequiredToObtainAuthenticatedToken(false); |
114 | | - } |
| 92 | + String requiredToObtainAuthenticatedToken = consumerElement |
| 93 | + .getAttribute("requiredToObtainAuthenticatedToken"); |
| 94 | + if (StringUtils.hasText(requiredToObtainAuthenticatedToken)) { |
| 95 | + consumer.addPropertyValue("requiredToObtainAuthenticatedToken", requiredToObtainAuthenticatedToken); |
| 96 | + } |
115 | 97 |
|
116 | | - consumers.put(key, consumer); |
117 | | - } |
| 98 | + consumers.put(key, consumer.getBeanDefinition()); |
| 99 | + } |
118 | 100 |
|
119 | | - builder.addPropertyValue("consumerDetailsStore", consumers); |
120 | | - } |
| 101 | + builder.addPropertyValue("consumerDetailsStore", consumers); |
| 102 | + } |
121 | 103 | } |
0 commit comments