View Javadoc
1   /*
2    * Copyright 2008-2009 Web Cohesion
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    *   https://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  
17  package org.springframework.security.oauth2.config.xml;
18  
19  import java.util.List;
20  
21  import org.springframework.beans.BeanMetadataElement;
22  import org.springframework.beans.factory.support.BeanDefinitionBuilder;
23  import org.springframework.beans.factory.support.ManagedMap;
24  import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
25  import org.springframework.beans.factory.xml.ParserContext;
26  import org.springframework.security.oauth2.provider.client.BaseClientDetails;
27  import org.springframework.security.oauth2.provider.client.InMemoryClientDetailsService;
28  import org.springframework.util.StringUtils;
29  import org.springframework.util.xml.DomUtils;
30  import org.w3c.dom.Element;
31  
32  /**
33   * @author Ryan Heaton
34   * @author Andrew McCall
35   */
36  public class ClientDetailsServiceBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
37  
38  	@Override
39  	protected Class<?> getBeanClass(Element element) {
40  		return InMemoryClientDetailsService.class;
41  	}
42  
43  	@Override
44  	protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
45  		List<Element> clientElements = DomUtils.getChildElementsByTagName(element, "client");
46  		ManagedMap<String, BeanMetadataElement> clients = new ManagedMap<String, BeanMetadataElement>();
47  		for (Element clientElement : clientElements) {
48  			BeanDefinitionBuilder client = BeanDefinitionBuilder.rootBeanDefinition(BaseClientDetails.class);
49  			String clientId = clientElement.getAttribute("client-id");
50  			if (StringUtils.hasText(clientId)) {
51  				client.addConstructorArgValue(clientId);
52  			}
53  			else {
54  				parserContext.getReaderContext().error("A client id must be supplied with the definition of a client.",
55  						clientElement);
56  			}
57  
58  			String secret = clientElement.getAttribute("secret");
59  			if (StringUtils.hasText(secret)) {
60  				client.addPropertyValue("clientSecret", secret);
61  			}
62  			String resourceIds = clientElement.getAttribute("resource-ids");
63  			if (StringUtils.hasText(clientId)) {
64  				client.addConstructorArgValue(resourceIds);
65  			}
66  			else {
67  				client.addConstructorArgValue("");
68  			}
69  			String redirectUri = clientElement.getAttribute("redirect-uri");
70  			String tokenValidity = clientElement.getAttribute("access-token-validity");
71  			if (StringUtils.hasText(tokenValidity)) {
72  				client.addPropertyValue("accessTokenValiditySeconds", tokenValidity);
73  			}
74  			String refreshValidity = clientElement.getAttribute("refresh-token-validity");
75  			if (StringUtils.hasText(refreshValidity)) {
76  				client.addPropertyValue("refreshTokenValiditySeconds", refreshValidity);
77  			}
78  			client.addConstructorArgValue(clientElement.getAttribute("scope"));
79  			client.addConstructorArgValue(clientElement.getAttribute("authorized-grant-types"));
80  			client.addConstructorArgValue(clientElement.getAttribute("authorities"));
81  			if (StringUtils.hasText(redirectUri)) {
82  				client.addConstructorArgValue(redirectUri);
83  			}
84  			client.addPropertyValue("autoApproveScopes", clientElement.getAttribute("autoapprove"));
85  
86  			clients.put(clientId, client.getBeanDefinition());
87  		}
88  
89  		builder.addPropertyValue("clientDetailsStore", clients);
90  	}
91  }