1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.security.oauth.config;
18
19 import org.springframework.beans.factory.support.BeanDefinitionBuilder;
20 import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
21 import org.springframework.beans.factory.xml.ParserContext;
22 import org.springframework.security.oauth.common.signature.HMAC_SHA1SignatureMethod;
23 import org.springframework.security.oauth.common.signature.SharedConsumerSecret;
24 import org.springframework.security.oauth.consumer.BaseProtectedResourceDetails;
25 import org.springframework.util.StringUtils;
26 import org.springframework.util.xml.DomUtils;
27 import org.w3c.dom.Element;
28
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32
33
34
35
36 public class ProtectedResourceDetailsBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
37
38 @Override
39 protected Class getBeanClass(Element element) {
40 return ProtectedResourceDetailsServiceFactoryBean.class;
41 }
42
43 @Override
44 protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
45 List consumerElements = DomUtils.getChildElementsByTagName(element, "resource");
46 for (Object item : consumerElements) {
47 BeanDefinitionBuilder resource = BeanDefinitionBuilder.rootBeanDefinition(BaseProtectedResourceDetails.class);
48 Element consumerElement = (Element) item;
49 String id = consumerElement.getAttribute("id");
50 if (StringUtils.hasText(id)) {
51 resource.addPropertyValue("id", id);
52 }
53 else {
54 parserContext.getReaderContext().error("A resource id must be supplied with the definition of a protected resource.", consumerElement);
55 }
56
57 String key = consumerElement.getAttribute("key");
58 if (StringUtils.hasText(key)) {
59 resource.addPropertyValue("consumerKey", key);
60 }
61 else {
62 parserContext.getReaderContext().error("A consumer key must be supplied with the definition of a protected resource.", consumerElement);
63 }
64
65 String secret = consumerElement.getAttribute("secret");
66 if (StringUtils.hasText(secret)) {
67 resource.addPropertyValue("sharedSecret", secret);
68 }
69 else {
70 parserContext.getReaderContext().error("A shared secret must be supplied with the definition of a resource.", consumerElement);
71 }
72
73 String requestTokenURL = consumerElement.getAttribute("request-token-url");
74 if (StringUtils.hasText(requestTokenURL)) {
75 resource.addPropertyValue("requestTokenURL", requestTokenURL);
76 }
77 else {
78 parserContext.getReaderContext().error("A request token URL must be supplied with the definition of a resource.", consumerElement);
79 }
80
81 String requestTokenMethod = consumerElement.getAttribute("request-token-method");
82 if (StringUtils.hasText(requestTokenMethod)) {
83 resource.addPropertyValue("requestTokenHttpMethod", requestTokenMethod);
84 }
85
86 String accessTokenURL = consumerElement.getAttribute("access-token-url");
87 if (StringUtils.hasText(accessTokenURL)) {
88 resource.addPropertyValue("accessTokenURL", accessTokenURL);
89 }
90 else {
91 parserContext.getReaderContext().error("An access token URL must be supplied with the definition of a resource.", consumerElement);
92 }
93
94 String accessTokenMethod = consumerElement.getAttribute("access-token-method");
95 if (StringUtils.hasText(accessTokenMethod)) {
96 resource.addPropertyValue("accessTokenHttpMethod", accessTokenMethod);
97 }
98
99 String userAuthorizationURL = consumerElement.getAttribute("user-authorization-url");
100 if (StringUtils.hasText(userAuthorizationURL)) {
101 resource.addPropertyValue("userAuthorizationURL", userAuthorizationURL);
102 }
103 else {
104 parserContext.getReaderContext().error("A user authorization URL must be supplied with the definition of a resource.", consumerElement);
105 }
106
107 String sigMethod = consumerElement.getAttribute("signature-method");
108 if (!StringUtils.hasText(sigMethod)) {
109 sigMethod = HMAC_SHA1SignatureMethod.SIGNATURE_NAME;
110 }
111 resource.addPropertyValue("signatureMethod", sigMethod);
112
113 String acceptsHeader = consumerElement.getAttribute("accepts-authorization-header");
114 if (StringUtils.hasText(acceptsHeader)) {
115 resource.addPropertyValue("acceptsAuthorizationHeader", Boolean.valueOf(acceptsHeader));
116 }
117
118 String headerRealm = consumerElement.getAttribute("authorization-header-realm");
119 if (StringUtils.hasText(headerRealm)) {
120 resource.addPropertyValue("authorizationHeaderRealm", headerRealm);
121 }
122
123 String use10a = consumerElement.getAttribute("use10a");
124 if (StringUtils.hasText(use10a)) {
125 resource.addPropertyValue("use10a", "true".equals(use10a));
126 }
127
128 List additionalParameters = DomUtils.getChildElementsByTagName(consumerElement, "addtionalParameter");
129 if (additionalParameters != null && !additionalParameters.isEmpty()) {
130 Map<String, String> additionalParams = new HashMap<String, String>();
131 for (Object additionalParameter : additionalParameters) {
132 additionalParams.put(((Element)additionalParameter).getAttribute("name"), ((Element)additionalParameter).getAttribute("value"));
133 }
134 resource.addPropertyValue("additionalParameters", additionalParams);
135 }
136
137 List additionalRequestHeaders = DomUtils.getChildElementsByTagName(consumerElement, "additionalRequestHeader");
138 if (additionalRequestHeaders != null && !additionalRequestHeaders.isEmpty()) {
139 Map<String, String> headers = new HashMap<String, String>();
140 for (Object additionalParameter : additionalRequestHeaders) {
141 headers.put(((Element)additionalParameter).getAttribute("name"), ((Element)additionalParameter).getAttribute("value"));
142 }
143 resource.addPropertyValue("additionalRequestHeaders", headers);
144 }
145
146 parserContext.getRegistry().registerBeanDefinition(id, resource.getBeanDefinition());
147 }
148 }
149 }